c - 如何在 atmega328p 中启用 timer0 中断?
问题描述
我正在尝试使用 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 ==========
推荐阅读
- php - 基于地图数组创建目录树
- scala - scala 和 URLClassLoader 的意外行为
- wso2 - 在 siddhi 中合并来自两个流的属性
- ios - 未找到 Mapbox Geocoder 框架
- c - C - 将反转的文件内容打印到标准输出
- python - 为什么 iloc 使用 [] 而不是 ()?
- python - 无法将用户和密码发送到 FancyURLopener 缺少 2 个必需的位置参数:'user' 和 'passwd'
- matlab - UITable 不断进行双 CellSelection 回调
- angular - 使用 recordrtc 进行 Webrtc 电话会议录音
- actions-on-google - 在谷歌上上传关于行动的琐事问题表时面临问题