//超声波模块程序
//作者 : Z H
//时间 2015/11/29
//Trig = P2^0
//Echo = P3^2
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcd_rs = P1^0 ;
sbit lcd_wr = P1^1 ;
sbit lcd_e = P2^5 ;
/
/
/
unsigned char table1[2][16]= {" MM "," "};
void delay1()
{ int i ;
for(i = 121 ;i>0;i-- )
;
}
void write_com(unsigned char com)
{
lcd_rs = 0;
lcd_wr = 0;
lcd_e = 1;
delay1();
P0 = com;
delay1();
lcd_e = 0;
lcd_wr = 1;
}
void write_data(unsigned char num)
{
lcd_rs =1;
lcd_wr =0;
P0 = num;
delay1();
lcd_e = 1;
delay1();
lcd_e = 0;
lcd_wr = 1;
}
void LcdInit()
{
lcd_e = 0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
// write_com(0x80);
}
//---------液晶显示数据处理----------
void LcdDataProcessing(int * DATA,int hang,int lie)
{
table1[hang-1][lie ] = (*DATA) / 1000 % 10 + 48;
table1[hang-1][lie+1] = (*DATA)/ 100% 10 + 48;
table1[hang-1][lie+2] = (*DATA)/ 10 % 10 + 48;
table1[hang-1][lie+3] = (*DATA) % 10 + 48;
}
//---------液晶显示程序----------
void LcdPrint(int * DATA,int hang,int lie )
{ int i ;
LcdDataProcessing( DATA,hang,lie);
if(hang-2)
{
write_com(0x80);
for( i = 0; table1[hang-1][i] != '\0' && i < 16;i ++)
{
write_data(table1[hang-1][i]);
}
}else
{
write_com(0x80 +0x40);
for( i=0; table1[hang-1][i] != '\0' && i < 16;i ++)
{
write_data(table1[hang-1][i]);
}
}
}
//
//
//
//unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,
// 0x6d,0x7d,0x07,0x7f,0x6f}; //0-9的码表
//
uint time = 0;
uint timeH = 0;
uint timeL = 0;
uint succeed_flag;
//
//
sbit dula=P2^0; //段选信号的锁存器控制
sbit wela=P2^1; //位选信号的锁存器控制
sbit Trig = P2^2;
sbit Echo = P3^2;
sbit led0=P1^2;
sbit led1=P1^3;
sbit led2=P1^4;
sbit led3=P1^5;
sbit led4=P1^6;
//
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=125;y>0;y--);
}
//
void delay_20us()
{
uchar a ;
for(a=0;a<100;a++);
}
***************************************************************
显示数据转换程序
//void display(uint temp)
// {
// uchar qian,ge,shi,bai;
// qian = temp/1000;
// bai=temp/100%10;
// shi=temp%100/10;
// ge=temp%10;
//
// wela=0;
// P0 = 0xfe; wela=1;
// wela=0;
// dula=0;
// P0=table[qian]; dula=1;
// dula=0;
// delay(10);
//
// wela=0;
// P0 = 0xfd;wela=1;
// wela=0;
// dula=0;
// P0=table[bai];dula=1;
// dula=0;
// delay(10);
//
// wela=0;
// P0 = 0xfb;wela=1;
// wela=0;
// dula=0;
// P0=table[shi]; dula=1;
// dula=0;
// delay(10);
//
// dula=0;
// P0=table[ge];dula=1;
// dula=0;
// wela=0;
// P0 = 0xf7; wela=1;
// wela=0;
// delay(10);
//
// }
//***************************************************************
void main()
{
uint s=123;
uint distance=0;
uint test =0;
LcdInit() ;
Trig=0; //首先拉低脉冲输入引脚
EA=1; //打开总中断0
TMOD=0x10; //定时器1,16位工作方式
while(1)
{
led0=1;
led1=1;
led2=1;
led3=1;
EA=0; //关总中断
Trig=1; //超声波输入端
delay_20us(); //延时20us
Trig=0; //产生一个20us的脉冲
while(Echo==0); //等待Echo回波引脚变高电平
succeed_flag=0; //清测量成功标志
EA=1;
EX0=1; //打开外部中断0
TH1=0; //定时器1清零
TL1=0; //定时器1清零
TF1=0; //计数溢出标志
TR1=1; //启动定时器1
delay(20); //等待测量的结果
TR1=0; //关闭定时器1
EX0=0; //关闭外部中断0
//LcdPrint(&s,1,0);
if(succeed_flag==1)
{
time=timeH*256+timeL;
distance=time*0.172; //厘米
LcdPrint(&distance,1,0);
if(distance <100.0 )
{led0=0;delay(100);}
else if(distance >100.0 &&distance <150.0 )
{led1=0;delay(100);}
else if(distance >150.0 && distance <200.0 )
{led2=0;delay(100);}
else if(distance >200.0 )
{led3=0;delay(100);}
}
if(succeed_flag==0)
{
distance=0; //没有回波则清零
test = !test; //测试灯变化
}
}
}
//***************************************************************
//外部中断0,用做判断回波电平
void exter() interrupt 0 // 外部中断0是0号
{
timeH =TH1; //取出定时器的值
timeL =TL1; //取出定时器的值
succeed_flag=1;//至成功测量的标志
EX0=0; //关闭外部中断
}
//****************************************************************
//定时器1中断,用做超声波测距计时
void timer1() interrupt 3 //
{
TH1=0;
TL1=0;
}
51单片机超声波测距1602显示
最新推荐文章于 2024-09-26 01:57:35 发布