rt-thread-official/bsp/ls1cdev/libraries/ls1c_delay.c

90 lines
1.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017-09-06 勤为本 first version
*/
#include "ls1c_clock.h"
/*
* 延时指定时间单位ms
* @j 延时时间单位ms
*/
void delay_ms(int j)
{
int k_max = clk_get_cpu_rate()/1000/92; // 除以1000表示ms另外一个除数为实验测得的经验值
int k = k_max;
for ( ; j > 0; j--)
{
for (k = k_max; k > 0; k--)
{
__asm__ ("nop"); // 注意,这里必须用内联汇编,否则会被优化掉
}
}
return ;
}
/*
* 延时指定时间单位us
* @n 延时时间单位us
*/
void delay_us(int n)
{
int count_1us = 252000000 / 1000000 / 84; // 延时1us的循环次数
// 252000000为cpu频率除以1000000表示延时单位为us92为实验测得的经验值
int count_max; // 延时n微秒的循环次数
int tmp;
// 微调
count_max = n * count_1us;
if (10 >= n) // <=10us
{
count_max = count_max / 3;
}
else if (100 >= n) // <= 100us
{
count_max = count_max - count_max / 5;
}
else // > 100us
{
count_max = count_max - count_max / 10;
}
// 延时
for (tmp = count_max; tmp > 0; tmp--)
{
__asm__ ("nop"); // 注意,这里必须用内联汇编,否则会被优化掉
}
return ;
}
/*
* 延时指定时间单位s
* @i 延时时间单位s
*/
void delay_s(int i)
{
for ( ; i > 0; i--)
{
delay_ms(1000);
}
return ;
}