In this tutorial we will how to use push button switch as a Latching. Latch circuit has tow stable state they are No and Off state. Here I am use PIC16F877A Microcontroller and Mikro C compiler for code editing. You can watch the video or read the written tutorial below.

 

 

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.

The TRIS 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 Diagram

 

Code

#define SW1 PORTD.RD4
#define SW2 PORTD.RD5
#define SW3 PORTD.RD6
#define SW4 PORTD.RD7

void main() {
     TRISB = 0X00;
     PORTB = 0X00;            // Clear PORTB
     while(1){
              if(SW1==1){

                         PORTB.F0 = ~PORTB.F0;    // Toggle RB0
                         while(SW1==1);     // switch debounce
                         }
              if(SW2==1){
                         PORTB.F1 = ~PORTB.F1;    // Toggle RB1
                         while(SW2==1);        // Switch debounce
                         }
              if(SW3==1){
                         PORTB.F2 = ~PORTB.F2;     // Toggle RB2
                         while(SW3==1);           // Switch debounce
                         }
              if(SW4==1){
                         PORTB.F3 = ~PORTB.F3;      // Toggle RB3
                         while(SW4==1);            // Switch debounce
                         }

              }

}

 

Download free source code 

 

download