简体   繁体   中英

XC8 compiler: program returning to beginning of main()

I'm new to programming PIC microcontrollers, and I've been having some difficulty with a piece of code. In the main() function, it sets some registers, then enters an infinite while loop. In this while loop, a second function is called, which creates a delay (without calling any other functions, including main() then returns. However, it seems that when the second function ends, the program goes back to the top of main() , rather than continuing in the while loop.

Edit 1: I've done some more tests, and I still get the problem without the function, so it's almost certain that the microcontroller is resetting every second or so, for some unknown reason. There's supposed to be a register that tells you the reason for the reset, so I'll have a look at that.

Edit 2: Here is a very basic version of the code, in which the RC0 LED flashes briefly every 2 seconds (it should stay off).

void main()
{
    TRISC = 0x00;
    PORTCbits.RC0 = 1;
    OSCCON = 0b00000010;
    TRISA = 0x00;
    PORTA = 0x00;
    while(1)
    {
        PORTCbits.RC0=0;
    }
}

Edit 3: It turns out it was the watchdog timer causing the resets, fixed now by putting a CLRWDT() in the loop.

Could be quite a few things, checking the RCON or equivalent register should help discern the cause, also provide the part number and part voltage. Some of the possibles are: - Watchdog timer tripping, - Brown-out on protection tripping, - Stack over/underflow, - Your code is jumping off into space (bad pointer?), - Your code is performing a software reset, - Your dividing by 0, - others...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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