In the last blog we discussed what a keypad module is . In this blog we will be discussing how to interface a 4×4 membrane type keypad module with stm32 microcontroller. Although the keypad module can be configured using both polling and interrupt methods , in this blog we will be discussing the interrupt method as it takes less CPU cycles , also since interrupt doesn’t require constant CPU attention. Keypad module along with stm32 microcontroller finds applications in industrial machines as well various projects such as bluetooth door lock , scientific calculator etc.
Hardware description
The keypad module consists of 3 layers:-
- The top layer is made of polymer which usually is the printed graphic overlay . It defines the function of each button in the keypad module . It is heat and water resistant and also prevents from electric discharge.
- The middle layer that usually consists of dome shaped stainless buttons which upon touch makes contact with the bottom layer completing the circuitry.
- The bottom layer consists of printed circuitry made of conductive material such as silver or copper oxide. When a key is pressed the dome shaped buttons make contact which completes the circuit and this information is conveyed to microcontroller in the form of signal by the external ribbon.
The 4×4 keypad module chosen by us consists of numbers from 0 to 9 along with 2 characters ‘*’ and ‘#’ in the first 3 columns while the last column consists of alphabets A, B ,C , D.
The internal circuit diagram of the keypad module
- As can be seen one end of each button is connected to a wire that is then connected to the connector pin as row and other end is connected to another wire which is the column wire. Both the row and column wires helps us pinpoint the button that is pressed.
- The connection are then made to the MCU in which the row part is configured as GPIO output and written high in our case this will be PA15, PB3, PB4 , PB5 pins the column part is then written as GPIO EXTI interrupt which will get triggered in case of a button push , this will be PB6,PB7,PB8,PB9 in our case .
- As can be seen in the figure since each row from one side is already HIGH once the connection is made by pressing the button this is read by the MCU pins PB6,PB7,PB8,PB9 and the interrupt is raised which is detected in the rising edge.
- Besides the keypad module connection UART1 is also enable as PA9 as TX and PA10 as RX
- This will be used to display the button pressed when the interrupt is fired using teraterm or similar software.
ALGORITHM
Configure pins PA15,PB3,PB4,PB5 as output pins with no pull up and low frequency
Configure pins PB6, PB7,PB8, PB9 as interrupt pins with no pull up and interrupt detection on the rising edge
Set the NVIC priority as 0 and enable IRQ
Incoming interrupts are handled using EXTI callback function
Initialize the UART1 function , configure the parameters and select PA9 as tx and PA10 as RX
Hal tick function is called to use key debouncing and prevent unwanted input noise from the buttons
2 variables currentMillis and previousMillis are declared which prevents extra activations or slow functions from triggering the interrupts this is done by subtracting the former from latter and the difference should be greater than 10
Each row is written GPIO 1 and checked if the corresponding column is gpio 1 as well. in case both the conditions are satisfied the interrupt is raised and a keypress is detected
The keypressed is displayed on Teraterm or similar software using UART1
SOFTWARE CONFIGURATION
Open the stm32cube ide and configure the sys mode, select the debug as serial wire and time base source as systick . In the GPIO section select the pins PA15, PB3,PB4, PB5 as output pins set parameters to default which is GPIO pins to no pull and operations at low frequency .
Next select the pins PB6,PB7,PB8,PB9 as EXTI interrupt pins also in the NVIC section enable the line EXTI line [9:5] interrupts .Also configure the GPIO EXTI as interrupt rising which would read the occurrence of the interrupt on the rising edge of the wave and operation as low frequency .
The NVIC priority is set to 0 (the highest) and the IRQ is enabled. All the button or key presses are handled by HAL_GPIO_EXTI_Callback function in which the keydebouncing is applied and each row is written GPIO HIGH incase a button press the respective column goes high as well raising the interrupt and the corresponding key is displayed using UART .for eg-The button press such as key 9 is detected when both PB3 and PB7 is HIGH as due to the press a connection is made and an interrupt is raised.
Also in the connectivity section select the UART1 set the parameters such as mode to asynchronous , baud rate 115200, Word-length as 8 bits , Stop bits as 1 , mode as TX and RX , hardware flow control as none and oversampling rate as 16 . The UART1 has pin PA9 as TX and PA10 as RX . The uart is used to display the key that is pressed on a teraterm or similar software with baud rate set to 115200
SOFTWARE CONFIGURATION
CODE
#include "main.h"
#include
#include
/* Private includes ----------------------------------------------------------*/
char buffer[32]={0};
/* 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 ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
GPIO_InitTypeDef GPIO_InitStructPrivate = {0};
uint32_t previousMillis = 0;
uint32_t currentMillis = 0;
uint8_t keyPressed = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
/* 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();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** 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 USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @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_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5, GPIO_PIN_RESET);
/*Configure GPIO pin : PA15 */
GPIO_InitStruct.Pin = GPIO_PIN_15;
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 : PB3 PB4 PB5 */
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
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);
/*Configure GPIO pins : PB6 PB7 PB8 PB9 */
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
}
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
currentMillis = HAL_GetTick();
if (currentMillis - previousMillis > 10) {
/*Configure GPIO pins : PB6 PB7 PB8 PB9 to GPIO_INPUT*/
GPIO_InitStructPrivate.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStructPrivate.Mode = GPIO_MODE_INPUT;
GPIO_InitStructPrivate.Pull = GPIO_NOPULL;
GPIO_InitStructPrivate.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructPrivate);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0);
if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6))
{
char KEYPRESSED[1]="D"; //ASCII value of D
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);
}
else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7))
{
char KEYPRESSEDC[1]="C";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSEDC);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of C
}
else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8))
{
char KEYPRESSEDB[1]="B";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSEDB);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of B
}
else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9))
{
char KEYPRESSEDA[1]="A";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSEDA);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of A
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0);
if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6))
{
char KEYPRESSEDP[1]="#";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSEDP);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of #
}
else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7))
{
char KEYPRESSED9[1]="9";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED9);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 9
}
else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8))
{
char KEYPRESSED6[1]="6";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED6);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 6
}
else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9))
{
char KEYPRESSED3[1]="3";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED3);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 3
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0);
if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6))
{
char KEYPRESSED0[1]="0";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED0);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 0
}
else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7))
{
char KEYPRESSED8[1]="8";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED8);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 8
}
else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8))
{
char KEYPRESSED5[1]="5";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED5);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 5
}
else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9))
{
char KEYPRESSED2[1]="2";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED2);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 2
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1);
if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6))
{
char KEYPRESSEDQ[1] = "*";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSEDQ);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of *
}
else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7))
{
char KEYPRESSED7[1] = "7";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED7);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 7
}
else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8))
{
char KEYPRESSED4[1] = "4";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED4);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 4
}
else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9))
{
char KEYPRESSED1[1] = "1";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED1);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 1
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1);
/*Configure GPIO pins : PB6 PB7 PB8 PB9 back to EXTI*/
GPIO_InitStructPrivate.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStructPrivate.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructPrivate);
previousMillis = currentMillis;
}
}
/* 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 */
CODE EXPLANATION
Intially include the library stdio.h and string.h that helps with sprintf functions and string functions . Next create a buffer that is char type array which stores the keypress character to display on the teraterm via uart.Initialize the uart next as uart1 and also gpio structure . Now the 32 bit variables previousMillis and currentMillis is declared which helps debouncing
Next in the main function the system clock configure function is called along with the UART1 and GPIO function that helps in initializing the configured peripherals .Next set the row pins PA15,PB3,P4,PB5 are set as HIGH.
Finally in the GPIO_EXTI_Callback the currentMillis function is assigned the time using HAL_GetTick function .The code enters the if condition when currentMillis and previousMillis function has difference greater than 10 sec as the previousMillis is assigned the value present in currentMillis at the end of the EXTI_Callback function.After entering the if condition each row is pulled HIGH simultaneously and in case of the button press the column is pulled HIGH which triggers the respective interrupt pin and the another if condition is satisfied in which the command to display the respective key is given .The sprintf function saves the keypressed and the HAL_Transmit function transmits the content of the buffer using UART1.
FUNCTIONAL DISCRIPTION
if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6))
{
char KEYPRESSED[1]="D"; //ASCII value of D
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
currentMillis = HAL_GetTick();
if (currentMillis - previousMillis > 10) {
/*Configure GPIO pins : PB6 PB7 PB8 PB9 to GPIO_INPUT*/
GPIO_InitStructPrivate.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStructPrivate.Mode = GPIO_MODE_INPUT;
GPIO_InitStructPrivate.Pull = GPIO_NOPULL;
GPIO_InitStructPrivate.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructPrivate);
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED);
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);
GPIO_InitStruct.Pin = GPIO_PIN_15;
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);
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
#include "main.h"
#include
#include
/* Private includes ----------------------------------------------------------*/
char buffer[32]={0};
/* 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 ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
GPIO_InitTypeDef GPIO_InitStructPrivate = {0};
uint32_t previousMillis = 0;
uint32_t currentMillis = 0;
uint8_t keyPressed = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
/* 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();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** 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 USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @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_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5, GPIO_PIN_RESET);
/*Configure GPIO pin : PA15 */
GPIO_InitStruct.Pin = GPIO_PIN_15;
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 : PB3 PB4 PB5 */
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
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);
/*Configure GPIO pins : PB6 PB7 PB8 PB9 */
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
}
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
currentMillis = HAL_GetTick();
if (currentMillis - previousMillis > 10) {
/*Configure GPIO pins : PB6 PB7 PB8 PB9 to GPIO_INPUT*/
GPIO_InitStructPrivate.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStructPrivate.Mode = GPIO_MODE_INPUT;
GPIO_InitStructPrivate.Pull = GPIO_NOPULL;
GPIO_InitStructPrivate.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructPrivate);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0);
if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6))
{
char KEYPRESSED[1]="D"; //ASCII value of D
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);
}
else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7))
{
char KEYPRESSEDC[1]="C";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSEDC);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of C
}
else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8))
{
char KEYPRESSEDB[1]="B";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSEDB);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of B
}
else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9))
{
char KEYPRESSEDA[1]="A";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSEDA);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of A
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0);
if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6))
{
char KEYPRESSEDP[1]="#";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSEDP);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of #
}
else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7))
{
char KEYPRESSED9[1]="9";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED9);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 9
}
else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8))
{
char KEYPRESSED6[1]="6";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED6);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 6
}
else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9))
{
char KEYPRESSED3[1]="3";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED3);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 3
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0);
if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6))
{
char KEYPRESSED0[1]="0";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED0);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 0
}
else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7))
{
char KEYPRESSED8[1]="8";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED8);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 8
}
else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8))
{
char KEYPRESSED5[1]="5";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED5);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 5
}
else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9))
{
char KEYPRESSED2[1]="2";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED2);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 2
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1);
if(GPIO_Pin == GPIO_PIN_6 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6))
{
char KEYPRESSEDQ[1] = "*";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSEDQ);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of *
}
else if(GPIO_Pin == GPIO_PIN_7 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7))
{
char KEYPRESSED7[1] = "7";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED7);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 7
}
else if(GPIO_Pin == GPIO_PIN_8 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8))
{
char KEYPRESSED4[1] = "4";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED4);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 4
}
else if(GPIO_Pin == GPIO_PIN_9 && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9))
{
char KEYPRESSED1[1] = "1";
sprintf(buffer,"KEYPRESSED:%s\r\n", KEYPRESSED1);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 25);//ASCII value of 1
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1);
/*Configure GPIO pins : PB6 PB7 PB8 PB9 back to EXTI*/
GPIO_InitStructPrivate.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStructPrivate.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructPrivate);
previousMillis = currentMillis;
}
}
/* 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 */
Author: Kunal Gupta
Author