在单片机中实现延迟n秒后执行代码,没有指定实现该功能的函数提供。
借助stc-isp选定相应的单片机型号和晶振型号可以帮助我们生成延迟函数。
将生成的函数直接拷贝到我们的代码里面,还需要加上一个头文件:#include <intrins.h>
实现led灯间隔1秒进行闪烁的代码如下:
#include <reg52.h>
#include <intrins.h>
sbit LED0 = P0^1;
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main(){
while(1){
LED0 = 0;
Delay1000ms();
LED0 = 1;
Delay1000ms();
}
}
原文:https://www.cnblogs.com/maycpou/p/13548929.html