using PWM function in STM32F302R8 nucleo board. [ SOLVED] I can't start PWM function . most of my code has been made by using Cube. I see nothing in ouput PC2 and PF0 for TIM1 chanel 3 and 3N.
I am French.
There is now more than one week that I try, see youtube and some comments on forum. I need your experience for solving the initialisation of a "simple" PWM.
Thanks in advance.
Joël
here under my code. in the main I have launch : PWM1_CH3N_setup();
void PWM1_CH3N_setup(void)
{
// STM32F302R8
// Clock Frequency = SystemCoreClock = 64 000 000 Hz
// Period = 38000 - 1 = 37999
// Prescaler = (SystemCoreClock/38000) - 1
// Pulse = 190000;
// ClockDivision = 0
// Counter direction = Up
// for try I use 1hz
/* Set TIM1 clock source as internal clock */
LL_RCC_SetTIMClockSource(LL_RCC_TIM1_CLKSOURCE_PLL);
/* TIM1 clock enable */
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
/* GPIO config */
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOF);
GPIO_InitStruct.Pin = PF0_PWM38khz_Pin ;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF6_TIM1; // proposed by CUBE
HAL_GPIO_Init(PF0_PWM38khz_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = PC2_PWM38khz_Pin ;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM1; // proposed by CUBE
HAL_GPIO_Init(PC2_PWM38khz_GPIO_Port, &GPIO_InitStruct);
/* TIM1 init function */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 64000;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 1000;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
/* Initialization Error */
error_data = 100 ;
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&TimHandle, &sClockSourceConfig) != HAL_OK)
{
/* Initialization Error */
error_data = 101 ;
Error_Handler();
}
if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
error_data = 102 ;
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 500;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
{
/* Initialization Error */
error_data = 103 ;
Error_Handler();
}
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* Initialization Error */
error_data = 104 ;
Error_Handler();
}
}
