#include "stm32c0xx_hal.h"
#define LED_VERTE_PIN GPIO_PIN_1
#define LED_ROUGE_PIN GPIO_PIN_11
#define LED_ORANGE_PIN GPIO_PIN_4
#define LED_BLEUE_PIN GPIO_PIN_12
// Déclaration de la broche pour le bouton
#define BOUTON_PIN GPIO_PIN_0
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
//ACTIVER PORT A
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = LED_ROUGE_PIN | LED_ORANGE_PIN | LED_BLEUE_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
//ACTIVER PORT B
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = LED_VERTE_PIN ;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_1 ;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_PinState prevButtonState = GPIO_PIN_SET;
while (1)
{
// Read the current button state
GPIO_PinState currentButtonState = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
// Check if the button state has changed
if (currentButtonState != prevButtonState)
{
// Read the button state again
currentButtonState = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
// Update the previous button state
prevButtonState = currentButtonState;
// Check if the button is pressed (active LOW)
if (currentButtonState == GPIO_PIN_RESET)
{
gestionBouton();
}
}
}
}
void gestionBouton(void)
{
GPIO_PinState boutonState = HAL_GPIO_ReadPin(GPIOA, BOUTON_PIN);
if (boutonState == GPIO_PIN_SET)
{
allumerLEDVerte(boutonState);
allumerLEDRouge(boutonState);
allumerLEDOrange(boutonState);
allumerLEDBleue(boutonState);
}
}
void allumerLEDVerte(GPIO_PinState state)
{
HAL_GPIO_WritePin(GPIOB, LED_VERTE_PIN, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOB, LED_VERTE_PIN, GPIO_PIN_RESET);
}
void allumerLEDRouge(GPIO_PinState state)
{
HAL_GPIO_WritePin(GPIOA, LED_ROUGE_PIN, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOA, LED_ROUGE_PIN, GPIO_PIN_RESET);
}
void allumerLEDOrange(GPIO_PinState state)
{
HAL_GPIO_WritePin(GPIOA, LED_ORANGE_PIN, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOA, LED_ORANGE_PIN, GPIO_PIN_RESET);
}
void allumerLEDBleue(GPIO_PinState state)
{
HAL_GPIO_WritePin(GPIOA, LED_BLEUE_PIN, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOA, LED_BLEUE_PIN, GPIO_PIN_RESET);
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
// Correction : Supprimer la ligne RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_PCLK1;
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();
}
}
void Error_Handler(void)
{
// Gérer l'erreur de manière appropriée, par exemple, clignoter une LED d'erreur ou signaler l'erreur via une interface de débogage.
while (1)
{
// Boucle infinie pour maintenir le microcontrôleur dans un état d'erreur
}
}