簡體   English   中英

如何在 function 調用期間使用中斷將我的程序或 go 重置回主程序

[英]How do I use an interrupt to reset my program or go back to main during a function call

我已經嘗試了所有方法,並且我知道 (void (code *)(void)) 0) ();,但是如您所知,如果您使用它並不會清除中斷並且您不能使用它再次。 什么是解決方法,我必須使用基於我的硬件的外部中斷。 我研究了無數小時,並瀏覽了我的幾本書。 我開始相信這不可能? 代碼:

#include <reg51.h>
void delay();
int i,a;
sbit btn=P3^2;
sbit sw1=P0^1;
void mode1();
void reset (void);

void main()
{
    EA=1;
    EX0=1;
    ET0=1;
    EX1=1;

while(1){
    EA=1;
    EX0=1;
    ET0=1;
    EX1=1;
        if(sw1){
            mode1();
        }
        P1=0x01;
    }
}

void ex0() interrupt 0{
    if(!btn){
        //reset();
        Enable_Watchdog(); 
    }
}
/*
void reset (void){
(//(void (code *) (void)) 0x0000) ();
    (void (code *)(void)) 0) ();
}
    */

void mode1(){

    unsigned char repeat1 = 0x80;
    unsigned char repeat0 = 0x01;
    int g=0;
    int i =0;
    int j=0;
    EA=1;
    EX0=1;
    ET0=1;
    EX1=1;
        //how many leds it must go accross minus 1 since repeat 1 and 0

        while(sw1){
        EA=1;
        EX0=1;
        ET0=1;
        EX1=1;
        for(i =0; i <7; i++){
                P1=repeat1 >> i;
                delay();
            }
        for(j=0; j<7; j++){
            P1=repeat0 << j;
            delay();
        }
    }
}


void delay(){
    int k, j;
    for(k=0; k<0xff; k++)
        for(j=0;j<0xff;j++);

}



您在問題的代碼中已經擁有的看門狗啟用有什么問題? 它會起作用,但需要一些技巧:

void ex0() interrupt 0
{
    if(!btn)
    {
        EA = 0;   // Disable interrupts

        // Set watchdog counter and timeout 
        // to zero for immediate reset
        PCA0L    = 0x00;
        PCA0H    = 0x00;
        PCA0CPL2 = 0x00; 

        // Enable watchdog
        PCA0MD  |= 0x40;

        for(;;);  // Wait for reset
    }
}

如果可以的話,更好的解決方案是將 GPIO 連接到復位引腳(帶上拉電阻)並直接復位。 復位時,GPIO 將處於三態,因此通過上拉釋放復位。 初始化 GPIO 時需要注意不要立即將其拉低。 可能最好將其保留為三態輸入,直到您真正想要進行重置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM