1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include "LCD1602.H" #include "DS18B20.h" #include "ADC0832.h"
extern unsigned char ReadAdc0832(unsigned char channel); extern void DelayMs(unsigned char t); unsigned int Temp[5];//5路温度值温度放大10倍 int DS18B20DATA[3];//DS18B20读到的16位数据 code unsigned char DecimalNum[16]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};//DS18B20小数部分对应的数 unsigned char code DuanMa[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; sbit LED=P3^1; sbit BEEP=P3^2; sbit LE=P3^0; sbit Key=P1^0; unsigned char CurID; // 显示段码值01234567 unsigned char LCDbuf[16]; void LM35Read(void) { Temp[0]=ReadAdc0832(0)\*10; DelayMs(10); Temp[1]=ReadAdc0832(1)\*10; DelayMs(10); } void DS18B20Read() { gettemp(DS18B20DATA); Temp[2]=(DS18B20DATA[0]>>4)\*10+DecimalNum[DS18B20DATA[0]&0x0f]; Temp[3]=(DS18B20DATA[1]>>4)\*10+DecimalNum[DS18B20DATA[1]&0x0f]; Temp[4]=(DS18B20DATA[2]>>4)\*10+DecimalNum[DS18B20DATA[2]&0x0f];//环境温度 }
//格式化数据 void ForamtData(int Data,unsigned char \*Str) { unsigned char t; t=Data/10; if(t>99) { \*Str++='1'; \*Str++=(t%100)/10+0x30; \*Str++=(t%10)+0x30; \*Str++='.'; \*Str++=Data%10+0x30; } if((t>9)&&(t<100)) { \*Str++=' '; \*Str++=t/10+0x30; \*Str++=(t%10)+0x30; \*Str++='.'; \*Str++=Data%10+0x30; } if((t<10)&&(t>0)) { \*Str++=' '; \*Str++=' '; \*Str++=(t%10)+0x30; \*Str++='.'; \*Str++=Data%10+0x30; } if(t==0) { \*Str++=' '; \*Str++=' '; \*Str++=' '; \*Str++='.'; \*Str++=Data%10+0x30; } \*Str=0; } //报警 void Warning(void) { unsigned char flag;//,//i; static unsigned int a,b; flag=0; a=Temp[4]+50; b=Temp[4]-50; //for(i=0;i<5;i++) //{ flag=(Temp[CurID]>(a)); flag=(Temp[CurID]<b)||flag; BEEP=!flag; LED=!flag; //}
} void main(void) { LE=1; P0=DuanMa[CurID+1]; LE=0; LCD\_Init(); LCD\_Write\_String(0,0,"Envir: C"); LCD\_Write\_String(5,1,"C "); LED=0; CurID=3; while(1) { LM35Read(); DS18B20Read(); LE=1; P0=DuanMa[CurID+1]; LE=0; ForamtData(Temp[4],LCDbuf); LCD\_Write\_String(6,0,LCDbuf); ForamtData(Temp[CurID],LCDbuf); LCD\_Write\_String(0,1,LCDbuf); Warning(); if(Key==0) { DelayMs(10); if(Key==0) CurID=(CurID+1)%4; while(Key==0); } // DelayMs(100); } }
|