In this project we will learn how to animate LED down to up using PIC Microcontroller. Here I we are using PIC16F877A Microcontroller and MikroC PRO for PIC compiler for this project. You con watch the following video or read the written tutorial below. 

 

 

 

I/O pin of PIC16F877A microcontroller

PIC16F877A pindiagram

PIC16F877A Microcontroller has A0-A7, B0-B7, C0-C7, D0-D7 and E0-E2 input/output port. Some pins for these I/O port are multiplex with an alternative function for the peripheral feature on the device. In general, when a peripheral is enable, that pin may not used as a general purpose I/O pin.


PORTB and TRISB register map
In this project we will use PORTB register for blinking LED. For  controlling input output PORTB it has two register TRISB and PORTB.
The TRISB Register controls the direction of Input/Output port. Here PORTB is 8bit wide, bi-directional port. The corresponding data direction register is TRISB. Setting a TRISB bit (= 1) will make the corresponding PORTB pin an Input. Clearing a TRISB bit (=0) will make the corresponding PORTB pin an Output. 

 

Animate LED blinking project source code

void main() {
     TRISB = 0X00;              // PORTB is output
     PORTB = 0X00;              // Clear PORTB
     while(1){                          // end less loop
              PORTB = 1 ;          //  1st led blinking
              delay_ms(500);         // 500ms delay
              PORTB = 2 ;          //  2nd led blinking
              delay_ms(500);         // 500ms delay
              PORTB = 4 ;            // 3rd led blinking
              delay_ms(500);         // 500ms delay
              PORTB = 8 ;            //  4th led blinking
              delay_ms(500);        // 500ms delay
              PORTB = 16 ;            // 5th led blinking
              delay_ms(500);         // 500ms delay
              PORTB = 32 ;           // 6th led blinking
              delay_ms(64);           // 500ms delay
              PORTB = 64 ;           //  7th led blinking
              delay_ms(500);
              PORTB = 128 ;            // 8th led blinking
              delay_ms(500);
              }
}

  

 

Code Explain


Here I am use MikroC pro for PIC compiler for code editing. In void main function I define PORTB is output by clearing TRISB register. Then we clear the PORTB register. In next, here I am use while loop for continues program execution and blinking LEDs. 

 

PORTB output value

down to up PORTB register value

 

Circuit Diagram

Multiple blinking LED with PIC16F877A

 

Component List

  1. PIC16F877A Microcontroller
  2. Crystal(8MHz)
  3. Push button
  4. led 3mm(8)
  5. Resister: 10k ,100R
  6. Breadboard
  7. Jumper Wire
  8. 5V DC Supplay

 

 

Download free source code 

 

 download