基本上,测试代码的目的是: *启动计时器(硬件计时器) - 打开两个LED - 等到计时器结束 - 关闭两个LED - 延迟0.9秒 - 重新开始。
for (;;) //forever
{
PORTB &= ~_BV(PINB7); //Turn OFF GREEN LED
_delay_ms(900); //RED LED stay off for 0.9s
timer_two(); // start 5 second timer
while(!(TIFR1 & _BV(OCF1A))) //WHILE 5 second timer flag is not set
{
PORTB |= _BV(PINB7); //Turn on GREEN LED
PORTA |= _BV(PINA6); //Turn on RED LED
} //leave while loop when five second timer flag is set
PORTA &= ~_BV(PINA6); //Turn off RED LED #THIS DOESN'T HAPPEN#
}
但是,按照原样编写的代码,两个LED都会亮起,但只有绿色LED熄灭。绿色LED继续像我预期的那样开启和关闭。
虽然它明显退出了while循环(因为它关闭了绿色LED),但它似乎不会在它之后执行任何操作。
我在这里错过了一个基本技巧吗?
(代码是针对AVR编译的,atmega644p是通过avr-gcc编译的,在Windows上)
答案 0 :(得分:1)
现在解决了。
由于定时器产生中断,LED根据定时器闪烁。然后程序将重新启动,因为没有给出中断处理程序。这将重置计时器启动前设置的LED。
禁用所有中断可使代码正常运行。cli(); //disable interrupts
答案 1 :(得分:0)
for (;;) //forever
{
PORTB &= ~_BV(PINB7); //Turn OFF GREEN LED
_delay_ms(900); //RED LED stay off for 0.9s
timer_two(); // start 5 second timer and executes all lines till the end
while(!(TIFR1 & _BV(OCF1A))) //WHILE 5 second timer flag is not set
{
PORTB |= _BV(PINB7); //Turn on GREEN LED
PORTA |= _BV(PINA6); //Turn on RED LED
} //leave while loop when five second timer flag is set
_delay_ms(4000); // delays the while
//OR while will be executed for 4sec
PORTA &= ~_BV(PINA6); //Turn off RED LED for 1sec
}
答案 2 :(得分:0)
如果你没有得到我所说的话,那就来执行......
你的时间只被执行一次..
或者你可以说你没有运行while循环..
您的运行代码是:
for (;;)
{
PORTB &= ~_BV(PINB7);
_delay_ms(900);
timer_two();
PORTB |= _BV(PINB7);
PORTA |= _BV(PINA6);
PORTA &= ~_BV(PINA6);
}
答案 3 :(得分:-1)
你的RED在执行时总是关闭..?
设置硬件计时器以执行
现在,它以晶体速度执行while(即,必须小于5ms)并执行最后一行4995ms的剩余时间。
得到了执行..?