top of page

UART Master and Slave using 16F877A micro-controller 

By Bernice Zuiya

Introduction

                          Master and slave  using the PIC16F877A micro-controller PIC16 basically showing how we can communicate two micro-controller PIC16 using the UART module. 

PIC16F877A micro-controller

              This project will simply show how to interface two PIC micro-controller using UART module, and the operation will be as to toggle some pins high or low from the slave side and the instruction coming from the master.

Working:

                By just seeing the code writen bellow, we can see a set of switches connected to one port of the master PIC micro-controller, the we create a variable char character to store the value read on the port where the switches are connected, and from the slave side there is also a number of leds connected to one port and the number of LEDs correspond to the number of switches used on the master side. we are using resistance in series with LEDs in such a way to limit the current, but the resistance used in master side may be used as pull-up or down resistor and in my case I used resistances with values equal to 10K ohm.

Step:

  • Initialize the UART module in the main function

  • Create a variable char for both side

  • Using the TRIS and PORT register config whether the port must be acting as input or output port. 

  • Connect the switches with a pull-up or down resistance for the master only

  • Connect LEDs with limiting current resistance in series at the slave's side

  • Create an infinite loop in both main function, for the master program write repeatedly the value stored in variable char to uart port, and the slave's side give the input character received from the serial port to the port configured as output port.

  • The baud-Rate must be equal from both side. 

Circuit Diagram

         The circuit diagram shown below is the very basic diagram required for blinking an led using the 16F877A micro-controller, you need to respect the connections to get best result.

  • a resistor of value 4k7 ohm is connected in series with +5 volt and the reset pin (1st pin)

  • Q1 is the crystal oscillator in this case its frequency oscillation is 20 MHz.

  • supply: the circuit is connected with a voltage maximum equal 5 volt

  • R3 is a limiting current resistor to protect the LED.

  • C1 & C2 are of 22pF.

Code:

   char port_;
    void main() {
    UART1_Init(9615);
    delay_ms(50);
    TRISD=0XFF;
    TRISB=0X00;
    PORTB=0X00;
    while(1){
    port_ = PORTD;
               delay_ms(10);
               PORTB=port_;
               UART1_write(port_);
         delay_ms(50);
     }
}

Master code:

     char port_;
     void main() {
     UART1_Init(9615);
     delay_ms(10);
     TRISD=0X00;
     PORTD=0X00;
     for(;;){
              port_=UART1_Read();
             delay_ms(10);
           PORTD = port_;
      }
 }

Slave code:

Download the full project here

bottom of page