I have a STM32F051R8 (STM32F0 discovery board) I am trying to use to generate a PWM using a timer and change the pulse width at runtime. My code is:
// configuration
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 65535;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
// runtime
if(HAL_GPIO_ReadPin (GPIOA, B1_Pin)) {
// Set The LED ON!
HAL_GPIO_WritePin(GPIOC, LD4_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, LD3_Pin, GPIO_PIN_RESET);
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 750);
htim1.Instance->CCR1 = 65534/2;
} else {
// Else .. Turn LED OFF!
HAL_GPIO_WritePin(GPIOC, LD3_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, LD4_Pin, GPIO_PIN_RESET);
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 500);
htim1.Instance->CCR1 = 65534/4;
}
My problem is the signal looks like this:
Why are the waves like that? I thought they were supposed to be square. Am I doing something wrong, or is there some kind of setting I must apply?