I've been working on and off this first time microchip for a year, I should be considered a total novice on the chip and with C programming,
Ultimately I want these chips to send simple 8 character async strings RS232 to dumb receivers. For now, I'm trying to prove life of the serial transmit and receive on the 16F1829 Microchip here on my table top.
I'm using the Pickit3 programmer with a the low pin count demo board (03-01831). I've connected the chips pins RC4 and RC5 via jumper (should be serial TX and RX).
The idea is I configure the chip (async, 9600ish baud, TX enable, RX enable, etc), then write to the TX, which gets jumped to the RX, then read from teh RX and confirm the loop with a lit LED. But so far, no luck.
Again, I'm a beginner, so don't assume anything is not overlooked.
--------------------------
Ultimately I want these chips to send simple 8 character async strings RS232 to dumb receivers. For now, I'm trying to prove life of the serial transmit and receive on the 16F1829 Microchip here on my table top.
I'm using the Pickit3 programmer with a the low pin count demo board (03-01831). I've connected the chips pins RC4 and RC5 via jumper (should be serial TX and RX).
The idea is I configure the chip (async, 9600ish baud, TX enable, RX enable, etc), then write to the TX, which gets jumped to the RX, then read from teh RX and confirm the loop with a lit LED. But so far, no luck.
Again, I'm a beginner, so don't assume anything is not overlooked.
--------------------------
Code:
#include <xc.h>
long int x, count;
char serial_out, serial_in;
#pragma config FOSC=INTOSC
void main(void) {
OSCCON = 0b01110000; //Set internal Osc speed to 8 MHz
APFCON0bits.RXDTSEL = 0; //Set RC5 as Serial RX
APFCON0bits.TXCKSEL = 1; //Set RC4 as Serial TX
ANSELC=0; //Set PortC to Digital
TXEN = 1; //Enable TX Circuit
SYNC = 0; //Asynchronous Operation (RX and TX)
SPEN = 1; //Configure RC4 as serial output
CREN = 1; //Enable RX Circuit
TRISCbits.TRISC4 = 0; //Set RC4 as output
TRISCbits.TRISC5 = 1; //Set RC5 as input
//Baud rate setting for 9600
SPBRGH=0; //=0
SPBRGL=0b00001100; //=12
BRGH=0; //Low speed baud
BRG16=0; //Using 8bit baud generator
TRISCbits.TRISC0 = 0; //using pin as output (LEDs)
TRISCbits.TRISC1 = 0; //using pin as output (LEDs)
TRISCbits.TRISC2 = 0; //using pin as output (LEDs)
TRISCbits.TRISC3 = 0; //using pin as output (LEDs)
LATC = 0; //clear all LEDs
serial_out = 'a'; //Char to transmit
count=0;
while(1){ //Forever loop
//Transmit one character
TXREG = serial_out;
while (TXIF==0 || count<10) { //Wait for Transmit Complete
count++;
}
count=0;
//Receive one character
serial_in = RCREG;
if ( serial_in == 'a' ){
LATCbits.LATC2 = 1; //Data Received
}
else {
LATCbits.LATC2 = 0;
}
if ( TXREG == 'a' ){
LATCbits.LATC3 = 1; //Data Sending
}
else {
LATCbits.LATC3 =0;
}
}
}