DELAY:MOV
R6,#50
DEL1:DJNZ
R6,REL1
就这么简单,DJNZ是减一不为零转移指令。镇袭如需要延时更长时间,在上述方法御敬兄上配合用循环实现。
#include <reg51.h>#define uchar unsigned char
#define uint unsigned int
unsigned char code smg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x00}
sbit tr=P1^0
sbit ec=P1^1
uint distance,time,tl,th
void delay_20us(void )
{
uchar a
for(a=0a<逗州100a++)
}
void delay(uint a )
{
while(a--)
}
void display(void )
{
P2=0xff
P2=0xfe
P0=smg[distance/100]
delay(200)
P2=0xff
P2=0xfd
P0=smg[distance%100/10]
delay(200)
P2=0xff
P2=0xfb
P0=smg[distance%10]
delay(200)
}
void main(void )
{
uchar i
TMOD=0X10
while(1)
{
tr=1/旁液/超声波输入端
delay_20us() //延时20us
tr=0//产生一个20us的脉冲
while(ec==0)//等待Echo回波引脚变高电平
TH1=0 //定时器1清零
TL1=0 //定时器1清零
TF1=0 //计数溢出标志
TR1=1 //启动定时器1
while(ec==1)
th=TH1
tl=TL1
TL1=0
TH1=0
TR1=0 //关闭定时器1
time=th*256+tl
distance=time*0.017 //厘米 */
display();
}
}
不懂M我运指物
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)