ajrox
Newbie level 6
- Joined
- Nov 26, 2013
- Messages
- 14
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 110
Code:
#include <LPC21xx.h>
//#include <LCD.h>
//#include <keypad.h>
/*#ifndef KEYPAD_H_
#define KEYPAD_H_
#define KEY_PAD_PORT 0x000003FC
void keypad_init(void);
unsigned char detect_key(void);
unsigned int getch_keypad_to_LCD(void);
#endif /*KEYPAD_H_*/
*/
const unsigned char key_pad_key[4][4]={'1','2','3','F',
'4','5','6','E',
'7','8','9','D',
'A','0','B','C'};
void delay()
{
unsigned int i=0;
for(i=0;i<20000;i++);
}
void keypad_init()
{
*IODIR0 |=0x000003FC;
*IOPIN0 = (unsigned int) (0x00<<4);
}
unsigned char detect_key()
{
int col=0,x;
while(1)
{
for(col=0;col<4;col++)
{
*IOCLR0 = *IOCLR0 | (~(0x0200>>col));
*IOSET0 = *IOSET0 | (0x0200>>col);
x= (*IOPIN0 & 0x3c)>>2;
switch(x)
{
case 0x01:
{
return key_pad_key[col][0];
}
case 0x02:
{
return key_pad_key[col][1];
}
case 0x04:
{
return key_pad_key[col][2];
}
case 0x08:
{
return key_pad_key[col][3];
}
}
}
}
}
int main()
{
unsigned char ch;
// write your program here
while(1)
{
keypad_init();
ch= detect_key();
lcd_init();
lcd_display_str(ch);
}
return 0;
}
void lcd_init()
{
*IODIR1 |=0X00FF0000; /*set direction for pin at which LCD is connect*/
*IODIR0 |=0x30000000; /*set direction for pin at which RS And Enable is connect*/
lcd_cmd(0x38);
delay();
lcd_cmd(0x0e);
delay();
lcd_cmd(0x01);
delay();
lcd_cmd(0x06);
delay();
lcd_cmd(0x80);
delay();
}
void lcd_cmd(unsigned int cmddata)
{
*IOCLR1 |=0X00FF0000; /*Clear all pin at which LCD is connect*/
*IOCLR0 |=0x10000000; /*Clear pin at which RS is connect*/
cmddata <<= 16;
*IOSET1=cmddata;
*IOCLR0 |=0x20000000; /*Clear pin at which EN is connect*/
delay();
*IOSET0 |=0x20000000;
delay();
*IOCLR0 |=0x20000000; /*Clear pin at which EN is connect*/
delay();
return;
}
void lcd_display(char *str, int len)
{
int i;
for(i=0;i<len;i++)
{
lcd_data(str[i]);
}
}
void lcd_data(unsigned int outdata)
{
*IOCLR1 |=0x00FF0000;
*IOSET0 |=0x10000000;
outdata <<= 16;
*IOSET1=outdata;
*IOCLR0 |=0x20000000;
delay();
*IOSET0 |=0x20000000;
delay();
*IOCLR0 |=0x00001000;
delay();
return;
}
I'm using oasis spirit 2 board for lpc 2148.
The keypad connections are as follows :-
P0.2 - ROW1
P0.3 - ROW2
P0.4 - ROW3
P0.5 - ROW4
P0.6 - COL1
P0.7 - COL2
P0.9 - COL3
P0.9 - COL4
In short IOPIN0 = 0x000003FC are keyboard connections in LPC2148 register terms.
The above code works perfectly on the development board.
However in the above code the function keypad_init() has *IODIR0 = 0x000003FC statement.
In LPC2148 setting 1 in IODIR declares the port as output and 0 as input.
Coventionally, in keypad the rows are output whereas columns are input but here rows and columns both are declared as output.
Secondly, in the function detect_key() cols are made HIGH one by one for key detection instead of grounding rows one by one.
The code works perfectly so these points I'm not able to understand. The development board manual doesn't specify anything about keypad except for the connections and I'm not able to contact the programmer of this code.
Can someone help me to understand the above two points?