c - 使用定时器中断使 LED 闪烁(atmega 128)

标签 c timer atmel

我正在尝试在不使用延迟功能的情况下使 LED 闪烁。 我遇到了使用定时器中断并尝试了它,它编译得很好。但输出固定为 PORTA = 0x01;所以,我认为 ISR 功能不起作用。代码中我缺少什么吗?谢谢。

#include <asf.h>
#include <avr/interrupt.h>

volatile unsigned int count=0;
volatile unsigned int tTime=0;

void port_init(void) {
    PORTA = 0xff;
}

ISR(TIMER0_OVF_vect)
{
    TCNT0 = 206;
    count++;
    if (count < 625) {
        PORTA=0x01;
    }
    else if ((count > 625) && (count < 1250)) {
        PORTA=0x00;
    }
    else {
        count=0;
    }
}

int main (void)
{ 
    board_init();
    port_init();

    TCCR0 = 0x06; //setting dispensing ratio
    TCNT0 = 206; //initial value of register
    TIMSK = 0x01; //enabling interrupt
    SREG=0x80;

    DDRA=0xff;
    while(1){
    }
}

最佳答案

您的ISR()中有逻辑错误功能。一次count前两位点击625 if/else-if条款将是 false (因为 625 既不小于也不大于 625),因此最终的 else子句将执行并重置 count为零。这样做的结果是设置 PORTA=0x00 的 else-if 子句永远不会执行。

要修复,请更改第一个 if来自<<= ,像这样:

if (count <= 625) {

关于c - 使用定时器中断使 LED 闪烁(atmega 128),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47709549/

相关文章:

C# - Windows 服务中的多个计时器

php - 启动和停止计时器 PHP

assembly - 汇编中定时器的问题 [ATmega8]

linux - 在 Intel x86 处理器上运行带有 Solaris OS(Sparc 体系结构)的 VM 将小 Endian 解析为大 Endian?

c - Malloc 无法在 atmega2561 和 freeRTOS 上分配内存

python - 在c或python中计算组合

c - 在平衡搜索树中插入元素

java - javax.swing.Timer 的困难

二维字符串数组的 C 内存布局

c - 如何理解“"main function' s prototype cannot provided by the program”?