hi folks,
I am trying to communicate with PC using RS232 and PIC18F4550. I am using following circuit diagram as it is and code attached. But I am not getting desired output. I really can't understand what is the problem. Please help me as early as possible.
The Code I am exactly using is..
// Program to depict the configuration of EUSART in PIC18F4550
// This code receives and then transmits the same data back to the PC .. // ..through PC's Serial Port
#include<p18f4550.h>
#include<usart.h>
// Configuration bits
/* _CPUDIV_OSC1_PLL2_1L, // Divide clock by 2
_FOSC_HS_1H, // Select High Speed (HS) oscillator
_WDT_OFF_2H, // Watchdog Timer off
MCLRE_ON_3H // Master Clear on
*/
void tx_data(unsigned char);
unsigned char rx_data(void);
unsigned char serial_data;
unsigned int i=0;
#define FREQ 12000000 // Frequency = 12MHz
#define baud 9600
#define spbrg_value (((FREQ/64)/baud)-1) // Refer to the formula for Baud rate calculation in Description tab
void main(void)
{
SPBRG=spbrg_value; // Fill the SPBRG register to set the Baud Rate
RCSTAbits.SPEN=1; // To activate Serial port (TX and RX pins)
TXSTAbits.TXEN=1; // To enable transmission
RCSTAbits.CREN=1; // To enable continuous reception
while(1)
{
serial_data=rx_data(); // Receive data from PC
tx_data(serial_data); // Transmit the same data back to PC
}
}
void tx_data(unsigned char data1)
{
TXREG=data1; // Store data in Transmit register
while(PIR1bits.TXIF==0); // Wait until TXIF gets low
}
unsigned char rx_data(void)
{
while(PIR1bits.RCIF==0); // Wait until RCIF gets low
return RCREG; // Retrieve data from reception register
}
Please tell me what is the mistake and what should I do to get the desired output..!!!!
SERIAL COMMUNICATION USING PIC18F4550