问题描述
我正在尝试使用 timer0 中断用 atmega328p 制作一个闪烁的 LED。
这是一个非常基本的东西,但 microchip Studio 给了我这个:
========== Build: 0 succeeded or up-to-date,1 Failed,0 skipped=======
#include<avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
#define F_cpu 1000000UL
int main(void)
{
//define inputs and outputs
DDRC = (1<<DDC4); // portc4 as output
TCNT0 = 0x00;
TCCR0A = 0x00;
TCCR0B = (1<<CS00) | (1<<CS01);
TimsK0 = (1<<TOIE0);
sei(); // set global interrupts
while (1)
{
PORTC4 |= (1<<PORTC4);
}
}
ISR(TIMER0_OVF_vect) {
TCNT0 = 0x00;
PORTC &= ~(1<<PORTC4);
}
解决方法
纠正了一些事情,也许这会有所帮助:
#define F_CPU 1000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
ISR(TIMER0_OVF_vect) {
// TCNT0 = 0x00; // Not necessary,it is done by hardware
PORTC = ~(1<<PINC4);
}
int main(void)
{
//define inputs and outputs
DDRC = (1<<PINC4);
// TCNT0 = 0x00; // Autoset to zero after initialisation
// TCCR0A = 0x00; // Autoset at startup
// Timer 0
// Mode: Overflow,Interrupt
// Prescaler: /64
TCCR0B = (1<<CS01) | (1<<CS00);
TIMSK0 = (1<<TOIE0);
sei(); // set global interrupts
while (1)
{
// PORTC |= (1<<PINC4); // is done in interrupt
}
}
Build succeeded.
========== Rebuild All: 1 succeeded,0 failed,0 skipped ==========