#include "stm32c0xx_hal.h"
void SystemClock_Config(void);
// Q1 : Déclaration de la fonction
void controlerLEDs(GPIO_PinState etat);
// Q2 : Déclaration des ports GPIO
GPIO_TypeDef *GPIO_PORT_LED = GPIOA;
GPIO_TypeDef *GPIO_PORT_BP = GPIOB;
int main(void)
{
HAL_Init();
SystemClock_Config();
GPIO_InitTypeDef GPIO_InitStruct;
// Q3 : Activation des horloges
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
// Initialisation des LEDs en sortie
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_4 |
GPIO_PIN_5 | GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIO_PORT_LED, &GPIO_InitStruct);
// Initialisation du bouton PB3 en entrée Pull-up
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIO_PORT_BP, &GPIO_InitStruct);
// Q4 : Boucle principale
while (1)
{
if (HAL_GPIO_ReadPin(GPIO_PORT_BP, GPIO_PIN_3) == GPIO_PIN_RESET)
{
// Q5 : Bouton appuyé -> Allumer les LEDs
controlerLEDs(GPIO_PIN_SET);
}
else
{
// Q6 : Bouton relâché -> Éteindre les LEDs
controlerLEDs(GPIO_PIN_RESET);
}
}
}
// Q5 et Q6 : Contrôle simultané des LEDs
void controlerLEDs(GPIO_PinState etat)
{
HAL_GPIO_WritePin(GPIO_PORT_LED,
GPIO_PIN_0 | GPIO_PIN_4 |
GPIO_PIN_5 | GPIO_PIN_6,
etat);
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6