Keyboard Interfacing with PIC18

Keyboard Interfacing with PIC18


Interfacing Diagram


Keyboard interfacing with PIC18

RC0,RC1,RC2,RC3 pins are Configured in Output Direction and act as First row, Second Row, Third Row, Fourth Row Respectively
RC4,RC5,RC6,RC7 pins are Configured in Input Direction and act as First column, Second column, Third column, Fourth column Respectively

Embedded C Program


#include<p18f4520.h>
#pragma config OSC=HS
#pragma config PWRT=OFF
#pragma config WDT=OFF
#pragma config DEBUG=OFF, LVP=OFF
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void msdelay(unsigned int itime);
#define ldata PORTD
#define rs PORTBbits.RB0
#define rw PORTBbits.RB1
#define en PORTBbits.RB2
int main(void)
{
TRISD = 0x00; // PORTD as output
TRISB = 0x00; // PORTB as output
ADCON1 = 0x0F; // All analog input channel pins are used as Digital I/O pin
TRISC = 0xF0; //Columns as input i.e.RC4-RC7
PORTC =0x00; // Initial value, RC0-RC7 as low
msdelay(15);
lcdcmd(0x38); // 16x2 LCD with 8-bit mode of operation
msdelay(15);
lcdcmd(0x0E); // display on, cursor blinking
msdelay(15);
lcdcmd(0x01); // clear display screen
msdelay(15);
lcdcmd(0x06); // increment cursor and shift right
msdelay(15);


while(1)
{
 lcdcmd(0x83); // force cursor on row first 4th position
msdelay(15);
 PORTC = 0xFE; // Ground first row
       if(( PORTC & 0xf0 )!= 0xf0) // check status of first row and all column's
{
switch(PORTC & 0xf0) // check column status and enter in perticular case
{
case 0xe0: lcddata('0');
             break;
case 0xd0: lcddata('1');
            break;
case 0xb0: lcddata('2');
           break;
case 0x70: lcddata('3');
            break;
 }
 }

 PORTC= 0xfd; // Ground second row

       if(( PORTC & 0xf0 )!= 0xf0) // check status of second row and all column's
{

       switch(PORTC & 0xf0)// check column status and enter in perticular case
{
case 0xe0: lcddata('4');
            break;
case 0xd0:  lcddata('5');
             break;
case 0xb0 : lcddata('6');
             break;
case 0x70 : lcddata('7');
            break;
 }
 }
PORTC = 0xfb;// Ground Third row
if(( PORTC & 0xf0 )!= 0xf0)// check status of Third row and all column's
{
switch(PORTC & 0xf0)// check column status and enter in perticular case
{
case 0xe0 : lcddata('8');
            break;
case 0xd0 : lcddata('9');
            break;
case 0xb0 : lcddata('A');
             break;
case 0x70 : lcddata('B');
            break;
 }
 }
PORTC = 0xF7;// Ground Third row
if(( PORTC & 0xf0 )!= 0xf0)// check status of fourth row and all column's
{
switch(PORTC & 0xf0)// check column status and enter in perticular case
{
case 0xe0 : lcddata('C');
             break;
case 0xd0 : lcddata('D');
              break;
case 0xb0 : lcddata('E');
             break;
case 0x70 : lcddata('F');
               break;
 }
 }
 msdelay(15);
}
}
void lcdcmd (unsigned char value)
{
ldata=value;
rs=0;   // command register
rw=0;
en=1;  // high
msdelay(1);
en=0;   //low
}

void lcddata (unsigned char value)
{
ldata=value;
rs=1; //data rgister
rw=0;
en=1; //high
msdelay(1);
en=0; // low
}
void msdelay (unsigned int itime)
{
int i,j;
for(i=0;i<itime;i++)
for(j=0;j<135;j++);
}

References


  • Created, Edited & Notes by Sujit Wagh
  • WikiNote Foundation

Last modified: Monday, 16 September 2019, 3:49 PM