I am trying to do uart program in pic. I want to display characters send by controller to pc. But i am not able to do this. following is my code
#include<pic.h>
__CONFIG(0x3f72);
#define FOSC 10000 //10Mhz==>10000Khz
#define BAUD_RATE 9.6 //9600 Baudrate
#define BAUD_VAL ((char)(FOSC/ (16 * BAUD_RATE )) – 1)
void main()
{
unsigned char ReceiveChar;
TRISC=0xc0; //RC7,RC6 set to usart mode(INPUT)
TXSTA=0x24; //Transmit Enable
SPBRG=BAUD_VAL; //9600 baud at 10Mhz
RCSTA=0x90; //Usart Enable, Continus receive enable
TXREG='0';
while(1)
{
if (RCIF==1) //char received? Send 'A' back to Terminal
{
ReceiveChar=RCREG;
if(TXIF==1)
TXREG=ReceiveChar;
}
}
}
Best Answer
Well your code looks OK, but do you know that microchip provides PIC18 peripheral library so you can directly use functions, instead of putting values to registers.
Following is the sample code you can use: