cancel
Showing results for 
Search instead for 
Did you mean: 

One Pulse Mode not working

MichalPorazko
Associate III

My goal is to use timers to follow the required timing steps for a module I’m working with.

 

MichalPorazko_0-1762109266115.png

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.

MichalPorazko_2-1762110115913.png

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).

MichalPorazko_3-1762110191312.png

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

 

1 REPLY 1
waclawek.jan
Super User

In Cube/HAL, what is called One-Pulse mode, is *NOT* just the One-Pulse mode of the timer as set by TIMx_

Instead, it implements a more complex functionality, where the target timer is to be started through its slave-mode controller set to Trigger mode. So, the HAL_TIM_OnePulse_Start() function does *NOT* start the timer and does *NOT* generate one pulse (or TIMx_RCR pulses in case of Advanced timer).

Cube is open source and you can read the source of its functions and try to understand their functionality yourself. OTOH, you can also write your own code, by directly accessing the registers, in that case the only documentation you need to read is the Reference Manual.

JW