cancel
Showing results for 
Search instead for 
Did you mean: 

Timer3 generating 5Hz pulse ?

antonius
Senior

Dear Member,

How can I generate 5Hz pulse on OC using TIM3 ?

Here is my setting on STM32CubeMx0690X0000089KbvQAE.jpg

Thanks

11 REPLIES 11
antonius
Senior

I got PC6 with LED and it's ON, but I don't know what the frequency is,

The setting is :

TIM3->CR1 |= TIM_CR1_CEN;

      HAL_TIM_Base_Start(&htim3);

      HAL_TIM_OnePulse_Start(&htim3,1);

What do I miss here ?

thanks

antonius
Senior

Please correct me :

The code :

 htim3.Instance = TIM3;

 htim3.Init.Prescaler = 12;

 htim3.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim3.Init.Period = 10000;

 htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4;

Frequency I got : 2097KHz/(4x10000x12) = 4.3 Hz

??

S.Ma
Principal

Recap question: What are the clocks available in the chip? MHz? LSI, LSE?

If you want 5 Hz, I would simply use Systick with 1 msec ISR, create a 32 bit milisec counter in RAM as global variable, when this counter reaches 5000, reset it by SW and toggle your output manually.

When time units are so far from kHz, SW implementation maybe good enough.

htim3.Init.ClockDivision does not do what you think it does. It determines the clock rate on the input filter (if you were using the timer channel as an INPUT, which you are not). It is not another prescaler that divides the timer's input clock to the CNT register, so it has no effect on your output signal.

And is your timer clock really 2.097 MHz?

Yes it is 2.097Mhz

Is this setting equal to 5Hz ?

===

 htim3.Instance = TIM3;

 htim3.Init.Prescaler = 10;

 htim3.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim3.Init.Period = 10000;

 htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4;

2.097M / *400k

How to to reset in Software ?

ClockDivision is unrelated to this, it is used by input filters.

Both Prescaler and Period are N-1 numbers

htim3.Init.Prescaler = 2097-1; // 1 KHz

htim3.Init.Period = 200 - 1; // 5 Hz

S.Ma
Principal

Reset = write 0 on it

uint32_t msec_counter = 0;
 
void My1msecSystickISR(void) // find the corresponding function in the cubemx generated code
{
msec_counter++;
if(msec_counter>=2000) {
 // hello, 0.2 sec elapsed for 5Hz period
LED_Toggle();
msec_counter = 0;
}
}

Abit confuse, is it Prescaler = 2097-1 and Prescaler = 200-1 ?