In this tutorial we will learn how to blinking LEDs. The LEDs blinking 5 item sequence automatically. Here I am use MikorC PRO for PIC compiler for code editing.  You can 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. 

 

Circuit Schematrics:

 

Code

void main() {
     int i;                                              // i integer variable
     TRISB = 0X00;                            // PORTB is output
     PORTB = 0X00;                           // Clear PORTB
     while(1){
              for(i=0;i<=5;i++){
                                PORTB = 0XFF;
                                delay_ms(150);
                                PORTB = 0X00;
                                delay_ms(150);
                                }
              for(i=0;i<=5;i++){
                                PORTB = 1 ;
                                delay_ms(150);
                                PORTB = 2;
                                delay_ms(150);
                                PORTB = 4 ;
                                delay_ms(150);
                                PORTB = 8;
                                delay_ms(150);
                                PORTB = 16 ;
                                delay_ms(150);
                                PORTB = 32;
                                delay_ms(150);
                                PORTB = 64 ;
                                delay_ms(150);
                                PORTB = 128;
                                delay_ms(150);
                                }
              for(i=0;i<=5;i++){
                                PORTB =128 ;
                                delay_ms(150);
                                PORTB = 64;
                                delay_ms(150);
                                PORTB = 32;
                                delay_ms(150);
                                PORTB = 16;
                                delay_ms(150);
                                PORTB = 8 ;
                                delay_ms(150);
                                PORTB = 4;
                                delay_ms(150);
                                PORTB = 2;
                                delay_ms(150);
                                PORTB = 1;
                                }
              for(i=0;i<=5;i++){
                                PORTB = 1 ;
                                delay_ms(150);
                                PORTB = 2;
                                delay_ms(150);
                                PORTB = 4 ;
                                delay_ms(150);
                                PORTB = 8;
                                delay_ms(150);
                                PORTB = 16 ;
                                delay_ms(150);
                                PORTB = 32;
                                delay_ms(150);
                                PORTB = 64 ;
                                delay_ms(150);
                                PORTB = 128;
                                delay_ms(150);
                                PORTB = 64;
                                delay_ms(150);
                                PORTB = 32;
                                delay_ms(150);
                                PORTB = 16;
                                delay_ms(150);
                                PORTB = 8 ;
                                delay_ms(150);
                                PORTB = 4;
                                delay_ms(150);
                                PORTB = 2;
                                }
              for(i=0;i<=5;i++){
                                PORTB = 24 ;
                                delay_ms(150);
                                PORTB = 36 ;
                                delay_ms(150);
                                PORTB = 66 ;
                                delay_ms(150);
                                PORTB = 129 ;
                                delay_ms(150);
                                PORTB = 66 ;
                                delay_ms(150);
                                PORTB = 36;
                                delay_ms(150);
                                }
              }
                                
}

 

Code Explain:
Here I am use Mikro C 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 continues blinking LEDs. 

 

Download free source code 

 

download