[英]How to enable timer0 interrupt in atmega328p?
我正在嘗試使用 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 ==========
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.