Code Monkey home page Code Monkey logo

experiment--05-interfacing-a-4x4-matrix-keypad-and-display-the-output-on-lcd's Introduction

EXPERIMENT--05-INTERFACING-A-4X4-MATRIX-KEYPAD-AND-DISPLAY-THE-OUTPUT-ON-LCD

Aim:

To Interface a 4X4 matrix keypad and show the output on 16X2 LCD display to ARM controller , and simulate it in Proteus

Components required:

STM32 CUBE IDE, Proteus 8 simulator .

Theory:

image

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.

Procedure :

LCD 16X2

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:

image image image

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

Procedure:

  1. click on STM 32 CUBE IDE, the following screen will appear image

  2. click on FILE, click on new stm 32 project image image

  3. select the target to be programmed as shown below and click on next

image

4.select the program name image

  1. corresponding ioc file will be generated automatically image

6.select the appropriate pins as gipo, in or out, USART or required options and configure image image

7.click on cntrl+S , automaticall C program will be generated image image 8. edit the program and as per required image

  1. Add necessary library files of LCD 16x2 , write the program and use project and build

image 10. once the project is bulild image

  1. click on debug option image

  2. Creating Proteus project and running the simulation We are now at the last part of step by step guide on how to simulate STM32 project in Proteus.

  3. Create a new Proteus project and place STM32F40xx i.e. the same MCU for which the project was created in STM32Cube IDE.

  4. After creation of the circuit as per requirement as shown below

image

  1. Double click on the the MCU part to open settings. Next to the Program File option, give full path to the Hex file generated using STM32Cube IDE. Then set the external crystal frequency to 8M (i.e. 8 MHz). Click OK to save the changes. https://engineeringxpert.com/wp-content/uploads/2022/04/26.png

  2. click on debug and simulate using simulation as shown below

image

CIRCUIT DIAGRAM

STM 32 CUBE PROGRAM :

Name : M Gautham 
Ref. No: 212221230027
/* USER CODE BEGIN Header */
/**
  ****************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ****************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ****************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "lcd.h"
#include "stdbool.h"

bool col1,col2,col3,col4;

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);

void key();

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  key();
	  HAL_Delay(1000);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

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\n");
		  HAL_Delay(500);
		  col1=1;
	  }
	  else if(!col2)
	 	  {
	 		  Lcd_cursor(&lcd, 0,1);
	 				  Lcd_string(&lcd, "Key 8\n");
	 		  HAL_Delay(500);
	 		  col2=1;
	 	  }
	  else if(!col3)
	 	  {
	 		  Lcd_cursor(&lcd, 0,1);
	 				  Lcd_string(&lcd, "Key 9\n");
	 		  HAL_Delay(500);
	 		  col3=1;

	 	  }
	  else if(!col4)
	 	  {
	 		  Lcd_cursor(&lcd, 0,1);
	 				  Lcd_string(&lcd, "Key %\n");
	 		  HAL_Delay(500);
	 		  col4=1;
	 	  }

	  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\n");
	  		  HAL_Delay(500);
	  		  col1=1;
	  	  }
	  	  else if(!col2)
	  	 	  {
	  	 		  Lcd_cursor(&lcd, 0,1);
	  	 				  Lcd_string(&lcd, "Key 5\n");
	  	 		  HAL_Delay(500);
	  	 		  col2=1;
	  	 	  }
	  	  else if(!col3)
	  	 	  {
	  	 		  Lcd_cursor(&lcd, 0,1);
	  	 				  Lcd_string(&lcd, "Key 6\n");
	  	 		  HAL_Delay(500);
	  	 		  col3=1;

	  	 	  }
	  	  else if(!col4)
	  	 	  {
	  	 		  Lcd_cursor(&lcd, 0,1);
	  	 				  Lcd_string(&lcd, "Key *\n");
	  	 		  HAL_Delay(500);
	  	 		  col4=1;
	  	 	  }

	  	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\n");
	  			  HAL_Delay(500);
	  			  col1=1;
	  		  }
	  		  else if(!col2)
	  		 	  {
	  		 		  Lcd_cursor(&lcd, 0,1);
	  		 				  Lcd_string(&lcd, "Key 2\n");
	  		 		  HAL_Delay(500);
	  		 		  col2=1;
	  		 	  }
	  		  else if(!col3)
	  		 	  {
	  		 		  Lcd_cursor(&lcd, 0,1);
	  		 				  Lcd_string(&lcd, "Key 3\n");
	  		 		  HAL_Delay(500);
	  		 		  col3=1;

	  		 	  }
	  		  else if(!col4)
	  		 	  {
	  		 		  Lcd_cursor(&lcd, 0,1);
	  		 				  Lcd_string(&lcd, "Key -\n");
	  		 		  HAL_Delay(500);
	  		 		  col4=1;
	  		 	  }

	  		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/C\n");
	  				  HAL_Delay(500);
	  				  col1=1;
	  			  }
	  			  else if(!col2)
	  			 	  {
	  			 		  Lcd_cursor(&lcd, 0,1);
	  			 				  Lcd_string(&lcd, "Key 0\n");
	  			 		  HAL_Delay(500);
	  			 		  col2=1;
	  			 	  }
	  			  else if(!col3)
	  			 	  {
	  			 		  Lcd_cursor(&lcd, 0,1);
	  			 				  Lcd_string(&lcd, "Key =\n");
	  			 		  HAL_Delay(500);
	  			 		  col3=1;

	  			 	  }
	  			  else if(!col4)
	  			 	  {
	  			 		  Lcd_cursor(&lcd, 0,1);
	  			 				  Lcd_string(&lcd, "Key +\n");
	  			 		  HAL_Delay(500);
	  			 		  col4=1;
	  			 	  }

	              else
	  			  {
	  				Lcd_cursor(&lcd, 0,1);
	  						Lcd_string(&lcd, "NO KEYS PRESSED\n");
	  				HAL_Delay(500);

	  			  }





}
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1, GPIO_PIN_RESET);

  /*Configure GPIO pins : PC0 PC1 PC2 PC3 */
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /*Configure GPIO pins : PA0 PA1 PA2 PA3 */
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : PA4 PA5 PA6 PA7 */
  GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : PB0 PB1 */
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

Output screen shots of proteus :

BUTTON OFF:

image

BUTTON ON:

image

CIRCUIT DIAGRAM (EXPORT THE GRAPHICS TO PDF AND ADD THE SCREEN SHOT HERE):

image

Result :

Interfacing a 4x4 keypad with ARM microcontroller are simulated in proteus and the results are verified.

experiment--05-interfacing-a-4x4-matrix-keypad-and-display-the-output-on-lcd's People

Contributors

muppirgautham avatar vasanthkumarch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.