51单片机12M晶振的延时程序

嵌入式系统 时间:2016-11-18来源:网络
这是本人慢慢调出来的参数,有误差是必须的,除非用汇编才会精确,后续我会更新修改,尽量精确。

调试环境:Keil V4.02

源代码如下:

#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;
}

关键词: 51单片机12M晶振延时程

加入微信
获取电子行业最新资讯
搜索微信公众号:EEPW

或用微信扫描左侧二维码

相关文章

查看电脑版