小编Vin*_*zal的帖子

在中断例程中使用 C++ 对象(和 volatile)的正确方法是什么?

我目前正在使用 Atmel AVR 微控制器 (gcc),但希望答案一般适用于微控制器世界,即通常是单线程但带有中断。

我知道volatile在访问可以在 ISR 中修改的变量时如何在 C 代码中使用。例如:

uint8_t g_pushIndex = 0;
volatile uint8_t g_popIndex = 0;
uint8_t g_values[QUEUE_SIZE];

void waitForEmptyQueue()
{
    bool isQueueEmpty = false;
    while (!isQueueEmpty)
    {
        // Disable interrupts to ensure atomic access.
        cli();
        isQueueEmpty = (g_pushIndex == g_popIndex);
        sei();
    }
}

ISR(USART_UDRE_vect) // some interrupt routine
{
    // Interrupts are disabled here.
    if (g_pushIndex == g_popIndex)
    {
        usart::stopTransfer();
    }
    else
    {
        uint8_t value = g_values[g_popIndex++];
        g_popIndex &= MASK;
        usart::transmit(value);
    }
}
Run Code Online (Sandbox Code Playgroud)

因为 …

c++ interrupt volatile isr

5
推荐指数
1
解决办法
1281
查看次数

标签 统计

c++ ×1

interrupt ×1

isr ×1

volatile ×1