#include "stm32c0xx_hal.h"
void SystemClock_Config(void);
// Q1 : Déclaration des fonctions
void allumerLEDsGaucheADroite(void);
void eteindreLEDsDroiteAGauche(void);
void allumerLEDsDrioteagauche(void);
void eteindreLEDgaucheadroite(void);
void controlerLEDs(uint16_t pin, GPIO_PinState etat);
// Q2 : Déclaration des ports
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
int etat = 0;
int sens = 0;
while (1)
{
if (HAL_GPIO_ReadPin(GPIO_PORT_BP, GPIO_PIN_3) == 0)
{
if (etat == 0)
{
if (sens == 0)
{
allumerLEDsGaucheADroite();
}
else
{
allumerLEDsDrioteagauche();
}
etat = 1;
}
}
else
{
if (etat == 1)
{
if (sens == 0)
{
eteindreLEDsDroiteAGauche();
}
else
{
eteindreLEDgaucheadroite();
}
etat = 0;
sens = !sens;
}
}
}
}
// Allumer gauche vers droite
void allumerLEDsGaucheADroite(void)
{
controlerLEDs(GPIO_PIN_0, GPIO_PIN_SET);
HAL_Delay(300);
controlerLEDs(GPIO_PIN_4, GPIO_PIN_SET);
HAL_Delay(300);
controlerLEDs(GPIO_PIN_6, GPIO_PIN_SET);
HAL_Delay(300);
controlerLEDs(GPIO_PIN_5, GPIO_PIN_SET);
HAL_Delay(300);
}
// Allumer droite vers gauche
void allumerLEDsDrioteagauche(void)
{
controlerLEDs(GPIO_PIN_5, GPIO_PIN_SET);
HAL_Delay(300);
controlerLEDs(GPIO_PIN_6, GPIO_PIN_SET);
HAL_Delay(300);
controlerLEDs(GPIO_PIN_4, GPIO_PIN_SET);
HAL_Delay(300);
controlerLEDs(GPIO_PIN_0, GPIO_PIN_SET);
HAL_Delay(300);
}
// Éteindre droite vers gauche
void eteindreLEDsDroiteAGauche(void)
{
controlerLEDs(GPIO_PIN_5, GPIO_PIN_RESET);
HAL_Delay(300);
controlerLEDs(GPIO_PIN_6, GPIO_PIN_RESET);
HAL_Delay(300);
controlerLEDs(GPIO_PIN_4, GPIO_PIN_RESET);
HAL_Delay(300);
controlerLEDs(GPIO_PIN_0, GPIO_PIN_RESET);
HAL_Delay(300);
}
// Éteindre gauche vers droite
void eteindreLEDgaucheadroite(void)
{
controlerLEDs(GPIO_PIN_0, GPIO_PIN_RESET);
HAL_Delay(300);
controlerLEDs(GPIO_PIN_4, GPIO_PIN_RESET);
HAL_Delay(300);
controlerLEDs(GPIO_PIN_6, GPIO_PIN_RESET);
HAL_Delay(300);
controlerLEDs(GPIO_PIN_5, GPIO_PIN_RESET);
HAL_Delay(300);
}
// Fonction unique pour commander une LED
void controlerLEDs(uint16_t pin, GPIO_PinState etat)
{
HAL_GPIO_WritePin(GPIO_PORT_LED, pin, etat);
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6