超声波LCD显示测距程序
超声波测距C语言源程序代码
/*{HZ即单位s的倒数}本晶振为12MHZ,因此外部的时钟频率为12MHZ,所以部的时钟频率为(12MHZ)/12=1MH即1000000HZ,而机械频率为1/(1MHZ),即每完成一次计算(即定时器的值加一)用时0.000001s,即1us(微秒).*//***********************************************************************************///具有模式选择.*include<reg52.h>*define UC unsigned char*define UI unsigned intvoid delay(UI); //延时9.56us程序sbit beep = P1^3; //用于声音报警sbit Lv = P1^7; //用于光报警sbit Hong = P1^6;sbit QD = P3^7;//K8 //P3^7口(K8)为确定键,sbit G* = P3^1;//K7 //P3^3口(K2)为修改键,sbit S* = P3^6;//K6 //P3^2(K3)为测量键.sbit B* = P3^0;//K5 //个(K7),十(K6),百(K5),三位修改键sbit a = P1^2;//百位//数码管位选sbit b = P1^1;//十位sbit c = P1^0;//个位sbit trig = P1^4; //方波发射端sbit echo = P1^5; //超声波接收端void IntConfiguration(); //用来"设置中断"的函数,P3^3口(K2)为修改键,P3^2(K3)为测量键.void TimeConfiguration(); //用来"设置定时器"的函数sbit K1 = P3^4;//动态sbit K4 = P3^5;//静态//用于进展模式切换(K1、K4键)void *ia*ian(); //修改函数,用来修改下限void shang*ian(); //修改函数,用来修改上限UI min[3]={0,5,0}; //报警极限,拆分为"百十个"三位UI ma*[3]={3,0,0}; //MIN,MA* 用来存储最大和最小值void MIN*ianshi(UI); //最小围和最大围的显示void MA**ianshi(UI);UC code CharacterCode[10] = {0*3f,0*06,0*5b,0*4f,0*66,0*6d,0*7d,0*07,0*7f,0*6f};//数码管数字字符(P2口)/********************************主函数*********************************************/ void main(){TimeConfiguration(); //设置定时器0IntConfiguration(); //设置中断允许,K4键为修改键,K8键为确定键while(1){MIN*ianshi(40); //1.50169000sMA**ianshi(40); //1.50098300s}}/*******************************超声波测距函数********************************************/void zhongduan_0() interrupt 0 //测量中断函数(外部中断0){UI moshi = 0;UI juli = 0;UI time = 0;UI MA*, MIN;UI TT = 0;//用于第一次测量时给P1^5口置一,以便正确读取数值UI t1, t2, t3;UI GE = 0, SHI = 0, BAI = 0; //先定义三个变量,用来显示测量的距离.a = 0;b = 0;c = 0;P2 =~ 0*00; //防止最后显示的那个数码管一直亮MA* = ma*[0]*100 + ma*[1]*10 + ma*[2]; //计算最大与最小值MIN = min[0]*100 + min[1]*10 + min[2];while(1)//下面进展测量{while(1) /*先进展模式判断*/{if(0 == K1){moshi = 1;break; //模式1为动态测量}if(0 == K4){moshi = 2;break; //模式2为静态测量}if(0 == QD)return; //完毕测量函数}/********************************************计算距离************************************************/loop: beep = 1;//关掉定时器Lv = 1;Hong = 1;//关掉灯a = 0;b = 0;c = 0;P2 =~ 0*00;//防止最后显示的那个数码管一直亮if( (0 == QD)&&(1 == moshi) )break;if( (0 == QD)&&(2 == moshi) ){delay(55500);if(0 == QD){delay(55500);if(0 == QD)break;}}t1 = 35,t2 = 35;t3 = 35;trig = 0;echo = 0;delay(2); //初始化拉低两个端口trig = 1;delay(2);trig = 0; //输出端输出27us的高电压,并将输出端口拉低while(echo == 0); //判断是否有回波返回,有则开启定时器TR0 = 1; //当有高电平输出时,开启定时器while(echo == 1);TR0 = 0; //当高电平变成低电平时,关闭定时器++TT; //测量值加一,记录测量次数if(1 == TT){delay(55500);TH0 = 0*00;TL0 = 0*00; //定时器的初值,定时器的定时为65536us.goto loop;}time = TL0 + TH0*256; //接下来显示测量的距离TH0 = 0*00;TL0 = 0*00; //定时器的初值,定时器的定时为65536us.juli = ( int )( (time*0.034)/2 );BAI = ( (juli%1000)/100 ); SHI = ( (juli%100)/10 ); GE = ( juli%10 );/******************************************两种模式的距离显示********************************************/if(juli > MA*){Hong = 0;Lv = 1;while( t1-- ){a = 0;b = 1;c = 1;P2 =~ CharacterCode[BAI];delay(400);a = 1;b = 0;c = 1;P2 =~ CharacterCode[SHI];delay(400);a = 1;b = 1;c = 0;P2 =~ CharacterCode[GE];delay(390);beep = 0;if( (1 == moshi)&&(0 == t1) )goto loop;if(moshi == 2){t1 = 2;if(0 == QD)goto loop;}}}else if(juli < MIN){Lv = 0;Hong = 1;while( t2-- ){a = 0;b = 1;c = 1;P2 =~ CharacterCode[BAI];delay(500);a = 1;b = 0;c = 1;P2 =~ CharacterCode[SHI];delay(500);a = 1;b = 1;c = 0;P2 =~ CharacterCode[GE];delay(400);beep = 0;delay(100);beep = 1;if( (1 == moshi)&&(0 == t2) )goto loop;if(2 == moshi){t2 = 2;if(0 == QD)goto loop;}}}else{beep = 1;Lv = 1;Hong = 1;while( t3-- ){a = 0;b = 1;c = 1;P2 =~ CharacterCode[BAI];delay(600);a = 1;b = 0;c = 1;P2 =~ CharacterCode[SHI];delay(600);a = 1;b = 1;c = 0;P2 =~ CharacterCode[GE];delay(600);if( (1 == moshi)&&(0 == t3) )goto loop;if(2 == moshi){t3 = 2;if(0 == QD)goto loop;}}}//显示完毕}}/***********************************************************************************/ void zhongduan_1() interrupt 1 //定时器溢出时的中断,显示测得的距离(定时器中断0){TH0 = 0*00;//定时器的初值,定时器的定时为65536us,TL0 = 0*00;}/***********************************************************************************/ void zhongduan_2() interrupt 2 //修改键(K4)的中断函数(外部中断1){*ia*ian();while(QD==0);shang*ian();}/**********************************************************************************/ void MIN*ianshi(UI TT) //显示最小距离{while(TT--){a = 0;b = 1;c = 1;P2 =~ CharacterCode[min[0]];delay(500);a = 1;b = 0;c = 1;P2 =~ CharacterCode[min[1]];delay(500);a = 1;b = 1;c = 0;P2 =~ CharacterCode[min[2]];delay(500);}P2 =~ 0*00;delay(55500);}void MA**ianshi(UI TT) //显示最大距离{while(TT--){a = 0;b = 1;c = 1;P2 =~ CharacterCode[ma*[0]];delay(500);a = 1;b = 0;c = 1;P2 =~ CharacterCode[ma*[1]];delay(500);a = 1;b = 1;c = 0;P2 =~ CharacterCode[ma*[2]];delay(500);}P2 =~ 0*00;delay(55500);}/***********************************************************************************/ void delay(UI T) //延时程序{while(T--);}/***********************************************************************************/ void IntConfiguration() //设置中断函数{//优先级设置PT2 = 0;PS = 0;PT1 = 0;P*1 = 0;PT0 = 1;P*0 = 0;IT1 = 1; //外部中断0为跳变沿触发E*1 = 1; //P3^3口(K4键)修改键,中断允许开启IT0 = 1; //外部中断1为跳变沿触发E*0 = 1; //P3^2口(K1键)测量键,中断允许开启ET0 = 1; //定时器0的中断允许开启EA = 1;}void TimeConfiguration() //设置定时器,以及定时器的初值{TMOD = 0*01;//设定只使用0号定时器; 模式:定时器; 工作方式:1号工作方式.//下面是定时器的初始值, TR0,TR1是用来开启定时器的TH0 = 0*00;//定时器的初值,定时器的定时为50us.TL0 = 0*00;/*1号定时器不用,所以没有TH1,TL1*/}/***********************************************************************************/ void *ia*ian() //修改下限{while(1){if(B*==0)//百位{P2=~0*00;min[0]++;if(min[0]==10)min[0]=0;delay(60000);}a = 0;b = 1;c = 1;P2 =~ CharacterCode[min[0]];delay(100);if(S*==0)//十位{P2=~0*00;min[1]++;if(min[1]==10)min[1]=0;delay(60000);}a = 1;b = 0;c = 1;P2 =~ CharacterCode[min[1]];delay(100);if(G*==0)//个位{P2=~0*00;min[2]++;if(min[2]==10)min[2]=0;delay(60000);}a = 1;b = 1;c = 0;P2 =~ CharacterCode[min[2]];delay(100);if(QD==0){a = 0;b = 0;c = 0;P2 = 0*ff;break;}}}void shang*ian() //修改上限{while(1){if(B*==0)//百位{P2=~0*00;ma*[0]++;if(ma*[0]==10)ma*[0]=0;delay(60000);}a = 0;b = 1;c = 1;P2 =~ CharacterCode[ma*[0]];delay(100);if(S*==0)//十位{P2=~0*00;ma*[1]++;if(ma*[1]==10)ma*[1]=0;delay(60000);}a = 1;b = 0;c = 1;P2 =~ CharacterCode[ma*[1]];delay(100);if(G*==0)//个位{P2=~0*00;ma*[2]++;if(ma*[2]==10)ma*[2]=0;delay(60000);}a = 1;b = 1;c = 0;P2 =~ CharacterCode[ma*[2]];delay(100);if(QD == 0){a = 0;b = 0;c = 0;P2 = 0*ff;while(QD == 0);break;}}}/************************************************************************************/。
超声波测距 lcd1602显示
};
}
/********************************************************/
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=(time*1.7)/100; //算出来是CM
{
WDLCM = ((WDLCM&0x01)<<7)|((WDLCM&0x02)<<5)|((WDLCM&0x04)<<3)|((WDLCM&0x08)<<1)|((WDLCM&0x10)>>1)|((WDLCM&0x20)>>3)|((WDLCM&0x40)>>5)|((WDLCM&0x80)>>7);
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TX=0;
}
/********************************************************/
void delayms(unsigned int ms)
{
while (DData[ListLength]>0x19) //若到达字串尾则退出
{
if (X <= 0xF) //X坐标应小于0xF
{
DisplayOneChar(X, Y, DData[ListLength]); //显示单个字符
ListLength++;
超声波测距程序_代码全
/*----------------------------------超声波子程序1、采用定时器0产生40KHZ的方波,同时也进行时间计数,等待中断;2、若超过30ms后,重新发送方波,重新计数,直到有中断(接收到返回信号)再做显示或其它处理。
3、在处理之后又后重新初始化定器0和中断0,又执行第一步------------------------------------*/#include <reg52.h>//1602已定义了#include "1602.h"#include "delay.h"#include <stdio.h>//包含sprintf函数sbitpwm=P3^7;//40KHZsbit led=P1^3;//接收到后亮灯unsigned intnum=0;//计数float s; //测出的距离bit flag=0;//中断0标志位unsigned char out_time=0;//超时标志位/*---------------------------------------定时器0用于产生40KHZ频率初始化---------------------------------------*/void T0_init(){TMOD=0X02; //定时器0工作在方式2,自动重装数据TL0=256-12; //产生40KHZTH0=256-12; //T=1/40k*1000000=25us即12.5us取反一次EA=1;ET0=1;TR0=1;}/*--------------------------------定时器0中断服务程序//用于工作计时、同时产生40KHZ方波---------------------------------*/void T0_time() interrupt 1{num++;if(num<30) pwm=~pwm;//产生15个40KHZ的方波if(num==2500) {num=0;out_time++;}//30ms后清除标志,同时}/*-----------------------------外部中断0初始化------------------------------*/void INT0_init(){IT0=1;//低电平触发方式EX0=1;//打开外部中断// PX0=1;//外部中断0为高优先级}/*-----------------------------外部中断0中断服务程序------------------------------*/void INT0_d() interrupt 0{EX0=0; //当中断了,证明接收到信号,关闭外断中断TR0=0; //关闭定时器0flag=1; //标志外部中断0产生}/*-------------------------------超声波测距函数//s的值为返回测距值--------------------------------*/void measure(){if(flag==1) //中断0标志,进入{flag=0; //清除标志位out_time=0; //清除超时标志位led=~led; //观察DelayMs(100); //延时,**适用STC12C5A60S2,低速单片机,要减少延时**s=(num*12)/2*0.34*0.001;//测距返回值TH0=256-12; //定时器0重新初始化TL0=256-12;TR0=1; //运行定时器0INT0_init(); //启动外部中断0}}/*-------------------------------超声波显示部分--------------------------------*/void display(){char displaytemp[16];//定义显示区域临时存储数组if(out_time==33) //大约1s的超时时间,再将显示为0.000{out_time=0; //清除标志位s=0; //显示0}sprintf(displaytemp,"distance:% 6.3f",s);//将数字转成字符串LCD_Write_String(0,1,displaytemp);//显示}//*********************主程序***********************//void main(){LCD_Init(); //1602初始化LCD_Clear(); //清屏T0_init(); //定时器0初始化INT0_init(); //启动外部中断0while(1){measure(); //超声波测距函数display(); //显示}}附链接文件程序:/* 1602.c *//*-----------------------------------------------名称:LCD1602引脚定义如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK------------------------------------------------*/#include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义#include<intrins.h>#include "1602.h"#include "delay.h"sbit RS = P2^6; //定义端口sbit RW = P2^5;sbit EN = P2^7;#define RS_CLR RS=0#define RS_SET RS=1#define RW_CLR RW=0#define RW_SET RW=1#define EN_CLR EN=0#define EN_SET EN=1#define DataPort P0/*------------------------------------------------判忙函数------------------------------------------------*/ bitLCD_Check_Busy(void){DataPort= 0xFF;RS_CLR;RW_SET;EN_CLR;_nop_();EN_SET;return (bit)(DataPort& 0x80);}/*------------------------------------------------写入命令函数------------------------------------------------*/ voidLCD_Write_Com(unsigned char com) {while(LCD_Check_Busy()); //忙则等待RS_CLR;RW_CLR;EN_SET;DataPort= com;_nop_();EN_CLR;}/*------------------------------------------------写入数据函数------------------------------------------------*/ voidLCD_Write_Data(unsigned char Data){while(LCD_Check_Busy()); //忙则等待RS_SET;RW_CLR;EN_SET;DataPort= Data;_nop_();EN_CLR;}/*------------------------------------------------清屏函数------------------------------------------------*/voidLCD_Clear(void){LCD_Write_Com(0x01);DelayMs(5);}/*------------------------------------------------写入字符串函数------------------------------------------------*/voidLCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) {if (y == 0){LCD_Write_Com(0x80 + x); //表示第一行}else{LCD_Write_Com(0xC0 + x); //表示第二行}while (*s){LCD_Write_Data( *s);s ++;}}/*------------------------------------------------写入字符函数------------------------------------------------*//* void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) {if (y == 0){LCD_Write_Com(0x80 + x);}else{LCD_Write_Com(0xC0 + x);}LCD_Write_Data( Data);}*//*------------------------------------------------初始化函数------------------------------------------------*/voidLCD_Init(void){LCD_Write_Com(0x38); /*显示模式设置*/DelayMs(5);LCD_Write_Com(0x38);DelayMs(5);LCD_Write_Com(0x38);DelayMs(5);LCD_Write_Com(0x38);LCD_Write_Com(0x08); /*显示关闭*/LCD_Write_Com(0x01); /*显示清屏*/LCD_Write_Com(0x06); /*显示光标移动设置*/DelayMs(5);LCD_Write_Com(0x0C); /*显示开及光标设置*/}/* 1602.h *//*-----------------------------------------------名称:LCD1602.h引脚定义如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK ------------------------------------------------*/#ifndef __1602_H__#define __1602_H__bitLCD_Check_Busy(void) ;voidLCD_Write_Com(unsigned char com) ;voidLCD_Write_Data(unsigned char Data) ;voidLCD_Clear(void) ;voidLCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) ; voidLCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) ; voidLCD_Init(void) ;#endif/* delay.c*/#include "delay.h"/*------------------------------------------------uS延时函数,含有输入参数unsigned char t,无返回值unsigned char 是定义无符号字符变量,其值的范围是0~255 这里使用晶振12M,精确延时请使用汇编,大致延时长度如下T=tx2+5 uS------------------------------------------------*/void DelayUs2x(unsigned int t){while(--t);}/*------------------------------------------------mS延时函数,含有输入参数unsigned char t,无返回值unsigned char 是定义无符号字符变量,其值的范围是0~255 这里使用晶振12M,精确延时请使用汇编------------------------------------------------*/voidDelayMs(unsigned int t){while(t--){//大致延时1mSDelayUs2x(245);DelayUs2x(245);}}/* delay.h*/#ifndef __DELAY_H__#define __DELAY_H__/*------------------------------------------------uS延时函数,含有输入参数unsigned char t,无返回值unsigned char 是定义无符号字符变量,其值的范围是0~255 这里使用晶振12M,精确延时请使用汇编,大致延时长度如下T=tx2+5 uS------------------------------------------------*/void DelayUs2x(unsigned int t);/*------------------------------------------------mS延时函数,含有输入参数unsigned char t,无返回值unsigned char 是定义无符号字符变量,其值的范围是0~255 这里使用晶振12M,精确延时请使用汇编------------------------------------------------*/voidDelayMs(unsigned int t);#endif。
超声波测距1602显示
//add 为显示字符的位置,x 为显示的字符
{ // //
Write_com(0x01); delay(5); WriteAddress(0x00);
delay(5); Write_com(0x80+0x40+add); WriteData(0x30+x); }
LCD1602()//1602 初显示 { LcdInt(); //调用 LCD 初始化函数 delay(10); Write_com(0x01); //清屏 delay(5); WriteAddress(0x00); //设置显示位置为第一行第一个字,这条语句可以不用写, 因为默认是从第一行第一个字显示
limi=s3/1000; //厘米 s4=s3%1000; haomi=s4/100;//毫米
if(s1>400000) { Write_com(0x80+0x40+3); WriteData('e'); delay(5); WriteData('r'); delay(5); WriteData('r'); delay(5); WriteData('o'); delay(5); WriteData('r'); delay(5); }
/***************************************************** 以上为 1602 模块语句 ***************************************************/
display(unsigned char add,unsigned char x)
/***************************************************** 函数功能:延时 1ms ***************************************************/
HC-SR04超声波测距的51单片机程序
lcdrs=1;
P2=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void chushihua1602()
{
lcden=0;
write_order(0x38);
write_order(0x0c);
write_order(0x06);
write_order(0x01);
void zd3() interrupt 3//T1中断用来扫描数L1=0x30;
timer++;
if(timer>=400)
{
timer=0;
trig=1;//800MS启动一次模块
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
{
write_date(a[n]);
delay(3);
}
}
}
a[11]=(s%10000%1000/100)+0x30;
a[12]=(s%10000%1000%100/10)+0x30;
a[13]=(s%10000%1000%100%10)+0x30;
}
else//超出测量范围显示
{
flag=0;
a[9]='w';
a[10]='r';
a[11]='o';
a[12]='n';
write_date(a[0]);
write_date(a[1]);
write_date(a[2]);
write_date(a[3]);
超声波测距 51单片机 LCD12864显示
if(flag==0)
{
led =1;
delay(200); //测试灯变化
}
}
}
#include <reg52.h> //包括一个52标准内核的头文件
#define uchar unsigned char //定义一下方便使用
#define uint unsigned int
#define ulong unsigned long
#define LCD_data P0
uchar code dis3[]={"距离:"};
uchar code dis4[]={"单位:mm"};
uchar code dis5[]={"危险!!"};
uchar code dis6[]={"安全"};
void delay(uint z)
{ uchar i,j ;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void write_cmd(uchar cmd)
{
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
P0=cmd;
delay(1);
LCD_EN=1;
delay(1);
LCD_EN=0;
}
void write_dat(uchar dat)
timer1() interrupt 3 // 定时器0中断是1号
{
TH1=0;
TL1=0;
}
void display()
LCD1602显示超声波测试的距离
while(!P14);
TR0=1;
while(P14);
TR0=0;
m=TL0;
n=TH0;
time=n*256+m;
//if(time<=10600&&time>=115)
dis=(12/11.0592)*(time*0.034)/2;
}
else
{ */
LcdWriteCom(0xC0); //写地址 80表示初始地址
LcdWriteData('+'); //显示正
//tp=dis;//因为数据处理有小数点所以将温度赋给一个浮点型变量
//如果温度是正的那么,那么正数的原码就是补码它本身
datas[0] = dis / 100;
datas[1] = dis % 100/10;
datas[2] = dis % 10;
//datas[3] = dis % 100 / 10;
//datas[4] = dis % 10;
LcdWriteCom(0xc2); //写地址 80表示初始地址
LcdWriteCom(0xc3); //写地址 80表示初始地址
LcdWriteData('0'+datas[1]); //十位
LcdWriteCom(0xc4); //写地址 80表示初始地址
LcdWriteData('0'+datas[2]); //个位
void time0_init(void)
{
TMOD|=0x01;
TH0=0;
TL0=0;
超声波测距程序LCD液晶显示
超声波测距程序L C D液晶显示Hessen was revised in January 2021#include<>#include<>#define uint unsigned int#define uchar unsigned char#define NOP() {_nop_();_nop_();_nop_();_nop_();}//------LCD引脚-----sbit LCD_RS=P2^6;sbit LCD_RW=P2^5;sbit LCD_EN=P2^7;//------超声波引脚-------sbit Tx=P3^3; //触发控制信号输入Trigsbit Rx=P3^2; //回响信号输出 Echouchar code table[]={"Distance Test:"}; //LCD第一行显示uchar temp_dis[]= {" cm"}; //LCD第二行long int t,distance;uchar cache[4]={0,0,0,0};//--------延时-------void delay(uint ms){uint t;while(ms--)for(t=0;t<120;t++);}//-------读LCD状态-------uchar read_lcd_state(){uchar state;LCD_RS=0;LCD_RW=1;LCD_EN=1;_nop_();state=P0;LCD_EN=0;_nop_();return state;}//-------忙等待------void lcd_busy_wait(){while((read_lcd_state() & 0x80)==0x80);NOP();}//----------LCD写指令----------void lcd_write_com(uchar com){lcd_busy_wait();LCD_RS=0; //RS为0时,写指令,RS为1时,写数据LCD_RW=0;P0=com;NOP();LCD_EN=1;NOP();LCD_EN=0;}//----------LCD写数据----------void lcd_write_data(uchar dat){lcd_busy_wait();LCD_RS=1;LCD_RW=0;P0=dat;NOP();LCD_EN=1;NOP();LCD_EN=0;}//-------LCD初始化-------void lcd_init(){LCD_EN=0;lcd_write_com(0x38); //LCD显示模式设置lcd_write_com(0x0c); //LCD显示开/关及光标设置lcd_write_com(0x06); //当写一个字符后地址指针加1,且光标加1lcd_write_com(0x01); //显示清屏}//---------设置液晶显示位置-----------void set_lcd_pos(uchar p){lcd_write_com(p|0x80);}//---------液晶显示程序----------void lcd_print(uchar p,uchar *s,uint low){uint num;set_lcd_pos(p);for(num=0;num<low;num++){lcd_write_data(s[num]);delay(1);}}void HC05_Init(){Tx=1; //触发脉冲NOP();NOP();NOP();NOP();Tx=0;distance=*t; //距离计算}void distance_convert(long int dat){cache[0]=dat/1000;cache[1]=dat/100%10;cache[2]=dat/10%10;cache[3]=dat%10;temp_dis[0]=cache[0]+'0';temp_dis[1]=cache[1]+'0';temp_dis[2]=cache[2]+'0';temp_dis[4]=cache[3]+'0';}//------------主程序-----------void main(){lcd_init();delay(5);TMOD=0x19;EA=1; //开总中断TR0=1; //启动定时器EX0=1; //开外部中断IT0=1; //设置为下降沿中断方式while(1){HC05_Init();distance_convert(distance);lcd_print(0x01,table,14);lcd_print(0x44,temp_dis,8);}}//外部中断0void int0() interrupt 0{t=(TH0*256+TL0); //计算高电平持续的时间,上升沿到来时候开始计时,下降沿到来进入外部中断,关闭计时器,停止计时TH0=0;TL0=0;}。
超声波测距LCD1602显示
超声波测距LCD1602显示超声波测距模块:1、采用IO口TRIG触发测距,给至少10us的高电平信号;2、模块自动发送8个40khz的方波,自动检测是否有信号返回;3、有信号返回,通过IO口ECHO输出一个高电平,高电平持续的时间就是超声波从发射到返回的时间。
测试距离=(高电平时间*声速(340M/S))/2。
液晶LCD1602电路接口超声波测距LCD1602显示程序源代码/*******************超声波测距1602显示********************* 单片机:51单片机* 开发环境:keil* 名称:超声波测距1602显示注意:用杜邦线将超声波模块的VCC接开发板5V对外供电接口用杜邦线将超声波模块的GND接开发板GND用杜邦线将超声波模块的Trig接单片机的P1.5用杜邦线将超声波模块的Echo接单片机的P1.6/************************包含头文件************************/#include <reg51.h>#define LCD_Data P0#define Busy 0x80/**************************宏定义**************************/#define SPEED_30C 3495 //30摄氏度时的声速,声速V=331.5+0.6*温度;#define SPEED_23C 3453 //23摄氏度时的声速,声速V=331.5+0.6*温度;/**************************位定义**************************/sbit ECHO = P2^1; //回声接收端口sbit TRIG = P2^0; //超声触发端口sbit BEEP = P2^3; //蜂鸣器sbit LCD_RS = P1^0;sbit LCD_RW = P1^1;sbit LCD_E = P2^5;/**********************定义变量和数组**********************/long int distance = 0; //距离变量unsigned char code table0[] = {" SL-51A "};unsigned char code table1[] = {" NO ECHO "};unsigned char code table2[] = {"Distance:xxx.xcm"};unsigned char count;void Delay5Ms(void);void delay(int In,int Out);void WriteDataLCD(unsigned char WDLCD);void WriteCommandLCD(unsigned char WCLCD,BuysC);unsigned char ReadDataLCD(void);unsigned char ReadStatusLCD(void);void LCDInit(void);void DisplayOneChar(unsigned char X,unsigned char Y,unsigned char DData);void DisplayListChar(unsigned char X,unsigned char Y,unsigned char code *DData); /**********************************************************//* 函数名称 : Delay_xMs *//* 函数描述 : 延时函数 *//* 输入参数 : x *//* 参数描述 : 延时时间 *//* 返回值 : 无 *//**********************************************************/void Delay_xMs(unsigned int x){unsigned int i,j;for(i=0;i<x;i++){for(j=0;j<3;j++){;}}}/**********************************************************//* 函数名称 : delayt *//* 函数描述 : 延时函数 *//* 输入参数 : x *//* 参数描述 : 延时时间数据 *//* 返回值 : 无 *//**********************************************************/void delayt(unsigned int x){unsigned char j;while(x-->0){for(j=0;j<125;j++){;}}}/*************************5ms延时函数**********************/void Delay5Ms(void){unsigned int TempCyc = 3552;while(TempCyc--);}/**************************延迟函数************************/void delay(int In,int Out) {int i,j;for(i=0;i<In;i++){for(j=0;j<Out;j++){;}}}/**********************************************************//* 函数名称 : Alarm *//* 函数描述 : 蜂鸣器发声函数 *//* 输入参数 : t *//* 参数描述 : 发声的次数 *//* 返回值 : 无 *//**********************************************************/void Alarm(unsigned char t){unsigned char i;for(i=0;i<t;i++){BEEP=0;delay(10,1000);BEEP=1;delay(10,1000);}}/**********************************************************//* 函数名称 : Init_timer *//* 函数描述 : 初始化单片机函数 *//* 输入参数 : 无 *//* 参数描述 : 无 *//* 返回值 : 无 *//**********************************************************/void Init_timer(void){TMOD=0x01; //定时器2初始化,设置为16位自动重装模式TL0=0x66;TH0=0xfc; //1msET0=1; //开定时器2EA=1; //总中断使能}/**********************************************************//* 函数名称 : Init_Parameter *//* 函数描述 : 初始化参数和IO口函数 *//* 输入参数 : 无 *//* 参数描述 : 无 *//* 返回值 : 无 *//**********************************************************/void Init_Parameter(void){TRIG=1;ECHO=1;count=0;distance=0;}/**********************************************************//* 函数名称 : display *//* 函数描述 : 显示数字 *//* 输入参数 : number,address *//* 参数描述 : number写入的数据 *//* 返回值 : 无 *//**********************************************************/ void display(int number){unsigned char b,c,d,e;b=(number/1000);c=(number/100)%10;d=(number/10)%10;e=number%10; DisplayOneChar(9,1,(0x30+b));DisplayOneChar(10,1,(0x30+c));DisplayOneChar(11,1,(0x30+d));DisplayOneChar(13,1,(0x30+e));}/**********************************************************//* 函数名称 : Trig_SuperSonic *//* 函数描述 : 发出声波函数 *//* 输入参数 : 无 *//* 参数描述 : 无 *//* 返回值 : 无 *//**********************************************************/ void Trig_SuperSonic(void) //出发声波{TRIG=1;delayt(1);TRIG=0;}/**********************************************************//* 函数名称 : Measure_Distance *//* 函数描述 : 计算距离函数 *//* 输入参数 : 无 *//* 参数描述 : 无 *//* 返回值 : 无 *//**********************************************************/ void Measure_Distance(void){unsigned char l;unsigned int h,y;TR0=1;while(ECHO){;}TR0=0;l=TL0;h=TH0;y=(h<<8)+l;y=y-0xfc66; //us部分distance=y+1000*count; //计算总时间TL0=0x66;TH0=0xfc;delayt(30);distance=SPEED_30C*distance/20000;}/*********************写数据函数***************************/void WriteDataLCD(unsigned char WDLCD){ReadStatusLCD(); //检测忙LCD_Data=WDLCD;LCD_E=0; //若晶振速度太高可以在这后加小的延时LCD_E=0; //延时LCD_RS=1;LCD_RW=0;LCD_E=1;LCD_E=0;}/***********************写指令函数*************************/void WriteCommandLCD(unsigned char WCLCD,BuysC) //BuysC为0时忽略忙检测{if (BuysC) ReadStatusLCD(); //根据需要检测忙LCD_Data=WCLCD;LCD_E=0;LCD_E=0;LCD_RS=0;LCD_RW=0;LCD_E=1;LCD_E=0;}/************************读数据函数************************/unsigned char ReadDataLCD(void){LCD_RS=1;LCD_RW=1;LCD_E=0;LCD_E=0;LCD_E=1;return(LCD_Data);}/************************读状态函数************************/unsigned char ReadStatusLCD(void){LCD_Data=0xFF;LCD_RS=0;LCD_RW=1;LCD_E=0;LCD_E=0;LCD_E=1;while (LCD_Data & Busy); //检测忙信号return(LCD_Data);}/************************LCD初始化*************************/void LCDInit(void) //LCD初始化{LCD_Data=0;WriteCommandLCD(0x38,0);Delay5Ms(); //三次显示模式设置,不检测忙信号WriteCommandLCD(0x38,0);Delay5Ms();WriteCommandLCD(0x38,0);Delay5Ms();WriteCommandLCD(0x38,1); //显示模式设置, 开始要求每次检测忙信号 WriteCommandLCD(0x08,1); //关闭显示WriteCommandLCD(0x01,1); //显示清屏WriteCommandLCD(0x06,1); //显示光标移动设置WriteCommandLCD(0x0C,1); //显示开及光标设置}/***********************清屏函数***************************/void LCD_Clear(void){WriteCommandLCD(0x01,1);Delay5Ms();}/*******************按指定位置显示一个字符******************/void DisplayOneChar(unsigned char X,unsigned char Y,unsigned char DData){Y&=0x1;X&=0xF; //限制X不能大于15,Y不能大于1if(Y)X|=0x40; //当要显示第二行时地址码+0x40;X|=0x80; //算出指令码WriteCommandLCD(X, 0); //这里不检测忙信号,发送地址码WriteDataLCD(DData);}/********************按指定位置显示一串字符*****************/void DisplayListChar(unsigned char X,unsigned char Y,unsigned char code *DData) {unsigned char ListLength;ListLength=0;Y&=0x1;X&=0xF; //限制X不能大于15,Y不能大于1 while (DData[ListLength]>=0x20) //若到达字串尾则退出{if(X<=0xF) //X坐标应小于0xF{DisplayOneChar(X, Y, DData[ListLength]); //显示单个字符ListLength++;X++;}}}/**********************************************************//* 函数名称 : main *//* 函数描述 : 主函数 *//* 输入参数 : 无 *//* 参数描述 : 无 *//* 返回值 : 无 *//**********************************************************/void main(void){LCDInit();Init_timer();Init_Parameter();Alarm(2);DisplayListChar(0,0,table0);DisplayListChar(0,1,table1);while(1){Trig_SuperSonic(); //触发超声波发射while(ECHO==0){;} //等待回声Measure_Distance(); //计算脉宽并转换为距离DisplayListChar(0,1,table2);display(distance); //显示距离Init_Parameter(); //参数重新初始化delayt(100); //延时,两次发射之间要至少有10ms间隔 }}/**********************************************************//* 函数名称 : timer0 *//* 函数描述 : T0中断处理函数 *//* 输入参数 : 无 *//* 参数描述 : 无 *//* 返回值 : 无 *//**********************************************************/void timer0 (void) interrupt 1{TF0=0;TL0=0x66;TH0=0xfc;count++;if(count==18) //超声波回声脉宽最多18ms{TR0=0;TL0=0x66;TH0=0xfc;count=0;}}。
超声波LCD显示测距程序
/***************************************************************************** */ /*Name:LCD12864(St7920/St7921)+超声波测距模块+STC89C52*//*DYP-ME007超声波测距模块DEMO程序*//*晶振:11.0592M*/*//*接线:模块TRIG接P1.2ECH0接P1.1 *//*RS(CS)接P3.3; *//*RW(SID)接P3.4; *//*E(SCLK)接P3.5; *//*PSB接GND串行模式*//***************************************************************************** */#include<AT89X51.H>#include<intrins.h>//引脚定义sbitRX=P1^1;sbitTX=P1^2;sbitCS=P3^3; //片选高电平有效单片LCD使用时可固定高电平sbitSID=P3^4; //数据sbitSCLK=P3^5; //时钟//FunctionDefinition函数声明voidDelay(intnum);voidInit_DS18B20(void);unsignedcharReadOneChar(void);voidWriteOneChar(unsignedchardat);unsignedintReadTemperature(void);voidclock_out(unsignedchardd);unsignedcharclock_in(void);unsignedcharread_clock(unsignedcharord);voidwrite_clock(unsignedcharord,unsignedchardd);voidDisp(void);voidid_case1_key(void);voidid_case2_key(void);voidSet_time(unsignedcharsel,bitsel_1);voidTimer0_Init(void);voidInit_1302(void);voidSet_Bell(unsignedcharsel,bitsel_1);voidBell(void);//12864voidWrite_char(bitstart,unsignedcharddata);voidSend_byte(unsignedcharbbyte);voidDelaynms(unsignedintdi);voidLcd_init(void);voidDisp_img(unsignedchar*img);voidLCD_Write_string(unsignedcharX,unsignedcharY,unsignedchar*s);voidLCD_set_xy(unsignedcharx,unsignedchary);unsignedcharcode:.-"};unsignedcharcodewaves[]={"超声波测距系统"};unsignedcharcodeznwk[]={"智能微控工作室"};unsignedcharcodeCM[]={"M"};unsignedinttime=0;longS=0;bitflag=0;unsignedchardisbuff[4] ={0,0,0,0,};unsignedcharcodelogo[]={/*--调入了一幅图像:logo.bmp--*//*--宽度x高度=128x64欢迎使用本产品LOGO--*//*--调入了一幅图像:C:\DocumentsandSettings\lwd\桌面\新建文件夹\X.bmp--*//*--宽度x高度=128x64--*/0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xE0,0x00,0x00,0x0C,0x00,0x00,0x40,0x70,0x00,0x00,0x00,0x80,0x00, 0x00,0x00,0x00,0xE0,0x00,0x00,0x1E,0x00,0x00,0x60,0x70,0x00,0x00,0x0F,0xE0,0x00, 0x00,0x00,0x01,0xC0,0x00,0x60,0x3C,0x00,0x00,0xE0,0x60,0x00,0x1F,0xFF,0xE0,0x00, 0x00,0x00,0x03,0x80,0x00,0x70,0x70,0x40,0x00,0xE0,0x7E,0x00,0x1F,0xE0,0xC0,0x00, 0x00,0x00,0x03,0x00,0x00,0x73,0xC3,0xF0,0x01,0xC3,0xFC,0x00,0x38,0xE1,0xC0,0x00, 0x00,0x03,0xC6,0x3C,0x00,0x33,0x9F,0xF0,0x03,0x8F,0xC0,0x00,0x30,0xC1,0xC0,0x00, 0x00,0x3F,0xEF,0xFE,0x00,0x03,0x1C,0xE0,0x07,0x01,0xCC,0x00,0x31,0xF1,0x80,0x00, 0x00,0x79,0xDF,0xBC,0x00,0x06,0x18,0xC0,0x0E,0x01,0xFE,0x00,0x77,0xF9,0x80,0x00, 0x00,0x01,0xB0,0x30,0x01,0xC6,0x19,0xC0,0x1E,0x7F,0xDE,0x00,0x6F,0x83,0x80,0x00, 0x00,0x23,0xAC,0x60,0x0F,0xE6,0xF1,0x80,0x3E,0x7B,0x38,0x00,0x61,0x83,0x00,0x00, 0x00,0x37,0x0E,0x00,0x0F,0xC7,0xB7,0x80,0xEC,0x63,0x30,0x00,0xE3,0x03,0x00,0x00, 0x00,0x3E,0x0E,0x00,0x01,0x8F,0x37,0x01,0xCC,0x6F,0xF0,0x00,0xC7,0xF7,0x00,0x00, 0x00,0x1E,0x0C,0x00,0x03,0x1E,0x66,0x03,0x0C,0x7F,0x00,0x01,0xFF,0xE6,0x00,0x00, 0x00,0x1E,0x1C,0x00,0x03,0x1C,0x60,0x02,0x19,0xAC,0x00,0x01,0x96,0x06,0x00,0x00, 0x00,0x3E,0x3E,0x00,0x03,0x18,0x60,0x00,0x19,0xFC,0x00,0x03,0x86,0x0E,0x00,0x00, 0x00,0x7E,0x36,0x00,0x03,0x00,0xC0,0x00,0x38,0x78,0x00,0x03,0x06,0x0C,0x00,0x00, 0x00,0xE6,0x67,0x00,0x03,0x00,0xC0,0x00,0x30,0x78,0x00,0x06,0x0C,0x0C,0x00,0x00, 0x01,0xC6,0xC3,0x80,0x7F,0xF0,0xC0,0x00,0x30,0xFE,0x00,0x0E,0x0C,0x1C,0x00,0x00, 0x03,0x81,0x83,0xC0,0x78,0xFF,0x80,0x00,0x61,0xCF,0x80,0x1C,0x08,0x18,0x00,0x00, 0x06,0x07,0x03,0xE0,0x00,0x1F,0xFF,0xE0,0x67,0x07,0xE0,0x38,0x18,0xF8,0x00,0x00, 0x08,0x0E,0x01,0xF8,0x00,0x07,0xFF,0x80,0xCE,0x03,0xF8,0x60,0x00,0x70,0x00,0x00, 0x00,0x18,0x01,0xF8,0x00,0x01,0xFC,0x00,0xC0,0x01,0xFC,0xC0,0x00,0x70,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x00,0x0C,0xE0,0x00,0x01,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x3F,0xE0,0x00,0xFF,0xF0,0x00,0x38,0x00,0xE0,0x00,0x00,0x00,0x0C,0x00,0x00,0x0F,0xFF,0x80,0x00,0xF8,0xF0,0x00,0x38,0x00,0xE0,0x00,0x00,0x00,0x0C,0xF0,0x00,0x0F,0xC7,0x00,0x00,0x60,0xE0,0x00,0x38,0x00,0xE0,0x00,0x00,0x00,0x1F,0xF0,0x00,0x01,0x8E,0x00,0x00,0x60,0xC0,0x00,0x70,0x01,0xC0,0x00,0x00,0x07,0xFF,0x80,0x00,0x01,0x8C,0x00,0x00,0xE1,0x80,0x00,0x70,0x01,0xC0,0x00,0x00,0x07,0xF8,0x00,0x00,0x01,0x98,0x00,0x00,0xDF,0xC0,0x00,0x70,0x01,0xC0,0x00,0x00,0x00,0x78,0x00,0x00,0x01,0xBF,0xE0,0x00,0xFF,0x00,0x00,0x60,0x01,0x80,0x00,0x00,0x00,0xFC,0x00,0x00,0x7F,0xFF,0xE0,0x00,0xC0,0x00,0x00,0xE0,0x03,0x80,0x00,0x00,0x01,0xFE,0x00,0x00,0x7F,0xF8,0x00,0x00,0x00,0x00,0x00,0xC0,0x03,0x00,0x00,0x00,0x03,0xB7,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x3C,0x00,0xC0,0x03,0x00,0x00,0x00,0x07,0x67,0x00,0x00,0x60,0x00,0x00,0x03,0x9F,0xFE,0x00,0x80,0x02,0x00,0x00,0x00,0x0E,0x63,0x80,0x00,0xE0,0x00,0x01,0xFF,0xCF,0x1E,0x01,0x80,0x06,0x00,0x00,0x00,0x1C,0xE1,0xE0,0x00,0xC0,0x00,0x00,0xE3,0x9C,0x38,0x01,0x80,0x06,0x00,0x00,0x00,0x38,0xC1,0xF8,0x01,0xC0,0x00,0x00,0xC3,0x18,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0xFC,0xFE,0x03,0x80,0x00,0x01,0xC7,0x18,0x60,0x00,0x00,0x00,0x00,0x00,0x01,0xDF,0xF8,0xFE,0x03,0x00,0x00,0x01,0x8E,0x1B,0xE0,0x07,0x00,0x1C,0x00,0x00,0x07,0x07,0x80,0x00,0x07,0x00,0x00,0x01,0xFF,0x3F,0xE0,0x07,0x00,0x1C,0x00,0x00,0x0C,0x03,0x80,0x00,0x0E,0x00,0x00,0x01,0xF0,0x38,0x00,0x0F,0x00,0x3C,0x00,0x00,0x00,0x03,0x00,0x00,0x1C,0x00,0x00,0x01,0x00,0x20,0x00,0x06,0x00,0x18,0x00,0x00,0x00,0x03,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};/***************************************************************************** */voidLcd_init(void) //初始化LCD{Delaynms(10);//启动等待,等LCM讲入工作状态CS=1;Write_char(0,0x30);//8位介面,基本指令集Write_char(0,0x0c);//显示打开,光标关,反白关Write_char(0,0x01);//清屏,将DDRAM的地址计数器归零}/***************************************************************************** */voidWrite_char(bitstart,unsignedcharddata)//写指令或数据{unsignedcharstart_data,Hdata,Ldata;if(start==0)start_data=0xf8; //写指令elsestart_data=0xfa;//写数据Hdata=ddata&0xf0; //取高四位Ldata=(ddata<<4)&0xf0;//取低四位Send_byte(start_data); //发送起始信号Delaynms(5);//延时是必须的Send_byte(Hdata); //发送高四位Delaynms(1);//延时是必须的Send_byte(Ldata); //发送低四位Delaynms(1);//延时是必须的}/***************************************************************************** */voidSend_byte(unsignedcharbbyte)//发送一个字节{unsignedchari;for(i=0;i<8;i++){SID=bbyte&0x80;//取出最高位SCLK=1;SCLK=0;bbyte<<=1;//左移}}/***************************************************************************** */voidDelaynms(unsignedintdi)//延时{unsignedintda,db;for(da=0;da<di;da++)for(db=0;db<10;db++);}/***************************************************************************** */voidDisp_img(unsignedchar*img) //图形方式12864显示字模221横向取膜{unsignedchari,j;unsignedintk=0;Write_char(0,0x36);//图形方式for(i=0;i<32;i++){Write_char(0,0x80+i);Write_char(0,0x80);for(j=0;j<16;j++){Write_char(1,img[k++]);}}for(i=0;i<32;i++){Write_char(0,0x80+i);Write_char(0,0x88);for(j=0;j<16;j++){Write_char(1,img[k++]);}}}/***************************************************************************** */voidClr_Scr(void)//清屏函数{Write_char(0,0x01);}/***************************************************************************** */voidLCD_set_xy(unsignedcharx,unsignedchary){ //设置LCD显示的起始位置,X为行,Y为列unsignedcharaddress;switch(x){case0:address=0x80+y;break;case1:address=0x80+y;break;case2:address=0x90+y;break;case3:address=0x88+y;break;case4:address=0x98+y;break;default:address=0x80+y;break;}Write_char(0,address);}/***************************************************************************** */voidLCD_Write_string(unsignedcharX,unsignedcharY,unsignedchar*s){ // 中英文字符串显示函数LCD_set_xy(X,Y);while(*s){Write_char(1,*s);s++;Delaynms(1);}}/***************************************************************************** */voidLCD_Write_number(unsignedchars)// 数字显示函数{Write_char(1,num[s]);Delaynms(1);}/***************************************************************************** */voidLcd_Mark2(void){Clr_Scr();//清屏LCD_Write_string(1,0,znwk);//LCD_Write_string(2,0,waves);//LCD_Write_string(3,7,CM);//}/********************************************************/voidConut(void){time=TH0*256+TL0;TH0=0;TL0=0;S=time*1.87/100;//算出来是CM 11。
超声波测距—1602显示
#include <reg52.h>#include <intrins.h>#define uchar unsigned char#define uint unsigned intsbit ech0=P3^2;sbit trig=P1^4;sbit rs=P1^2;sbit rw=P1^1;sbit en=P1^0;unsigned int time,timeH,timeL,distance;unsigned char succeed_flag;/**************************延时函数*******************************/ void delay(unsigned int z){unsigned char x,y;for(x=z;x>0;x--)for(y=110;y>0;y--); //延时1ms}/*************************延时20us***********************************/ void delay_20us(){unsigned char a;for(a=0;a<10;a++);}/**************液晶1602显示程序**************************/void write_com(unsigned char com) //写命令{rs=0;rw=0;P2=com;delay(5);en=1;delay(5);en=0;}void write_data(unsigned char date) //写数据{rs=1;rw=0;P2=date;delay(5);en=1;delay(5);en=0;}/************************液晶字符输入**************************/ void zifu_1602(unsigned char *str) //写字符{while(*str!='\0'){write_data(*str);str++;}}/**************************数字输入*****************************/ void shuzi_1602(unsigned char dat) //写数字{unsigned char dat1;dat1=dat+'0';write_data(dat1);}/************************液晶输入的位置*************************/ void weizhi_1602(unsigned char x,unsigned char y){if(x==1){write_com(0x80+y); //第一行}if(x==2){write_com(0x80+0x40+y); //第二行}}/***************************液晶显示函数***************************/ void display(unsigned int k){unsigned char aa,bb,cc;aa=distance / 100;bb=distance / 10%10;cc=distance % 10;weizhi_1602(1,4);shuzi_1602(aa);weizhi_1602(1,5);shuzi_1602(bb);weizhi_1602(1,6);shuzi_1602(cc);}/****************************液晶初始化******************************/ void init(){rw=0;en=0;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);write_com(0x80);weizhi_1602(1,2);zifu_1602("S");weizhi_1602(1,3);zifu_1602("=");weizhi_1602(1,7);zifu_1602("c");weizhi_1602(1,8);zifu_1602("m");}void main(){init();trig=0;EA=1;TMOD=0X10; //定时器1,16位工作方式while(1){EA=0;trig=1; //触发控制信号delay_20us();trig=0; //产生一个延时为20us的波while(ech0==0); //等待echo回波引脚边为高电平succeed_flag=0; //清测量成功标志EA=1;EX0=1; //开外部中断0TH1=0;TL1=0; //定时器1清0TF1=0; //计数溢出标志TR1=1; //启动定时器1delay(20); //等待测量结果TR1=0; //关闭定时器1EX0=0; //关闭外部中断0if(succeed_flag==1){time=timeH*256+timeL;distance=time*1.87/100; //cmdisplay(distance);}if(succeed_flag==0) //无回波,清0{distance=0;}}}void exter() interrupt 0 //外部中断0,判断回波电平{timeH=TH1;timeL=TL1; //取出定时器值succeed_flag=1; //成功测量的标志EX0=0; //关闭外部中断}void timer1() interrupt 3 //定时器1中断,用做超声波测距计时{TH1=0;TL1=0;}。
超声波测距程序流程
超声波测距程序流程下载温馨提示:该文档是我店铺精心编制而成,希望大家下载以后,能够帮助大家解决实际的问题。
文档下载后可定制随意修改,请根据实际需要进行相应的调整和使用,谢谢!并且,本店铺为大家提供各种各样类型的实用资料,如教育随笔、日记赏析、句子摘抄、古诗大全、经典美文、话题作文、工作总结、词语解析、文案摘录、其他资料等等,如想了解不同资料格式和写法,敬请关注!Download tips: This document is carefully compiled by theeditor. I hope that after you download them,they can help yousolve practical problems. The document can be customized andmodified after downloading,please adjust and use it according toactual needs, thank you!In addition, our shop provides you with various types ofpractical materials,such as educational essays, diaryappreciation,sentence excerpts,ancient poems,classic articles,topic composition,work summary,word parsing,copy excerpts,other materials and so on,want to know different data formats andwriting methods,please pay attention!1. 初始化初始化超声波传感器,设置相关参数,如触发引脚、接收引脚等。
基于单片机的超声波测距程序
LCD程序#include <reg51.h>#include <intrins.h>#define uchar unsigned char #define uint unsigned int sbit lcdrs=P2^0; //定义LCD引脚sbit lcdrw=P2^1;sbit lcden=P2^2;void writelcdcmd(uchar); void writelcddat(uchar);void delayms(uint z)//不精确延时{uchar x;uint y;for(x=z;x>0;x--)for(y=125;y>0;y--);}void LCD_BUSY()//LCD忙检测{ uchar sta;P0=0xff;lcdrs=0;lcdrw=1;do{lcden=1;sta=P0;lcden=0;}while(sta & 0x80);}void writelcdcmd(uchar cmd)//LCD写命令{LCD_BUSY();delayms(1);lcdrs=0;lcdrw=0;lcden=0;P0=cmd;delayms(1);lcden=1;delayms(1);lcden=0;}void writelcddat(uchar dat)//LCD写数据{LCD_BUSY();delayms(1);lcdrs=1;lcdrw=0;lcden=0;P0=dat;delayms(1);lcden=1;delayms(1);lcden=0;}void inilcd() //LCD初始化{ delayms(15);writelcdcmd(0x38);delayms(5);writelcdcmd(0x0c);delayms(5);writelcdcmd(0x06);delayms(5);writelcdcmd(0x01);delayms(5);}void play(unsigned char *p)//显示//{while(*p!='\0'){writelcddat(*p);p++;delayms(1);}}超声波模块程序#include <reg52.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P1^0; //定义蜂鸣器引脚sbit Trig=P3^5;//定义HC-SR04两个引脚sbit Echo=P3^6 ;sbit ADD=P2^5;//定义加SV按键引脚sbit DUC=P2^4;//定义减SV按键引脚sbit CONTINOUS=P2^6;//定义连续加减SV引脚uint SV=3000;uint value1;float SS,CLOCKCYCLE;uchar code str[]={"PV:"};uchar code str1[]={"cm"};extern voidwritelcdcmd(uchar);extern voidwritelcddat(uchar);extern void play(uchar *p);extern void inilcd();void delayus(uint i) //微秒级延时{while(i--);}void initsr04()//HC-SR04初始化{TMOD=0x11;TH0=0;TL0=0;EA=1;TR0=0;CLOCKCYCLE=12/11. 0592;SS=332+0.607*25;Trig=0;Echo=0;}float mesuringdistance()//测距{TH0=0;TL0=0;Trig=1;delayus(6);//拉高Trig 至少20usTrig=0;while(!Echo);TR0=1;while(Echo);TR0=0;value1=(SS*CLOCKC YCLE*((float)TH0*256+(flo at)TL0))/2/1000;return(value1);}void displaysr04()//1602显示距离{ uchar disdat1[9];ADD=DUC=1;if(ADD==0)//按下一次SV加1{SV++;while(!ADD);}if(DUC==0)//按下一次SV减1{SV=SV-1;while(!DUC);}if(CONTINOUS==0){if(ADD==0)//按下一次SV加1{SV++;}if(DUC==0)//按下一次SV减1{SV--;}}/*******************************************//*************当前值显示******************************/disdat1[0]=value1/1000+0x30;disdat1[1]=value1%1000/100+0x30;disdat1[2]=value1%100/10+0x30;disdat1[3]=value1%10+0x30;writelcdcmd(0x80);play(str);if(disdat1[0]==0x30){disdat1[0]=0x20;if(disdat1[1]==0x30)disdat1[1]=0x20;}writelcdcmd(0x83);writelcddat(disdat1[0]);writelcdcmd(0x84);writelcddat(disdat1[1]);writelcdcmd(0x85);writelcddat(disdat1[2]);writelcdcmd(0x86);writelcddat(0x2e);writelcdcmd(0x87);writelcddat(disdat1[3]);writelcdcmd(0x88);play(str1);/*******************************************************************//***********设定值显示***********************/disdat1[4]=SV/1000+0x30;disdat1[5]=SV%1000/100+0x30;disdat1[6]=SV%100/10+0x30;disdat1[7]=SV%10+0x30;if(disdat1[4]==0x30){disdat1[4]=0x20;if(disdat1[5]==0x30)disdat1[5]=0x20;}writelcdcmd(0xc0);writelcddat('S');writelcdcmd(0xc1);writelcddat('v');writelcdcmd(0xc2);writelcddat(':');writelcdcmd(0xc3);writelcddat(disdat1[4]);writelcdcmd(0xc4);writelcddat(disdat1[5]);writelcdcmd(0xc5);writelcddat(disdat1[6]);writelcdcmd(0xc6);writelcddat(0x2e);writelcdcmd(0xc7);writelcddat(disdat1[7]);writelcdcmd(0xc8);play(str1);}void SOUND(){if(SV>value1)BEEP=0;elseBEEP=1;}主程序#include<reg52.h>sbit BEEP=P2^3;#define uint unsigned int#define uchar unsigned char extern void writelcdcmd(uchar);extern void writelcddat(uchar);extern void play(uchar *p); extern void inilcd();extern void initsr04(); extern float mesuringdistance();extern void delayms(uint); extern void displaysr04(); extern void SOUND();void main(){delayms(2000);inilcd();initsr04();while(1){mesuringdistance();displaysr04();SOUND();delayms(65000);}}。
超声波测距流程
超声波测距流程下载温馨提示:该文档是我店铺精心编制而成,希望大家下载以后,能够帮助大家解决实际的问题。
文档下载后可定制随意修改,请根据实际需要进行相应的调整和使用,谢谢!并且,本店铺为大家提供各种各样类型的实用资料,如教育随笔、日记赏析、句子摘抄、古诗大全、经典美文、话题作文、工作总结、词语解析、文案摘录、其他资料等等,如想了解不同资料格式和写法,敬请关注!Download tips: This document is carefully compiled by theeditor. I hope that after you download them,they can help yousolve practical problems. The document can be customized andmodified after downloading,please adjust and use it according toactual needs, thank you!In addition, our shop provides you with various types ofpractical materials,such as educational essays, diaryappreciation,sentence excerpts,ancient poems,classic articles,topic composition,work summary,word parsing,copy excerpts,other materials and so on,want to know different data formats andwriting methods,please pay attention!超声波测距流程超声波测距流程概述如下:超声波测距流程主要包括以下几个步骤:一、超声波发射1.1 超声波发射器产生超声波1.2 超声波通过发射器传播二、超声波接收2.1 超声波遇到障碍物后产生回波2.2 回波被接收器接收三、信号处理3.1 接收到的信号经过放大和滤波处理3.2 计算出回波传播时间四、距离计算4.1 根据声速计算出距离4.2 距离计算结果输出五、数据处理5.1 对距离结果进行平滑处理5.2 判断距离是否在合理范围内六、显示输出6.1 将距离结果显示在显示屏上6.2 根据距离结果进行相应控制总结:超声波测距流程涵盖了从超声波发射、接收、信号处理、距离计算、数据处理到显示输出的完整过程,通过精确计算距离,实现了对障碍物距离的准确测量。
超声波LCD显示测距程序
超声波L C D显示测距程序文件管理序列号:[K8UY-K9IO69-O6M243-OL889-F88688]/************************************************************** ****************/ /*Name:LCD12864(St7920/St7921)+超声波测距模块+STC89C52*//*DYP-ME007超声波测距模块DEMO程序*//*晶振:11.0592M*/*//*接线:模块TRIG接P1.2ECH0接P1.1*//*RS(CS)接P3.3;*//*RW(SID)接P3.4;*//*E(SCLK)接P3.5;*//*PSB接GND串行模式*//************************************************************** ****************/#include<AT89X51.H>#include<intrins.h>//引脚定义sbitRX=P1^1;sbitTX=P1^2;sbitCS=P3^3; //片选高电平有效单片LCD使用时可固定高电平sbitSID=P3^4; //数据sbitSCLK=P3^5; //时钟//FunctionDefinition 函数声明voidDelay(intnum);voidInit_DS18B20(void);unsignedcharReadOneChar(void);voidWriteOneChar(unsignedchardat); unsignedintReadTemperature(void);voidclock_out(unsignedchardd);unsignedcharclock_in(void);unsignedcharread_clock(unsignedcharord);voidwrite_clock(unsignedcharord,unsignedchardd);voidDisp(void);voidid_case1_key(void);voidid_case2_key(void);voidSet_time(unsignedcharsel,bitsel_1);voidTimer0_Init(void);voidInit_1302(void);voidSet_Bell(unsignedcharsel,bitsel_1);voidBell(void);//12864voidWrite_char(bitstart,unsignedcharddata);voidSend_byte(unsignedcharbbyte);voidDelaynms(unsignedintdi);voidLcd_init(void);voidDisp_img(unsignedchar*img);voidLCD_Write_string(unsignedcharX,unsignedcharY,unsignedchar*s );voidLCD_set_xy(unsignedcharx,unsignedchary); unsignedcharcode:.-"};unsignedcharcodewaves[]={"超声波测距系统"}; unsignedcharcodeznwk[]={"智能微控工作室"}; unsignedcharcodeCM[]={"M"};unsignedinttime=0;longS=0;bitflag=0;unsignedchardisbuff[4] ={0,0,0,0,};unsignedcharcodelogo[]={/*--调入了一幅图像:logo.bmp--*//*--宽度x高度=128x64欢迎使用本产品LOGO--*//*--调入了一幅图像:C:\DocumentsandSettings\lwd\桌面\新建文件夹\X.bmp--*//*--宽度x高度=128x64--*/0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x0C,0x00,0x00,0x40,0x70,0x00,0x0 0,0x00,0x80,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x1E,0x00,0x00,0x60,0x70,0x00,0x0 0,0x0F,0xE0,0x00,0x00,0x00,0x01,0xC0,0x00,0x60,0x3C,0x00,0x00,0xE0,0x60,0x00,0x1 F,0xFF,0xE0,0x00,0x00,0x00,0x03,0x80,0x00,0x70,0x70,0x40,0x00,0xE0,0x7E,0x00,0x1 F,0xE0,0xC0,0x00,0x00,0x00,0x03,0x00,0x00,0x73,0xC3,0xF0,0x01,0xC3,0xFC,0x00,0x3 8,0xE1,0xC0,0x00,0x00,0x03,0xC6,0x3C,0x00,0x33,0x9F,0xF0,0x03,0x8F,0xC0,0x00,0x3 0,0xC1,0xC0,0x00,0x00,0x3F,0xEF,0xFE,0x00,0x03,0x1C,0xE0,0x07,0x01,0xCC,0x00,0x3 1,0xF1,0x80,0x00,0x00,0x79,0xDF,0xBC,0x00,0x06,0x18,0xC0,0x0E,0x01,0xFE,0x00,0x7 7,0xF9,0x80,0x00,0x00,0x01,0xB0,0x30,0x01,0xC6,0x19,0xC0,0x1E,0x7F,0xDE,0x00,0x6 F,0x83,0x80,0x00,0x00,0x23,0xAC,0x60,0x0F,0xE6,0xF1,0x80,0x3E,0x7B,0x38,0x00,0x6 1,0x83,0x00,0x00,0x00,0x37,0x0E,0x00,0x0F,0xC7,0xB7,0x80,0xEC,0x63,0x30,0x00,0xE 3,0x03,0x00,0x00,0x00,0x3E,0x0E,0x00,0x01,0x8F,0x37,0x01,0xCC,0x6F,0xF0,0x00,0xC 7,0xF7,0x00,0x00,0x00,0x1E,0x0C,0x00,0x03,0x1E,0x66,0x03,0x0C,0x7F,0x00,0x01,0xF F,0xE6,0x00,0x00,0x00,0x1E,0x1C,0x00,0x03,0x1C,0x60,0x02,0x19,0xAC,0x00,0x01,0x9 6,0x06,0x00,0x00,0x00,0x3E,0x3E,0x00,0x03,0x18,0x60,0x00,0x19,0xFC,0x00,0x03,0x8 6,0x0E,0x00,0x00,0x00,0x7E,0x36,0x00,0x03,0x00,0xC0,0x00,0x38,0x78,0x00,0x03,0x0 6,0x0C,0x00,0x00,0x00,0xE6,0x67,0x00,0x03,0x00,0xC0,0x00,0x30,0x78,0x00,0x06,0x0 C,0x0C,0x00,0x00,0x01,0xC6,0xC3,0x80,0x7F,0xF0,0xC0,0x00,0x30,0xFE,0x00,0x0E,0x0 C,0x1C,0x00,0x00,0x03,0x81,0x83,0xC0,0x78,0xFF,0x80,0x00,0x61,0xCF,0x80,0x1C,0x0 8,0x18,0x00,0x00,0x06,0x07,0x03,0xE0,0x00,0x1F,0xFF,0xE0,0x67,0x07,0xE0,0x38,0x1 8,0xF8,0x00,0x00,0x08,0x0E,0x01,0xF8,0x00,0x07,0xFF,0x80,0xCE,0x03,0xF8,0x60,0x0 0,0x70,0x00,0x00,0x00,0x18,0x01,0xF8,0x00,0x01,0xFC,0x00,0xC0,0x01,0xFC,0xC0,0x0 0,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x00,0x0C,0xE0,0x00,0x01,0xE0,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x3F,0xE0,0x00,0xFF,0xF0,0x00,0x3 8,0x00,0xE0,0x00,0x00,0x00,0x0C,0x00,0x00,0x0F,0xFF,0x80,0x00,0xF8,0xF0,0x00,0x3 8,0x00,0xE0,0x00,0x00,0x00,0x0C,0xF0,0x00,0x0F,0xC7,0x00,0x00,0x60,0xE0,0x00,0x3 8,0x00,0xE0,0x00,0x00,0x00,0x1F,0xF0,0x00,0x01,0x8E,0x00,0x00,0x60,0xC0,0x00,0x7 0,0x01,0xC0,0x00,0x00,0x07,0xFF,0x80,0x00,0x01,0x8C,0x00,0x00,0xE1,0x80,0x00,0x7 0,0x01,0xC0,0x00,0x00,0x07,0xF8,0x00,0x00,0x01,0x98,0x00,0x00,0xDF,0xC0,0x00,0x7 0,0x01,0xC0,0x00,0x00,0x00,0x78,0x00,0x00,0x01,0xBF,0xE0,0x00,0xFF,0x00,0x00,0x6 0,0x01,0x80,0x00,0x00,0x00,0xFC,0x00,0x00,0x7F,0xFF,0xE0,0x00,0xC0,0x00,0x00,0xE 0,0x03,0x80,0x00,0x00,0x01,0xFE,0x00,0x00,0x7F,0xF8,0x00,0x00,0x00,0x00,0x00,0xC 0,0x03,0x00,0x00,0x00,0x03,0xB7,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x3C,0x00,0xC 0,0x03,0x00,0x00,0x00,0x07,0x67,0x00,0x00,0x60,0x00,0x00,0x03,0x9F,0xFE,0x00,0x8 0,0x02,0x00,0x00,0x00,0x0E,0x63,0x80,0x00,0xE0,0x00,0x01,0xFF,0xCF,0x1E,0x01,0x8 0,0x06,0x00,0x00,0x00,0x1C,0xE1,0xE0,0x00,0xC0,0x00,0x00,0xE3,0x9C,0x38,0x01,0x8 0,0x06,0x00,0x00,0x00,0x38,0xC1,0xF8,0x01,0xC0,0x00,0x00,0xC3,0x18,0x30,0x00,0x0 0,0x00,0x00,0x00,0x00,0xE0,0xFC,0xFE,0x03,0x80,0x00,0x01,0xC7,0x18,0x60,0x00,0x0 0,0x00,0x00,0x00,0x01,0xDF,0xF8,0xFE,0x03,0x00,0x00,0x01,0x8E,0x1B,0xE0,0x07,0x0 0,0x1C,0x00,0x00,0x07,0x07,0x80,0x00,0x07,0x00,0x00,0x01,0xFF,0x3F,0xE0,0x07,0x0 0,0x1C,0x00,0x00,0x0C,0x03,0x80,0x00,0x0E,0x00,0x00,0x01,0xF0,0x38,0x00,0x0F,0x0 0,0x3C,0x00,0x00,0x00,0x03,0x00,0x00,0x1C,0x00,0x00,0x01,0x00,0x20,0x00,0x06,0x0 0,0x18,0x00,0x00,0x00,0x03,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x02,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,};/************************************************************** ****************/voidLcd_init(void) //初始化LCD{Delaynms(10);//启动等待,等LCM讲入工作状态CS=1;Write_char(0,0x30);//8位介面,基本指令集Write_char(0,0x0c);//显示打开,光标关,反白关Write_char(0,0x01);//清屏,将DDRAM的地址计数器归零}/************************************************************** ****************/voidWrite_char(bitstart,unsignedcharddata)//写指令或数据{unsignedcharstart_data,Hdata,Ldata;if(start==0)start_data=0xf8; //写指令elsestart_data=0xfa;//写数据Hdata=ddata&0xf0; //取高四位Ldata=(ddata<<4)&0xf0;//取低四位Send_byte(start_data); //发送起始信号Delaynms(5);//延时是必须的Send_byte(Hdata); //发送高四位Delaynms(1);//延时是必须的Send_byte(Ldata); //发送低四位Delaynms(1);//延时是必须的}/************************************************************** ****************/voidSend_byte(unsignedcharbbyte)//发送一个字节{unsignedchari;for(i=0;i<8;i++){SID=bbyte&0x80;//取出最高位SCLK=1;SCLK=0;bbyte<<=1;//左移}}/************************************************************** ****************/voidDelaynms(unsignedintdi)//延时{unsignedintda,db;for(da=0;da<di;da++)for(db=0;db<10;db++);}/************************************************************** ****************/voidDisp_img(unsignedchar*img) //图形方式12864显示字模221横向取膜{unsignedchari,j;unsignedintk=0;Write_char(0,0x36);//图形方式for(i=0;i<32;i++){Write_char(0,0x80+i);Write_char(0,0x80);for(j=0;j<16;j++){Write_char(1,img[k++]);}}for(i=0;i<32;i++){Write_char(0,0x80+i);Write_char(0,0x88);for(j=0;j<16;j++){Write_char(1,img[k++]);}}}/************************************************************** ****************/voidClr_Scr(void)//清屏函数{Write_char(0,0x01);}/************************************************************** ****************/voidLCD_set_xy(unsignedcharx,unsignedchary){ //设置LCD显示的起始位置,X为行,Y为列unsignedcharaddress;switch(x){case0:address=0x80+y;break;case1:address=0x80+y;break;case2:address=0x90+y;break;case3:address=0x88+y;break;case4:address=0x98+y;break;default:address=0x80+y;break;}Write_char(0,address);}/************************************************************** ****************/voidLCD_Write_string(unsignedcharX,unsignedcharY,unsignedchar*s) { // 中英文字符串显示函数LCD_set_xy(X,Y);while(*s){Write_char(1,*s);s++;Delaynms(1);}}/************************************************************** ****************/voidLCD_Write_number(unsignedchars)// 数字显示函数{Write_char(1,num[s]);Delaynms(1);}/************************************************************** ****************/voidLcd_Mark2(void){Clr_Scr();//清屏LCD_Write_string(1,0,znwk);//LCD_Write_string(2,0,waves);//LCD_Write_string(3,7,CM);//}/********************************************************/ voidConut(void){time=TH0*256+TL0;TH0=0;TL0=0;S=time*1.87/100;//算出来是CM 11。
超声波测距12864显示程序带语音播报
#include <string.h>
#include "stdio.h"
#define uchar unsigned char
#define uint unsigned int
#define LCD_DATA P2 //数据口
sbit RS=P3^7;
flag=0;
time=0;
P16=1;
}
if((flag==1)&&(time>=200)&&(time<=500)){
Dis_string(1,0,"距离");
Dis_string(1,7,"米");
display(time,4);
Dis_string(2,2,string2);
}
void main()
{
flag=0;
P0=0XFF;
P14=0;
P15=0;
P16=0;
LcdInit(); //液晶初始化
Clear_txt();//清除液晶上的的文本
Dis_string(0,2,"欢迎使用");
Dis_string(2,0,"超声波测距离系统");
void DelayMs(uint z)
{
uint x,y;
for(x=z;x!=0;x--)
for(y=255;y!=0;y--);
}
//延时函数
void delay(unsigned int MS)
{
超声波测距仪程序
void offmsd();
void main() //主函数
{
EA=1; //开中断
TMOD=0x11; //设定时器0为计数,设定时器1定时
EX0=0; //关闭外部中断
}
//****************************************************************
//定时器1中断,用做超声波测距计时
void timer1() interrupt 3 //
{
TH1=0;
TL1=0;
}
制作的超声波测距源程序
#include <REG2051.H>
#define k1 P3_4
#define csbout P3_5 //超声波发送
#define csbint P3_7 //超声波接收
P3=~digit&opto; //依次显示各位数
P1=~buffer; //显示数据送P1口
delay(20); //延时处理
P1=0xff; //P1口置高电平(关闭)
}
else timeToBuffer(); //将值转换成LED段码
offmsd();
scanLED(); //显示函数
if(s<sj2)
{
buffer[2]=0x76;
buffer[1]=0x76;
buffer[0]=0x76;
}
else if(s<sj1)
TR1=1;
while(1)
{
keyscan();
if(jpjs<1)
51单片机驱动超声波测距模块C51程序
51单片机驱动超声波测距模块C51程序51单片机驱动超声波测距模块C51程序#include;#define uchar unsigned char#define uint unsigned int#define ulong unsigned long//******************* 函数声明**************************void init_T otal(); //总初始化void init_T0(); //初始化定时器T0void init_T1(); //初始化定时器T1void init_inter0();//初始化外部中断1void send_T();void delay(uint z);//延时一段时间void delay_300us();//延时300usvoid delay_100us();//延时100us//*********************************************** *********sbit lcdrs=P1^7;sbit lcdrw=P3^1;sbit lcden=P1^5;//1602液晶控制端sbit send=P1^0;//sbit BEEP=P2^5;sbit wei=P2^6;sbit duan=P2^7;volatile uchar Count_TH ,Count_TL;//分别读计数器T1的高位TH1,低位TL1uchar t0,flag;uint time;uchar code table1[]=" distance "; uchar code table2[]=" ";//初始化显示void write_com(uchar com)//1602写指令函数{lcdrs=0;P0=com;delay(5);lcden=1;delay(5);lcden=0;void write_data(uchar datb)//1602写数据函数{ lcdrs=1;P0=datb;delay(1);lcden=1;delay(1);lcden=0;}void distance(uchar addr,uint datb){uchar bai,shi,ge ;bai=datb/100;shi=datb%100/10;ge=datb%10;write_com(0x80+0x40+addr);write_data(0x30+bai);write_data(0x30+shi);write_data(0x30+ge);}/************************************************ *************************************** 名称:void init_T otal()* 功能:总初始化* 入口参数:NULL* 全局变量:NULL* 返回值:NULL**************************************************************************************/void init_T otal(){init_T0(); //初始化定时器T0为工作方式2 init_T1(); //初始化定时器T1为工作方式1 init_inter0();//初始化外部中断1EA=1; //开总中断}/*************************************************************************************** 名称:void init_T0()* 功能:初始化定时器T0为工作方式2* 入口参数:NULL* 全局变量:NULL* 返回值:NULL**************************************************************************************/void init_T0(){TMOD=0X12;TH0=0XE7;TL0=0XE7;EA=0;ET0=1;TR0=1;}/************************************************ *************************************** 名称:void init_T1()* 功能:初始化定时器T1为工作方式1* 入口参数:NULL* 全局变量:NULL* 返回值:NULL************************************************* *************************************/void init_T1(){TMOD=0X12;TH1=0;TL1=0;EA=0;ET1=1;TR1=1;}/************************************************ *************************************** 名称:void init_inter1()* 功能:初始化外部中断1为低电平触发方式* 入口参数:NULL* 全局变量:NULL* 返回值:NULL************************************************* *************************************/void init_inter0(){IT0=0; //低电平触发EA=0;EX0=0; //关外部中断1}/************************************************ *************************************** 名称:void inter_T0() interrupt 1* 功能:定时器T0中断函数产生40KHZ的方波* 入口参数:NULL* 全局变量:NULL* 返回值:NULL************************************************* *************************************/void inter_T0() interrupt 1{send=~send;}/************************************************ *************************************** 名称:void inter_T1() interrupt 3* 功能:定时器T1中断函数* 入口参数:NULL* 全局变量:NULL* 返回值:NULL************************************************* *************************************/void inter_T1() interrupt 3{TR1=0;EX0=0; //关外部中断0TH1=0;TL1=0;flag=2;}/************************************************ *************************************** 名称:void inter1() interrupt 2* 功能:外部中断1函数* 入口参数:NULL* 全局变量:Count_TH,Count_TL* 返回值:NULL************************************************* *************************************/void inter0() interrupt 0{TR1=0;EX0=0;flag=1;}/************************************************ *************************************** 名称:void send_T()* 功能:发送10个超声波脉冲* 入口参数:NULL* 全局变量:Count* 返回值:NULL************************************************* *************************************/void send_T(){delay_100us();//发送100us的方波TR0=0;//关定时器T0}/************************************************ *************************************** 名称:void delay(uint z)* 功能:延时一段时间* 入口参数:z* 全局变量:NULL* 返回值:NULL************************************************* *************************************/void delay(uint z){uint x,y;for(x=z;x>;0;x--)for(y=1000;y>;0;y--);}/************************************************ *************************************** 名称:void delay_300us()* 功能:延时300us* 入口参数:NULL* 全局变量:NULL* 返回值:NULL************************************************* *************************************/void delay_300us(){uint x;for(x=75;x>;0;x--);}/************************************************ *************************************** 名称:void delay_100us()* 功能:延时100us* 入口参数:NULL* 全局变量:NULL* 返回值:NULL************************************************* *************************************/void delay_100us(){uint x;for(x=24;x>;0;x--);}//*********************************************** ****************************************void main(){uint t,s;init_T otal();//总初始化while(1){TR1=1;//启动定时器1TR0=1;//启动定时器0send_T(); //发送100us超声波脉冲delay_300us();//延时300us跳过盲区EX1=1; //开外部中断1while(!flag); //等待回波或定时器T1溢出if(flag==1) //回波{Count_TH=TH1;//读计数器T1的高位TH1 Count_TL=TL1;//读计数器T1的低位TL1 t=Count_TH*256+Count_TL;s=(33140*t)/400000;distance(4,s);//液晶1602显示距离 }/* else //定时器T1溢出{Count_TH=0XFE;Count_TL=0X7F;// P2=Count_TH;P2=Count_TL;}*/delay(150);flag=0;//标志位清0TH1=0; //T1清0重新计时TL1=0;}}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
超声波L C D显示测距程序Company number【1089WT-1898YT-1W8CB-9UUT-92108】/************************************************************** ****************/ /*Name:LCD12864(St7920/St7921)+超声波测距模块+STC89C52*//*DYP-ME007超声波测距模块DEMO程序*//*晶振:11.0592M*/*//*接线:模块TRIG接P1.2ECH0接P1.1*//*RS(CS)接P3.3;*//*RW(SID)接P3.4;*//*E(SCLK)接P3.5;*//*PSB接GND串行模式*//************************************************************** ****************/#include<AT89X51.H>#include<intrins.h>//引脚定义sbitRX=P1^1;sbitTX=P1^2;sbitCS=P3^3; //片选高电平有效单片LCD使用时可固定高电平sbitSID=P3^4; //数据sbitSCLK=P3^5; //时钟//FunctionDefinition 函数声明voidDelay(intnum);voidInit_DS18B20(void);unsignedcharReadOneChar(void);voidWriteOneChar(unsignedchardat); unsignedintReadTemperature(void);voidclock_out(unsignedchardd);unsignedcharclock_in(void);unsignedcharread_clock(unsignedcharord);voidwrite_clock(unsignedcharord,unsignedchardd);voidDisp(void);voidid_case1_key(void);voidid_case2_key(void);voidSet_time(unsignedcharsel,bitsel_1);voidTimer0_Init(void);voidInit_1302(void);voidSet_Bell(unsignedcharsel,bitsel_1);voidBell(void);//12864voidWrite_char(bitstart,unsignedcharddata);voidSend_byte(unsignedcharbbyte);voidDelaynms(unsignedintdi);voidLcd_init(void);voidDisp_img(unsignedchar*img);voidLCD_Write_string(unsignedcharX,unsignedcharY,unsignedchar*s );voidLCD_set_xy(unsignedcharx,unsignedchary); unsignedcharcode:.-"};unsignedcharcodewaves[]={"超声波测距系统"}; unsignedcharcodeznwk[]={"智能微控工作室"}; unsignedcharcodeCM[]={"M"};unsignedinttime=0;longS=0;bitflag=0;unsignedchardisbuff[4] ={0,0,0,0,};unsignedcharcodelogo[]={/*--调入了一幅图像:logo.bmp--*//*--宽度x高度=128x64欢迎使用本产品LOGO--*//*--调入了一幅图像:C:\DocumentsandSettings\lwd\桌面\新建文件夹\X.bmp--*//*--宽度x高度=128x64--*/0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x0C,0x00,0x00,0x40,0x70,0x00,0x0 0,0x00,0x80,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x1E,0x00,0x00,0x60,0x70,0x00,0x0 0,0x0F,0xE0,0x00,0x00,0x00,0x01,0xC0,0x00,0x60,0x3C,0x00,0x00,0xE0,0x60,0x00,0x1 F,0xFF,0xE0,0x00,0x00,0x00,0x03,0x80,0x00,0x70,0x70,0x40,0x00,0xE0,0x7E,0x00,0x1 F,0xE0,0xC0,0x00,0x00,0x00,0x03,0x00,0x00,0x73,0xC3,0xF0,0x01,0xC3,0xFC,0x00,0x3 8,0xE1,0xC0,0x00,0x00,0x03,0xC6,0x3C,0x00,0x33,0x9F,0xF0,0x03,0x8F,0xC0,0x00,0x3 0,0xC1,0xC0,0x00,0x00,0x3F,0xEF,0xFE,0x00,0x03,0x1C,0xE0,0x07,0x01,0xCC,0x00,0x3 1,0xF1,0x80,0x00,0x00,0x79,0xDF,0xBC,0x00,0x06,0x18,0xC0,0x0E,0x01,0xFE,0x00,0x7 7,0xF9,0x80,0x00,0x00,0x01,0xB0,0x30,0x01,0xC6,0x19,0xC0,0x1E,0x7F,0xDE,0x00,0x6 F,0x83,0x80,0x00,0x00,0x23,0xAC,0x60,0x0F,0xE6,0xF1,0x80,0x3E,0x7B,0x38,0x00,0x6 1,0x83,0x00,0x00,0x00,0x37,0x0E,0x00,0x0F,0xC7,0xB7,0x80,0xEC,0x63,0x30,0x00,0xE 3,0x03,0x00,0x00,0x00,0x3E,0x0E,0x00,0x01,0x8F,0x37,0x01,0xCC,0x6F,0xF0,0x00,0xC 7,0xF7,0x00,0x00,0x00,0x1E,0x0C,0x00,0x03,0x1E,0x66,0x03,0x0C,0x7F,0x00,0x01,0xF F,0xE6,0x00,0x00,0x00,0x1E,0x1C,0x00,0x03,0x1C,0x60,0x02,0x19,0xAC,0x00,0x01,0x9 6,0x06,0x00,0x00,0x00,0x3E,0x3E,0x00,0x03,0x18,0x60,0x00,0x19,0xFC,0x00,0x03,0x8 6,0x0E,0x00,0x00,0x00,0x7E,0x36,0x00,0x03,0x00,0xC0,0x00,0x38,0x78,0x00,0x03,0x0 6,0x0C,0x00,0x00,0x00,0xE6,0x67,0x00,0x03,0x00,0xC0,0x00,0x30,0x78,0x00,0x06,0x0 C,0x0C,0x00,0x00,0x01,0xC6,0xC3,0x80,0x7F,0xF0,0xC0,0x00,0x30,0xFE,0x00,0x0E,0x0 C,0x1C,0x00,0x00,0x03,0x81,0x83,0xC0,0x78,0xFF,0x80,0x00,0x61,0xCF,0x80,0x1C,0x0 8,0x18,0x00,0x00,0x06,0x07,0x03,0xE0,0x00,0x1F,0xFF,0xE0,0x67,0x07,0xE0,0x38,0x1 8,0xF8,0x00,0x00,0x08,0x0E,0x01,0xF8,0x00,0x07,0xFF,0x80,0xCE,0x03,0xF8,0x60,0x0 0,0x70,0x00,0x00,0x00,0x18,0x01,0xF8,0x00,0x01,0xFC,0x00,0xC0,0x01,0xFC,0xC0,0x0 0,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x00,0x0C,0xE0,0x00,0x01,0xE0,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x3F,0xE0,0x00,0xFF,0xF0,0x00,0x3 8,0x00,0xE0,0x00,0x00,0x00,0x0C,0x00,0x00,0x0F,0xFF,0x80,0x00,0xF8,0xF0,0x00,0x3 8,0x00,0xE0,0x00,0x00,0x00,0x0C,0xF0,0x00,0x0F,0xC7,0x00,0x00,0x60,0xE0,0x00,0x3 8,0x00,0xE0,0x00,0x00,0x00,0x1F,0xF0,0x00,0x01,0x8E,0x00,0x00,0x60,0xC0,0x00,0x7 0,0x01,0xC0,0x00,0x00,0x07,0xFF,0x80,0x00,0x01,0x8C,0x00,0x00,0xE1,0x80,0x00,0x7 0,0x01,0xC0,0x00,0x00,0x07,0xF8,0x00,0x00,0x01,0x98,0x00,0x00,0xDF,0xC0,0x00,0x7 0,0x01,0xC0,0x00,0x00,0x00,0x78,0x00,0x00,0x01,0xBF,0xE0,0x00,0xFF,0x00,0x00,0x6 0,0x01,0x80,0x00,0x00,0x00,0xFC,0x00,0x00,0x7F,0xFF,0xE0,0x00,0xC0,0x00,0x00,0xE 0,0x03,0x80,0x00,0x00,0x01,0xFE,0x00,0x00,0x7F,0xF8,0x00,0x00,0x00,0x00,0x00,0xC 0,0x03,0x00,0x00,0x00,0x03,0xB7,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x3C,0x00,0xC 0,0x03,0x00,0x00,0x00,0x07,0x67,0x00,0x00,0x60,0x00,0x00,0x03,0x9F,0xFE,0x00,0x8 0,0x02,0x00,0x00,0x00,0x0E,0x63,0x80,0x00,0xE0,0x00,0x01,0xFF,0xCF,0x1E,0x01,0x8 0,0x06,0x00,0x00,0x00,0x1C,0xE1,0xE0,0x00,0xC0,0x00,0x00,0xE3,0x9C,0x38,0x01,0x8 0,0x06,0x00,0x00,0x00,0x38,0xC1,0xF8,0x01,0xC0,0x00,0x00,0xC3,0x18,0x30,0x00,0x0 0,0x00,0x00,0x00,0x00,0xE0,0xFC,0xFE,0x03,0x80,0x00,0x01,0xC7,0x18,0x60,0x00,0x0 0,0x00,0x00,0x00,0x01,0xDF,0xF8,0xFE,0x03,0x00,0x00,0x01,0x8E,0x1B,0xE0,0x07,0x0 0,0x1C,0x00,0x00,0x07,0x07,0x80,0x00,0x07,0x00,0x00,0x01,0xFF,0x3F,0xE0,0x07,0x0 0,0x1C,0x00,0x00,0x0C,0x03,0x80,0x00,0x0E,0x00,0x00,0x01,0xF0,0x38,0x00,0x0F,0x0 0,0x3C,0x00,0x00,0x00,0x03,0x00,0x00,0x1C,0x00,0x00,0x01,0x00,0x20,0x00,0x06,0x0 0,0x18,0x00,0x00,0x00,0x03,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x02,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,};/************************************************************** ****************/voidLcd_init(void) //初始化LCD{Delaynms(10);//启动等待,等LCM讲入工作状态CS=1;Write_char(0,0x30);//8位介面,基本指令集Write_char(0,0x0c);//显示打开,光标关,反白关Write_char(0,0x01);//清屏,将DDRAM的地址计数器归零}/************************************************************** ****************/voidWrite_char(bitstart,unsignedcharddata)//写指令或数据{unsignedcharstart_data,Hdata,Ldata;if(start==0)start_data=0xf8; //写指令elsestart_data=0xfa;//写数据Hdata=ddata&0xf0; //取高四位Ldata=(ddata<<4)&0xf0;//取低四位Send_byte(start_data); //发送起始信号Delaynms(5);//延时是必须的Send_byte(Hdata); //发送高四位Delaynms(1);//延时是必须的Send_byte(Ldata); //发送低四位Delaynms(1);//延时是必须的}/************************************************************** ****************/voidSend_byte(unsignedcharbbyte)//发送一个字节{unsignedchari;for(i=0;i<8;i++){SID=bbyte&0x80;//取出最高位SCLK=1;SCLK=0;bbyte<<=1;//左移}}/************************************************************** ****************/voidDelaynms(unsignedintdi)//延时{unsignedintda,db;for(da=0;da<di;da++)for(db=0;db<10;db++);}/************************************************************** ****************/voidDisp_img(unsignedchar*img) //图形方式12864显示字模221横向取膜{unsignedchari,j;unsignedintk=0;Write_char(0,0x36);//图形方式for(i=0;i<32;i++){Write_char(0,0x80+i);Write_char(0,0x80);for(j=0;j<16;j++){Write_char(1,img[k++]);}}for(i=0;i<32;i++){Write_char(0,0x80+i);Write_char(0,0x88);for(j=0;j<16;j++){Write_char(1,img[k++]);}}}/************************************************************** ****************/voidClr_Scr(void)//清屏函数{Write_char(0,0x01);}/************************************************************** ****************/voidLCD_set_xy(unsignedcharx,unsignedchary){ //设置LCD显示的起始位置,X为行,Y为列unsignedcharaddress;switch(x){case0:address=0x80+y;break;case1:address=0x80+y;break;case2:address=0x90+y;break;case3:address=0x88+y;break;case4:address=0x98+y;break;default:address=0x80+y;break;}Write_char(0,address);}/************************************************************** ****************/voidLCD_Write_string(unsignedcharX,unsignedcharY,unsignedchar*s) { // 中英文字符串显示函数LCD_set_xy(X,Y);while(*s){Write_char(1,*s);s++;Delaynms(1);}}/************************************************************** ****************/voidLCD_Write_number(unsignedchars)// 数字显示函数{Write_char(1,num[s]);Delaynms(1);}/************************************************************** ****************/voidLcd_Mark2(void){Clr_Scr();//清屏LCD_Write_string(1,0,znwk);//LCD_Write_string(2,0,waves);//LCD_Write_string(3,7,CM);//}/********************************************************/ voidConut(void){time=TH0*256+TL0;TH0=0;TL0=0;S=time*1.87/100;//算出来是CM 11。