Embedded C program for PC to PC serial communication using UART in PIC18F using MPLAB IDE

For UART PC to PC Serial Communication: Go on Project -> Right Click -> Properties-> Simulation-> Option Categories->Uart1 IO Option-> Enable It

Program:

/*Baud Rate GENERATION
 * n    => required baudrate
 * BRGH     =   0
 * SPBRG    =   (Fosc / (64 * n)) -1
 * For 9600 baudrate, SPBRG ~=77
 */

#include<xc.h>
#define Fosc 48000000UL




void InitUART(unsigned int baudrate)
{
  TRISCbits.RC6 = 0;                        //TX pin set as output
                                            //RX pin set as input
TRISCbits.RC7 = 1; 
  SPBRG = (unsigned char)(((Fosc /64)/baudrate)-1);
  BAUDCON = 0b00000000;                     //Non-inverted data; 8-bit baudrate generator

  TXSTA = 0b00100000;                       //Asynchronous 8-bit; Transmit enabled; Low speed baudrate select
  RCSTA = 0b10010000;                       //Serial port enabled; 8-bit data; single receive enabled
}



void main(void)
{
    InitUART(9600);


    char a[8]="\nwelcome";
    for(int i=0;i<8;i++)
    {
        while(TXIF==0);    // Wait till the transmitter register becomes empty
            TXIF=0; 
        TXREG=a[i];
           
    }
    do
    {
    while(RCIF==0);    // Wait till the data is received 
    RCIF=0;  
    TXREG=RCREG;

    }while(1);
}

Output:

Fig: Before Sending Message

Fig: Sending Message

Fig: Received Message:




Previous
Next Post »