dolphinlabs
...
Doplhin labs
News

PIC18F45209-4x4 keyboard Interfacing


Program Name: Interfacing of4x4 keypad and displaying key pressed on LCD

Connections:  RC0=RS, RC1=RW, RC2=EN

                        PORTD=data lines of LCD

            RB0-RB3= ROW1-ROW4               

                        RB4-RB7= COL1-COL4

                                                                             

Hardware connections for PIC184520 Board:

1.      Give supply to the Board with the help of adapter.

2.      Connect +5V pin of PIC controller to +5v supply and GND pin of controller to GND pin of supply.

3.      Connect +5V pin of LCD to +5V supply and GND pin of LCD to GND pin of supply

4.      Connect data lines of LCD to PORT D of controller.

5.      Connect RS to RC0, RW to RC1 and EN to RC2 pin of controller.

6.      Connect COL1-COL4 of keypad to RB4-RB7 pins of controller

7.      Connect ROW1-ROW4 of keypad to RB0-RB3 pins of controller

 

Circuit diagram:

 

Program Code:

#include<p18f4520.h>

#define lcd PORTD                             //PORTD =LCD data pins

#define rs PORTCbits.RC0            //rs=PORTC.0

#define rw PORTCbits.RC1           //rw=PORTC.1

#define en PORTCbits.RC2           //en=PORTC.2

/*-----Function Declaration--------*/

void lcddata(unsigned char);

void lcdcmd(unsigned char);

void delay(unsigned int);

 

#define HIGH 1

#define LOW 0

#define R1 PORTBbits.RB0

#define R2 PORTBbits.RB1

#define R3 PORTBbits.RB2

#define R4 PORTBbits.RB3

#define C1 PORTBbits.RB4

#define C2 PORTBbits.RB5

#define C3 PORTBbits.RB6

#define C4 PORTBbits.RB7

unsigned char val = 0;

void main(void)

{

    ADCON1 = 0x0F;

    TRISD = 0x00;   //make port D as output

    TRISC = 0x00;   //make port C as output

    TRISB = 0xf0;

    INTCON2bits.RBPU = LOW;    // Enable Weak internal pull-ups in pin RB4 and RB5

                en=0;                                                                      //enable idle low

                delay(250);

                lcdcmd(0x38);                                                     //initialize lcd

                delay(250);

                lcdcmd(0x0e);                                                      //display on,cursor on

                delay(15);

                lcdcmd(0x01);                                                     //clear lcd

                delay(15);

                lcdcmd(0x06);                                                     //shift cursor right

                delay(15);

                lcdcmd(0x80);                                                     //display on 1st row,1st column

                delay(15);

    while(1)

    {

        lcdcmd(0x80);

        LATB = 0xf0;

        if(C1 == LOW)

{

            R1 = HIGH;

            if(C1 == HIGH)

               lcddata('7');

            else {

                R2 = HIGH;

                if(C1 == HIGH)

                    lcddata('4');

                else {

                    R3 = HIGH;

                    if(C1 == HIGH)

                        lcddata('1');

                    else lcddata('C');

        }

            }

    }

    if(C2 == LOW){

            R1 = HIGH;

            if(C2 == HIGH)

                lcddata('8');

            else {

                R2 = HIGH;

                if(C2 == HIGH)

                    lcddata('5');

                else{

                    R3 = HIGH;

                    if(C2 == HIGH)

                        lcddata('2');

                    else lcddata('0');

                }

            }

    }

    if(C3 == LOW){

            R1 = HIGH;

            if(C3 == HIGH)

               lcddata('9');

            else {

                R2 = HIGH;

                if(C3 == HIGH)

                  lcddata('6');

                  else{

                    R3 = HIGH;

                    if(C3 == HIGH)

                       lcddata('3');

                    else lcddata('=');

                }

            }

    }

    if(C4 == LOW){

            R1 = HIGH;

            if(C4 == HIGH)

                lcddata('/');

            else {

                R2 = HIGH;

                if(C4 == HIGH)

                    lcddata('*');

                    else{

                        R3 = HIGH;

                        if(C4 == HIGH)

                            lcddata('-');

                        else lcddata('+');

                }

            }

    }

      

    }  

}

 void lcdcmd(unsigned char c)

{

                lcd=c;                                                                                     //put commands on pins

                rs=0;

                rw=0;

                en=1;                                                                                      //strobe the enable pin

                delay(1);

                en=0;

}             

void lcddata(unsigned char d)

{

                lcd=d;

                rs=1;

                rw=0;

                en=1;

                delay(1);

                en=0;

}

void delay(unsigned int k)

{

                unsigned int i,j;

                for(i=0;i<k;i++)

                  for(j=0;j<135;j++);

}

 

 

 


Get SOCIAL







SAFE & SECURE SHOPPING

visa card master card maestro card
Maverick IT Industries Pvt. Ltd.