AT89C51超声波测距系统(SRF04)+温度补偿+报警电路+LCD1602显示(带详尽程序注释)
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
测距+温度补偿+报警电路
单片机源程序如下:
- #include "config.h"
- #include "delay.h"
- #include "lcd1602.h"
- #include "ds18b20.h"
- #include "chaoshengbo.h"
- /******************************************************************************/
- // 数组定义
- /******************************************************************************/
- uint8 lcd1602Table1[8]={0x00,0x06,0x09,0x09,0x06,0x00,0x00,0x00};//构造温度符号
- uint8 lcd1602Table2[7]={'0','0','0','.','0','0','\0'}; //存放需要显示的温度信息
- uint8 disbuff[4]={'0','0','0','\0'};//存放需要显示的距离数字
- uint16 max,min,average=0;//最大值、最小值变量定义
- uint16 S[10]; //存放每次计算的距离信息
- uint16 time=0; //存放时间差
- uint8 tempflag;//温度正负标志位
- float temp1;//存放每次测量的温度信息
- bit flag1=0;//定时器时间溢出标志位
- /******************************************************************************/
- // main函数
- /******************************************************************************/
- int main(void){
- beep=0; //扬声器初始化
- Lcd1602_Init(); //液晶初始化
- while(Ds18b20_Init())
- Lcd1602_Show_String(1,1,"NO SENSOR!");//初始化检测总线上是否有DS18B20
- Lcd1602_Show_String(1,1,"Initial OK");
- Lcd1602_Write_Cmd(0x01); //清屏LCD1602
- Lcd1602_Custom_Char(1,10,0,lcd1602Table1);//构造温度符号,位置为第1行第10列
- Lcd1602_Show_String(1,11,"C");
- Lcd1602_Show_String(1,1,"T:");//温度字母“T”
- Lcd1602_Show_String(2,1,"h:");//距离字母“h”
- Lcd1602_Show_String(2,6,"cm");//单位cm
-
- while(1){
- Ds18b20_Get_Temp(lcd1602Table2); //读取温度
- if(tempflag==0)
- {
- Lcd1602_Show_String(1,3,"+"); //显示正号
- Lcd1602_Show_String(1,4,lcd1602Table2);//显示温度
- }
- else
- {
- Lcd1602_Show_String(1,3,"-"); //显示负号
- Lcd1602_Show_String(1,4,lcd1602Table2);//显示温度
- }
- super_distance(disbuff); //超声波测距
- Lcd1602_Show_String(2,3,disbuff);//显示距离
- }
- }
复制代码
所有资料51hei提供下载:
超声波测距.zip
(94.61 KB, 下载次数: 200)
|