微秒(us)延时 程序

微秒级的延时最好用systick 1 来计算。使用方法3(wait_loop_index–)时间变动会比较大。

函数 10us 100us 500us 900us
us_delay1 11.2 101.2 501.3 901.2
us_delay3 21 93 34 83

测试函数,测试函数是根据SysTick来计算的。

void us_test(char argc,char **argv)
{
	int i,type,us,start,end,delta;
	
	type = atoi(argv[2]);
	us = atoi(argv[1]);
	
	for(i=0;i<10;i++)
    {    
		start = SysTick->VAL;
		switch (type)
		{
			case 1:
				us_delay1(us);
				break;
			case 2:
				us_delay2(us);
				break;
			case 3:
				us_delay3(us);
				break;
			default:
				break;
		}
		end = SysTick->VAL;
		delta = start > end ? start - end : 79999 + start - end;
		printf("%5d us. start:%5d, now:%5d, delta:%5d \n",us,start,end,delta);
	}
}
MSH_CMD_EXPORT(us_test,xxx);
//======================delay======================
// ref: rt_hw_us_delay() at drv_common.c(rt-thread 4.01)
//note:us_tick= 80, reload = 79,999. if us>999,(delta < us_tick * us) can't happend.
void us_delay1(uint32_t us)
{
    rt_uint32_t start, now, delta, reload, us_tick;
    //这个if另加的,原来一旦超过999就会死循环
	if(us>999)
	{
		printf("arg error:us>999");
		return;
	}
    start = SysTick->VAL;
    reload = SysTick->LOAD;
    us_tick = SystemCoreClock / 1000000UL;	//80
	
    do {
        now = SysTick->VAL;
        delta = start > now ? start - now : reload + start - now;
    } while(delta < us_tick * us);
	
}

//ref:https://www.rt-thread.org/document/site/programming-manual/timer/timer/#_14 里的高精度延时
void us_delay2(uint32_t us)
{
    rt_uint32_t start;
    /* 获得延时经过的 tick 数 */
    us = us * (SysTick->LOAD/(1000000/RT_TICK_PER_SECOND));//us*80,000
    /* 获得当前时间 */
    start = SysTick->VAL;
    /* 循环获得当前时间,直到达到指定的时间后退出循环 */
    while (start - SysTick->VAL< us);
}

//ref:stm32l4xx_hal_pwr_ex.c里用于延时的语句,做了*2的调整
void us_delay3(uint32_t us)
{
	uint32_t wait_loop_index;
	wait_loop_index = us*SystemCoreClock/(1000000*2);
	
	while(wait_loop_index != 0)
	{
		wait_loop_index--;
	}
}

100us测试结果

函数 最大偏差(基本<10%) 总结 问题 出处-作者
us_delay1 10us 非常稳定 超过1,000us会进入死循环 rt_hw_us_delay() at drv_common.c
us_delay2 7,000us 有bug,有时会跳 没有判断systick reload的情况 rt-thread 官网 高精度延时例程
us_delay3 400us 稳定 有点笨 stm32l4xx_hal_pwr_ex.c 关于wait_loop_index的延时语句

方法1作者@SummerGift 感谢在2018-11-7做的更新,最好对参数做个限制,不然会进入死循环。
方法2作者@RT-Thread 官网 高精度延时例程也是有bug的。

下面测试样本


us_delay1
100 us. start:57821, now:49725, delta: 8096
100 us. start:22203, now:14107, delta: 8096
100 us. start:66631, now:58535, delta: 8096
100 us. start:31098, now:23002, delta: 8096
100 us. start:75517, now:67421, delta: 8096
100 us. start:39983, now:31887, delta: 8096
100 us. start: 4157, now:76061, delta: 8095
100 us. start:48620, now:40524, delta: 8096
100 us. start:13095, now: 4999, delta: 8096
100 us. start:56741, now:48645, delta: 8096
100 us. start:21216, now:13120, delta: 8096
100 us. start:65643, now:57547, delta: 8096
100 us. start:30017, now:21921, delta: 8096
100 us. start:74217, now:66121, delta: 8096
100 us. start:38683, now:30587, delta: 8096
100 us. start: 3087, now:74990, delta: 8096
100 us. start:47550, now:39454, delta: 8096
100 us. start:10383, now: 2287, delta: 8096
100 us. start:54037, now:45941, delta: 8096
100 us. start:18206, now:10110, delta: 8096
100 us. start:62263, now:54167, delta: 8096
100 us. start:26737, now:18641, delta: 8096
100 us. start:70687, now:62591, delta: 8096
100 us. start:34426, now:26330, delta: 8096
100 us. start:78843, now:70747, delta: 8096
100 us. start:43199, now:35103, delta: 8096
100 us. start: 7674, now:79207, delta: 8466
100 us. start:51761, now:43665, delta: 8096
100 us. start:16035, now: 7939, delta: 8096
100 us. start:59750, now:51654, delta: 8096
100 us. start:24217, now:16121, delta: 8096
100 us. start:68636, now:60540, delta: 8096
100 us. start:33102, now:25006, delta: 8096
100 us. start:77183, now:69087, delta: 8096
100 us. start:41649, now:33553, delta: 8096
100 us. start: 6124, now:78019, delta: 8104
100 us. start:50570, now:42474, delta: 8096
100 us. start:15045, now: 6949, delta: 8096
100 us. start:59010, now:50914, delta: 8096
100 us. start:23477, now:15381, delta: 8096
100 us. start:67855, now:59759, delta: 8096
100 us. start:32182, now:24086, delta: 8096
100 us. start:76603, now:68507, delta: 8096
100 us. start:41069, now:32973, delta: 8096
100 us. start: 5544, now:77437, delta: 8106
100 us. start:49990, now:41894, delta: 8096
100 us. start:14465, now: 6369, delta: 8096
100 us. start:58482, now:50386, delta: 8096
100 us. start:22956, now:14860, delta: 8096
100 us. start:67216, now:59120, delta: 8096
100 us. start:31682, now:23586, delta: 8096
100 us. start:76103, now:68007, delta: 8096
100 us. start:40569, now:32473, delta: 8096
100 us. start: 5038, now:76934, delta: 8103
100 us. start:49485, now:41389, delta: 8096
100 us. start:13960, now: 5864, delta: 8096
100 us. start:57960, now:49864, delta: 8096
100 us. start:22226, now:14130, delta: 8096
100 us. start:66646, now:58550, delta: 8096
100 us. start:31112, now:23016, delta: 8096
100 us. start:75533, now:67437, delta: 8096
100 us. start:39999, now:31903, delta: 8096
100 us. start: 3429, now:75322, delta: 8106
100 us. start:47882, now:39786, delta: 8096
100 us. start:12139, now: 4043, delta: 8096
100 us. start:56130, now:48034, delta: 8096
100 us. start:20596, now:12500, delta: 8096
100 us. start:64956, now:56860, delta: 8096
100 us. start:29212, now:21116, delta: 8096
100 us. start:73632, now:65536, delta: 8096
100 us. start:38099, now:30003, delta: 8096
100 us. start: 2568, now:74464, delta: 8103
100 us. start:47024, now:38928, delta: 8096
100 us. start:11219, now: 3123, delta: 8096
100 us. start:55190, now:47094, delta: 8096
100 us. start:19656, now:11560, delta: 8096
100 us. start:63688, now:55592, delta: 8096
100 us. start:28154, now:20058, delta: 8096
100 us. start:72583, now:64487, delta: 8096
100 us. start:37008, now:28912, delta: 8096
100 us. start: 1265, now:73159, delta: 8105
100 us. start:45714, now:37618, delta: 8096
100 us. start:10189, now: 2093, delta: 8096
100 us. start:54069, now:45973, delta: 8096
100 us. start:18206, now:10110, delta: 8096
100 us. start:61534, now:53438, delta: 8096
100 us. start:26001, now:17905, delta: 8096
100 us. start:70182, now:62086, delta: 8096
100 us. start:34648, now:26552, delta: 8096
100 us. start:78468, now:70372, delta: 8096
100 us. start:42934, now:34838, delta: 8096
100 us. start: 7409, now:79312, delta: 8096
100 us. start:51819, now:43723, delta: 8096
100 us. start:16065, now: 7969, delta: 8096
100 us. start:60036, now:51940, delta: 8096
100 us. start:24511, now:16415, delta: 8096
100 us. start:68932, now:60836, delta: 8096
100 us. start:33398, now:25302, delta: 8096
100 us. start:77829, now:69733, delta: 8096
100 us. start:42304, now:34208, delta: 8096


msh />us_test 100 2
100 us. start:75119, now:67148, delta: 7971
100 us. start:39571, now:31600, delta: 7971
100 us. start: 4122, now:79615, delta: 4506
100 us. start:52181, now:44210, delta: 7971
100 us. start:16719, now: 8748, delta: 7971
100 us. start:60982, now:53011, delta: 7971
100 us. start:25237, now:17266, delta: 7971
100 us. start:69796, now:61825, delta: 7971
100 us. start:34400, now:26429, delta: 7971
100 us. start:78923, now:70952, delta: 7971
100 us. start:43519, now:35548, delta: 7971
100 us. start: 8073, now: 102, delta: 7971
100 us. start:52365, now:44394, delta: 7971
100 us. start:16899, now: 8928, delta: 7971
100 us. start:61072, now:53101, delta: 7971
100 us. start:25667, now:17696, delta: 7971
100 us. start:69836, now:61865, delta: 7971
100 us. start:34440, now:26469, delta: 7971
100 us. start:78923, now:70952, delta: 7971
100 us. start:43519, now:35548, delta: 7971
100 us. start: 8073, now: 102, delta: 7971
100 us. start:52365, now:44394, delta: 7971
100 us. start:16899, now: 8928, delta: 7971
100 us. start:55477, now:47506, delta: 7971
100 us. start:20081, now:12110, delta: 7971
100 us. start:64636, now:56665, delta: 7971
100 us. start:29212, now:21241, delta: 7971
100 us. start:73771, now:65800, delta: 7971
100 us. start:38369, now:30398, delta: 7971
100 us. start: 2935, now:79611, delta: 3323
100 us. start:52175, now:44204, delta: 7971
100 us. start:16709, now: 8738, delta: 7971
100 us. start:60976, now:53005, delta: 7971
100 us. start:25221, now:17250, delta: 7971
100 us. start:69779, now:61808, delta: 7971
100 us. start:34383, now:26412, delta: 7971
100 us. start:78925, now:70954, delta: 7971
100 us. start:43529, now:35558, delta: 7971
100 us. start: 8083, now: 112, delta: 7971
100 us. start:52381, now:44410, delta: 7971
100 us. start:16919, now: 8948, delta: 7971
100 us. start:61082, now:53111, delta: 7971
100 us. start:25677, now:17706, delta: 7971
100 us. start:69836, now:61865, delta: 7971
100 us. start:34440, now:26469, delta: 7971
100 us. start:78923, now:70952, delta: 7971
100 us. start:40563, now:32592, delta: 7971
100 us. start: 4673, now:79613, delta: 5059
100 us. start:52185, now:44214, delta: 7971
100 us. start:16719, now: 8748, delta: 7971
100 us. start:60982, now:53011, delta: 7971
100 us. start:25237, now:17266, delta: 7971
100 us. start:64161, now:56190, delta: 7971
100 us. start:28757, now:20786, delta: 7971
100 us. start:73321, now:65350, delta: 7971
100 us. start:37919, now:29948, delta: 7971
100 us. start: 2485, now:79617, delta: 2867
100 us. start:52181, now:44210, delta: 7971
100 us. start:16719, now: 8748, delta: 7971
100 us. start:60982, now:53011, delta: 7971
100 us. start:25237, now:17266, delta: 7971
100 us. start:69796, now:61825, delta: 7971
100 us. start:34400, now:26429, delta: 7971
100 us. start:78923, now:70952, delta: 7971
100 us. start:43519, now:35548, delta: 7971
100 us. start: 8073, now: 102, delta: 7971
100 us. start:52365, now:44394, delta: 7971
100 us. start:16899, now: 8928, delta: 7971
100 us. start:55477, now:47506, delta: 7971
100 us. start:20081, now:12110, delta: 7971
100 us. start:64636, now:56665, delta: 7971
100 us. start:29212, now:21241, delta: 7971
100 us. start:73771, now:65800, delta: 7971
100 us. start:38369, now:30398, delta: 7971
100 us. start: 2935, now:79611, delta: 3323
100 us. start:52175, now:44204, delta: 7971
100 us. start:16709, now: 8738, delta: 7971
100 us. start:60976, now:53005, delta: 7971
100 us. start:25221, now:17250, delta: 7971
100 us. start:69779, now:61808, delta: 7971
100 us. start:34383, now:26412, delta: 7971
100 us. start:78925, now:70954, delta: 7971
100 us. start:43529, now:35558, delta: 7971
100 us. start: 8083, now: 112, delta: 7971
100 us. start:52381, now:44410, delta: 7971
100 us. start:16919, now: 8948, delta: 7971
100 us. start:61082, now:53111, delta: 7971
100 us. start:25677, now:17706, delta: 7971
100 us. start:69836, now:61865, delta: 7971
100 us. start:34440, now:26469, delta: 7971
100 us. start:78923, now:70952, delta: 7971
100 us. start:40603, now:32632, delta: 7971
100 us. start: 4673, now:79613, delta: 5059
100 us. start:52185, now:44214, delta: 7971
100 us. start:16719, now: 8748, delta: 7971
100 us. start:60982, now:53011, delta: 7971
100 us. start:25237, now:17266, delta: 7971
100 us. start:69796, now:61825, delta: 7971
100 us. start:34400, now:26429, delta: 7971
100 us. start:78923, now:70952, delta: 7971


msh />us_test 100 3
100 us. start:65215, now:57729, delta: 7486
100 us. start:30012, now:22521, delta: 7491
100 us. start:75009, now:67518, delta: 7491
100 us. start:40093, now:32602, delta: 7491
100 us. start: 5089, now:77237, delta: 7851
100 us. start:49800, now:42309, delta: 7491
100 us. start:14875, now: 7384, delta: 7491
100 us. start:59132, now:51641, delta: 7491
100 us. start:24217, now:16726, delta: 7491
100 us. start:69271, now:61780, delta: 7491
100 us. start:34348, now:26857, delta: 7491
100 us. start:79051, now:71560, delta: 7491
100 us. start:43996, now:36505, delta: 7491
100 us. start: 8540, now: 1049, delta: 7491
100 us. start:53155, now:45664, delta: 7491
100 us. start:18200, now:10709, delta: 7491
100 us. start:62567, now:55076, delta: 7491
100 us. start:27642, now:20151, delta: 7491
100 us. start:72681, now:65190, delta: 7491
100 us. start:37759, now:30268, delta: 7491
100 us. start:78609, now:71118, delta: 7491
100 us. start:43684, now:36193, delta: 7491
100 us. start: 8538, now: 1047, delta: 7491
100 us. start:53155, now:45664, delta: 7491
100 us. start:18200, now:10709, delta: 7491
100 us. start:62567, now:55076, delta: 7491
100 us. start:27642, now:20151, delta: 7491
100 us. start:72681, now:65190, delta: 7491
100 us. start:60663, now:53172, delta: 7491
100 us. start:25638, now:18147, delta: 7491
100 us. start:70725, now:63234, delta: 7491
100 us. start:35731, now:28240, delta: 7491
100 us. start: 808, now:72957, delta: 7850
100 us. start:45560, now:38069, delta: 7491
100 us. start:10583, now: 3092, delta: 7491
100 us. start:55216, now:47725, delta: 7491
100 us. start:20260, now:12769, delta: 7491
100 us. start:65342, now:57851, delta: 7491
100 us. start:30377, now:22886, delta: 7491
100 us. start:75453, now:67962, delta: 7491
100 us. start:40462, now:32971, delta: 7491
100 us. start: 5546, now:77694, delta: 7851
100 us. start:44542, now:37051, delta: 7491
100 us. start: 9573, now: 2082, delta: 7491
100 us. start:54087, now:46596, delta: 7491
100 us. start:19092, now:11601, delta: 7491
100 us. start:64182, now:56691, delta: 7491
100 us. start:29176, now:21685, delta: 7491
100 us. start:68569, now:61078, delta: 7491
100 us. start:33592, now:26101, delta: 7491
100 us. start:78675, now:71184, delta: 7491
100 us. start:43759, now:36268, delta: 7491
100 us. start: 8783, now: 1292, delta: 7491
100 us. start:53165, now:45674, delta: 7491
100 us. start:18160, now:10669, delta: 7491
100 us. start:62897, now:55406, delta: 7491
100 us. start:27926, now:20435, delta: 7491
100 us. start:73003, now:65512, delta: 7491
100 us. start:38012, now:30521, delta: 7491
100 us. start: 3098, now:75246, delta: 7851
100 us. start:47810, now:40319, delta: 7491
100 us. start:12834, now: 5343, delta: 7491
100 us. start:57451, now:49960, delta: 7491
100 us. start:22480, now:14989, delta: 7491
100 us. start:66376, now:58885, delta: 7491
100 us. start:31406, now:23915, delta: 7491
100 us. start:76499, now:69008, delta: 7491
100 us. start:41513, now:34022, delta: 7491
100 us. start: 6589, now:78737, delta: 7851
100 us. start:51301, now:43810, delta: 7491
100 us. start:16324, now: 8833, delta: 7491
100 us. start:61066, now:53575, delta: 7491
100 us. start:26071, now:18580, delta: 7491
100 us. start:71157, now:63666, delta: 7491
100 us. start:36157, now:28666, delta: 7491
100 us. start: 1243, now:73391, delta: 7851
100 us. start:45893, now:38402, delta: 7491
100 us. start:10969, now: 3478, delta: 7491
100 us. start:55716, now:48225, delta: 7491
100 us. start:20755, now:13264, delta: 7491
100 us. start:65832, now:58341, delta: 7491
100 us. start:30862, now:23371, delta: 7491
100 us. start:75939, now:68448, delta: 7491
100 us. start:40968, now:33477, delta: 7491
100 us. start: 6044, now:78193, delta: 7850
100 us. start:50484, now:42993, delta: 7491
100 us. start:15560, now: 8069, delta: 7491
100 us. start:58572, now:51081, delta: 7491
10


之前收集的,可以不看。
下面的例程都是摘自rt-thread中,可以参考着写。
1 是根据tick,

static void retry_delay_100us(void) {
    /* 100 microsecond delay */
    rt_thread_delay((RT_TICK_PER_SECOND * 1 + 9999) / 10000);
}

rt_err_t rt_thread_delay(rt_tick_t tick)
{
    return rt_thread_sleep(tick);
}
rt_err_t rt_thread_sleep(rt_tick_t tick)
{
    register rt_base_t temp;
    struct rt_thread *thread;

    /* set to current thread */
    thread = rt_thread_self();
    RT_ASSERT(thread != RT_NULL);
    RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);

    /* disable interrupt */
    temp = rt_hw_interrupt_disable();

    /* suspend thread */
    rt_thread_suspend(thread);

    /* reset the timeout of thread timer and start it */
    rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
    rt_timer_start(&(thread->thread_timer));

    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

    rt_schedule();

    /* clear error number of this thread to RT_EOK */
    if (thread->error == -RT_ETIMEOUT)
        thread->error = RT_EOK;

    return RT_EOK;
}

2 根据系统时钟SystemcoreClock,执行n调指令

wait_loop_index = ((PWR_FLAG_SETTING_DELAY_US * SystemCoreClock) / 1000000U) + 1U;
while ((HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_VOSF)) && (wait_loop_index != 0U))
 {
   wait_loop_index--;
 }

3 drv_common.c

void rt_hw_us_delay(rt_uint32_t us)
{
    rt_uint32_t start, now, delta, reload, us_tick;
    start = SysTick->VAL;
    reload = SysTick->LOAD;
    us_tick = SystemCoreClock / 1000000UL;
    do {
        now = SysTick->VAL;
        delta = start > now ? start - now : reload + start - now;
    } while(delta < us_tick * us);
}

  1. systick的数值是载入reload(80,000)后,硬件每SystemCoreClock(or 1/8)自动递减(每次-1相当于走过一个硬件时钟)。减到0进入systick的中断。这个中断可以做为系统时钟OS Tick。
    /* Configure the Systick interrupt time /
    HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
    /
    Configure the Systick */
    HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
    微秒(us)延时 程序_第1张图片 ↩︎

你可能感兴趣的:(rt-thread,C,嵌入式开发,程序,测试,微秒,us,延时)