我正在尝试在不使用延迟功能的情况下使 LED 闪烁。 我遇到了使用定时器中断并尝试了它,它编译得很好。但输出固定为 PORTA = 0x01;所以,我认为 ISR 功能不起作用。代码中我缺少什么吗?谢谢。
#include <asf.h>
#include <avr/interrupt.h>
volatile unsigned int count=0;
volatile unsigned int tTime=0;
void port_init(void) {
PORTA = 0xff;
}
ISR(TIMER0_OVF_vect)
{
TCNT0 = 206;
count++;
if (count < 625) {
PORTA=0x01;
}
else if ((count > 625) && (count < 1250)) {
PORTA=0x00;
}
else {
count=0;
}
}
int main (void)
{
board_init();
port_init();
TCCR0 = 0x06; //setting dispensing ratio
TCNT0 = 206; //initial value of register
TIMSK = 0x01; //enabling interrupt
SREG=0x80;
DDRA=0xff;
while(1){
}
}
最佳答案
您的ISR()
中有逻辑错误功能。一次count
前两位点击625 if/else-if
条款将是 false
(因为 625 既不小于也不大于 625),因此最终的 else
子句将执行并重置 count
为零。这样做的结果是设置 PORTA=0x00 的 else-if 子句永远不会执行。
要修复,请更改第一个 if
来自<
至<=
,像这样:
if (count <= 625) {
关于c - 使用定时器中断使 LED 闪烁(atmega 128),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47709549/