Hallo Ich programmiere ATMEGA88 mit AvrDragon.Ich muss Schrittmotor steuern. ich habe Timer1 Probleme.Ich verwende 2 Schalter für Motor, einer für Start/Stop,andere für Schnell/Langsam .wenn ich einmal schnell/langsam Schalter betätigt alles in Ordnung aber wenn ich zweite mal betätigt dann motor hält und warte ca. 1 minute dann funktioniert weiter. wenn ich gleiche Code mit Timer2 oder Timer0 probiere die beiden Schalter richtig funktionieren.Aber Warum? #include <avr\io.h> #include <inttypes.h> #include <avr\interrupt.h> #include <util/delay.h> #include <stdbool.h> volatile int i=0; //schrittanzahl float u=0; //Umdrehung float f_max=10; //wenn Schalter Schnell betätigt->Frequenz=10 float f_min=5; //wenn Schalter langsam betätigt->frequenz=5 void motor2(); void InitPorts() { DDRB =0b00000110; //PB1->richtung PB2 ->takt Ausgänge DDRC &= ~(1<<DDC4); //PC4 Schnell/langsam schalter PORTC |= (1<<PC4); } //Funktion für Schalter(gedrückt oder nicht) bool check_button(volatile uint8_t *port, uint8_t pin, bool depressed){ if(!(*port&_BV(pin))){ _delay_ms(10); if(!(*port&_BV(pin))){ if(depressed)while(!(*port&_BV(pin))); return(true); } } return(false); } void InitTimer1(void) //Timer initialisierung { TIFR1 =0b00000010; // Interrupt Request loeschen (sicherheitshalber) TIMSK1 =0b00000010; // Enable Output Compare A Interrupt TCCR1A =0b01000000; TCCR1B =0b00001101; // Prescaler 1024, CTC Mode 4, Timer1 Start OCR1A =((18432000/8)/(2*1024*f_max))-1; // 18432000 ist Taktfrequenz TCNT1 =0; } ISR (TIMER1_COMPA_vect) { if(check_button(&PINC,PINC5,true)); //PC5 Start/Stop Schalter i++; motor2(); } void motor2() { if (i<150) { PORTB^= (1<<PB2); } else if (i>=150) { PORTB^= (1<<PB1); //Richtung ändern _delay_ms(5); i=0; u++; if (u==6) //6 Umdrehung hält Motor { u=0; loop_until_bit_is_clear (PINC,PC5); //warte bis start/stop schlater betätigt } } } int main() { sei(); InitPorts(); InitTimer1(); do{ // Taste betätigt, Schalter an(langsam mit f_min dreht der Motor) if(check_button(&PINC,PINC4,false)) { OCR1A =((18432000/8)/(2*1024*f_min))-1; } // Taste nicht betätigt, Schalter aus(schnell mit f_max dreht der Motor) else { OCR1A =((18432000/8)/(2*1024*f_max))-1; } } while(1); } Hat jemand eine Idee?
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.