LPC2148 – Keypad Interfacing

Hi all…. Today we will see Keypad interfacing with LPC2148. Before that i would suggest you to go through this link to know about the Keypad characteristics.  Let’s start…

Suggest To Read

Components Required

  • 4×4 Keypad or 3×4 Keypad (Here we will discuss both codes)
  • LPC2148 Microcontroller

Keypad interfacing with LPC2148

4×4 Matrix Keypad Interfacing

Circuit Diagram

Keypad

  • R1 – P1.16
  • R2 – P1.17
  • R3 – P1.18
  • R4 – P1.19
  • C1 – P1.20
  • C2 – P1.21
  • C3 – P1.22
  • C4 – P1.23

Serial

  • RX – P0.0
  • TX – P0.1

Code 

This code might be looking like complicate. Please go through this code slowly.

#include<lpc214x.h>

#define c1 (IOPIN1&1<<20)
#define c2 (IOPIN1&1<<21)
#define c3 (IOPIN1&1<<22)
#define c4 (IOPIN1&1<<23)

void ser_init(void);
void tx(unsigned char c);
unsigned char rx(void);
void tx_string(unsigned char *s);

unsigned char r_loc,c_loc;
unsigned char key[4][4]={"789/","456*","123-","C0=+"};
unsigned char keypad(void);

int main()
{
	IO1DIR=0x0f<<16;
	ser_init();
	while(1) {
		tx(keypad());
	}
}

unsigned char keypad()
{
	IO1PIN=0xf0<<16;

	while(c1 && c2 && c3 && c4);
	while(!c1 || !c2 || !c3 || !c4) {
		if(!c1 && c2 && c3 && c4)		c_loc=0;
		else if(c1 && !c2 && c3 && c4)	c_loc=1;
		else if(c1 && c2 && !c3 && c4)	c_loc=2;
		else if(c1 && c2 && c3 && !c4)	c_loc=3;

		IO1CLR = 1<<16;
		IO1SET = 0x0e<<16;
		if(!c1 || !c2 || !c3 || !c4) {
			r_loc=0;
			break;
		}

		IO1CLR = 1<<17;
		IO1SET = 0x0d<<16;
		if(!c1 || !c2 || !c3 || !c4) {
			r_loc=1;
			break;
		}

		IO1CLR = 1<<18;
		IO1SET = 0x0b<<16;
		if(!c1 || !c2 || !c3 || !c4)	{
			r_loc=2;
			break;
		}

		IO1CLR = 1<<19;
		IO1SET = 0x07<<16;
		if(!c1 || !c2 || !c3 || !c4)	{
			r_loc=3;
			break;
		}
	}
	while(!c1 || !c2 || !c3 || !c4);
	return (key[r_loc][c_loc]);
}

void ser_init()
{
	VPBDIV=0x02;					//PCLK = 30MHz
	PINSEL0=0x5;
	U0LCR=0x83;
	U0DLL=195;
	U0DLM=0;
	U0LCR=0x03;
	U0TER=(1<<7);
}

void tx(unsigned char c)
{
	U0THR=c;
	while((U0LSR&(1<<5))==0);
}

void tx_string(unsigned char *s)
{
	while(*s) {
		tx(*s++);
	}
}

unsigned char rx()
{
	while((U0LSR&(1<<0))==0);
	return U0RBR;
}	 

Output

Whatever we are typing in the keypad, it will send to serial terminal.

3×4 Matrix Keypad Interfacing

Circuit Diagram

Keypad

  • R1 – P1.16
  • R2 – P1.17
  • R3 – P1.18
  • R4 – P1.19
  • C1 – P1.20
  • C2 – P1.21
  • C3 – P1.22

Serial

  • RX – P0.0
  • TX – P0.1

Code

Whatever we are typing in the keypad, it will send to serial terminal.

#include<lpc214x.h>

void ser_init(void);
void tx(unsigned char c);
unsigned char rx(void);
void tx_string(unsigned char *s);

#define c1 (IOPIN1&1<<20)
#define c2 (IOPIN1&1<<21)
#define c3 (IOPIN1&1<<22)

unsigned char r_loc,c_loc;
unsigned char key[4][3]={"123","456","789","*0#"};
unsigned char keypad(void);

int main()
{
	IO1DIR=0x0f<<16;
	ser_init();
	while(1) {
		tx(keypad());
	}
}

unsigned char keypad()
{
	IO1PIN=0xf0<<16;

	while(c1 && c2 && c3);
	while(!c1 || !c2 || !c3) {
		if(!c1 && c2 && c3)		c_loc=0;
		else if(c1 && !c2 && c3)	c_loc=1;
		else if(c1 && c2 && !c3)	c_loc=2;

		IO1CLR = 1<<16;
		IO1SET = 0x0e<<16;
		if(!c1 || !c2 || !c3) {
			r_loc=0;
			break;
		}

		IO1CLR = 1<<17;
		IO1SET = 0x0d<<16;
		if(!c1 || !c2 || !c3) {
			r_loc=1;
			break;
		}
		
		IO1CLR = 1<<18;
		IO1SET = 0x0b<<16;
		if(!c1 || !c2 || !c3) {
			r_loc=2;
			break;
		}

		IO1CLR = 1<<19;
		IO1SET = 0x07<<16;
		if(!c1 || !c2 || !c3) {
			r_loc=3;
			break;
		}
	}
	while(!c1 || !c2 || !c3);
	return (key[r_loc][c_loc]);
}

void ser_init()
{
	VPBDIV=0x02;					//PCLK = 30MHz
	PINSEL0=0x5;
	U0LCR=0x83;
	U0DLL=195;
	U0DLM=0;
	U0LCR=0x03;
	U0TER=(1<<7);
}

void tx(unsigned char c)
{
	U0THR=c;
	while((U0LSR&(1<<5))==0);
}

void tx_string(unsigned char *s)
{
	while(*s) {
		tx(*s++);
	}
}

unsigned char rx()
{
	while((U0LSR&(1<<0))==0);
	return U0RBR;
}

Output

3 1 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
1
0
Would love your thoughts, please comment.x
Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh