【问题标题】:interrupt based LEDs up counter on an atmega32atmega32 上基于中断的 LED 计数器
【发布时间】:2021-05-12 17:01:35
【问题描述】:

我正在设计一个基于中断的数字计数器,它使用 atmega32 显示在 8 个 LED 上递增的值。我的问题是我的 ISR(中断服务程序)无法点亮 LED,因为我从 INT0 递增下面是我制作的代码,只有 ISR 没有点亮 LED

enter image description here

【问题讨论】:

标签: c avr isr atmega32


【解决方案1】:

SR 中,count 变量在堆栈上。它的值不会在调用中持续存在。


这是您的原始代码[带注释]:

SR(INT0_vect)
{
    DDRA = 0xFF;
// NOTE/BUG: this is on the stack and is reinitialized to zero on each
// invocation
    unsigned char count = 0;

// NOTE/BUG: this will _always_ output zero
    PORTA = count;
// NOTE/BUG: this has no effect because count does _not_ persist upon return
    count++;

    return;
}

您需要添加static 才能使值持久化:

void
SR(INT0_vect)
{
    DDRA = 0xFF;
    static unsigned char count = 0;

    PORTA = count;
    count++;
}

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2022-10-24
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多