2017-06-23 05:22 PM
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 #timSolved! Go to Solution.
2017-06-25 06:51 PM
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.
2017-06-24 09:07 PM
You don't actually mention what your problem is.
2017-06-25 12:52 AM
I concluded that you want to delay at 1us granularity. There are many ideas shown within this forum:
2017-06-25 02:24 PM
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?
MT2017-06-25 06:51 PM
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.
2017-06-25 08:14 PM
What do you mean by
Clock faster for less jitter
2017-06-25 11:44 PM
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.