#include <lpc22xx.h>
#define O1 0X00038000
#define O2 0X00034000
#define O3 0X0002C000
#define O4 0X0001C000
#define CLR 0x0003C000
/*#define col0 0x00
#define col1 0x01
#define col2 0x02
#define col3 0x03 */
void delay(int);
void init_lcd();
void Delay(unsigned long b);
void write_command(int cmd);
void write_data(int dat);
void lcd_data(int l);
void lcd_command(char cmd);
/* printlcd (int keypad[int m][int n])
//int m,n;
{
int m,n;
lcd_data(*(*(keypad+m)+n));
Delay(20000);
} */
printlcd (unsigned int x)
//int m,n;
{
//int m,n;
lcd_data(x);
Delay(20000);
}
int main()
{
int keypad [4] [4] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
unsigned int row,column;
IODIR0 = 0x003FC000;
init_lcd();
IOCLR0 = 0x0003FC00; //Clear all the port pins
IOCLR0 = 0x0003C000; //P0.17,P0.16,P0.15,P0.14 AS OUTPUT as ROWS
IOSET0 = 0x00003C00; // P0.13, P0.12, P0.11 and P0.10 are input as COLUMNS
while(1)
{
do
{
IO0CLR = 0x0003C000; //Ground all the rows at once
column = IOPIN0;
column &= 0X00003C00; //MASK USED BITS
} while(column == 0X00003C00); //CHECK UNTIL ALL KEYS ARE OPEN
do
{
do
{
delay(20); //Call delay
column = IOPIN0; // See if any key is pressed
column &= 0X00003C00;// Mask unused bits
} while(column == 0X00003C00); // Keep checking for key press
delay (20);
column = IOPIN0; //Read columns
column &= 0X00003C00; // mask unused bits
} while (column == 0X00003C00); // Wait for Keypress
while(1)
{
IOSET0 = 01; //ground row0
column = IOPIN0; //Read colums
column &= 0X00003C00; //Mask unused bits
if(column != 0X00003C00) //Column detected
{
row = 0; // Save row lacation
break; // Exit while loop
}
IOSET0 = 02; // Ground row1
column = IOPIN0; // Read columns
column &= 0X00003C00; //Mask unused bits
if(column != 0X00003C00) //Column detected
{
row = 1; // Save row location
break; // Exit while loop
}
IOSET0 = 03; // Ground row2
column = IOPIN0; // Read columns
column &= 0X00003C00; //Mask unused bits
if(column != 0X00003C00) //Column detected
{
row = 2; // Save row lacation
break; // Exit while loop
}
IOSET0 = 04; // Ground row3
column = IOPIN0; // Read columns
column &= 0X00003C00; //Mask unused bits
if(column != 0X00003C00) //Column detected
{
row = 3; // Save row lacation
break; // Exit while loop
}
}
if (column == 0x00003800)
printlcd(keypad [row] [0]);
else if(column == 0x00003400)
printlcd(keypad [row] [1]);
else if (column == 0x00002C00)
printlcd (keypad [row] [2]);
else if(column == 0x00001C00)
printlcd(keypad [row] [3]);
}
}
void delay(int n) // generates one milisecond delay
{
int i,j;
for (i=1; i<=n; i++);
for(j=0; j<=100000; j++);
}
void init_lcd() {
IO1DIR |= 0x00FE0000;
Delay(200000) ;
write_command(0x30 << 16);
Delay(100000);
write_command(0x30 << 16);
Delay(100000);
write_command(0x30 << 16);
Delay(100000);
write_command(0x20 << 16);
lcd_command(0x01); /* clear display */
lcd_command(0x06); /* auto address inc */
lcd_command(0x0c); /* cursor off */
lcd_command(0x80); /* first location */
}
void write_command(int cmd) {
IO1CLR |= 0x00f00000; /* Clear D4-D7 */
IO1CLR |= 0x00040000; /* Read/Write = 0 */
IO1CLR |= 0X00020000; /* Register Select = 0,Command */
IO1SET |= 0x00f00000 & cmd; /* Set D4-D7 */
IO1SET |= 0X00080000; /* Enable = 1 */
Delay(30000);
IO1CLR |= 0x00080000; /* set E to low */
}
void write_data(int dat) {
IO1CLR |= 0x00f00000; /* Clear D4-D7 */
IO1CLR |= 0x00040000; /* Read/Write = 0 */
IO1SET |= 0X00020000; /* Register Select = 1,Data */
IO1SET |= 0x00f00000 & dat; /* Set D4-D7 */
IO1SET |= 0X00080000; /* Enable = 1 */
Delay(30000); //delay ~2ms
IO1CLR |= 0x00080000; /* Set E to low */
}
void lcd_data(int dat)
{
write_data(dat << 16);
write_data(dat << 20);
}
void lcd_command(char cmd)
{
write_command(cmd << 16);
write_command(cmd << 20);
}
void Delay(unsigned long b)
{
while (--b!=0);
}