【发布时间】:2021-06-11 23:35:39
【问题描述】:
我正在尝试使用 ISR 使 LED 闪烁 3 秒,这里使用了 atmega328p。我正在尝试使用 TIMER1(16 BIT) 在 isr 中创建 1 秒延迟,然后将其循环 3 秒。 LED连接到PD7,我不知道为什么它不闪烁,谁能指出错误。我用的是simulIDE,这是电路,timer_circuit
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define SET_BIT(PORT,BIT) PORT |= (1<<BIT)
#define CLR_BIT(PORT,BIT) PORT &= ~(1<<BIT)
unsigned int count = 0;
void timer()
{
TCCR1A = 0x00; // Normal mode of operation
TCNT1 = 0xC2F8; // decimal value is 49912
TCCR1B |= ((1 << CS10) | (1 << CS12));
TCCR1B &= ~(1 << CS11); //1024 prescaler
sei(); // Global interrupt
}
int main(void)
{
SET_BIT(DDRD,PD7);
timer();
while(1)
{
TIMSK1 |= TOIE1;
if(count>=3)
{
SET_BIT(PORTD,PD7);
count=0;
}
}
;
return 0;
}
ISR(TIMER1_OVF_vect)
{
count++;
}
【问题讨论】:
-
别忘了清除中断。这通常涉及写入控制寄存器,或者是读取/写入特定数据寄存器的副作用。