本帖最后由 accumulation 于 2019-8-20 23:39 编辑
制作出来的实物图如下:
单片机源程序如下:
- #include<STC15W408AS.H>
- #include<intrins.h>
- extern void InitLcd12864();
- extern void LcdAreaClean(unsigned char x,unsigned char y,unsigned char len);
- extern void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str);
- extern bit ReadSensor(unsigned char *buf,unsigned char *flag);
- extern bit ReadSensorb(unsigned char *buf,unsigned char *flag);
- extern void InitUART();
- extern void InitDS1307();
- extern void Ds1307Read(unsigned char *buf);
- extern void SendString(char *s);
- extern void KeyScan();
- extern bit KeyDrive();
- void ConfigTimer0();
- bit flag2s = 0;
- bit flag200ms = 0;
- void main()
- {
- bit ack=0;
- bit flagset;
- unsigned char addr;
- unsigned int humidity;
- unsigned int temperature;
- unsigned char showbuf[13];
- unsigned char buf[8];
- unsigned char flag;
- unsigned char psec=0xAA;
- InitLcd12864();
- InitUART();
- InitDS1307();
- ConfigTimer0();
- LcdShowStr(0,0,"湿度");
- LcdShowStr(0,1,"温度");
- LcdShowStr(4,0,"湿度");
- LcdShowStr(4,1,"温度");
- while(1)
- {
- flagset=KeyDrive();
- if(flag200ms&&flagset)
- {
- flag200ms=0;
- Ds1307Read(buf);
- if(psec!=buf[0])
- {
- showbuf[0] = '2';
- showbuf[1] = '0';
- showbuf[2] = (buf[6]>>4) + '0';
- showbuf[3] = (buf[6]&0x0F) + '0';
- showbuf[4] = '-';
- showbuf[5] = '-';
- showbuf[6] = (buf[5]>>4) + '0';
- showbuf[7] = (buf[5]&0x0F) + '0';
- showbuf[8] = '-';
- showbuf[9] = '-';
- showbuf[10] = (buf[4]>>4) + '0';
- showbuf[11] = (buf[4]&0x0F) + '0';
- showbuf[12] = '\0';
- SendString(showbuf);
- SendString("\r\n");
- LcdShowStr(1,2,showbuf);
-
- showbuf[0] = (buf[3]>>4) + '0';
- showbuf[1] = (buf[3]&0x0F) + '0';
- showbuf[2] = '\0';
- SendString(showbuf);
- SendString("\r\n");
- LcdShowStr(7,3,showbuf);
-
- showbuf[0] = (buf[2]>>4) + '0';
- showbuf[1] = (buf[2]&0x0F) + '0';
- showbuf[2] = ':';
- showbuf[3] = ':';
- showbuf[4] = (buf[1]>>4) + '0';
- showbuf[5] = (buf[1]&0x0F) + '0';
- showbuf[6] = ':';
- showbuf[7] = ':';
- showbuf[8] = (buf[0]>>4) + '0';
- showbuf[9] = (buf[0]&0x0F) + '0';
- showbuf[10] = '\0';
- SendString(showbuf);
- SendString("\r\n");
- LcdShowStr(1,3,showbuf);
- psec=buf[0];
- }
- }
-
- if(flag2s&&flagset)
- {
- flag2s = 0;
-
- ack=ReadSensor(buf,&flag);
- switch (flag)
- {
- case 0: SendString("传感器A发送开始信号\r\n");break;
- case 1: SendString("传感器A收到起始信号\r\n");break;
- case 2: SendString("传感器A起始1信号超时\r\n");break;
- case 3: SendString("传感器A起始2信号超时\r\n");break;
- case 4: SendString("传感器A未收到起始信号\r\n");break;
- default : break;
- }
- if(ack)
- {
- ack=0;
- addr = buf[0]+buf[1]+buf[2]+buf[3];
- if(addr==buf[4])
- {
- humidity=buf[0]*256+buf[1];
- showbuf[0] = '0'+((humidity/100)%10);
- showbuf[1] = '0'+((humidity/10)%10);
- showbuf[2] = '.';
- showbuf[3] = '0'+(humidity%10);
- showbuf[4] = '\0';
- SendString(showbuf);
- SendString("\r\n");
- LcdShowStr(2,0,showbuf);
-
- temperature = (buf[2]*256)+buf[3];
- showbuf[0] = '0'+((temperature/100)%10);
- showbuf[1] = '0'+((temperature/10)%10);
- showbuf[2] = '.';
- showbuf[3] = '0'+(temperature%10);
- showbuf[4] = '\0';
- SendString(showbuf);
- SendString("\r\n");
- LcdShowStr(2,1,showbuf);
-
- }
- else
- SendString("传感器A效验错误\r\n");
- }
- else
- SendString("传感器A无返回值\r\n");
- ack=ReadSensorb(buf,&flag);
- switch (flag)
- {
- case 0: SendString("传感器B发送开始信号\r\n");break;
- case 1: SendString("传感器B收到起始信号\r\n");break;
- case 2: SendString("传感器B起始1信号超时\r\n");break;
- case 3: SendString("传感器B起始2信号超时\r\n");break;
- case 4: SendString("传感器B未收到起始信号\r\n");break;
- default : break;
- }
- if(ack)
- {
- ack=0;
-
- addr = buf[0]+buf[1]+buf[2]+buf[3];
- if(addr==buf[4])
- {
- humidity=buf[0]*256+buf[1];
- showbuf[0] = '0'+((humidity/100)%10);
- showbuf[1] = '0'+((humidity/10)%10);
- showbuf[2] = '.';
- showbuf[3] = '0'+(humidity%10);
- showbuf[4] = '\0';
- SendString(showbuf);
- SendString("\r\n");
- LcdShowStr(6,0,showbuf);
- temperature = (buf[2]*256)+buf[3];
- showbuf[0] = '0'+((temperature/100)%10);
- showbuf[1] = '0'+((temperature/10)%10);
- showbuf[2] = '.';
- showbuf[3] = '0'+(temperature%10);
- showbuf[4] = '\0';
- SendString(showbuf);
- SendString("\r\n");
- LcdShowStr(6,1,showbuf);
- }
- else
- SendString("传感器B效验错误\r\n");
- }
- else
- SendString("传感器B无返回值\r\n");
- }
- }
- }
- void ConfigTimer0()
- {
- AUXR |= 0x80;
- TMOD &= 0xF0;
- TH0 = (65536-33177600/1000)>>8;
- TL0 = 65536-33177600/1000;
- TR0 = 1;
- ET0 = 1;
- EA = 1;
- }
- void InterruptTimer0() interrupt 1
- {
- static unsigned int time2s = 0;
- static unsigned char time200ms = 0;
- time2s++;
- time200ms++;
- if(time200ms >= 200)
- {
- time200ms = 0;
- flag200ms = 1;
- }
- if(time2s >= 2000)
- {
- time2s = 0;
- flag2s = 1;
- }
- KeyScan();
- }
复制代码
所有程序资料51hei提供下载:
15w.rar
(94.58 KB, 下载次数: 51)
|