我有一个 Atmega328p,我已经将一个 LED 连接到它的 D4 引脚,我希望它每秒钟打开/关闭一次 LED。
我找到了本教程,并根据一些在线 AVR 定时器计算器和我使用的 12MHZ 外部晶体将其更改为:
#define F_CPU 12000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
int main(void)
{
DDRD |= 1 << 4;
PORTD |= 1 << 4;
ICR1 = 0xB71B;
TCCR1B |= (1 << WGM12);
// Mode 4, CTC on OCR1A
TIMSK1 |= (1 << ICIE1);
//Set interrupt on compare match
TCCR1B |= (1 << CS12);
// set prescaler to 256 and starts the timer
sei();
// enable interrupts
while (1)
{
// we have a working Timer
}
}
ISR (TIMER1_COMPA_vect)
{
PORTD |= 0 << 4;
// action to be done every 200ms
}
无论我如何更改 ICR1 值,LED 总是亮或灭。我怎样才能让它工作?