#include<REGX52.H> #include<intrins.h> #include<absacc.h> #include<stdio.h> #define ucharunsigned char #defineuint unsigned int; sbit k1=P3^5; sbit k2=P3^4; sbit csb=P3^1; //TRIG sbitcsbint=P3^2; //ECHO sbit bg=P1^6; //液晶屏背光控制 sbit fmq=P1^3; //蜂鸣器 sbit bjdg=P1^0; //报警灯光 sbit RS =P2^5; //定义液晶屏定义端口 sbit RW = P2^6; //定义液晶屏定义端口 sbit EN = P2^7; //定义液晶屏定义端口 sbit DQ=P3^0; //ds18b20 端口 #define RS_CLRRS=0 #define RS_SET RS=1 #define RW_CLRRW=0 #define RW_SETRW=1 #define EN_CLREN=0 #define EN_SETEN=1 unsigned charaa[]={' ',' ','D','i','s','t','a','n','c','e',':'}; //Distance unsigned charbb[11]={' ',' ',' ',' ','.',' ',' ',' ',' ',' ',}; unsigned charcc[]={'A','.','A','A','m'}; unsigned chardd[] ={'S','H','U','I','-','W','E','I','-','J','I','A','N','-','C','E'}; //unsigned charee[] ={' ',' ',' ',' ','J','I','A','N','-','C','E',' ',' ',' ',' ',' '}; unsigned charff[] ={' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; unsigned charzf,a1,a2,a3,xs,e,n,m,z; int jq=0; unsigned intdz,k,s,j,bgz,k; int temp; floattemperature,csbc,wdz; bit wh; /*--------------------------------------函数声明-----------------------------------------*/ unsigned intReadTemperature(void); bitInit_DS18B20(void); unsigned charReadOneChar(void); voidWriteOneChar(unsigned char dat); voidLCD_Write_String(unsigned char x,unsigned char y,unsigned char *s); voidLCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data); void init(); voidwrite_com(unsigned char com); voidwrite_data(unsigned char date); voidDelay400Ms(void); voidDelayUs2x(unsigned char t); voidDelayMs(unsigned char t); voidDelayUs2x(unsigned char t) { while(--t); } voidDelayMs(unsigned char t) { while(t--) //大致延时1mS { DelayUs2x(245); DelayUs2x(245); } } /*--------------------------------------18b20初始化----------------------------------------*/ bitInit_DS18B20(void) { bit dat=0; DQ = 1; //DQ复位 DelayUs2x(5); //稍做延时 DQ =0; //单片机将DQ拉低 DelayUs2x(200); //精确延时 大于480us 小于960us DelayUs2x(200); DQ =1; //拉高总线 DelayUs2x(50); //15~60us 后 接收60-240us的存在脉冲 dat=DQ; //如果x=0则初始化成功, x=1则初始化失败 DelayUs2x(25); //稍作延时返回 returndat; } /*-------------------------------------读取一个字节--------------------------------------*/ unsigned charReadOneChar(void) { unsigned char i=0; unsigned char dat = 0; for (i=8;i>0;i--) { DQ= 0; // 给脉冲信号 dat>>=1; DQ= 1; // 给脉冲信号 if(DQ) dat|=0x80; DelayUs2x(25); } return(dat); } /*----------------------------------写入一个字节---------------------------------------*/ voidWriteOneChar(unsigned char dat) { unsignedchar i=0; for(i=8; i>0; i--) { DQ= 0; DQ= dat&0x01; DelayUs2x(25); DQ= 1; dat>>=1; } DelayUs2x(25); } /*---------------------------------读取温度-----------------------------------*/ unsigned intReadTemperature(void) { unsigned char a=0; int b=0; int t=0; float tt=0; while(Init_DS18B20()); //检测初始化是否成功 WriteOneChar(0xCC); // 跳过读序号列号的操作 WriteOneChar(0x44); // 启动温度转换 DelayMs(10); Init_DS18B20(); WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0xBE); //读取温度寄存器等(共可读9个) 前两个是温度 a=ReadOneChar(); //低位 b=ReadOneChar(); //高位 t=b; t<<=8; t=t|a; tt=t*0.0625; t=tt*10+0.5; return(t); } main() { TH0=0; TL0=0; TMOD=0X19; //T1,T0为16位定时器 EA=0; bg=0; n=0; m=0; z=0; init(); LCD_Write_String(0,0,dd); DelayMs(100); DelayMs(100); DelayMs(100); DelayMs(100); LCD_Write_String(0,0,ff); Init_DS18B20(); //初始化DS18B20温度传感器 dz=80; bb[6]=0xdf; bb[7]=0x43; e=4; cc[0]=dz/100+'0'; cc[2]=dz/10%10+'0'; cc[3]=dz%10+'0'; LCD_Write_String(0,0,aa); LCD_Write_String(11,1,cc); while(1) { temp=ReadTemperature(); //测量温度 fmq=1; DelayMs(100); if(temp<0) //温度正负值判断处理 { temp=-(temp-1); zf=1; bb[0]='-'; } else { zf=0; bb[0]=' '; } csb=1; //启动一次HC-SR04超声波测距模块 _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); csb=0; ET0=1; //启动计数器T0,用以计时 TR0=1; EA=1; j=60; //延时 while(j--) { } csbint=1; j=0
|