timer not sending the correct number of pulses
Hi all,
I am trying to send x number of 100 ms pulses (50 ms on, 50 ms off) with a PWM in one-pulse mode. when I wired the output to a pulse-counting device, I noticed that it is picking up 10x less pulses than is programmed. I believe there is an issue with how I programmed the timer, but could not figure it out for the life of me. Any ideas on what might be causing this issue? I am using a B-L072Z-LRWAN1. here are the relevant blocks of code.
Main loop. the variable pulsesToSend is being received by another LoRa board, and that part works no issue. it is getting the correct number there:
while(HAL_TIM_OnePulse_Start_IT(&htim2, TIM_CHANNEL_1) == HAL_BUSY);
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if((pulsesToSend>0) && pulseComplete) //execute if there are pulses to send AND the PWM cycle is finished
{
__HAL_TIM_SET_COUNTER(&htim2, 0);
__HAL_TIM_ENABLE(&htim2);
pulseComplete = false;
pulsesToSend--;
pulsesSent++;
}
}the callback function:
//this function keeps track of the number of pulses sent.
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM2) pulseComplete = true;
}Timer set-up:
static void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 63;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 49999;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OnePulse_Init(&htim2, TIM_OPMODE_SINGLE) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM2;
sConfigOC.Pulse = 25000;
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
setPeriod = htim2.Init.Period;
/* USER CODE END TIM2_Init 2 */
HAL_TIM_MspPostInit(&htim2);
}
