我目前正在使用PIC16F1829微控制器。然而,我卡在了中断例程的适当执行方法上。我希望中断例程能够退出所有函数(LED动画),这些函数在main()超级循环中以不同的开关情况列出。我应该使用一个全局变量,使函数在检测到中断标志后检查并更改它,还是可以只使用堆栈指针来实现此目的?我目前使用堆栈指针操作编写了ISR,但您能否请告诉我这是否是正确的方法?
void interrupt ISR(void){
if (TMR0IF && TMR0IE){ // (16MHz / (4 * 16 * 256)) [ 1.024 ms ]
TMR0IF = 0; // Clear Overflow Flag
if (++counter > 4531){ // 19531 * 1.024 ms [ ~ 20 sec ]
LED_Animation++; // Change the animation at a fixed time interval
counter = 0;
while (STKPTR > 1){ // STKPTR incremented due to an Interrupt
TOSH = 0; // TOSH:TOSL points to the Top of a Stack [ 0x00 ]
TOSL = 0;
STKPTR--; // Current value of the Stack Pointer
}
}
}
}
void main(){
STKPTR++; // Save this location in the stack
Sys_Init();
while(1){
switch(LED_Animation) {
case 0: LED_LizzardFeed(); break;
case 1: LED_Paparazzi(); break;
case 2: LED_FillnClear(); break;
case 3: LED_Slide(); break;
case 4: LED_MeetUp(); break;
case 5: LED_Chaser(); break;
default: LED_Animation = 0;
}
}