The program generates three signals each of them with a different duty cycle value but the same period since all of them comes from channels form the timer TIM1. Notice the sConfig.Pulse
can not exceed the maximum value of TimHandle.Init.Period
.
#include "bsp.h"
TIM_HandleTypeDef TimHandle; /*TIM control structure*/
TIM_OC_InitTypeDef sConfig; /*PWM channel structure*/
int main( void )
{
HAL_Init(); /*Init library*/
/* Config TIM1 to generate a signal with a 120Hz frequency*/
TimHandle.Instance = TIM1;
TimHandle.Init.Prescaler = 100;
TimHandle.Init.Period = 1334;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_PWM_Init( &TimHandle );
/* set values to config each channel with a high polarity */
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
/* Config PWM channel with a 25% duty cycle */
sConfig.Pulse = 333;
HAL_TIM_PWM_ConfigChannel( &TimHandle, &sConfig, TIM_CHANNEL_1 );
/* Config PWM channel with a 50% duty cycle */
sConfig.Pulse = 667;
HAL_TIM_PWM_ConfigChannel( &TimHandle, &sConfig, TIM_CHANNEL_2 );
/* Config PWM channel with a 75% duty cycle */
sConfig.Pulse = 1000;
HAL_TIM_PWM_ConfigChannel( &TimHandle, &sConfig, TIM_CHANNEL_3 );
/* start all the three channels */
HAL_TIM_PWM_Start( &TimHandle, TIM_CHANNEL_1 );
HAL_TIM_PWM_Start( &TimHandle, TIM_CHANNEL_2 );
HAL_TIM_PWM_Start( &TimHandle, TIM_CHANNEL_3 );
while(1)
{
}
}
msps.c
/*function called by HAL_TIM_PWM_Init to configure the slected pins as PWM outputs*/
void HAL_TIM_PWM_MspInit( TIM_HandleTypeDef *htim )
{
GPIO_InitTypeDef GPIO_InitStruct;
__TIM1_CLK_ENABLE(); /*eneable clock in TIM1*/
__GPIOC_CLK_ENABLE(); /*eneable clock on port C*/
/*config pins C8 as TIM1_CH1, C9 as TIM1_CH2 and C10 as TIM1_CH3*/
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
HAL_GPIO_Init( GPIOC, &GPIO_InitStruct );
}