📄 测距模块程序.c
字号:
#include <reg51.h>
#include <stdio.h>
#define uchar unsigned char;
#define uint unsigned int;
#define C=340;
//D1--D4:四位数码管COM端,在本例中为共阴极;DP:小数位。
sbit D1=P1^7;
sbit D2=P1^6;
sbit D3=P1^5;
sbit D4=P1^4;
sbit DP=P0^7;
sbit BLNH=P1^1;
sbit BLNK=P1^2;
sbit INIT=P1^0;
bit flag;
float t,s;
uchar xm0,xm1,xm2,xm3,buffer[4];
uchar code TABLE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uint i;
void Delay(i)
{
unsigned int j;
while(i--)
{
for(j=0;j<125;j++)
{;}
}
}
void timeToBuffer() //转换段码功能模块
{
xm0=((int)s)/100;
xm1=((int)(((int)(s*10))/100))%10;
xm2=((int)s)%10;
xm3=((int)(s*10))%10;
buffer[3]=TABLE[xm3];
buffer[2]=TABLE[xm2];
buffer[1]=TABLE[xm1];
buffer[0]=TABLE[xm0];
P0=buffer[0];
D1=0;
Delay(1);
D1=1;
P0=buffer[1];
D2=0;
Delay(1);
D2=1;
P0=buffer[2];
D3=0;
DP=1;
Delay(1);
D3=1;
P0=buffer[3];
D4=0;
Delay(1);
D4=1;
}
time0()interrupt 3 using 1 //p1.0口发送脉冲
{
INIT=0;
TR0=0;
TH0=0;
TL0=0;
Delay(5);
INIT=1;
TR0=1;
TH1=(65536-30000)/256;
TL1=(65536-30000)%256;
}
void service_echo()interrupt 0 using 2 //回波,外部中断
{
int y;
TR0=0;
t=TH0;
t=t*256+TL0;
t=(t-16)*4.5/420000+16*8.5/420000;
s=t*34000/2;
TH0=0;
TL0=0;
TR0=1;
for(y=0;y<10;y++)
{
timeToBuffer();
}
}
main()
{
TMOD=0x15;
TH1=(65536-30000)/256;
TL1=(65536-30000)%256;
BLNH=0;
BLNK=0;
TH0=0;
TL0=0;
IP=0x01;
EA=1;
EX0=1;
ET1=1;
TR0=1;
TR1=1;
IT0=1;
for(;;)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -