我一直在 8051 微控制器上编程,在处理中断时发现了一些奇怪的东西。假设发生了中断。在处理该中断时,正在发生另一个具有更高优先级的中断。控制器不应该跳入服务较高优先级的中断,然后返回到较低的中断吗?
以下说明了我的问题。连接键盘以触发外部中断 1(较低优先级)并启用定时器 0 中断(较高优先级)。
// With this snippet, the LED-s are blinking as expected.
#include <8051.h>
#include <stdint.h>
#include <stdbool.h>
__xdata __at (0x9000) uint8_t KEYPAD;
__xdata __at (0xA000) uint8_t LED;
uint8_t LedState = 0x00;
bool Running = false;
void StopperIsr() __interrupt TF0_VECTOR
{
LedState = ~LedState;
LED = LedState;
TR0 = 0; // Prevent the timer restating right away.
Running = false;
}
void StopperStart()
{
TL0 = 0;
TH0 = 0;
TR0 = 1; // Start timer 0
Running = true;
}
void main()
{
ET0 = 1; // Enable timer 0 interrupt.
EA = 1; // Enable global interrupts.
TMOD = T0_M0; // Set timer 0 to 16-bit mode.
while(1) {
if (false == Running) {
StopperStart();
}
}
}
// The stopper is used inside external interrupt 1 ISR and since timer 0 has
// higher priority, the LED-s should be blinking just like in the previous snippet.
// This is not the case. Instead, on keypress, the ISR is called (LED gets 0xFF),
// but timer 0 ISR is never entered.
#include <8051.h>
#include <stdint.h>
#include <stdbool.h>
__xdata __at (0x9000) uint8_t KEYPAD;
__xdata __at (0xA000) uint8_t LED;
uint8_t LedState = 0x00;
bool Running = false;
void StopperStart()
{
TL0 = 0;
TH0 = 0;
TR0 = 1; // Start timer 0.
Running = true;
}
void StopperIsr() __interrupt TF0_VECTOR
{
LedState = ~LedState;
LED = LedState;
TR0 = 0; // Stop the timer.
Running = false;
}
void KeypadIsr() __interrupt IE1_VECTOR
{
LedState = 0xFF;
LED = LedState;
while(1) {
if (!Running) {
StopperStart();
}
}
}
void main()
{
EX1 = 1; // Enable keypad interrupt on external interrupt 1.
ET0 = 1; // Enable timer 0 interrupt.
TMOD = T0_M0; // Set timer 0 to 16-bit mode.
EA = 1; // Enable global interrupts.
KEYPAD = 0; // Reset the keypad to its initial state.
}