In this project we will learn how to animate LED down to up and up to down 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;              // portb is clear
     while(1){
              PORTB = 1;             // 0b0000 00001
              delay_ms(500);       // 500ms delay
              PORTB = 2;            // 0b0000 00010
              delay_ms(500);      // 500ms delay
              PORTB = 4;           // 0b0000 0100
              delay_ms(500);        // 500ms delay
              PORTB = 8;           // 0b0000 1000
              delay_ms(500);       // 500ms delay
              PORTB = 16;           // 0b0001 0000
              delay_ms(500);        // 500ms delay
              PORTB = 32;           // 0b0010 0000
              delay_ms(500);         // 500ms delay
              PORTB = 64;           // 0b0100 00000
              delay_ms(500);        // 500ms delay
              PORTB = 128;         // 0b1000 0000
              delay_ms(500);        // 500ms delay
            
              PORTB = 64;            // 0b0100 0000
              delay_ms(500);         // 500ms delay
              PORTB = 32;            // 0b0010 0000
              delay_ms(500);          // 500ms delay
              PORTB = 16;              // 0b00001 0000
              delay_ms(500);           // 500ms delay
              PORTB = 8;               // 0b0000 1000
              delay_ms(500);           // 500ms delay
              PORTB = 4;                // 0b0000 0100
              delay_ms(500);            // 500ms delay
              PORTB = 2;                // 0b0000 0010
              delay_ms(500);           // 500ms delay
              }

}

  

 

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 for down to up

down to up PORTB register value

 

PORTB output value for up to down

Aminate Up to down portb output 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