WiDo定时器程序编译不通过
Arduino Uno上的程序移植到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() );
} wido 好像是Leonardo,不是UNO的主控吧
恩恩,但是都是AVR的处理器啊,Leonardo是ATmega32U4,也有定时器2 本帖最后由 Eric 于 2014-12-14 20:34 编辑
因为你的库文件没有更新,你的库里面没有更新,在libraries\MsTimer2文件夹里的MsTimer2.cpp里面写的,你的库应该没有这句话,不知道我说的对不对
库文件压缩包我放附件里了,你试试看,能否解决
我记得没有错的话,atmega32u4内核的主控是没有timer2的。。。。o(╯□╰)o
楼主你可以了解下timer4,路线二是去搜一下google,arduino leonardo compatible timer library。。。 Eric 发表于 2014-12-14 20:31
因为你的库文件没有更新,你的库里面没有更新,在libraries\MsTimer2文件夹里的MsTimer2.cpp里面写的,你的库 ...
eric大神,牛逼,兼容的库都找到了。。。 lauren 发表于 2014-12-14 23:26
eric大神,牛逼,兼容的库都找到了。。。
因为吃过这亏,所以记忆犹新啊,想当年也是哥哥你交给我地,嘿嘿嘿
上图是atmega32U4的定时器概述,截图自atmega32u4的数据手册,确实没有定时器2。
因此那个库的意思应该是,如果使用了Leonardo,那么切换到它的定时器4来兼容,防止没有定时器2出现错误。
谢谢各位,可是加载了那个库之后编译还是不通过 liuximing 发表于 2014-12-15 12:20
谢谢各位,可是加载了那个库之后编译还是不通过
加载了库之后你的代码要改的。。。timer2相关的代码都是不可能编译过得,学着用库里面的函数和功能吧! liuximing 发表于 2014-12-15 12:20
谢谢各位,可是加载了那个库之后编译还是不通过
不过有库,使用定时器很方便的,肯定比你自己写timer配置方便多了:lol 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
} TIMER_INTR_NAME预定义如下:
#if defined(__AVR_ATmega32U4__)
#define TIMER_INTR_NAME TIMER1_OVF_vect
#else
#define TIMER_INTR_NAME TIMER2_OVF_vect
#endif
页:
[1]