// STM32 Nucleo-L031K6 HAL Blink + printf() example
// Simulation: https://wokwi.com/projects/367244067477216257
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stm32l0xx_hal.h>
// Define GPIO pins for the 7-segment display
#define SEG_A_PIN GPIO_PIN_0
#define SEG_B_PIN GPIO_PIN_1
#define SEG_C_PIN GPIO_PIN_2
#define SEG_D_PIN GPIO_PIN_3
#define SEG_E_PIN GPIO_PIN_4
#define SEG_F_PIN GPIO_PIN_5
#define SEG_G_PIN GPIO_PIN_6
#define SEG_PORT GPIOB
// Define GPIO pins for the buttons (0-9, +, -, Enter)
#define BTN_0_PIN GPIO_PIN_0
#define BTN_1_PIN GPIO_PIN_1
#define BTN_2_PIN GPIO_PIN_2
#define BTN_3_PIN GPIO_PIN_3
#define BTN_4_PIN GPIO_PIN_4
#define BTN_5_PIN GPIO_PIN_5
#define BTN_6_PIN GPIO_PIN_6
#define BTN_7_PIN GPIO_PIN_7
#define BTN_8_PIN GPIO_PIN_8
#define BTN_9_PIN GPIO_PIN_9
#define BTN_ADD_PIN GPIO_PIN_10
#define BTN_SUB_PIN GPIO_PIN_11
#define BTN_ENTER_PIN GPIO_PIN_12
#define BTN_PORT GPIOA
// 7-segment digit encoding (common cathode)
const uint8_t seven_seg_digits[] = {
0xC0, // 0
0xF9, // 1
0xA4, // 2
0xB0, // 3
0x99, // 4
0x92, // 5
0x82, // 6
0xF8, // 7
0x80, // 8
0x90 // 9
};
// Global variables
int first_number = 0;
int second_number = 0;
int operation = 0; // 0: None, 1: Add, 2: Subtract
int display_value = 0;
int result_ready = 0;
// Function prototypes
void SystemClock_Config(void);
void GPIO_Init(void);
void displayNumber(int number);
int readButton(void);
int main(void) {
HAL_Init();
SystemClock_Config();
GPIO_Init();
while (1) {
int button = readButton();
if (button != -1) {
if (button >= 0 && button <= 9) { // Number buttons
if (result_ready == 0 && operation == 0) {
first_number = button;
} else if (result_ready == 0 && operation > 0) {
second_number = button;
}
display_value = button;
} else if (button == 10) { // Add (+)
operation = 1;
} else if (button == 11) { // Subtract (-)
operation = 2;
} else if (button == 12) { // Enter
if (operation == 1) {
display_value = first_number + second_number;
if (display_value > 9) display_value = 9; // Clamp to single digit
} else if (operation == 2) {
display_value = first_number - second_number;
if (display_value < 0) display_value = 0; // Clamp to zero
}
result_ready = 1;
}
// Update the 7-segment display
displayNumber(display_value);
}
}
}
// Function to initialize GPIO
void GPIO_Init(void) {
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
// Initialize 7-segment GPIO pins
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = SEG_A_PIN | SEG_B_PIN | SEG_C_PIN | SEG_D_PIN | SEG_E_PIN | SEG_F_PIN | SEG_G_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(SEG_PORT, &GPIO_InitStruct);
// Initialize button GPIO pins
GPIO_InitStruct.Pin = BTN_0_PIN | BTN_1_PIN | BTN_2_PIN | BTN_3_PIN | BTN_4_PIN |
BTN_5_PIN | BTN_6_PIN | BTN_7_PIN | BTN_8_PIN | BTN_9_PIN |
BTN_ADD_PIN | BTN_SUB_PIN | BTN_ENTER_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(BTN_PORT, &GPIO_InitStruct);
}
// Function to display a number on the 7-segment display
void displayNumber(int number) {
if (number < 0 || number > 9) {
// Turn off all segments for invalid numbers
HAL_GPIO_WritePin(SEG_PORT, SEG_A_PIN | SEG_B_PIN | SEG_C_PIN | SEG_D_PIN |
SEG_E_PIN | SEG_F_PIN | SEG_G_PIN, GPIO_PIN_SET);
return;
}
uint8_t segments = seven_seg_digits[number];
HAL_GPIO_WritePin(SEG_PORT, SEG_A_PIN, !(segments & 0x01));
HAL_GPIO_WritePin(SEG_PORT, SEG_B_PIN, !(segments & 0x02));
HAL_GPIO_WritePin(SEG_PORT, SEG_C_PIN, !(segments & 0x04));
HAL_GPIO_WritePin(SEG_PORT, SEG_D_PIN, !(segments & 0x08));
HAL_GPIO_WritePin(SEG_PORT, SEG_E_PIN, !(segments & 0x10));
HAL_GPIO_WritePin(SEG_PORT, SEG_F_PIN, !(segments & 0x20));
HAL_GPIO_WritePin(SEG_PORT, SEG_G_PIN, !(segments & 0x40));
}
// Function to read which button is pressed
int readButton(void) {
for (int i = 0; i < 13; i++) {
if (HAL_GPIO_ReadPin(BTN_PORT, (1 << i)) == GPIO_PIN_RESET) {
HAL_Delay(200); // Debounce delay
return i;
}
}
return -1; // No button pressed
}
// Function to configure system clock
void SystemClock_Config(void) {
// Default system clock configuration generated by STM32CubeMX
// Add your clock setup here if needed
}
Loading
st-nucleo-l031k6
st-nucleo-l031k6
ERC Warnings
gnd1:GND: Short circuit