Home » STM32

Category: STM32

Contrôle d’une led avec un µC STM32

Exemple de contrôle d’une led sur le port PE8 avec un microcontrôleur STM32F407 :

void led_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;

	  // configuration en sortie
	  GPIO_InitStruct.Pin = GPIO_PIN_8;
	  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	  GPIO_InitStruct.Pull = GPIO_NOPULL;
	  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

}

void led_control(GPIO_PinState state_led)
{
	HAL_GPIO_WritePin(GPIOE, GPIO_PIN_8, state_led);
}

void led_toggle(void)
{
  HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_8);
}

 

STM32 – PWM complementary output exemple

GPIO_InitTypeDef GPIO_RGB_InitStruct;
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;

__HAL_RCC_GPIOB_CLK_ENABLE();

// PB7 - TIMER17
 GPIO_RGB_InitStruct.Pin = GPIO_PIN_7;
 GPIO_RGB_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_RGB_InitStruct.Pull = GPIO_NOPULL;
 GPIO_RGB_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 GPIO_RGB_InitStruct.Alternate = GPIO_AF2_TIM17;
 HAL_GPIO_Init(GPIOB, &GPIO_RGB_InitStruct);


 __HAL_RCC_TIM17_CLK_ENABLE();

// Init TIMER17 for 800KHz frequency
 // 48MHz/(19+1)/(2+1) = 800Khz
 TIMER_RGB_InitStruct.Instance = TIM17;
 TIMER_RGB_InitStruct.Init.Prescaler = 0;
 TIMER_RGB_InitStruct.Init.CounterMode = TIM_COUNTERMODE_UP;
 TIMER_RGB_InitStruct.Init.Period = 59;

TIMER_RGB_InitStruct.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

HAL_TIM_Base_Init(&TIMER_RGB_InitStruct);

HAL_TIM_PWM_Init(&TIMER_RGB_InitStruct);

// Output Compare Configuration
 TIMER_RGB_OC_InitStruct.OCMode = TIM_OCMODE_PWM1;
 TIMER_RGB_OC_InitStruct.OCIdleState = TIM_OCIDLESTATE_RESET;
 TIMER_RGB_OC_InitStruct.OCNIdleState = TIM_OCNIDLESTATE_RESET;// ATTENTION ! TIM_OCNIDLESTATE_RESET
 TIMER_RGB_OC_InitStruct.Pulse = 1;// PWM 0 %
 TIMER_RGB_OC_InitStruct.OCPolarity = TIM_OCPOLARITY_HIGH;
 TIMER_RGB_OC_InitStruct.OCNPolarity = TIM_OCNPOLARITY_HIGH;// ATTENTION ! TIM_OCNPOLARITY_HIGH
 TIMER_RGB_OC_InitStruct.OCFastMode = TIM_OCFAST_DISABLE;

HAL_TIM_PWM_ConfigChannel(&TIMER_RGB_InitStruct, &TIMER_RGB_OC_InitStruct, TIM_CHANNEL_1);

HAL_TIMEx_PWMN_Start(&TIMER_RGB_InitStruct,TIM_CHANNEL_1);// ATTENTION HAL_TIMEx_PWMN_Start