I measuring the compare output of the TIM21 timer which is being shown as the following:
I have set the compare to toggle at 0 and the reload at 9677.
The input clock is 16 Mhz.
I would have expected a uniform square wave to be produce because to my understanding of the documentation the output pin would toggle itself every time at 0 with the frequency of ~1.6 KHz.
16 Mhz / 9677 = ~1.6KHz
However this is not the case from the waveform I have captured.
Is there a gap in my understanding of a compare operation?
The below is the code I am using to configure the timer, the compare output and the GPIO pin if it helps:
static void MX_TIM21_Init(void)
{
/* USER CODE BEGIN TIM21_Init 0 */
/* USER CODE END TIM21_Init 0 */
LL_TIM_InitTypeDef TIM_InitStruct = {0};
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Peripheral clock enable */
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM21);
/* USER CODE BEGIN TIM21_Init 1 */
/* USER CODE END TIM21_Init 1 */
TIM_InitStruct.Prescaler = 0;
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_InitStruct.Autoreload = 9677-LL_TIM_IC_FILTER_FDIV1_N2;
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
LL_TIM_Init(TIM21, &TIM_InitStruct);
LL_TIM_EnableARRPreload(TIM21);
LL_TIM_SetClockSource(TIM21, LL_TIM_CLOCKSOURCE_INTERNAL);
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_TOGGLE;
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE;
TIM_OC_InitStruct.CompareValue = 0;
TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
LL_TIM_OC_Init(TIM21, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
LL_TIM_OC_DisableFast(TIM21, LL_TIM_CHANNEL_CH1);
LL_TIM_SetTriggerOutput(TIM21, LL_TIM_TRGO_RESET);
LL_TIM_EnableMasterSlaveMode(TIM21);
LL_TIM_OC_EnablePreload(TIM21, LL_TIM_CHANNEL_CH1);
/* USER CODE BEGIN TIM21_Init 2 */
LL_TIM_EnableCounter(TIM21);
/* USER CODE END TIM21_Init 2 */
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
/**TIM21 GPIO Configuration
PA2 ------> TIM21_CH1
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_2;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
I am using the LL stm32 library.
I had initially thought that the pin would toggle and it would reset to the poliarity that is set by OCPolarity, however reading the documentation and researching the operation of compare I have not found evidence that this is true. Furthermore, When setting the OCPolarity to LOW, I find that the signal just stays high and seems like there is no toggling occurring at all.
Any guidance on understanding what is going on here is much appreciated.
Thank you all.
