cancel
Showing results for 
Search instead for 
Did you mean: 

Microsecond timer using Tim3

Matthieu Tanguay
Associate II
Posted on June 24, 2017 at 02:22

Hello,

I think variation of this question has been asked a couple of time, but i'm trying to understand why what I am writing is not working.

Because I want to get some experience with the HAL librairies I got myself a NucleoF446RE and an accessory shieldwith a P9813 LED Driver.

I also found this code that I am modifying to use HAL:

https://codebender.cc/library/ChainableLED#ChainableLED.cpp

.

I think my problems come from my micro seconds delay function. I used the Cube to generate the base code but here is what pertain to Timer3 :

MX_TIM3_Init();
 HAL_TIM_Base_Start(&htim3);
static void MX_TIM3_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
 TIM_MasterConfigTypeDef sMasterConfig;
htim3.Instance = TIM3;
 htim3.Init.Prescaler = 84;
 htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim3.Init.Period = 0xffff;//Full 16bit
 htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
 {
 Error_Handler();
 }
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
 if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
 {
 Error_Handler();
 }
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
 {
 Error_Handler();
 }
}
 
//Function for micro seconds Delay
void microseconds_Delay(int Delay){
 uint32_t initTime = __HAL_TIM_GET_COUNTER(&htim3);
while((__HAL_TIM_GET_COUNTER(&htim3)-initTime) < Delay){
 }
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Thank you in advance.

MT

#nucleo-f446re #microseconds #tim
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on June 26, 2017 at 03:51

htim3.Init.Prescaler = 84 - 1; // N-1

It if is 16-bit, use a 16-bit value so it doesn't screw up the wrapping maths. Is a signed int appropriate?

//Function for micro seconds Delay
void microseconds_Delay(int Delay){
 uint16_t initTime = (uint16_t)__HAL_TIM_GET_COUNTER(&htim3);
while(((uint16_t)__HAL_TIM_GET_COUNTER(&htim3)-initTime) < Delay){
 }
}

Initialize the free running counter once, and leave it alone, it will tick at 1MHz (1us). Clock faster for less jitter, ie when you read counter immediately prior to a tick.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..

View solution in original post

6 REPLIES 6
john doe
Lead
Posted on June 25, 2017 at 06:07

You don't actually mention what your problem is.

Posted on June 25, 2017 at 09:52

I concluded that you want to delay at 1us granularity. There are many ideas shown within this forum:

https://community.st.com/0D50X00009XkeRYSAZ

Matthieu Tanguay
Associate II
Posted on June 25, 2017 at 23:24

Yes, I was trying to use a Timer to get micro seconds delay. I read plenty on doing with sysTick and register level, but wanted to know if it is possible to get the same results with a timer.

One more question spawned from this discussion. When I initialize my timer i do : TIM_HandleTypeDef htim3; is it a bad thing to call this in multiple files? I have it once in the main file generated by the Cube, but I use my timer in an other instance and redefine it. Could that be the issue that I am seeing?

MT
Posted on June 26, 2017 at 03:51

htim3.Init.Prescaler = 84 - 1; // N-1

It if is 16-bit, use a 16-bit value so it doesn't screw up the wrapping maths. Is a signed int appropriate?

//Function for micro seconds Delay
void microseconds_Delay(int Delay){
 uint16_t initTime = (uint16_t)__HAL_TIM_GET_COUNTER(&htim3);
while(((uint16_t)__HAL_TIM_GET_COUNTER(&htim3)-initTime) < Delay){
 }
}

Initialize the free running counter once, and leave it alone, it will tick at 1MHz (1us). Clock faster for less jitter, ie when you read counter immediately prior to a tick.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Matthieu Tanguay
Associate II
Posted on June 26, 2017 at 05:14

What do you mean by 

Clock faster for less jitter

Posted on June 26, 2017 at 06:44

If you clock at 84 MHz, your a lot more likely to get to 1us  +/-12ns (84 ticks), rather than 0-1us (1 tick)

With a 1MHz clock, you could read the value the instant before it increments, thus the second read causes the loop to exit.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..