name :A.Anbuselvam
reg :212222240009
To Interface a 4X4 matrix keypad and show the output on 16X2 LCD display to ARM controller , and simulate it in Proteus
STM32 CUBE IDE, Proteus 8 simulator .
![](https://private-user-images.githubusercontent.com/36288975/239758557-2a4a795e-1674-4329-ae07-3f5e8d5073e2.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUzMTU2MzMsIm5iZiI6MTcyNTMxNTMzMywicGF0aCI6Ii8zNjI4ODk3NS8yMzk3NTg1NTctMmE0YTc5NWUtMTY3NC00MzI5LWFlMDctM2Y1ZThkNTA3M2UyLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MDIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTAyVDIyMTUzM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTdiN2IxZmI0MmU5NjJhMjYxMjNmMWZmMTcwZjUxYWU3Yjg3Y2Q4NDQ5NjI1NDhiZWE1MjQ4OTc2NDVhZmViOTImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.xarm2dFeFOnTSSzlHiqqtitU8utCZf8ZV30yNURfGrE)
4×4 Keypad Module Pin Diagram
4x4 Keypad module Pin Diagram 4×4 Keypad module Pin Diagram Pin Number Pin Name Description 1 R1 Taken out from 1st ROW 2 R2 Taken out from 2nd ROW 3 R3 Taken out from 3rd ROW 4 R4 Taken out from 4th ROW 5 C1 Taken out from 1st COLUMN 6 C2 Taken out from 2nd COLUMN 7 C3 Taken out from 3rd COLUMN 8 C4 Taken out from 4th COLUMN 4×4 Matrix Keypad Module Hardware Overview These Keypad modules are made of thin, flexible membrane material. The 4 x4 keypad module consists of 16 keys, these Keys are organized in a matrix of rows and columns. All these switches are connected to each other with a conductive trace. Normally there is no connection between rows and columns. When we will press a key, then a row and a column make contact.
16×2 LCD is named so because; it has 16 Columns and 2 Rows. There are a lot of combinations available like, 8×1, 8×2, 10×2, 16×1, etc. But the most used one is the 16*2 LCD, hence we are using it here.
All the above mentioned LCD display will have 16 Pins and the programming approach is also the same and hence the choice is left to you. Below is the Pinout and Pin Description of 16x2 LCD Module:
4-bit and 8-bit Mode of LCD:
The LCD can work in two different modes, namely the 4-bit mode and the 8-bit mode. In 4 bit mode we send the data nibble by nibble, first upper nibble and then lower nibble. For those of you who don’t know what a nibble is: a nibble is a group of four bits, so the lower four bits (D0-D3) of a byte form the lower nibble while the upper four bits (D4-D7) of a byte form the higher nibble. This enables us to send 8 bit data.
Whereas in 8 bit mode we can send the 8-bit data directly in one stroke since we use all the 8 data lines.
8-bit mode is faster and flawless than 4-bit mode. But the major drawback is that it needs 8 data lines connected to the microcontroller. This will make us run out of I/O pins on our MCU, so 4-bit mode is widely used. No control pins are used to set these modes. LCD Commands:
There are some preset commands instructions in LCD, which we need to send to LCD through some microcontroller. Some important command instructions are given below:
Hex Code
Command to LCD Instruction Register
0F
LCD ON, cursor ON
01
Clear display screen
02
Return home
04
Decrement cursor (shift cursor to left)
06
Increment cursor (shift cursor to right)
05
Shift display right
07
Shift display left
0E
Display ON, cursor blinking
80
Force cursor to beginning of first line
C0
Force cursor to beginning of second line
38
2 lines and 5×7 matrix
83
Cursor line 1 position 3
3C
Activate second line
08
Display OFF, cursor OFF
C1
Jump to second line, position 1
OC
Display ON, cursor OFF
C1
Jump to second line, position 1
C2
Jump to second line, position 2
Select a new STM32 Project.
Select GPIO Ports
PC0 , PC1 , PC2 , PC3 , PA0 , PA1 , PA2 , PA3 , PB0 , PB1 -> Output
PC4 , PC5 , PC7 , PC8 -> Input
Configure the Input Ports at Pull up Mode followed by generating the code.
Build Debug and Create 'hex.file'
Open a new Proteus Project.
Select STM32F401RB, LCD 16*2 and Keypad.
Connect PA0 to D7 , PA1 to D6 , PA2 to D5 , PA3 to D4 , PB0 to RS , PB1 to E , PC0 to r1 , PC1 to r2 , PC2 to r3 , PC3 to r4 , PC4 to c1 , PC5 to c2 , PC6 to c3 and PC7 to c4.
Check the execution of the output using Run Option.
#include "main.h"
#include"stdbool.h"
#include"lcd.h"
bool col1,col2,col3,col4;
void key();
while (1)
{
key();
HAL_Delay(1000);
}
void key(){
Lcd_PortType ports[]={GPIOA,GPIOA,GPIOA,GPIOA};
Lcd_PinType pins[]={GPIO_PIN_3,GPIO_PIN_2,GPIO_PIN_1,GPIO_PIN_0};
Lcd_HandleTypeDef lcd;
lcd=Lcd_create(ports,pins,GPIOB,GPIO_PIN_0,GPIOB,GPIO_PIN_1,LCD_4_BIT_MODE);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_0,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_1,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,GPIO_PIN_SET);
col1=HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_4);
col2=HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_5);
col3=HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_6);
col4=HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_7);
if(!col1){
Lcd_cursor(&lcd,0,1);
Lcd_string(&lcd,"key 7 pressed");
col1=1;
}
else if(!col2){
Lcd_cursor(&lcd,0,1);
Lcd_string(&lcd,"key 8 pressed");
col2=1;
}
else if(!col3){
Lcd_cursor(&lcd,0,1);
Lcd_string(&lcd,"key 9 pressed");
col3=1;
}
else if(!col4){
Lcd_cursor(&lcd,0,1);
Lcd_string(&lcd,"key / pressed");
col4=1;
}
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3, GPIO_PIN_SET);
col1 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_4);
col2 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_5);
col3 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_6);
col4 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_7);
if(!col1)
{
Lcd_cursor(&lcd, 0,1);
Lcd_string(&lcd, "key 4 pressed");
col1=1;
}
else if(!col2)
{
Lcd_cursor(&lcd, 0,1);
Lcd_string(&lcd, "key 5 pressed");
col2=1;
}
else if(!col3)
{
Lcd_cursor(&lcd, 0,1);
Lcd_string(&lcd, "key 6 pressed");
col3=1;
}
else if(!col4)
{
Lcd_cursor(&lcd, 0,1);
Lcd_string(&lcd, "key X pressed");
col4=1;
}
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3, GPIO_PIN_SET);
col1 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_4);
col2 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_5);
col3 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_6);
col4 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_7);
if(!col1)
{
Lcd_cursor(&lcd, 0,1);
Lcd_string(&lcd, "key 1 pressed");
col1=1;
}
else if(!col2)
{
Lcd_cursor(&lcd, 0,1);
Lcd_string(&lcd, "key 2 pressed");
col2=1;
}
else if(!col3)
{
Lcd_cursor(&lcd, 0,1);
Lcd_string(&lcd, "key 3 pressed");
col3=1;
}
else if(!col4)
{
Lcd_cursor(&lcd, 0,1);
Lcd_string(&lcd, "key - pressed");
col4=1;
}
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3, GPIO_PIN_RESET);
col1 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_4);
col2 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_5);
col3 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_6);
col4 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_7);
if(!col1)
{
Lcd_cursor(&lcd, 0,1);
Lcd_string(&lcd, "key ON pressed");
col1=1;
}
else if(!col2)
{
Lcd_cursor(&lcd, 0,1);
Lcd_string(&lcd, "key 0 pressed");
col2=1;
}
else if(!col3)
{
Lcd_cursor(&lcd, 0,1);
Lcd_string(&lcd, "key = pressed");
col3=1;
}
else if(!col4)
{
Lcd_cursor(&lcd, 0,1);
Lcd_string(&lcd, "key + pressed");
col4=1;
}
else{
Lcd_cursor(&lcd,0,1);
Lcd_string(&lcd,"no key pressed");
HAL_Delay(500);
}
}
![](https://private-user-images.githubusercontent.com/119559871/279257145-46f7d440-208d-413b-adc1-074c98aeffbd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUzMTU2MzMsIm5iZiI6MTcyNTMxNTMzMywicGF0aCI6Ii8xMTk1NTk4NzEvMjc5MjU3MTQ1LTQ2ZjdkNDQw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yZT02OTM3N2NmNGYzMWI0ZGU1YWVkMTg0YzIyNWNmMzMyY2ZlMDY1MmVhOWQwMDQyYzgwOWU1NWMyZjQyYWY2MWRiJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.BaOsCwfBRdu2oBe61D3BUNWORlZt6bBBy4mUUApm8UI)
![](https://private-user-images.githubusercontent.com/119559871/279257231-8bfda57a-5b1e-4ab0-8526-4a3f62739789.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUzMTU2MzMsIm5iZiI6MTcyNTMxNTMzMywicGF0aCI6Ii8xMTk1NTk4NzEvMjc5MjU3MjMxLThiZmRhNTdhLTViMWUtNGFiMC04NTI2LTRhM2Y2MjczOTc4OS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTAyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDkwMlQyMjE1MzNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT01NTRjMmRiZjlkMjkzNzllMjI0NmZlY2UyZTlmZWQzNzU5NTU0MmM5NWQyYThlMDIzYjllZDllMjRjNmFiZDRjJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.DqszqswlnHxIX2gNeGVMNNTiwDhKe8ucOXns3RHJTvk)
Interfacing a 4x4 keypad with ARM microcontroller are simulated in proteus and the results are verified.