user3893595
user3893595

Reputation: 53

Program exits endless loop (PIC microcontroller)?

I wrote a program for the PIC 16f690 microcontroller after noticing my programs seemed to be exiting an endless loop.

There are LEDs on pins 3,4,5 of PORTC.

#include <xc.h>
#define _XTAL_FREQ 4000000

void main(void) {

    TRISC = 0x00;
    PORTC = 0b00111000;

    while (1)
    {
        __delay_ms(1000);
        PORTC = 0x00;
    }
}

As far as I understand, the LEDS should be on for ~1sec, and then be off forever. Instead they keep blinking every second.

I wrote this in MPLABX and programmed using PICkit3 and C8 compiler.

Upvotes: 5

Views: 2346

Answers (2)

Mariano Ruiz
Mariano Ruiz

Reputation: 16

I had the same issue but in place of leds, I was using serial communications. while(1) was not really a loop forever and execute the stuff between brackets.

As soon as I disabled the watchdog, only the stuff inside the brackets was called after the first iteration.

#pragma config WDTE = OFF        // Watchdog Timer Enable (WDT enabled)

I was also observing trash after every uart packet and that went away with the watchdog disabled.

FYI pic 16f1708.

This caused some erratic behavior since the uart initialization functions, IO setup, and oscillator divider were called every loop iteration.

Upvotes: 0

ouah
ouah

Reputation: 145899

You are likely being bitten by the watchdog. Disable the watchdog for your tests or clear it before it reset the MCU.

Upvotes: 6

Related Questions