#include <stm32c0xx_hal.h>
#include <string.h>
I2C_HandleTypeDef hi2c1;
#define LCD_ADDRESS (0x4E << 1) // Replace with your LCD's I2C address
#define PWR_REGULATOR_VOLTAGE_SCALE1
#define RCC_PLL_NONE
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
void LCD_Init(void);
void LCD_Clear(void);
void LCD_Print(uint8_t line, char* text);
void LCD_SetCursor(uint8_t row, uint8_t col);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
LCD_Init();
LCD_Clear();
LCD_SetCursor(0x80, 0);
LCD_Print(0, "Counter: 0");
int counter = 0;
while (1)
{
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET) { // Button for increment
counter++;
HAL_Delay(200); // Debounce delay
}
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET) { // Button for decrement
counter--;
HAL_Delay(200); // Debounce delay
}
// Update the LCD display with the current counter value
LCD_SetCursor(0x80, 9); // Set cursor to the second digit of the counter
char counterStr[4]; // Buffer to store the counter as a string
snprintf(counterStr, sizeof(counterStr), "%d", counter);
LCD_Print(0, counterStr);
HAL_Delay(100); // Add a small delay to avoid flickering
}
}
// System Clock Configuration
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
// I2C Initialization
static void MX_I2C1_Init(void) {
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x00707CBB; // Adjust the timing as per your I2C bus speed
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
}
// LCD Initialization
void LCD_Init(void)
{
HAL_Delay(50); // Wait for the LCD to power up
// Initialize the LCD in 4-bit mode
uint8_t init_commands[] = {0x03, 0x03, 0x03, 0x02}; // Initialize commands
HAL_I2C_Master_Transmit(&hi2c1, LCD_ADDRESS, init_commands, sizeof(init_commands), HAL_MAX_DELAY);
// uint8_t more_commands[] = {LCD_CMD_FUNCTION_SET, LCD_CMD_DISPLAY_ON_OFF, LCD_CMD_ENTRY_MODE_SET};
// HAL_I2C_Master_Transmit(&hi2c1, LCD_ADDRESS, more_commands, sizeof(more_commands), HAL_MAX_DELAY);
}
// Clear the LCD screen
void LCD_Clear(void)
{
uint8_t clear_command[] = {0x01}; // Command to clear the screen
HAL_I2C_Master_Transmit(&hi2c1, LCD_ADDRESS, clear_command, sizeof(clear_command), HAL_MAX_DELAY);
}
// Print text on the LCD at the specified line (0 or 1)
void LCD_Print(uint8_t line, char* text)
{
uint8_t address = (line == 0) ? 0x80 : 0xC0; // Line 1 or 2 address
LCD_SetCursor(address, 0); // Set the cursor position
while (*text)
{
uint8_t data[] = {*text++}; // Send one character at a time
HAL_I2C_Master_Transmit(&hi2c1, LCD_ADDRESS, data, sizeof(data), HAL_MAX_DELAY);
}
}
// Set the cursor position on the LCD (0x80 to 0x8F for line 1, 0xC0 to 0xCF for line 2)
void LCD_SetCursor(uint8_t address, uint8_t col)
{
uint8_t set_cursor_command[] = {address + col}; // Command to set the cursor position
HAL_I2C_Master_Transmit(&hi2c1, LCD_ADDRESS, set_cursor_command, sizeof(set_cursor_command), HAL_MAX_DELAY);
}
void Error_Handler(void) {
while (1) {
}
}