11 Eylül 2017 Pazartesi

Keypad

We can connect a keypad and show pressed key on LCD.

Proteus:
PORTB connected to LCD as in our previous examples. 


MCC: PORTB is set to output for LCD. PortC 0,1,2,3 pins are output and 4,5,6 pins are input. I noticed that if you don't select "Analog"for RB pins in MCC , LCD is not working.

XC8: logic of the program is to make PORTC, 0,1,2,3 high sequentially and check 4,5,6 pins. for example set RC0 high. if "1" is pressed RC4 will be high, "2" will make RC5 high and , "3" pressed will do RC6 high. This is how the keypad switch is working.   

in the code, we are making PORTC, 0,1,2,3 ping high in a for loop and setting the key pressed parameter "c" with if conditions. Then, writing the key pressed "c" to LCD.  


include "mcc_generated_files/mcc.h"
#include "myxlcd.h"
#include "delays.h"
/*
                         Main application
 */
void main(void)
{
    // Initialize the device
    SYSTEM_Initialize();
  int i=0;
    unsigned char c;
    OpenXLCD(FOUR_BIT & LINES_5X7);
    WriteCmdXLCD(DON&CURSOR_OFF&BLINK_OFF); 
    while (1)
    {
        
        for (i=0; i<4; i++){
        if (i==0){
        IO_RC0_SetHigh();
        if (IO_RC4_PORT==1) c='1';
        if (IO_RC5_PORT==1) c='2';
        if (IO_RC6_PORT==1) c='3';
        IO_RC0_SetLow();
        } //if
        if (i==1){
        IO_RC1_SetHigh();
        if (IO_RC4_PORT==1) c='4';
        if (IO_RC5_PORT==1) c='5';
        if (IO_RC6_PORT==1) c='6';
        IO_RC1_SetLow();
        } //if
        if (i==2){
        IO_RC2_SetHigh();
        if (IO_RC4_PORT==1) c='7';
        if (IO_RC5_PORT==1) c='8';
        if (IO_RC6_PORT==1) c='9';
        IO_RC2_SetLow();
        } //if
        if (i==3){
        IO_RC3_SetHigh();
        if (IO_RC4_PORT==1) c='*';
        if (IO_RC5_PORT==1) c='0';
        if (IO_RC6_PORT==1) c='#';
        IO_RC3_SetLow();
        } //if
        
        while(BusyXLCD());
        WriteCmdXLCD(0x01); //clear LCD
        putrsXLCD("Keypad Example");
        SetDDRamAddr(0x40); // goto second line
        putrsXLCD("Key=");putcXLCD(c);
        __delay_ms(100);
        } //for
        
    }

Hiç yorum yok:

Yorum Gönder