本帖最后由 accumulation 于 2019-8-20 23:39 編輯
制作出來的實物圖如下:
IMG_0462.JPG (1.89 MB, 下載次數: 31)
下載附件
2019-7-22 12:14 上傳
IMG_0461.JPG (1.76 MB, 下載次數: 30)
下載附件
2019-7-22 12:14 上傳
QQ截圖20190722121026.png (117.34 KB, 下載次數: 26)
下載附件
2019-7-22 12:14 上傳
單片機源程序如下:
- #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)
2019-7-22 12:26 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|