Header Ads

Push button switch interfacing with PIC Microcontroller (Latching)

Welcome to MINA TECHNOLOGY.
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 Explain:
Here I am use MikorC Pro for PIC compiler for code editing.
First time we define push button switch connection at PORTD. Switch 1 is connected RD4 pin, switch 2 is connected  RD5 pin, switch 3 is connected RD6 pin and switch 4 is connected RD7 pin.
In void main function we define PORTB is output by clearing TRISB register. Then we create a endless while loop. In while loop we write four "if" condition for four Push button switch.
First condition, if switch 1 is high then RB0 pin is toggle. The next condition is same too working.
And end of the program we close the bracket.

Mikro C 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

                         }

              }



    



}


Click the download button for source code:
downlaod


2 comments:

  1. File not supported
    Please upload file again

    ReplyDelete
  2. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work! geek squad

    ReplyDelete

Theme images by 5ugarless. Powered by Blogger.