4x4 Keypad interfacing to PIC18f458

Status
Not open for further replies.
this was my code

#include "p18f45k20.h"
#include "xlcd.h"
#include "delays.h"

#pragma config FOSC = INTIO67 ,IESO = OFF
#pragma config WDTEN = OFF, MCLRE = OFF, LVP = OFF

#define COL_0 PORTDbits.RD4
#define COL_1 PORTDbits.RD5
#define COL_2 PORTDbits.RD6
#define COL_3 PORTDbits.RD7

#define ROW_A PORTDbits.RD0
#define ROW_B PORTDbits.RD1
#define ROW_C PORTDbits.RD2
#define ROW_D PORTDbits.RD3

void DelayFor18TCY(void)
{
Nop();Nop();Nop();Nop();
Nop();Nop();Nop();Nop();
Nop();Nop();Nop();Nop();
}
void delay(void)
{
int i;
for(i = 0; i < 5000; i++);
}
void DelayPORXLCD(void)
{
Delay1KTCYx(120);//Delay of 15ms
//cycles = (TimeDelay * FOSC)/4
//cycles = (15ms * 16MHz)/4
//cycles = 60,000
return;
}
void DelayXLCD(void)
{
Delay1KTCYx(20);//Delay of 5ms
//cycles = (TimeDelay * FOSC)/4
//cycles = (5ms * 16MHz)/4
//cycles = 20,000
return;
}

typedef unsigned char Byte;
Byte row;
Byte Keypad_Value[4][4] = {'0','1','2','3',
'4','5','6','7',
'8','9','A','B',
'C','D','E','F'};
Byte Keypad(void);
void Lcddisplay_Str(Byte *Str);

void main(void)
{
Byte data[10] = "Keypad";
TRISD = 0b11110000;
TRISB = 0b00000000;
OSCCONbits.IRCF = 0x03;
PORTD = 0xF0;
//configure Lcd
OpenXLCD(FOUR_BIT & LINES_5X7);
while(BusyXLCD()); //Wait if LCD busy
WriteCmdXLCD(SHIFT_DISP_LEFT); //set lcd write left to right
while(BusyXLCD());
Lcddisplay_Str(data);

row = 0;
while(1)
{
WriteCmdXLCD(0xC0);
while(BusyXLCD());
WriteDataXLCD(Keypad());
}
}
Byte Keypad(void)
{
COL_0 = COL_1 = COL_2 = COL_3 = 1;
delay();
ROW_A = ROW_B = ROW_C = ROW_D = 0;
if(!COL_0 || !COL_1 || !COL_2 || !COL_3)
{
while(1)
{
ROW_A = 0; ROW_B = 1; ROW_C = 1; ROW_D = 1;
if(!COL_0 || !COL_1 || !COL_2 || !COL_3)
{
row = 0;
break;
}
ROW_A = 1; ROW_B = 0; ROW_C = 1; ROW_D = 1;
if(!COL_0 || !COL_1 || !COL_2 || !COL_3)
{
row = 1;
break;
}
ROW_A = 1; ROW_B = 1; ROW_C = 0; ROW_D = 1;
if(!COL_0 || !COL_1 || !COL_2 || !COL_3)
{
row = 2;
break;
}
ROW_A = 1; ROW_B = 1; ROW_C = 1; ROW_D = 0;
if(!COL_0 || !COL_1 || !COL_2 || !COL_3)
{
row = 3;
break;
}
}
if(COL_0 == 0 && COL_1 == 1 && COL_2 == 1 && COL_3 == 1)
{
return (Keypad_Value[row][0]);
}
else if(COL_0 == 1 && COL_1 == 0 && COL_2 == 1 && COL_3 == 1)
{
return (Keypad_Value[row][1]);
}
else if(COL_0 == 1 && COL_1 == 1 && COL_2 == 0 && COL_3 == 1)
{
return (Keypad_Value[row][2]);
}
else if(COL_0 == 1 && COL_1 == 1 && COL_2 == 1 && COL_3 == 0)
{
return (Keypad_Value[row][3]);
}
else
{
//do noting
}
}
//return 0xFF;
}
void Lcddisplay_Str(Byte *Str)
{
while(*Str)
{
WriteDataXLCD(*Str++);
delay();
}
}
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…