pranavm1502
Newbie level 6
Hi,
I have written this code to check out USART protocol on PIC18F4550 (44-pin). Tx and Rx are shorted with wire. The LED connected to RB3 and ground should go off when the data is transmitted.
It is not at all working. Please help in debugging.
I have written this code to check out USART protocol on PIC18F4550 (44-pin). Tx and Rx are shorted with wire. The LED connected to RB3 and ground should go off when the data is transmitted.
It is not at all working. Please help in debugging.
Code:
#include <p18f4550.h>
#define baud 9600
#define freq 12000000
#define spbrg_value (((freq/64)/baud)-1)
void rx_data()
{
while (PIR1bits.RCIF == 0); //wait until 8-bit data is received
if(RCREG==0xFF)
PORTB=0;
}
void tx_data(char data)
{
TXREG=data; // store data on transmit register
while (PIR1bits.TXIF == 0); //Wait until transmitter register to get full
}
void main()
{
int i,j;
unsigned char serial_data;
TRISB=0;
PORTBbits.RB3=1;
INTCON2bits.RBPU=0;
SPBRG=spbrg_value; //Input the spbgr resister for baud rate
RCSTAbits.SPEN = 1; //Activate serial comm. (Tx and Rx pins)
TXSTAbits.TXEN = 1; //Enable transmission
RCSTAbits.CREN = 1; //Enable cont. receiving.
RCSTAbits.RX9 = 0; //Setting it on 8-bit mode
while(1)
{
//Receive data
tx_data(0xFF); //Transmit recieved data
//for(i=0;i<1000;i++)
// for(j=0;j<760;j++);
rx_data;
}
}