// RS232 serial IO functions
#include <htc.h>
//**********************************************************
//* Define serial communications control functons
void UARTInit(void)
{
OSCCON=0x71; // Fosc use internal oscillator 8MHz
TXSTA=0; // set up transmitter
TXEN=1; // Asychronous, 8 bit, BRGH=1
BRGH=1;
TRMT=1;
TRISC6=1;
RCSTA=0; // set up receiver
SPEN=1; // Asychronous, 8 bit, continuous receive, serial enable
CREN=1;
TRISC7=0;
// calculate Baud Rate = FOSC/(16 (X + 1))
//#define BaudRate 51 //SPBRG value for 9600 Baud when Fosc=8MHz and BRGH=1
#define BaudRate 8 //SPBRG value for 57600 Baud when Fosc=8MHz and BRGH=1
SPBRG=BaudRate; // setup baud rate
}
// put a char to RS232 serial line
int putchar(const int ch)
{
while(!TRMT);
TXREG=ch;
//delay(1);
}
// put a string to RS232 serial line
void putString(const char *ch)
{
while(*ch>0) { putchar(*ch); ch++; }
}
// check if a character has ben received - set true if so
int uartReceived(void)
{
return RCIF; // return character received
}
// get a character from serial line
int getchar(void)
{
while(!uartReceived()); // wait for a character received
return RCREG; // return the character
}