PIC16F877A – Interfacing Switch
PIC 16F877A Mini Development Board has 3 Switches (SW1,SW2 & SW3) connected to port pins RC3, RC2 & RB5 via jumpers J8, J9 & J15 respectively. All the switches are pulled up to +5V via resistors (as shown in the schematic given below) which keeps the corresponding port pin in logic high state. When a switch is pressed, it gets ground, which brings the port pin to logic low state. On shorting the jumpers J8, J9 & J15, the switches can be used as pull-up keys and if the jumpers are left open, then the port pins can be used independently.
SchematicSample Code
Sample code to test the switches is given below. In the code, switch press is detected by polling and upon pressing a switch, its name gets displayed on the LCD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
/************************************************************************* HEADER FILES **************************************************************************/ #include<pic.h> /************************************************************************* FUNCTION DECLARATIONS **************************************************************************/ void Delay_us(char Delay); void Delay_100us(unsigned long int Delay); void Lcd_Data(int Value); void Lcd_Cmd(int Value); void Send2Lcd(const char Adr, const char *Lcd); /************************************************************************* MAIN FUNCTION **************************************************************************/ void main() { TRISC = 0X0C; /* RC1(EN),RC0(RS)->O/P,RC3(SW1),RC2(SW2)->I/P */ TRISD = 0X00; /* PORTD(LCD data lines) configured as output */ TRISB5 = 1; /* RB5 configured as i/p for SW3 interfacing */ Lcd_Cmd(0X30); /* LCD Specification Command */ Delay_us(35); Lcd_Cmd(0X30); /* LCD Specification Command */ Delay_us(35); Lcd_Cmd(0X30); /* LCD Specification Command */ Delay_us(35) Lcd_Cmd(0X38); /* Double Line Display Command */ Lcd_Cmd(0X06); /* Auto Increment Location Address Command */ Lcd_Cmd(0X0C); /* Display ON Command */ Lcd_Cmd(0X01); /* Clear Display Command */ Delay_100us(30); while(1) { if(RC3==0) /* If SW1 is pressed, RC3 gets low */ { Send2Lcd(0x80,"SW1 PRESSED "); } else if(RC2==0) /* If SW2 is pressed, RC2 gets low */ { Send2Lcd(0x80,"SW2 PRESSED "); } else if(RB5==0) /* If SW3 is pressed, RB5 gets low */ { Send2Lcd(0x80,"SW3 PRESSED "); } else { Send2Lcd(0x80,"NO SW PRESSED "); } } } /************************************************************************* * Function : Lcd_Cmd * * * * Description : Function to send a command to LCD * * * * Parameters : Value - Command to be given * **************************************************************************/ void Lcd_Cmd(int Value) { PORTD = Value; /* Write the command to data lines */ RC0 = 0; /* RC0=0(RS=0 for command Register Selection) */ RC1 = 1; /* RC1=1(Enable=1, Enable pin high) */ Delay_us(30); RC1 = 0; /* RC1=0(Enable=1, Enable pin low) */ } /************************************************************************* * Function : Lcd_Data * * * * Description : Function to display a character on LCD * * * * Parameters : Value - Character to be displayed * **************************************************************************/ void Lcd_Data(int Value) { PORTD = Value; /* Write the character to data lines */ RC0 = 1; /* RC0=1(RS=1 for Data Register Selection) */ RC1 = 1; /* RC1=1(Enable=1, Enable pin high) */ Delay_us(30); RC1 = 0; /* RC1=0(Enable=0, Enable pin low) */ } /************************************************************************* * Function : Send2Lcd * * * * Description : Function to send string of data to LCD * * * * Parameters : Adr - Location to display string * * String to be displayed * **************************************************************************/ void Send2Lcd(const char Adr, const char *Lcd) { Lcd_Cmd(Adr); /* Address of location to display string */ while(*Lcd!='\0') /* Check for termination character */ { Lcd_Data(*Lcd); /* Display the character on LCD */ Lcd++; /* Increment the pointer */ } } /************************************************************************* * Function : Delay_us * * * * Description : Function for 1 microsecond delay * * * * Parameter : Delay - delay in microseconds * **************************************************************************/ void Delay_us(char Delay) { while((--Delay)!=0); } /************************************************************************* * Function : Delay_100us * * * * Description : Function for delay * * * * Parameter : Delay - delay interval * **************************************************************************/ void Delay_100us(unsigned long int Delay) { Delay = Delay*15; while((--Delay)!=0); } /*************************** END OF PROGRAM ****************************/ |
Topics related to PIC16F877A Development Board-Mini
- PIC16F877A Mini Development Board – Overview
- PIC16F877A Mini Development Board – Interfacing LED
- PIC16F877A Mini Development Board – Interfacing LCD
- PIC16F877A Mini Development Board – Serial communication(USART)
- PIC16F877A Mini Development Board – Interfacing Switch
- PIC16F877A Mini Development Board – Interfacing Buzzer
- PIC16F877A Mini Development Board – Interfacing POT(ADC)
- PIC16F877A Mini Development Board – Interfacing Temperature sensor
- PIC 16F877A Mini Development Board – Interfacing Servo Motor
- PIC 16F877A Mini Development Board – Interfacing μRFID Reader
Resources
- Datasheets
How to buy?
- Click here to buy rhydoLABZ PIC 16F877A Mini Development Board
- Click here to buy rhydoLABZ PIC 18F4520 Mini Development Board
- Click here to buy rhydoLABZ PIC 18F4550 Mini Development Board
- Click here to buy rhydoLABZ PIC 18F4580 Mini Development Board
SupportPlease share your ideas with us, visit our forum for discussion
Leave a Reply
You must be logged in to post a comment.