06.08.2013, 09:46 PM
Ungeregelt läuft schonmal alles. Jetzt muss ich nur noch den Wandler an den ADC-Port anschließen und die Regelung anschalten
Code:
//Compiler Zeug
#define F_CPU 14745600UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
//Funktionen
void init(void);
//Variablen
uint16_t ist, out, i;
int16_t pre;
const uint16_t soll[200] = { 512, 518, 524, 530, 537, 543, 549, 555, 561, 568, 574, 580, 585, 591, 597, 603, 608, 614, 619, 624, 630, 635, 640, 644, 649, 653, 658, 662, 666, 670, 674, 677, 681, 684, 687, 690, 693, 696, 698, 700, 702, 704, 706, 707, 708, 709, 710, 711, 711, 711, 711, 711, 711, 710, 710, 709, 708, 706, 705, 703, 701, 699, 697, 694, 692, 689, 686, 683, 679, 676, 672, 668, 664, 660, 656, 651, 647, 642, 637, 632, 627, 622, 616, 611, 606, 600, 594, 588, 583, 577, 571, 565, 558, 552, 546, 540, 534, 527, 521, 515, 508, 502, 496, 489, 483, 477, 471, 465, 458, 452, 446, 440, 435, 429, 423, 417, 412, 407, 401, 396, 391, 386, 381, 376, 372, 367, 363, 359, 355, 351, 347, 344, 340, 337, 334, 331, 329, 326, 324, 322, 320, 318, 317, 315, 314, 313, 313, 312, 312, 312, 312, 312, 312, 313, 314, 315, 316, 317, 319, 321, 323, 325, 327, 330, 333, 336, 339, 342, 346, 349, 353, 357, 361, 365, 370, 374, 379, 383, 388, 393, 399, 404, 409, 415, 420, 426, 432, 438, 443, 449, 455, 462, 468, 474, 480, 486, 493, 499, 505, 511 };
int16_t err[200] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int main(void){
init();
//erst 5 Sekunden "normal" 50:50 takten,
_delay_ms(5000); //(Zeit zum die Brücke anschalten)
TCCR0 = (1<<WGM01) | (1<<CS01); //(Dann den Regler anschalten)
while(1){
}
return 0;
}
ISR(TIMER0_COMP_vect){
ADCSRA |= (1<<ADSC); //Sampeln starten
while (ADCSRA & (1<<ADSC) ) {
} //warten
ist = ADC; //ADC lesen
ist = 0;
err[i] += soll[i] - ist;
i++;
pre = soll[i] + err[i];
if(pre < 1){
pre = 1;
}else if(pre > 1022){
pre = 1022;
}
out = pre;
OCR1C = soll[i];
//OCR1C = out;
if(i == 199){
i = 0;
}
}
void init(void){
i = 0;
MCUCR = (1<<IVCE);
MCUCR = 0;
//PORTS
DDRB = (1<<PB7); //PWM_OC1C
//TIMER1 (PWM)
TCNT1 = 0;
OCR1C = soll[0];
ICR1 = 1023;
TCCR1A = (1<<COM1C1) | (1<<COM1C0); //Set OC1C on compare match when up-counting. Clear OC1C on compare match when downcounting.
TCCR1B = (1<<WGM13) | (1<<CS10); //PS1, Mode=8
//TIMER0 (Regelinterrupt)
TCNT0 = 0;
OCR0 = 184; //ergibt mit PS8 99.82us
//TCCR0 = (1<<WGM01) | (1<<CS01); //CTC, PS8
TIMSK |= (1<<OCIE0); //Interrupt an
//ADC
ADMUX = (1<<REFS0); //ADC0, ref=AVCC
ADCSRA = (1<<ADPS1) | (1<<ADPS0); //PS8 -> 70us
ADCSRA |= (1<<ADEN); //anschalten - steht automaisch auf single conversion
//SPI
SPCR = 0;
sei();
}
Pffffffffft. "Da entwich das Vakuum" - Heinrich Physik, 1857.