/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 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 <string.h>
#define ROW1_PIN GPIO_PIN_7
#define ROW1_PORT GPIOB
#define ROW2_PIN GPIO_PIN_6
#define ROW2_PORT GPIOB
#define ROW3_PIN GPIO_PIN_10
#define ROW3_PORT GPIOA
#define ROW4_PIN GPIO_PIN_3
#define ROW4_PORT GPIOB
#define COL1_PIN GPIO_PIN_10
#define COL1_PORT GPIOB
#define COL2_PIN GPIO_PIN_4
#define COL2_PORT GPIOB
#define COL3_PIN GPIO_PIN_5
#define COL3_PORT GPIOB
#define COL4_PIN GPIO_PIN_15
#define COL4_PORT GPIOA
/* 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);
/* USER CODE BEGIN PFP */
uint8_t Scan_Keypad(void);
/* 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)
{
/*Row 1-PB7,
Row 2-PB6,
Row 3-PA10,
Row 4-PB3,
Col 1-PB10,
Col 2-PB4,
Col 3-PB5,
Col 4-PA 15,*/
uint8_t key = Scan_Keypad();
if (key == 1) {
printf("1\n"); // Print "1" when key 1 is pressed
} else if (key == 4) {
printf("4\n"); // Print "4" when key 2 is pressed
} else if (key == 7) {
printf("7\n"); // Print "7" when key 3 is pressed
}
else if (key == '*') {
printf("*\n"); // Print "*" when key * is pressed
}
if (key == 2) {
printf("2\n"); // Print "2" when key 2 is pressed
} else if (key == 5) {
printf("5\n"); // Print "5" when key 5 is pressed
} else if (key == 8) {
printf("8\n"); // Print "8" when key 8 is pressed
}
else if (key == 0) {
printf("0\n"); // Print "0" when key 0 is pressed
}
if (key == 3) {
printf("3\n"); // Print "1" when key 3 is pressed
} else if (key == 6) {
printf("6\n"); // Print "4" when key 6 is pressed
} else if (key == 9) {
printf("9\n"); // Print "7" when key 9 is pressed
}
else if (key == '#') {
printf("#\n"); // Print "*" when key # is pressed
}
if (key == 'A') {
printf("A\n"); // Print "1" when key A is pressed
} else if (key == 'B') {
printf("B\n"); // Print "4" when key B is pressed
} else if (key == 'C') {
printf("C\n"); // Print "7" when key C is pressed
}
else if (key == 'D') {
printf("D\n"); // Print "*" when key D is pressed
}
}
}
uint8_t Scan_Keypad(void) {
// Set all columns high
HAL_GPIO_WritePin(COL1_PORT, COL1_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(COL2_PORT, COL2_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(COL3_PORT, COL3_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(COL4_PORT, COL4_PIN, GPIO_PIN_SET);
// Scan the first column (for key 1, 4, 7, *.)
HAL_GPIO_WritePin(COL1_PORT, COL1_PIN, GPIO_PIN_RESET); // Set COL1 low
// Check the rows
if (HAL_GPIO_ReadPin(ROW1_PORT, ROW1_PIN) == GPIO_PIN_RESET) {
return 1; // Key "1" is pressed (Row 1, Col 1)
} else if (HAL_GPIO_ReadPin(ROW2_PORT, ROW2_PIN) == GPIO_PIN_RESET) {
return 4; // Key "2" is pressed (Row 2, Col 1)
} else if (HAL_GPIO_ReadPin(ROW3_PORT, ROW3_PIN) == GPIO_PIN_RESET) {
return 7; // Key "3" is pressed (Row 3, Col 1)
}
else if (HAL_GPIO_ReadPin(ROW4_PORT, ROW4_PIN) == GPIO_PIN_RESET) {
return '*'; // Key "*" is pressed (Row 4, Col 1)
}
// Set the column back high before scanning next column
HAL_GPIO_WritePin(COL1_PORT, COL1_PIN, GPIO_PIN_SET);
// Set all columns high
HAL_GPIO_WritePin(COL1_PORT, COL1_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(COL2_PORT, COL2_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(COL3_PORT, COL3_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(COL4_PORT, COL4_PIN, GPIO_PIN_SET);
// Scan the first column (for key 2 , 5 ,8 ,0)
HAL_GPIO_WritePin(COL2_PORT, COL2_PIN, GPIO_PIN_RESET); // Set COL2 low
// Check the rows
if (HAL_GPIO_ReadPin(ROW1_PORT, ROW1_PIN) == GPIO_PIN_RESET) {
return 2; // Key "1" is pressed (Row 1, Col 2)
} else if (HAL_GPIO_ReadPin(ROW2_PORT, ROW2_PIN) == GPIO_PIN_RESET) {
return 5; // Key "2" is pressed (Row 2, Col 2)
} else if (HAL_GPIO_ReadPin(ROW3_PORT, ROW3_PIN) == GPIO_PIN_RESET) {
return 8; // Key "3" is pressed (Row 3, Col 2)
}
else if (HAL_GPIO_ReadPin(ROW4_PORT, ROW4_PIN) == GPIO_PIN_RESET) {
return 0; // Key "*" is pressed (Row 4, Col 2)
}
// Set the column back high before scanning next column
HAL_GPIO_WritePin(COL2_PORT, COL2_PIN, GPIO_PIN_SET);
// Set all columns high
HAL_GPIO_WritePin(COL1_PORT, COL1_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(COL2_PORT, COL2_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(COL3_PORT, COL3_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(COL4_PORT, COL4_PIN, GPIO_PIN_SET);
// Scan the first column (for key 3, 6, 9, #.)
HAL_GPIO_WritePin(COL3_PORT, COL3_PIN, GPIO_PIN_RESET); // Set COL3low
// Check the rows
if (HAL_GPIO_ReadPin(ROW1_PORT, ROW1_PIN) == GPIO_PIN_RESET) {
return 3; // Key "3" is pressed (Row 1, Col 3)
} else if (HAL_GPIO_ReadPin(ROW2_PORT, ROW2_PIN) == GPIO_PIN_RESET) {
return 6; // Key "6" is pressed (Row 2, Col 3)
} else if (HAL_GPIO_ReadPin(ROW3_PORT, ROW3_PIN) == GPIO_PIN_RESET) {
return 9; // Key "9" is pressed (Row 3, Col 3)
}
else if (HAL_GPIO_ReadPin(ROW4_PORT, ROW4_PIN) == GPIO_PIN_RESET) {
return '#'; // Key "#" is pressed (Row 4, Col 3)
}
// Set the column back high before scanning next column
HAL_GPIO_WritePin(COL3_PORT, COL3_PIN, GPIO_PIN_SET);
// Set all columns high
HAL_GPIO_WritePin(COL1_PORT, COL1_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(COL2_PORT, COL2_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(COL3_PORT, COL3_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(COL4_PORT, COL4_PIN, GPIO_PIN_SET);
// Scan the first column (for key A, B, C, D.)
HAL_GPIO_WritePin(COL4_PORT, COL4_PIN, GPIO_PIN_RESET); // Set COL1 low
// Check the rows
if (HAL_GPIO_ReadPin(ROW1_PORT, ROW1_PIN) == GPIO_PIN_RESET) {
return 'A'; // Key "A" is pressed (Row 1, Col 4)
} else if (HAL_GPIO_ReadPin(ROW2_PORT, ROW2_PIN) == GPIO_PIN_RESET) {
return 'B'; // Key "B" is pressed (Row 2, Col 4)
} else if (HAL_GPIO_ReadPin(ROW3_PORT, ROW3_PIN) == GPIO_PIN_RESET) {
return 'C'; // Key "C" is pressed (Row 3, Col 4)
}
else if (HAL_GPIO_ReadPin(ROW4_PORT, ROW4_PIN) == GPIO_PIN_RESET) {
return 'D'; // Key "D" is pressed (Row 4, Col 4)
}
// Set the column back high before scanning next column
HAL_GPIO_WritePin(COL4_PORT, COL4_PIN, GPIO_PIN_SET);
// You can extend this for other keys by scanning other columns and rows
}
/**
* @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.HSIDiv = RCC_HSI_DIV1;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != 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_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10|GPIO_PIN_4|GPIO_PIN_5, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
/*Configure GPIO pins : PB10 PB4 PB5 */
GPIO_InitStruct.Pin = GPIO_PIN_10|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 pin : PA10 */
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*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 PB6 PB7 */
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
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 */