STM32CubeMX timer control microsecond delay detailed explanation

Publisher:烟雨江湖Latest update time:2018-07-19 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Written at the beginning, why is there a need for a timer for microsecond delay? 
1. In the HAL firmware library, only Systick is used as a delay counter, and the millisecond delay HAL_Delay(). In order to increase the precise microsecond delay, the Systick configuration parameters are generally changed, but the HAL firmware library uses the HAL_Delay() function in many places, so it is recommended that you do not modify the Systick parameters automatically configured by the system; 
2. Personally, I think that when the operating system is added, Systick will be occupied, and the MCU system's own time base must select other timers. In summary, the changes made to Systick are basically useless; 
therefore, the method of using a timer to control subtle delays is more flexible. The user needs to add very little code, which is economical and practical, energy-saving and environmentally friendly- -; 
Step 1. Configure the clock. 
Write the picture description here
Note that you must make sure that the crystal frequency marked in the red part is consistent with the actual crystal frequency. 
Step 2. Configure the timer  .
When configuring the timer, you need to clarify the following points: 
1. Timer clock frequency; 
2. Timer overflow frequency;

First, let's look at the more important registers, as shown below: 
Write the picture description here
The red line marks the place where we get: 
Timer operating frequency = **APBx Timer Clock/PSC register value divided by the internal clock + 1; 
for example, 
if the timer clock frequency is 84MHz, without using the internal clock division, to make the timer operating frequency 1MHz (1us), as follows: 
1MHz=84MHz/(83+1);

Timer overflow frequency = timer operating frequency / arr

Regarding when the arr reload value is loaded, the new value will be loaded only after an overflow update event occurs;

The relevant configuration is as follows: 
System clock configuration, see the figure above; 
here I use the TIM14 general timer for 1us delay, the configuration is as follows: 
Write the picture description here

Step 3. Write the code


void delay_us(uint16_t us)

{

    uint16_t differ = 0xffff-us-5;

    /*To prevent interrupts from interrupting the delay and causing counting errors.

     For example, the delay is 1us starting from 0xfffE, but it is interrupted by an interrupt

    (The counter is still counting at this time), the reason is that the count reaches 0xffff)

    The count stops, but because the count value is missed and the arr value is overloaded,

    The actual delay is (0xffff+1)us

    */


    HAL_TIM_Base_Start(&htim14);


    __HAL_TIM_SetCounter(&htim14,differ);


    while(differ<0xffff-5)

    {

        differ=__HAL_TIM_GetCounter(&htim14);

    }


    HAL_TIM_Base_Stop(&htim14);

}

 

Because the upward counting method is used, it needs to be converted, but the downward counting method does not need it; 

The test code is as follows:


 /* USER CODE BEGIN WHILE */

 while (1)

 {

 /* USER CODE END WHILE */


 /* USER CODE BEGIN 3 */

HAL_GPIO_TogglePin(LED0_GPIO_Port,LED0_Pin);

delay_us(5);

 }

 /* USER CODE END 3 */

The actual waveform is as follows: 
Write the picture description here

Keywords:STM32 Reference address:STM32CubeMX timer control microsecond delay detailed explanation

Previous article:STM32 - System tick timer
Next article:How to generate microsecond (us) delay in STM32 CubeMX

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号