fosc =20.000000void UART_Init(void){
RC1STAbits.SPEN=0;// begin of setup disable serial port.// transmitter
TX1STAbits.TXEN=1;// continues transmit enable bit
TX1STAbits.TX9=0;// 8 bit transmission
TX1STAbits.SYNC=0;// asynchronous operation
ANSELCbits.ANSC6=0;// digital
TRISCbits.TRISC6=0;// output// receiver
RC1STAbits.CREN=1;// continues receive enable bit
RC1STAbits.RX9=0;// there is only one sync
ANSELCbits.ANSC7=0;// digital
TRISCbits.TRISC7=1;// input// baudrate.
TX1STAbits.BRGH=1;
BAUD1CONbits.BRG16=1;
SPBRG =520;// baudrate 9600
RC1STAbits.SPEN=1;// end of setup enable serial port}void UART_TxChar(uchar ch){while(TXIF==0);
TXREG=ch;}
uchar UART_RxChar(){
uchar Discard;while(1){while(RCIF==0);// Wait till the data is received if(RCSTAbits.FERR){
Discard = RCREG; Read the register and wait for the next byte
break;}if(RCSTAbits.OERR){
RCSTAbits.CREN=0;break;}return RCREG;// Return the received data }}
I would recommend that you use the TRMT bit instead of the TXIF bit. I know this chip is a bit different from most other PIC MCUs in that the TXIF is set when the Tx side is idle (rather than at the end of a transmission or on initialisation) and your code should work, but it will lead you into trouble when you try to use this technique on other MCUs.
Can you please show us the config settings?
Also I assume that the 'fosc = 20.00000' is just to tell us the crystal frequency and not a valid instruction (which it isn't).
Also which compiler are you using?
Susan