cancel
Showing results for 
Search instead for 
Did you mean: 

Decimate two Onepulse timers

DPatr.2
Associate III

I am using STM32F429ZIT6 - Nucleo

I have TIM2 as master in encoder mode generating TRGO on OC3REF to TIM1 & TIM8 slaves in Output compare One pulse mode.

I can generate pulses on both Channel 1 of TIM1 & TIM8 but I would like to have a ratio

n:1 of one pulse generation.

I tried doing this by setting/resetting CEN bit on CR1.like this :

#define STEP 20
#define DistToStart 100
#define FORWARD 0
#define BACKWARD 1
 
uint32_t trig_pos = 0;
volatile uint32_t i = 0;
 
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim){
 
	//TIM2->CR1 Bit 4 = DIR  // DIR = 0 Upcounting, DIR = 1 Downcounting
 
	i++;
 
	if(htim == &htim2)
 
	{
               // Update next encoder position
		trig_pos = STEP*i +DistToStart;	
 
                 //Write in TIM2->CCR3 (32 bits) Next encoder position compare value
		__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3,trig_pos); 		
                
                // Set CEN bit in TIM1->CR1 to enable TIM1 CNT
		TIM1->CR1 |= (1<<0);		
								
                // Reset CEN bit in TIM8->CR1 to disable TIM8 CNT
		TIM8->CR1 &= ~(1<<0);										
 
	}
 
	if(i%3 == 0){
                // Set CEN bit in TIM8->CR1 to enableTIM8 CNT
		TIM8->CR1 |= (1<<0);
	}
 
}

No matter what i is, I observe pulse output on oscilloscope.

When I go in debug mode, the instruction

TIM8->CR1 |= (1<<0);

doesnt show the register has been updated

Not sure what is the issue. Any Idea ?

2 REPLIES 2
TDK
Guru

Should ensure HAL_TIM_OC_DelayElapsedCallback is only being called one one timer instance, or move your variables and checks inside the if statement.

> When I go in debug mode, the instruction

> TIM8->CR1 |= (1<<0);

> doesnt show the register has been updated

If the timer is in onepulse, it will run and maybe disable itself before CEN is read.

No need to disable timers if they're in onepulse mode. They will disable themselves at the end of the pulse.

If you feel a post has answered your question, please click "Accept as Solution".

Consider "full HW" solution.

  • Route TIM2 TRGO asi trigger source for TIM8 (one pulse)
  • Route TIM2 TRGO as count source for TIM3(or another timer)
  • Set TIM3 ARR to "n" and enable TIM3 "update event" to its TRGO
  • Route TIM3 TRGO as trigger source to TIM1 (one pulse)