//主程序#include<reg52.h> #include<math.h> #include<intrins.h> #include<stdio.h> #include "1602.h"#include "Basic.h"#include"DHT11.h"#include"SR04.h"#include"syn6288.h"intFLAG=0; //距离级别1表示禁止,2表示警告,3表示通行,4表示数据过大sbitLED_R = P2^3;sbit LED_Y = P2^4;sbit LED_G = P2^5;sbit BELL = P2^6;float s_data=0; //距离数据,单位厘米float temperature=0;//温度数据,单位摄氏度float humidity=0; //湿度数据char s_str[10];char sp_str[20];char t_str[8];char h_str[8];//报警函数,根据级别发生警报void Warning() { in
ti; switch(FLAG) { case 1: //红 {MOD}警告 LED_G=1;LED_Y=1; for(i=0;i<25;i++){LED_R=0;BELL=0;Delay(20);LED_R=1;BELL=1;Delay(20);} break; case 2: //黄 {MOD}警告 LED_G=1;LED_R=1; for(i=0;i<50;i++){LED_Y=0;BELL=0;Delay(10);LED_Y=1;BELL=1;Delay(10);} break; case 3: case 4: BELL=0;LED_G=0;LED_R=1;LED_Y=1; Delay(1000);Delay(1000);Delay(1000);Delay(1000);Delay(1000); break; default: break; } }void main() { inti; LCD_Init(); //初始化液晶 Delay(5); //延时有助于稳定 LCD_Clear(); //清屏 SR04_init(); SYN6288_Init(); TMOD|=0x01; //定时器0工作方式1 GATE=0 软件可启动 计数脉冲为机器周期 TH0=0; //初始值 TL0=0; ET0=1; //允许T0中断 EA=1; //开总中断 BkMusic(1); Speech("大家好,欢迎使用****的倒车雷达"); LCD_Write_String(0,0,"Reversing radar"); for(i=0;i<15;i++) Delay(1000); BkMusic(0); Delay(1000); while(1) {//读距离数据,温度湿度数据 s_data=SR04_read_data(); getDHT11(&temperature,&humidity); //处理距离数据 包括校正和变成字符串形式 if(s_data>=1000) {s_data=999;FLAG=4;} s_data=(s_data*331.45/340)*sqrt((1+temperature/273.15)*(1+0.00544*humidity)); sprintf(s_str,"s:%3.1fcm",s_data); sprintf(sp_str,"距离%3.1f",s_data); sprintf(h_str,"H:%2.0f%%RH",humidity); sprintf(t_str,"T:%2.0f`C",temperature); Speech(sp_str); LCD_Clear(); //1602显示距离数据 LCD_Write_String(0,0,t_str); LCD_Write_String(7,0,h_str); LCD_Write_String(0,1,s_str); //防止发射信号干扰,等待发音完成 Delay(1000); Delay(1000); Delay(1000); Delay(1000); Delay(1000); Delay(1000); //判断数据大小 并修改FLAG值 if(s_data>100.0) FLAG=3; else if(s_data>50.0) FLAG=2; else FLAG=1; //警报 Warning(); BELL=0; } }//显示模块#include<reg52.h> #include<intrins.h> #include "Basic.h" sbit RS=P2^0; //寄存器选择位,将RS位定义为P2.0引脚sbit RW=P2^1; //读写选择位,将RW位定义为P2.1引脚sbit EN=P2^2; //使能信号位,将E位定义为P2.2引脚sbit BF=P0^7; //忙碌标志位,,将BF位定义为P0.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//判忙函数bit LCD_Check_Busy() { bit result; RS_CLR; RW_SET; EN_SET; _nop_(); result=(bit)(DataPort&0x80); EN_CLR; returnresult;}//写入指令函数void LCD_Write_Com(unsignedchar 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() {LCD_Write_Com(0x01); Delay(1);}//写入字符串函数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_Init() { LCD_Write_Com(0x38); /*显示模式设置*/ Delay(5); LCD_Write_Com(0x38); Delay(5); LCD_Write_Com(0x38); Delay(5); LCD_Write_Com(0x38); LCD_Write_Com(0x08); /*显示关闭*/ LCD_Write_Com(0x01); /*显示清屏*/ LCD_Write_Com(0x06); /*显示光标移动设置*/ Delay(5); LCD_Write_Com(0x0C); /*显示开及光标设置*/}//温湿度模块#include<reg52.h>#include "Basic.h"sbit bit11=P1^2;unsigned char U8FLAG; //数据的脉长标志unsigned char U8T_data_H; //温度高8位unsigned char U8T_data_L; //温度低8位unsigned char U8RH_data_H; //湿度高8位unsigned char U8RH_data_L; //湿度低8位unsigned char U8checkdata; //校验 8位float F16T,F16RH; //用于最终读取的温湿度数据 //获取8为数据 char COM(){ unsignedchar i,U8temp,U8comdata; for(i=0;i<8;i++) { U8FLAG=2; while(!bit11&&U8FLAG++);//读取的高电位延时大于20us则读取的是1否则读取的是0 Delay_10us(); Delay_10us(); U8temp=0; if(bit11) U8temp=1; U8FLAG=2; while(bit11&&U8FLAG++); if(U8FLAG==1) break; U8comdata<<=1; U8comdata|=U8temp; } return U8comdata;}//温湿度读取子程序voidgetDHT11(float * F16T,float * F16RH){GO1: bit11=0; //主机拉低18ms Delay(180); bit11=1; //总线由上拉电阻拉高 主机延时20us Delay_10us(); Delay_10us(); Delay_10us(); Delay_10us(); bit11=1; //主机设为输入 判断从机响应信号 if(!bit11) //判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行 { U8FLAG=2; while((!bit11)&&U8FLAG++);//响应中,等待拉高 U8FLAG=2; while(bit11&&U8FLAG++); //按顺序接收数据 U8RH_data_H=COM(); U8RH_data_L=COM(); U8T_data_H=COM(); U8T_data_L=COM(); U8checkdata=COM(); bit11=1; //数据校验 if((U8T_data_H+U8T_data_L+U8RH_data_H+U8RH_data_L)!=U8checkdata) goto GO1; } //转换数据格式 *F16T=(float)U8T_data_H+(float)U8T_data_L/256; *F16RH=(float)U8RH_data_H+(float)U8RH_data_L/256; }//测距模块#include<reg52.h> #include"Basic.h" sbit TRIG = P1^0;sbit ECHO = P1^1;//初始化void SR04_init(){ TRIG=0; ECHO=0;}//发送超声波void StartModule(){ TRIG =1; DelayUs2x(10); TRIG =0;}//读取距离数据并返回float SR04_read_data(){ float time=0; float s=0; StartModule(); while(!ECHO); //当RX为零时等待 TR0=1; //开启计数 while(ECHO); //当RX为1计数并等待 TR0=0; //关闭计数 time=TH0*256+TL0; TH0=0; TL0=0; s= time/58; return s;}//语音模块#include <reg52.h>#include "Basic.h"unsigned char nBkm = 0x00;#define HEADLEN 5 //数据包头的长度#define BKM_OFFSET 4 //背景音乐命令偏移#define LEN_OFFSET 2 //长度字节的偏移量#define BKM_MAX 15 //背景音乐数量unsigned char head[HEADLEN] ={0xfd,0x00,0x00,0x01,0x00};//sbit BUSY = P1^7;//BUSY引脚没有使用//晶振为11.059MHZ时,设定串口波特率为9600bit/s,方式3 void SYN6288_Init(){ SCON =0xD8; //设置串口工作方式 TMOD|=0x20; //设置定时器1 PCON =0x00; //设置SMOD=0 TH1 =0xFD; TR1 =1; }//串口发送数据void SendChar(unsigned char n){ SBUF = n; while (TI==0);//发送数据 TI=0;} //背景音乐(参数为0表示关闭背景音乐)void BkMusic(unsigned charnum){ num %= BKM_MAX + 1; nBkm = num;}//发声程序voidSpeech(unsigned char *buf){ unsigned char i = 0; //循环计数变量 unsigned char xor = 0x00; //校验码初始化 unsigned char ch = 0x00; unsigned char len =0x00; while(buf[len++]); //发送数据包头(0xFD + 2字节长度 + 1字节命令字 + 1字节命令参数) for(i = 0; i < HEADLEN; i++) { if(i == BKM_OFFSET) ch = nBkm << 3; //写入背景音乐 else if(i == LEN_OFFSET) ch = len + 3; else ch = head
; xor ^= ch; SendChar(ch); Delay(1); } //发送文字内容 for(i = 0; i < len; i++) { xor ^= buf; SendChar(buf); Delay(1); } SendChar(xor); //发送校验位 Delay(50); //while(BUSY); //等待合成结束 Delay(50); }
-
一周热门 更多>