First of all people are not going to check every thing of your code,so you should provide proper information ,anyway I am doing it for you…
As SYNC bit is 0, you are using asynchronous mode. You are using 12 MHz for clock and your baud rate is 9600 as you set BRGH = 1 and SPBRG = 77 or 0x4D.
Now about your problem, you can’t receive anything, and it’s normal you will never be able to receive anything as per your code. In your code you didn’t used any interrupts so you must continuously check for receiving data ,other wise you will miss it ,you used a for loop and couple of times you called the function to receive data ,you cant predict when the data will arrive you have to check it continuously. Even though you check it continuously you wouldn’t receive any data as your receiving logic is incorrect. You have to keep CREN bit always on to receive data at any moment.
The code should be something like this, the program will receive a char and increment it and send it back. Clock 12Mhz,baud rate 9600,one stop bit and no 9th bit and no flow control ,full duplex communication
Normally I use 18F , it have EUSART which have an additional register BAUDCON for controlling the baud generator, it’s a little better ,their more things to consider while doing asynchronous serial communication, like framing error and overrun error.
Code:
void main()
{
char temp = 0x00;
PORTC = 0x00;
TRISC = 0xFF;
SPBRG = 0x4D;
//Setting the TXSTA Register
TX9 = 0; // 6th bit - Selects 8-bit transmission
TXEN = 1; // 5th bit - Transmit enabled, any write to TXREG will cause transmission.
SYNC = 0; // 4th bit - Asynchronous mode
BRGH = 1; // 2nd bit - Low speed ,depends on baud rate calculation
//Setting the RXSTA Register
SPEN = 1; // 7th bit - Serial port enabled (configures RX/DT and TX/CK pins as serial port pins)
RX9 = 0; // 6th bit - Selects 8-bit reception
CREN = 1; // 4th bit - Enables receiver
ADDEN = 0; // 3rd bit - Disables address detection
while(1)
{
//Receive data
while(!RCIF){};
temp = RCREG;
//Transmit back data, increment the char and send it back
temp++;
while(!TXIF){};
TXREG = temp;
}
}
Good Luck
EDIT : I think EDA is having some kind problem ,it posted my post twice so now I have to edit it,and their is a huge problem with scrolling too.