• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

Pic UART resetting.

Joined
May 27, 2008
Messages
3,629 (0.58/day)
System Name Ultra 64
Processor NEC VR4300 (MIPS R4300i)
Motherboard proprietary design
Cooling Fanless aircooled
Memory 4.5MB 250 MHz RDRAM
Video Card(s) 62.5 MHz Reality Coprocessor
Storage 32 - 512 Mbit ROM Cartridge
Display(s) 720x576
Case Clear Blue Funtastic
Audio Device(s) 16-bit CD quality
Power Supply proprietary design
Mouse N64 mouse for use with N64DD
Keyboard N64 keyboard for use with N64DD
So ive been exploring working with PIC microcontrollers. The pic in question is a pic16f688 and heres the datasheet.

So ive been trundling along ok without any real debugging (as a JavaScript C# dev that was a challenge in itself) but now im playing with the UART to provide serial output so i can print some debugging info to the computer and herein lies my issue.

Essentially the pic seems to be resetting itself. The code compiles and runs fine then after about a second or two seems to reset and repeat. I determine its resetting because in my `main` i print (through the uart) out `conected` and this is being logged every couple of seconds. This should only run once when it first powers up.

Im using the mplab X with the free version of the xc8 compiler.

So i have no idea what could cause it or even where to begin trouble shooting. The code i adapted the https://github.com/g4lvanix/PIC-tutorials/blob/master/USART.X/main.c code for my pic. Mostly pointing to different ports, in my case port C instead of B.

Heres the full code heres gist, easier to read:

Code:
#define _XTAL_FREQ 4000000
#include <pic16f688.h>
#include <xc.h>

// CONFIG
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSC oscillator: CLKOUT function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select bit (MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown Out Detect (BOR enabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor
//#pragma config CLKOUTEN = OFF

#define STRLEN 12

volatile unsigned char t;
volatile unsigned char rcindex;
volatile unsigned char rcbuf[STRLEN];

void USART_init(void)
{
    TXSTAbits.TXEN = 1;     // enable transmitter
    TXSTAbits.BRGH = 1;     // high baud rate mode
    RCSTAbits.CREN = 1;     // enable continous receiving

    // configure I/O pins
    //ANSELbits.ANSB5 = 0;   // RX input type is digital
    TRISCbits.TRISC4 = 1;     // RX pin is input
    TRISCbits.TRISC5 = 1;     // TX pin is input (automatically configured)

    SPBRG = 25;            // set baud rate to 9600 baud (4MHz/(16*baudrate))-1

    //PIE1bits.RCIE = 1;      // enable USART receive interrupt
    RCSTAbits.SPEN = 1;     // enable USART
}

void USART_putc(unsigned char c)
{
    while (!TXSTAbits.TRMT); // wait until transmit shift register is empty
    TXREG = c;               // write character to TXREG and start transmission
}

void USART_puts(unsigned char *s)
{
    while (*s)
    {
        USART_putc(*s);     // send character pointed to by s
        s++;                // increase pointer location to the next character
    }
}

void main(void)
{
//    OSCCONbits.IRCF = 0x0D; // INTOSC frequency 4MHz

    USART_init();

    USART_puts((unsigned char *)"Init complete!\n");

  //INTCONbits.PEIE = 1;    // enable peripheral interrupts
  //INTCONbits.GIE = 1;     // enable interrupts

    while(1)
    {
        __delay_ms(500);
        USART_puts((unsigned char *)"test\n");
    }

}

I generally dont like 'Heres my whole app find the problem' questions but i am really clueless as to where to narrow it down sorry.

Any help would be hugely appreciated.
 
9cc.gif


Dam its the watchdog timer, its enabled but im not resetting it so it resets my pic for me.

EDIT: Even more annoying i think this is the cause of some strange behavior in my other projects which caused me to look into the serial coms for debugging. Oh well at least this has been a good exercise.
 
Last edited:
Back
Top