7042| 14
|
[已解决] WiDo定时器程序编译不通过 |
//**************************** Function Set up Timer2 ***************************** int SetupTimer2() { TCCR2A = 0; //Timer2 mode 0, normal operating mode TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20; //clock-select value for divide-by-1024 prescale return(0); //same as TCCR2B = 0x07 } //************************* Timer2 Interrupt Svc Routine ************************** //Timer2 overflow interrupt-service routine (ISR) ISR(TIMER2_OVF_vect) { TCNT2=Timer2_start_value; timer_count++; } //**************************** Function Serial-Input ****************************** //function to read serial port as soon as it has data ready byte SerialInput_ND() { timer_count = 0; TCNT2 = Timer2_start_value; TIMSK2 = 1<<TOIE2; while (Serial.available() == 0) { if (timer_count > 255) { TIMSK2 = 0<<TOIE2; return (0); } } TIMSK2 = 0<<TOIE2; return ( Serial.read() ); } |
void Timerszcl::set(unsigned long ms, void (*f)()) { float prescaler = 0.0; #if defined (__AVR_ATmega168__) || defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1280__) || defined (__AVR_ATmega2560__) TIMSK2 &= ~(1<<TOIE2); TCCR2A &= ~((1<<WGM21) | (1<<WGM20)); TCCR2B &= ~(1<<WGM22); ASSR &= ~(1<<AS2); TIMSK2 &= ~(1<<OCIE2A); if ((F_CPU >= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64 TCCR2B |= (1<<CS22); TCCR2B &= ~((1<<CS21) | (1<<CS20)); prescaler = 64.0; } else if (F_CPU < 1000000UL) { // prescaler set to 8 TCCR2B |= (1<<CS21); TCCR2B &= ~((1<<CS22) | (1<<CS20)); prescaler = 8.0; } else { // F_CPU > 16Mhz, prescaler set to 128 TCCR2B |= ((1<<CS22) | (1<<CS20)); TCCR2B &= ~(1<<CS21); prescaler = 128.0; } #elif defined (__AVR_ATmega8__) TIMSK &= ~(1<<TOIE2); TCCR2 &= ~((1<<WGM21) | (1<<WGM20)); TIMSK &= ~(1<<OCIE2); ASSR &= ~(1<<AS2); if ((F_CPU >= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64 TCCR2 |= (1<<CS22); TCCR2 &= ~((1<<CS21) | (1<<CS20)); prescaler = 64.0; } else if (F_CPU < 1000000UL) { // prescaler set to 8 TCCR2 |= (1<<CS21); TCCR2 &= ~((1<<CS22) | (1<<CS20)); prescaler = 8.0; } else { // F_CPU > 16Mhz, prescaler set to 128 TCCR2 |= ((1<<CS22) && (1<<CS20)); TCCR2 &= ~(1<<CS21); prescaler = 128.0; } #elif defined (__AVR_ATmega128__) TIMSK &= ~(1<<TOIE2); TCCR2 &= ~((1<<WGM21) | (1<<WGM20)); TIMSK &= ~(1<<OCIE2); if ((F_CPU >= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64 TCCR2 |= ((1<<CS21) | (1<<CS20)); TCCR2 &= ~(1<<CS22); prescaler = 64.0; } else if (F_CPU < 1000000UL) { // prescaler set to 8 TCCR2 |= (1<<CS21); TCCR2 &= ~((1<<CS22) | (1<<CS20)); prescaler = 8.0; } else { // F_CPU > 16Mhz, prescaler set to 256 TCCR2 |= (1<<CS22); TCCR2 &= ~((1<<CS21) | (1<<CS20)); prescaler = 256.0; } #elif defined (__AVR_ATmega32U4__) TIMSK1 &= ~(1<<TOIE1); TCCR1A &= ~((1<<WGM11) | (1<<WGM10)); TCCR1B &= ~((1<<WGM12) | (1<<WGM13)); TCCR1C = 0; TIMSK1 &= ~(1<<OCIE1A); if ((F_CPU >= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64 TCCR1B |= (1<<CS10) | (1<<CS11); TCCR1B &= ~(1<<CS12); prescaler = 64.0; } else if (F_CPU < 1000000UL) { // prescaler set to 8 TCCR1B |= (1<<CS11); TCCR1B &= ~((1<<CS12) | (1<<CS10)); prescaler = 8.0; } else { // F_CPU > 16Mhz, prescaler set to 128 TCCR1B |= (1<<CS12); TCCR1B &= ~((1<<CS11) | (1<<CS10)); prescaler = 128.0; } #endif tcnt2 = 256 - (int)((float)F_CPU * 0.001 / prescaler); if (ms == 0) msecs = 1; else msecs = ms; func = f; } |
相应的定时器中断处理如下: ISR(TIMER_INTR_NAME) { #if defined (__AVR_ATmega168__) || defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1280__) || defined (__AVR_ATmega2560__) TCNT2 = Timerszcl::tcnt2; #elif defined (__AVR_ATmega128__) TCNT2 = Timerszcl::tcnt2; #elif defined (__AVR_ATmega8__) TCNT2 = Timerszcl::tcnt2; #elif defined (__AVR_ATmega32U4__) TCNT1H = 255; TCNT1L = 0; #endif } |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed