Search code examples
cxc8

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.


Solution

  • 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...