Skip to content

Instantly share code, notes, and snippets.

@ste2425
Created May 17, 2016 19:09
Show Gist options
  • Save ste2425/cdba7c5557234f08f1a25d9ff2e2922b to your computer and use it in GitHub Desktop.
Save ste2425/cdba7c5557234f08f1a25d9ff2e2922b to your computer and use it in GitHub Desktop.
flawed uart for pic. It resets.
#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");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment