我一直在編程8051微控制器,並且在處理中斷時發現了一些奇怪的東西。假設發生了中斷。服務該中斷時,會發生另一個具有較高優先級的中斷。控制器不應該跳轉到服務於較高優先級的中斷,然後返回到較低優先級的中斷嗎?8051μCU - 如何處理併發中斷?
以下說明我的問題。連接鍵盤以觸發外部中斷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.
}
第二個示例缺少while(1);'main() –
而且它也缺少中斷優先級設置。 –
@TurboJ我可以在星期一到達控制器,但我相信優先級設置是我錯過的。出於某種原因,我有一個印象,優先號碼確定。諮詢數據表讓我意識到情況並非如此。謝謝! – gilden