#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stm32c0xx_hal.h>
// LCD control pins
#define LCD_RS_PIN GPIO_PIN_0
#define LCD_RS_PORT GPIOA
#define LCD_EN_PIN GPIO_PIN_1
#define LCD_EN_PORT GPIOA
#define LCD_D4_PIN GPIO_PIN_2
#define LCD_D4_PORT GPIOA
#define LCD_D5_PIN GPIO_PIN_3
#define LCD_D5_PORT GPIOA
#define LCD_D6_PIN GPIO_PIN_4
#define LCD_D6_PORT GPIOA
#define LCD_D7_PIN GPIO_PIN_5
#define LCD_D7_PORT GPIOA
// Push button pins
#define BUTTON_INC_PIN GPIO_PIN_0 // Replace with the actual pin connected to the increment button.
#define BUTTON_INC_PORT GPIOB
#define BUTTON_DEC_PIN GPIO_PIN_1 // Replace with the actual pin connected to the decrement button.
#define BUTTON_DEC_PORT GPIOB
// LCD commands
#define LCD_CLEAR_DISPLAY 0x01
#define LCD_RETURN_HOME 0x02
#define LCD_FUNCTION_SET 0x28 // 4-bit mode, 2 lines, 5x8 font
#define LCD_DISPLAY_ON_OFF_CONTROL 0x0C // Display on, cursor off, blink off
#define LCD_ENTRY_MODE_SET 0x06 // Increment cursor position, no display shift
// Counter variable
volatile uint32_t counter = 0;
// Function to initialize the LCD (similar to previous examples)
void LCD_Init(void);
// Function to send a character to the LCD (similar to previous examples)
void LCD_SendChar(char data);
// Function to display a string on the LCD (similar to previous examples)
void LCD_DisplayString(char* str);
// Function to handle button presses
void Button_Handler(void);
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_USART2_UART_Init(void);
void initGPIO()
{
// Initialize GPIO pins for push buttons (Input with pull-up).
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = BUTTON_INC_PIN | BUTTON_DEC_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(BUTTON_INC_PORT, &GPIO_InitStruct);
__HAL_RCC_GPIOA_CLK_ENABLE();
}
int main(void)
{
HAL_Init();
SystemClock_Config();
LCD_Init();
initGPIO();
MX_USART2_UART_Init();
uint8_t received;
while (1)
{
Button_Handler();
}
return 0;
}
void Button_Handler(void)
{
if (HAL_GPIO_ReadPin(BUTTON_INC_PORT, BUTTON_INC_PIN) == GPIO_PIN_RESET)
{
// Increment the counter when the increment button is pressed.
counter++;
HAL_Delay(200); // Debounce delay (adjust as needed).
}
if (HAL_GPIO_ReadPin(BUTTON_DEC_PORT, BUTTON_DEC_PIN) == GPIO_PIN_RESET)
{
// Decrement the counter when the decrement button is pressed.
if (counter > 0)
{
counter--;
}
HAL_Delay(200); // Debounce delay (adjust as needed).
}
// Display the updated counter value on the LCD.
char display_str[16]; // Assuming a 16x2 LCD.
snprintf(display_str, sizeof(display_str), "Counter: %lu", counter);
LCD_DisplayString(display_str);
}
/**
@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 USART2 Initialization Function
@param None
@retval None
*/
static void MX_USART2_UART_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
// PA2 ------> USART2_TX
// PA3 ------> USART2_RX
GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
/* User can add his own implementation to report the HAL error return state */
}
// The following makes printf() write to USART2:
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
int _write(int file, uint8_t *ptr, int len)
{
switch (file)
{
case STDOUT_FILENO:
HAL_UART_Transmit(&huart2, ptr, len, HAL_MAX_DELAY);
break;
case STDERR_FILENO:
HAL_UART_Transmit(&huart2, ptr, len, HAL_MAX_DELAY);
break;
default:
return -1;
}
return len;
}
void LCD_SendCommand(uint8_t command)
{
// Set RS (Register Select) pin low to send a command.
HAL_GPIO_WritePin(LCD_RS_PORT, LCD_RS_PIN, GPIO_PIN_RESET);
// Send the high 4 bits of the command.
HAL_GPIO_WritePin(LCD_D4_PORT, LCD_D4_PIN, (command >> 4) & 0x01);
HAL_GPIO_WritePin(LCD_D5_PORT, LCD_D5_PIN, (command >> 5) & 0x01);
HAL_GPIO_WritePin(LCD_D6_PORT, LCD_D6_PIN, (command >> 6) & 0x01);
HAL_GPIO_WritePin(LCD_D7_PORT, LCD_D7_PIN, (command >> 7) & 0x01);
// Toggle the EN (Enable) pin to send the command.
HAL_GPIO_WritePin(LCD_EN_PORT, LCD_EN_PIN, GPIO_PIN_SET);
HAL_Delay(1); // Adjust the delay as needed.
HAL_GPIO_WritePin(LCD_EN_PORT, LCD_EN_PIN, GPIO_PIN_RESET);
// Send the low 4 bits of the command.
HAL_GPIO_WritePin(LCD_D4_PORT, LCD_D4_PIN, (command >> 0) & 0x01);
HAL_GPIO_WritePin(LCD_D5_PORT, LCD_D5_PIN, (command >> 1) & 0x01);
HAL_GPIO_WritePin(LCD_D6_PORT, LCD_D6_PIN, (command >> 2) & 0x01);
HAL_GPIO_WritePin(LCD_D7_PORT, LCD_D7_PIN, (command >> 3) & 0x01);
// Toggle the EN (Enable) pin to send the command.
HAL_GPIO_WritePin(LCD_EN_PORT, LCD_EN_PIN, GPIO_PIN_SET);
HAL_Delay(1); // Adjust the delay as needed.
HAL_GPIO_WritePin(LCD_EN_PORT, LCD_EN_PIN, GPIO_PIN_RESET);
// Add a longer delay after sending certain commands (e.g., LCD_CLEAR_DISPLAY).
if (command == LCD_CLEAR_DISPLAY || command == LCD_RETURN_HOME)
{
HAL_Delay(2); // Adjust the delay as needed.
}
}
void LCD_SendChar(char data)
{
// Set RS (Register Select) pin high to send character data.
HAL_GPIO_WritePin(LCD_RS_PORT, LCD_RS_PIN, GPIO_PIN_SET);
// Send the high 4 bits of the character.
HAL_GPIO_WritePin(LCD_D4_PORT, LCD_D4_PIN, (data >> 4) & 0x01);
HAL_GPIO_WritePin(LCD_D5_PORT, LCD_D5_PIN, (data >> 5) & 0x01);
HAL_GPIO_WritePin(LCD_D6_PORT, LCD_D6_PIN, (data >> 6) & 0x01);
HAL_GPIO_WritePin(LCD_D7_PORT, LCD_D7_PIN, (data >> 7) & 0x01);
// Toggle the EN (Enable) pin to send the character.
HAL_GPIO_WritePin(LCD_EN_PORT, LCD_EN_PIN, GPIO_PIN_SET);
HAL_Delay(1); // Adjust the delay as needed.
HAL_GPIO_WritePin(LCD_EN_PORT, LCD_EN_PIN, GPIO_PIN_RESET);
// Send the low 4 bits of the character.
HAL_GPIO_WritePin(LCD_D4_PORT, LCD_D4_PIN, (data >> 0) & 0x01);
HAL_GPIO_WritePin(LCD_D5_PORT, LCD_D5_PIN, (data >> 1) & 0x01);
HAL_GPIO_WritePin(LCD_D6_PORT, LCD_D6_PIN, (data >> 2) & 0x01);
HAL_GPIO_WritePin(LCD_D7_PORT, LCD_D7_PIN, (data >> 3) & 0x01);
// Toggle the EN (Enable) pin to send the character.
HAL_GPIO_WritePin(LCD_EN_PORT, LCD_EN_PIN, GPIO_PIN_SET);
HAL_Delay(1); // Adjust the delay as needed.
HAL_GPIO_WritePin(LCD_EN_PORT, LCD_EN_PIN, GPIO_PIN_RESET);
}
void LCD_DisplayString(char* str)
{
// Set RS (Register Select) pin high to send character data.
HAL_GPIO_WritePin(LCD_RS_PORT, LCD_RS_PIN, GPIO_PIN_SET);
// Iterate through the characters in the string and send them to the LCD.
for (uint8_t i = 0; i < strlen(str); i++)
{
LCD_SendChar(str[i]);
}
}