简体   繁体   English

如何在 function 调用期间使用中断将我的程序或 go 重置回主程序

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

I've tried everything, and I am aware of the (void (code *)(void)) 0) ();, but as you know if you use it doesn't clear back the interrupts and you can't use it again.我已经尝试了所有方法,并且我知道 (void (code *)(void)) 0) ();,但是如您所知,如果您使用它并不会清除中断并且您不能使用它再次。 What is a workaround, and I have to use an external interrupt based on my HW.什么是解决方法,我必须使用基于我的硬件的外部中断。 I've researched countless hours and looked through several of my books.我研究了无数小时,并浏览了我的几本书。 I'm starting to believe it may not be possible?我开始相信这不可能? Code:代码:

#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++);

}



What is wrong with the watchdog enable you already have in the code in the question?您在问题的代码中已经拥有的看门狗启用有什么问题? It will work, but needs soem finessing:它会起作用,但需要一些技巧:

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
    }
}

A better solution if you are able is to connect a GPIO to the reset pin (with a pull-up) and reset it directly.如果可以的话,更好的解决方案是将 GPIO 连接到复位引脚(带上拉电阻)并直接复位。 On reset, the GPIO will tri-state, so release the reset - by virtue of the pull-up.复位时,GPIO 将处于三态,因此通过上拉释放复位。 You need to take care when initialising the GPIO that you do not immediately pull it low.初始化 GPIO 时需要注意不要立即将其拉低。 Probably best to leave it as a tri-stated input until you actually want to effect a reset.可能最好将其保留为三态输入,直到您真正想要进行重置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM