Bidirectional data communication between two microcontroller use PIC16F877A and mikroC compiler
In this tutorial, we will learn how data communication using the USART module of PIC Microcontroller.
You can watch the following video or read the written tutorial below.
USART Module
Now let's take a closer look at the USART module. The Universal Synchronous Asynchronous Receiver Transmitter ( USART ) module is one of the two serial I/O modules.
USART is also known as a Serial Communication Interface or SCI. The USART can be configured as a full duplex asynchronous system that can communicate with peripheral devices such as CRT terminals and personal computer, or it can be configured as a half duplex synchronous system that can communicate with peripheral device such as A/D or D/A integrated circuits, serial EEPROMs etc.
The USART can be configured in the following modes:
- Asynchronous ( Full duplex )
- Synchronous - Master ( Half duplex )
- Synchronous - Slave ( Half duplex )
In this tutorial we have used Asynchronous Half duplex data transmission.
In Asynchronous Transmission, data is sent in form of byte or character. This transmission is the half duplex type transmission. In this transmission start bits and stop bits are added with data. It does not require synchronization.
Half duplex
In this mode data is transmission in either direction, but is not simultaneously. Data transmission terminal 1 to terminal 2 or terminal 2 to terminal 1.
Circuit schematic
Here's the circuit schematic. It has two parts Transceiver 1 and Transceiver 2. Push Buttons and LEDs are connected PORTB of these microcontrollers. Here the TX pin of first microcontroller is connected to RX pin at the second microcontroller and vice versa the TX pin of second microcontroller is connected to RX pin at the first microcontroller.
Frist Transreceiver Source Code
# define SW1 PORTB.RB0
# define SW2 PORTB.RB1
# define SW3 PORTB.RB2
unsigned char uart_rd = 0;
void main() {
UART1_Init(9600); // Initialize the UART module at 9600Kbps
delay_ms(100); // Wait for UART module stabilization
TRISB5_bit = 0 ;
TRISB6_bit = 0 ;
TRISB7_bit = 0;
PORTB = 0X00;
while(1){
if(SW1){
UART1_Write('1');
}
if(SW2){
UART1_Write('2');
}
if(SW3){
UART1_Write('3');
}
if(UART1_Data_Ready()){
uart_rd = UART1_Read();
if(uart_rd =='4'){
RB5_bit = ~ RB5_bit;
}
if(uart_rd =='5'){
RB6_bit = ~ RB6_bit;
}
if(uart_rd =='7'){
RB7_bit = ~ RB7_bit;
}
}
}
}
Code Explain
Here's the Mikro C code for first microcontroller. In this code we are included UART library.
In define section, we have defined Switch connections. In this case Switch 1, 2 and 3 are serially connected to RB0, RB1 and RB2 pins. We have defined a unsigned character variable witch name is "uart_rd".
In main section, we have initialized the USART module at 9600 Kilo bits per second ( Kbps). Here's the 100 millisecond ( ms ) delay witch stabilize the USART module. Now, we have set RB5, RB6 and RB7 pin is an output by clearing TRISB register. And we have cleared the PORTB register.
In the main loop, we have created three if statement. If I press the Switch 1, the condition will be true. And It will send the character "1" by UART1_Write ( ) function. Next, have created if statement witch check whether there is data to be received. if data is receive in buffer register, the UART1_Data_Ready( ) function returns 1. Using the UART1_Read() function we read and store the data into the “uart_rd” variable. Next, we have created three if statement witch compare the uart_rd variable value with predefined value.
Second Transreceiver Source Code
# define SW1 PORTB.RB0
# define SW2 PORTB.RB1
# define SW3 PORTB.RB2
unsigned char uart_rd;
void main() {
UART1_Init(9600); // Initialize the UART module at 9600Kbps
delay_ms(100); // Wait for UART module stabilization
TRISB5_bit = 0 ;
TRISB6_bit = 0 ;
TRISB7_bit = 0;
PORTB = 0X00;
uart_rd = 0 ;
while(1){
if(UART1_Data_Ready()){
uart_rd = UART1_Read();
if(uart_rd =='1'){
RB5_bit = ~ RB5_bit;
}
if(uart_rd =='2'){
RB6_bit = ~ RB6_bit;
}
if(uart_rd =='3'){
RB7_bit = ~ RB7_bit;
}
}
if(SW1){
UART1_Write('4');
}
if(SW2){
UART1_Write('5');
}
if(SW3){
UART1_Write('7');
}
}
}
Sender Code Explain
Here's the Mikro C code for second microcontroller. We have made some change in our previous code. Here's we have changed the digits.
Download free source code
Post Comments