51单片机12M晶振的延时程序
调试环境:Keil V4.02
本文引用地址:https://www.eepw.com.cn/article/201611/315933.htm源代码如下:
#include
#include
//--延时0.2*n(ms)函数,若需延时1ms,则*5。适合延时50ms以下或左右的--//
void DelayMSx02(unsigned char n)
{
unsigned char x, y;
for(x=n; x>0; x--)
for(y=96; y>0; y--); //for循环中的"--"位置前后都可以
}
//--延时t*2+5(us)函数 --//
void DelayUSx2a5(unsigned char t)
{
while(--t);//while循环中要注意"--"的位置,放前面比放后面时间要短很多
}
//--大概延时1mS--//
void DelayMS(unsigned char t)
{
while(t--)
{
DelayUSx2a5(234);
DelayUSx2a5(256);
}
}
int main()
{
DelayMS(1); //延时1ms
DelayMSx02(5*1); //延时1ms
DelayUSx2a5(1); //延时7us
_nop_(); //延时1us
return 0;
}
评论