LPC2148 – Keypad Interfacing

This article is a continuation of the series of tutorials on the LPC2148 Microcontroller (ARM7). The aim of this series is to provide easy and practical examples that anyone can understand. In the previous tutorial, we have interfaced the Relay with LPC2148 (ARM7). Now, 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…

Suggestion 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 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 the serial terminal.

Keypad interfacing with LPC2148

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 the 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

Keypad interfacing with LPC2148

In our next tutorial, we will see how to interface the GSM module with LPC2148 (ARM7). If you want to use FreeRTOS on LPC2148, then please refer FreeRTOS series.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
Bootloader Tutorials

4 2 votes
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