#include "stm32c0xx_hal.h"
#include <string.h>
#include "lcd.h"
#include "keypad.h"
#define PASSWORD_LENGTH 4
// LCD Pin Mapping on GPIOB
#define RS_Pin GPIO_PIN_0
#define E_Pin GPIO_PIN_1
#define D4_Pin GPIO_PIN_2
#define D5_Pin GPIO_PIN_3
#define D6_Pin GPIO_PIN_4
#define D7_Pin GPIO_PIN_5
#define LCD_Port GPIOB
// === MAIN SETUP ===
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
char Correct_Password[PASSWORD_LENGTH + 1] = "1234";
char Entered_Password[PASSWORD_LENGTH + 1] = {0};
uint8_t password_index = 0;
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
LCD_Init();
LCD_SetCursor(0, 0);
LCD_WriteString("ENTER PASSWORD:");
while (1) {
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
// Configure HSI as system clock source
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
// NO PLL available in basic config for STM32C031C6 on Wokwi
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
// Clock configuration: use HSI for SYSCLK
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void) {
__HAL_RCC_GPIOA_CLK_ENABLE(); // Ensure GPIOA clock is enabled
__HAL_RCC_GPIOB_CLK_ENABLE();
//GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitTypeDef GPIO_InitStruct = {0};
// LCD Pins PB0–PB5: Output
GPIO_InitStruct.Pin = RS_Pin|E_Pin|D4_Pin|D5_Pin|D6_Pin|D7_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LCD_Port, &GPIO_InitStruct);
// Keypad Rows PB6–PB9: Output
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// Keypad Cols PB10–PB13: Input with Pull-Up
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
//FOR RGB LED'S
// __HAL_RCC_GPIOA_CLK_ENABLE(); // Ensure GPIOA clock is enabled
GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
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);
}
void Error_Handler(void)
{
// You can blink an LED here or print an error if needed
while (1)
{
// Infinite loop to indicate error
}
}
// #include "stm32c0xx_hal.h"
// #include "lcd.h"
// #include "keypad.h"
// #include <string.h>
// // prototypes
// void SystemClock_Config(void);
// static void MX_GPIO_Init(void);
// ADC_HandleTypeDef hadc1;
// int main(void) {
// HAL_Init();
// SystemClock_Config();
// MX_GPIO_Init();
// LCD_Init();
// LCD_Set_Cursor(0, 0);
// LCD_Write_String("Testing LCD");
// HAL_Delay(3000); // wait 3 seconds
// LCD_Clear();
// while (1) {
// char key = keypad_get_key();
// if (key != '\0') {
// LCD_Clear();
// LCD_Set_Cursor(0, 0);
// LCD_Write_Char(key);
// printf("Key pressed: %c\n", key);
// }
// }
// }
// void SystemClock_Config(void)
// {
// RCC_OscInitTypeDef RCC_OscInitStruct = {0};
// RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
// __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);
// /** 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();
// }
// }
// static void MX_ADC1_Init(void)
// {
// /* USER CODE BEGIN ADC1_Init 0 */
// /* USER CODE END ADC1_Init 0 */
// ADC_ChannelConfTypeDef sConfig = {0};
// /* USER CODE BEGIN ADC1_Init 1 */
// /* USER CODE END ADC1_Init 1 */
// /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
// */
// hadc1.Instance = ADC1;
// hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
// hadc1.Init.Resolution = ADC_RESOLUTION_12B;
// hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
// hadc1.Init.ScanConvMode = ADC_SCAN_SEQ_FIXED;
// hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
// hadc1.Init.LowPowerAutoWait = DISABLE;
// hadc1.Init.LowPowerAutoPowerOff = DISABLE;
// hadc1.Init.ContinuousConvMode = DISABLE;
// hadc1.Init.NbrOfConversion = 1;
// hadc1.Init.DiscontinuousConvMode = DISABLE;
// hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
// hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
// hadc1.Init.DMAContinuousRequests = DISABLE;
// hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
// hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_1CYCLE_5;
// hadc1.Init.OversamplingMode = DISABLE;
// hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
// if (HAL_ADC_Init(&hadc1) != HAL_OK)
// {
// Error_Handler();
// }
// /** Configure Regular Channel
// */
// sConfig.Channel = ADC_CHANNEL_0;
// sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
// if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
// {
// Error_Handler();
// }
// /* USER CODE BEGIN ADC1_Init 2 */
// /* USER CODE END ADC1_Init 2 */
// }
// /**
// * @brief GPIO Initialization Function
// * @param None
// * @retval None
// */
// static void MX_GPIO_Init(void)
// {
// // __HAL_RCC_GPIOA_CLK_ENABLE();
// // __HAL_RCC_GPIOB_CLK_ENABLE();
// GPIO_InitTypeDef GPIO_InitStruct = {0};
// // Rows: PB6 - PB9 -> Output
// GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
// 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);
// // Columns: PB10 - PB13 -> Input
// GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
// GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
// GPIO_InitStruct.Pull = GPIO_PULLUP; // use PULLUP to detect LOW on press
// HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// // LCD pins (GPIOA) setup goes here (already done if working before)
// // GPIO_InitTypeDef GPIO_InitStruct = {0};
// // Set column pins as input with pull-down
// GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
// GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
// GPIO_InitStruct.Pull = GPIO_PULLDOWN;
// HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// GPIO_InitStruct.Pin = GPIO_PIN_15;
// HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); // PB15 column
// /* GPIO Ports Clock Enable */
// __HAL_RCC_GPIOA_CLK_ENABLE();
// __HAL_RCC_GPIOB_CLK_ENABLE();
// __HAL_RCC_GPIOD_CLK_ENABLE();
// /*Configure GPIO pin Output Level */
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5
// |GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9
// |GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
// |GPIO_PIN_15, GPIO_PIN_RESET);
// /*Configure GPIO pin Output Level */
// HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_10
// |GPIO_PIN_15|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5
// |GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET);
// /*Configure GPIO pin Output Level */
// HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);
// /*Configure GPIO pins : PA2 PA3 PA4 PA5
// PA6 PA7 PA8 PA9
// PA11 PA12 PA13 PA14
// PA15 */
// GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5
// |GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9
// |GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
// |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 : PB0 PB1 PB2 PB10
// PB15 PB3 PB4 PB5
// PB6 PB7 PB8 PB9 */
// GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_10
// |GPIO_PIN_15|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5
// |GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
// 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 : PD0 PD1 */
// GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
// GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
// GPIO_InitStruct.Pull = GPIO_NOPULL;
// HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
// /*Configure GPIO pin : PD3 */
// GPIO_InitStruct.Pin = 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(GPIOD, &GPIO_InitStruct);
// /* USER CODE BEGIN MX_GPIO_Init_2 */
// /* USER CODE END MX_GPIO_Init_2 */
// }
// /* 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 *-------------------------------------------------------*/
Loading
st-nucleo-c031c6
st-nucleo-c031c6