0% found this document useful (0 votes)
1 views

Tutorial 12_Working with serial communication (LO3 & LO4)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Tutorial 12_Working with serial communication (LO3 & LO4)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Tutorial 11 – Working with Serial Communication

Write a program in micro C to communicate with two PIC16F877A microcontrollers using serial
communication protocol.

Solution

// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit
(RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code
protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all
program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.


// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 20000000

#include <xc.h>

#define SBIT_TXEN 5
#define SBIT_SPEN 7
#define SBIT_CREN 4

void UART_Init(int baudRate)


{
TRISC=0x80; // Configure Rx pin as input and Tx as output
TXSTA=(1<<SBIT_TXEN); // Asynchronous mode, 8-bit data & enable transmitter
RCSTA=(1<<SBIT_SPEN) | (1<<SBIT_CREN); // Enable Serial Port and 8-bit continuous receive
SPBRG = (20000000UL/(long)(64UL*baudRate))-1; // baud rate @20Mhz Clock
}

void UART_TxChar(char ch)


{
while(TXIF==0); // Wait till the transmitter register becomes empty
TXIF=0; // Clear transmitter flag
TXREG=ch; // load the char to be transmitted into transmit reg
}

char UART_RxChar()
{
while(RCIF==0); // Wait till the data is received
RCIF=0; // Clear receiver flag
return(RCREG); // Return the received data to calling function
}

int main()
{
char i,a[]={"SLIIT Serial communication"};
char ch;

UART_Init(9600); //Initialize the UART module with 9600 baud rate


for(i=0;a[i]!=0;i++)
{
UART_TxChar(a[i]); // Transmit predefined string
}

while(1)
{
ch = UART_RxChar(); // Receive a char from serial port
UART_TxChar(ch); // Transmit the received char
}
}

You might also like