#include "stm32c0xx_hal.h"
// Define pin numbers for start_stop, increment, and decrement buttons
uint8_t startStopState;
uint8_t incrementState;
uint8_t decrementState;
void SystemClock_Config(void);
void displayCounter(int counter);
int main(void) {
HAL_Init();
SystemClock_Config();
// Initialize GPIO structures
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Enable GPIO clock for Port A
__HAL_RCC_GPIOA_CLK_ENABLE();
// Enable GPIO clock for Port B
__HAL_RCC_GPIOB_CLK_ENABLE();
// Enable GPIO clock for Port D
__HAL_RCC_GPIOD_CLK_ENABLE();
// Configure pins PA0 - PA7 as output
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// Configure pins PB0 - PB7 as output
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// Configure pins PD3, PB8, and PB9 as input with pull-up
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// Initial display
displayCounter();
// Initialize variables
_Bool counting = 0;
_Bool countingDirection = 1; // true for counting up, false for counting down
int counterValue = 0;
int decrementButtonPrevState = GPIO_PIN_RESET;
int incrementButtonPrevState = GPIO_PIN_RESET;
while (1) {
int startStopState = HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_3);
int incrementState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8);
int decrementState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9);
// Toggle counting state when the start/stop button is pressed
if (startStopState == GPIO_PIN_SET) {
counting = !counting; // Toggle counting state
HAL_Delay(250); // Debounce delay
}
if (counting) {
// Change counting direction when the decrement button is pressed
if (decrementState == GPIO_PIN_SET && decrementButtonPrevState == GPIO_PIN_RESET) {
countingDirection = 0; // Toggle counting direction
HAL_Delay(250); // Debounce delay
}
// Change direction to count up if previously counting down
if (incrementState == GPIO_PIN_SET && incrementButtonPrevState == GPIO_PIN_RESET && !countingDirection) {
countingDirection = 1; // Change direction to count up
HAL_Delay(250); // Debounce delay
}
// Increment or decrement counter based on counting direction
counterValue = (counterValue + (countingDirection ? 1 : -1) + 10000) % 10000;
// displayCounter(); // Update display (implementation dependent)
HAL_Delay(250); // Debounce delay
} else {
// Increment or decrement counter by 1 when paused
if (incrementState == GPIO_PIN_SET) {
counterValue = (counterValue + 1) % 10000; // Increment counter
} else if (decrementState == GPIO_PIN_SET) {
counterValue = (counterValue - 1 + 10000) % 10000; // Decrement counter
}
// Update display if either increment or decrement button is pressed
if (incrementState == GPIO_PIN_SET || decrementState == GPIO_PIN_SET) {
// displayCounter(); // Update display (implementation dependent)
HAL_Delay(250); // Debounce delay
}
}
decrementButtonPrevState = decrementState;
incrementButtonPrevState = incrementState;
}
}
// display counter
void displayCounter(int counter) {
// get each digit in different place
int ones = counter%10;
int tens = (counter/10)%10;
int hundreds = (counter/100)%10;
int thousands = counter/1000;
// decode ones digit
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, (ones & 0x01) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, (ones & 0x02) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, (ones & 0x04) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, (ones & 0x08) ? GPIO_PIN_SET : GPIO_PIN_RESET);
// decode tens digit
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, (tens & 0x01) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, (tens & 0x02) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, (tens & 0x04) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, (tens & 0x08) ? GPIO_PIN_SET : GPIO_PIN_RESET);
// decode hundreds digit
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, (hundreds & 0x01) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, (hundreds & 0x02) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, (hundreds & 0x04) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, (hundreds & 0x08) ? GPIO_PIN_SET : GPIO_PIN_RESET);
// decode tens digit
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, (thousands & 0x01) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, (thousands & 0x02) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, (thousands & 0x04) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, (thousands & 0x08) ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
// System Clock Configuration
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();
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6