2025-11-02 11:23 AM
My goal is to use timers to follow the required timing steps for a module I’m working with.
To achieve this, I decided to use two timers — TIM1 and TIM2.
TIM2 is responsible for counting the T1 time step. For this, I’m using One-Pulse Mode, so TIM2 is configured to count a 2 µs delay.
After TIM2 finishes, TIM1 should be triggered. TIM1 also operates in One-Pulse Mode, this time generating 25 pulses to imitate the PD_SCK signal (as shown in the datasheet timing diagram).
Each pulse should stay HIGH for half of the full period (20 µs), so 10 µs HIGH and 10 µs LOW.
During the generation of those 25 pulses, the DOUT pin (connected to TIM2_CH2) should be sampled. At that time, TIM2 should be disabled.
Unfortunately, it is not working, this is my code:
void start_measurement(void){
if (HAL_TIM_OnePulse_Start(&htim2, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_Base_Start(&htim1) != HAL_OK)
{
Error_Handler();
}
}
}
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim){
if (htim->Instance == TIM1) {
hx711_timer1_PWM_low_callback(active_hx711);
if (tim2_needs_rearm != 0U)
{
if (HAL_TIM_OnePulse_Start_IT(&htim2, TIM_CHANNEL_2) != HAL_OK)
Error_Handler();
}
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM2)
{
if (HAL_TIM_OnePulse_Stop_IT(&htim2, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
tim2_needs_rearm = 1U;
return;
}
if (htim->Instance == TIM1) {
if (active_hx711->measurement_count <= measurement_threshold){
hx711_update_reading(active_hx711);
active_hx711->measurement_count = (uint8_t)(active_hx711->measurement_count + 1U);
}
if (active_hx711->tx_in_progress == 0U){
pack_data(active_hx711);
if (HAL_UART_Transmit_IT(&huart1, (uint8_t *)( active_hx711->tx_buffer), HX711_TX_BUFFER_SIZE) == HAL_OK){
active_hx711->tx_in_progress = 1U;
active_hx711->measurement_count = 0U;
}
}
}
}
The result should be printed over UART, but I receive nothing.
Something seems to have broken after I added the changes proposed in this post:
:link: ST Forum: Callback function for HAL_TIM_OnePulse_Start_IT
I added the following code to both TIM1 and TIM2 initializations:
/* USER CODE BEGIN TIM2_Init 2 */
// Clear interrupt bit in SR that was set as a side effect of generating an update event in TIM_Base_SetConfig
__HAL_TIM_CLEAR_IT(&htim2, TIM_IT_UPDATE);
// enable timer update interrupt
__HAL_TIM_ENABLE_IT(&htim2,TIM_IT_UPDATE);
/* USER CODE END TIM2_Init 2 */but I just got some random signs through UART