In this project we will learn how to animate LED 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 for blinking LED. For  controlling 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){
          PORTB = 128;
          delay_ms(500);             //500ms delay
          PORTB = 64;
          delay_ms(500);
          PORTB = 32;
          delay_ms(500);
          PORTB = 16;
          delay_ms(500);
          PORTB = 8;
          delay_ms(500);
          PORTB = 4;
          delay_ms(500);
          PORTB =2;
          delay_ms(500);
          PORTB = 1;
          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

animate led pin 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