2019-08-29 04:24 PM
Im using F030F4P6 controller. Here's TIM16 configuration:
Here's clock configuration:
main.c:
/* USER CODE BEGIN WHILE */
while (1) {
toggleAll();
delayUs(1000);
/* USER CODE END WHILE */
...
delayUs function:
void delayUs(uint32_t delay) {
usCounter = 0;
HAL_TIM_Base_Start(&htim16);
while(usCounter<delay){
usCounter = __HAL_TIM_GET_COUNTER(&htim16);
}
HAL_TIM_Base_Stop(&htim16);
usCounter = 0;
}
Complete code at:
http://github.com/bartoszp1992/ProjectPhantom
Missing desc:
Problem:
Not working. Program is turning on all LEDs once, and stucks at delayUs() function. Doesn't know why.
Thanks for response!
2019-08-29 04:41 PM
>>Doesn't know why
Start by using the debugger, and check to see that TIM16->CNT increments.
htim16.Init.Prescaler = 1; // Needs to be MHz of TIMCLK -1 (here assumes TIMCLK = 2 MHz, is that correct?)
htim16.Init.CounterMode = TIM_COUNTERMODE_UP;
htim16.Init.Period = 23; // Needs to be maximal, ie 0xFFFF
2019-08-30 01:17 PM
Okey, thanks for response. That was stupid. But i tried other method. I just turned on interrupts from timer 16 in cube MX(NVIC tab).
And now, my interrupt looks like this:
if (htim->Instance == TIM16) {
usCounter++;
}
(of course in Period elapsed callback).
And my delayUs() looks like this:
void delayUs(uint32_t delay) {
usCounter = 0;
HAL_TIM_Base_Start_IT(&htim16);
while(usCounter<=delay);
HAL_TIM_Base_Stop_IT(&htim16);
usCounter = 0;
}
I turned off GPIOS for LED 5 and 6(CLK and DIO also:) ) and turn on debugger.
Debugger says, that program stucks at interrput and incrementing usCounter integer to 423987209837042301 or something. Why it cant stops while loop when reach e.g 10, set as delayUs argument?
2019-08-30 01:23 PM
Edit:
while i remove while, uc also stucks and only incrementing usCounter.
2019-08-30 02:47 PM
Unfortunately 1 MHz is impractically fast to interrupt, which is why the TIM->CNT method is normally used.
I would tend just to initialize the TIM once, and read the TIM->CNT start value, and then delta the count as it increments.
ie
uint16_t start = TIM16->CNT;
while(((uint16_t)TIM16->CNT - start) < delay);
2019-08-31 02:31 AM
Hi bartoszp,
If you only need a delay function that waste us CPU time, you do not need to activate timer interrupts.
Consider my example on STM32F0 with Timer 2 (32 bit) as an attached file. Leave me your comment if it could help you.
Regards
rrom
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.