7650 views|7 replies

466

Posts

0

Resources
The OP
 

STM32 simple and easy accurate delay (only one timer is needed) [Copy link]

This post was last edited by weizhongc on 2017-11-7 17:19 When I was testing an analog i2c, I was annoyed that the tick timer was used and there was no precise delay of us. I wrote one on impulse, which is very simple and easy to use. The system clock is 72M. The routine is as follows
  1. void delay_init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //Clock enable //Timer TIM4 initialization TIM_TimeBaseStructure.TIM_Period = 40000; //Set the value of the auto-reload register period to load the activity at the next update event TIM_TimeBaseStructure.TIM_Prescaler = 0; //Set the prescaler value used as the divisor of the TIMx clock frequency TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Set the clock division: TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM up counting mode TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //Initialize the time base unit of TIMx according to the specified parameters } //Maximum delay 65536/72 = 910 us void delay_us(uint32 nTime) { /* Clear the counter and enable the timer*/ nTime*=72; TIM4->CNT = 0; TIM_Cmd(TIM4,ENABLE); while( (TIM4->CNT) < nTime); TIM_ClearFlag(TIM4, TIM_FLAG_Update); TIM_Cmd(TIM4,DISABLE); } void delay_ms(u16 ms) { while(ms--) { delay_us(500); delay_us(500); } }
复制代码


This post is from stm32/stm8

Latest reply

I just started learning STM32 from scratch, it looks very good. Thank you.  Details Published on 2017-11-22 22:07
 

3414

Posts

0

Resources
2
 
It is a rogue to delay the timer without giving the clock configuration
This post is from stm32/stm8

Comments

Yes, 0 frequency division  Details Published on 2017-11-7 17:17
 
Personal signature

So TM what......?

 

39

Posts

1

Resources
3
 
What frequency does the system clock use?
This post is from stm32/stm8

Comments

I'm using 72M.  Details Published on 2017-11-7 17:18
 
 

466

Posts

0

Resources
4
 
ljj3166 posted on 2017-11-7 15:24 It is a rogue to use a timer delay without giving the clock configuration
Yes, 0 frequency division
This post is from stm32/stm8
 
 
 

466

Posts

0

Resources
5
 
Network loner posted on 2017-11-7 16:24 What frequency does the system clock use?
It uses 72M
This post is from stm32/stm8
 
 
 

466

Posts

0

Resources
6
 
In fact, there are many things in the program that affect the delay, so it cannot be said to be an accurate delay.
This post is from stm32/stm8
 
 
 

2

Posts

1

Resources
7
 
I just started learning STM32 from scratch, it looks very good. Thank you.
This post is from stm32/stm8
 
 
 

466

Posts

0

Resources
8
 
Happy holidays to the host
This post is from stm32/stm8
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list