gpt4 book ai didi

c - AVR编程,中断处理

转载 作者:太空狗 更新时间:2023-10-29 15:05:56 25 4
gpt4 key购买 nike

我使用 atmega168a-pu 和中断开发了一个 C 应用程序。我使用以下中断:

ISR(TIMER0_COMPA_vect);
ISR(TIMER0_COMPB_vect);
ISR (TIMER2_COMPA_vect);
ISR(SPI_STC_vect);
ISR(TIMER1_COMPA_vect);
ISR (PCINT1_vect);

我的代码看起来像

int main(void){
///initialization etc.
sei();
while(1){
///do some stuff and wait the interrupts
}
return 0;
}

我想在中断发生时阻止所有其他中断,并在退出中断函数之前启用中断。

你能用代码片段解释一下我该怎么做吗?

编辑:http://www.nongnu.org/avr-libc/user-manual/optimization.html#optim_code_reorder指出这种用法会导致重新排序问题。

function(){
cli();
..
sei();
}

最佳答案

我之前贴在这里的答案是基于原来的问题,没有提到avr-gcc的重新排序问题。显然,我使用 AVR 工作已经很久了,但是关于禁用中断的铃声响起

修改后的问题答案

保护中断不被打断

Atmel 在数据手册中写到中断处理:

When an interrupt occurs, the Global Interrupt Enable I-bit is cleared and all interrupts are disabled. The user software can write logic one to the I-bit to enable nested interrupts. All enabled interrupts can then interrupt the current interrupt routine. The I-bit is automatically set when a Return from Interrupt instruction – RETI – is executed.

因此您要求的行为已经在硬件中实现。

重新排序问题

我也对这个重新排序问题做了一些调查。显然,这是否是编译器的错误存在巨大分歧。似乎重新排序的主要风险是中断被禁用的时间比预期的要长。在我的研究过程中,我没有找到解决方案,除了导致更多加载/存储事件的解决方案,我认为这不是一个真正的选择。

关于c - AVR编程,中断处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17680879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com