1)通过超声波传感器采集距离数据,距离值保留整数。
2)通过数码管显示距离显示界面和参数显示界面,界面可以通过按键进行切换。
3)通过按键设置与调整距离参数。
4)通过串口向PC端发送当前距离数据。
5)通过LED指示灯进行报警功能。
3.2 数码管显示
1)距离显示界面
图2 距离显示界面(155cm)
2)参数显示界面
图3 参数显示界面(155cm)
3.3 按键功能
1)按键S4定义为“切换”按键,按下S4按键,能够切换“距离显示界面”、“参数显示界面”。
图4 按键S4‘切换’
2)按键S8定义为“参数”按键,在“距离显示界面”时,按下S8按键,可将当前距离数据设置为距离参数。
3)按键S12定义为“加”按键,在“参数显示界面”时,按下S12按键,当前距离参数加10cm。
图5 按键S12‘加’
4)按键S16定义为“减”按键,在“距离参数界面”时,按下S16按键,当前距离参数减10cm,参数值减到0为止。
图6 按键S16‘减’
5)按键S9定义为“发送”按键,每次按下,串口将当前检测的距离数据发送给PC端的串口调试工具。
注意:
(1)按键S12和按键S16仅在“距离参数界面”时有效
(2)按键S8仅在“距离显示界面”时有效,当按键S8按下后,参数显示界面参数值随之变化,但不会发生界面跳转。
(3)判断按键是否按下时,需进行消抖操作,避免单次按键操作,触发多次结果。
3.4 串口功能
串口发送格式
注意:
(1)D为大写字母,其余均为小写字母。
(2)距离数据保留整数。
3.5 LED指示灯功能
1)当前界面处于距离显示界面时,L1指示灯点亮,否则熄灭。
2)当前界面处于参数显示界面时,L2指示灯点亮,否则熄灭。
3)当实时距离超过距离参数时,L3指示灯以0.2s的时间间隔亮、灭,否则熄灭。
4)指示灯L4-L8处于常熄灭状态。
3.6 初始化
1)上电数码管处于距离显示界面。
2)距离参数值为30cm。
单片机源程序如下:
#include"reg52.h"
#include"intrins.h"
sfr AUXR=0x8e;
sfr P4=0xc0;
sbit TX=P1^0;
sbit RX=P1^1;
sbit R1=P3^0;
sbit R2=P3^1;
sbit R3=P3^2;
sbit R4=P3^3;
sbit C1=P3^4;
sbit C2=P3^5;
sbit C3=P4^2;
sbit C4=P4^4;
unsigned char code SMG[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc1};
unsigned int time;
unsigned char set_temp=30;
unsigned char disp_mode=0,dis_nu,commend,now_temp;
void Delay12us() //@11.0592MHz
{
unsigned char i;
_nop_();
_nop_();
_nop_();
i = 30;
while(--i);
}
void select573(unsigned char n)
{
switch(n)
{
case 4:P2=(P2&0x1f)|0x80;break;
case 5:P2=(P2&0x1f)|0xa0;break;
case 6:P2=(P2&0x1f)|0xc0;break;
case 7:P2=(P2&0x1f)|0xe0;break;
case 0:P2=(P2&0x1f)|0x00;break;
}
}
void disply_SMG(unsigned char pos,value)
{
select573(6);
P0=0x01<<pos;
select573(6);
P0=value;
}
void init_buzz()
{
select573(4);
P0=0xff;
select573(5);
P0=0x00;
select573(0);
}
void init()
{
TMOD = 0x02;
TH0=0xfd;
TL0=0xfd;
TR0=1;
SCON = 0x50;
ES=1;
EA=1;
AUXR=0x00;
}
void revice() interrupt 4
{
if(RI == 1)
{
commend = SBUF;
RI=0;
}
}
void sendbyte(unsigned char date)
{
SBUF = date;
while(TI == 0);
TI = 0;
}
void sendstring(unsigned char *str)
{
while(*str != '\0')
{
sendbyte(*str++);
}
}
void wave_send()
{
unsigned i;
for(i=0;i<8;i++)
{
TX=1;
Delay12us();
TX=0;
Delay12us();
}
}
unsigned char wave_co()
{
TMOD&=0x0f;
TH1=TL1=0;
wave_send();
TR1=1;
while((RX==1)&&(TF1==0));
TR1=0;
if(TF1==0)
{
time=TH1<<8|TL1;
return(time*0.017);
}
else
TF1=0;
return 0;
}
void wave() interrupt 3
{
now_temp=wave_co();
}
void scankey()
{
unsigned char i;
for(i=0;i<4;i++)
{
switch(i)
{
case 0:R1=0;R2=R3=R4=1;
if(C1==0)
dis_nu=0;
else if(C2==0)
dis_nu=1;
else if(C3==0)
dis_nu=2;
else if(C4==0)
dis_nu=3;
break;
case 1:R2=0;R1=R3=R4=1;
if(C1==0)
dis_nu=4;
else if(C2==0)
dis_nu=5;
else if(C3==0)
dis_nu=6;
else if(C4==0)
dis_nu=7;
break;
case 2:R3=0;R2=R1=R4=1;
if(C1==0)
dis_nu=8;
else if(C2==0)
{
sendstring("Distance:\n\r");
sendbyte(time);
}
else if(C3==0)
dis_nu=9;
else if(C4==0)
dis_nu=0;
break;
case 3:R4=0;R2=R3=R1=1;
if(C1==0)
{
disp_mode=~disp_mode;
}
else if(C2==0)
{
set_temp=now_temp;
}
else if(C3==0)
{
set_temp=(set_temp+10);
}
else if(C4==0)
{
set_temp=(set_temp-10);
if(set_temp<0)
set_temp=0;
}
break;
}
}
if(i>4)i=0;
}
void Delay500us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 60;
j = 934;
do
{
while (--j);
} while (--i);
}
void show_SMG()
{
if(disp_mode==0)
{
disply_SMG(0,SMG[12]);
Delay500us();
disply_SMG(1,SMG[1]);
Delay500us();
disply_SMG(2,SMG[11]);
Delay500us();
disply_SMG(3,SMG[11]);
Delay500us();
disply_SMG(4,SMG[11]);
Delay500us();
disply_SMG(5,SMG[now_temp/100]);
Delay500us();
disply_SMG(6,SMG[now_temp%100/100]);
Delay500us();
disply_SMG(7,SMG[now_temp%10]);
Delay500us();
}
if(disp_mode==1)
{
disply_SMG(0,SMG[12]);
Delay500us();
disply_SMG(1,SMG[2]);
Delay500us();
disply_SMG(2,SMG[11]);
Delay500us();
disply_SMG(3,SMG[11]);
Delay500us();
disply_SMG(4,SMG[11]);
Delay500us();
disply_SMG(5,SMG[set_temp/100]);
Delay500us();
disply_SMG(6,SMG[set_temp%100/100]);
Delay500us();
disply_SMG(7,SMG[set_temp%10]);
Delay500us();
}
}
void main()
{
init_buzz();
wave_co();
init();
while(1)
{
scankey();
show_SMG();
}
}
|