找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2435|回复: 1
打印 上一主题 下一主题
收起左侧

两路am2302温湿度传感器单片机程序 ds1307时钟芯片 12864显示

[复制链接]
跳转到指定楼层
楼主
本帖最后由 accumulation 于 2019-8-20 23:39 编辑

制作出来的实物图如下:


单片机源程序如下:
  1. #include<STC15W408AS.H>
  2. #include<intrins.h>
  3. extern void InitLcd12864();
  4. extern void LcdAreaClean(unsigned char x,unsigned char y,unsigned char len);
  5. extern void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str);
  6. extern bit ReadSensor(unsigned char *buf,unsigned char *flag);
  7. extern bit ReadSensorb(unsigned char *buf,unsigned char *flag);
  8. extern void InitUART();
  9. extern void InitDS1307();
  10. extern void Ds1307Read(unsigned char *buf);
  11. extern void SendString(char *s);
  12. extern void KeyScan();
  13. extern bit KeyDrive();
  14. void ConfigTimer0();
  15. bit flag2s = 0;
  16. bit flag200ms = 0;

  17. void main()
  18. {
  19.         bit ack=0;
  20.         bit flagset;
  21.         unsigned char addr;
  22.         unsigned int humidity;
  23.         unsigned int temperature;
  24.         unsigned char showbuf[13];
  25.         unsigned char buf[8];
  26.         unsigned char flag;
  27.         unsigned char psec=0xAA;
  28.         InitLcd12864();
  29.         InitUART();
  30.         InitDS1307();
  31.         ConfigTimer0();
  32.         LcdShowStr(0,0,"湿度");
  33.         LcdShowStr(0,1,"温度");
  34.         LcdShowStr(4,0,"湿度");
  35.         LcdShowStr(4,1,"温度");
  36.         while(1)
  37.         {
  38.                 flagset=KeyDrive();
  39.                 if(flag200ms&&flagset)
  40.                 {
  41.                         flag200ms=0;        
  42.                         Ds1307Read(buf);
  43.                         if(psec!=buf[0])        
  44.                         {        
  45.                                 showbuf[0] = '2';
  46.                                 showbuf[1] = '0';
  47.                                 showbuf[2] = (buf[6]>>4) + '0';
  48.                                 showbuf[3] = (buf[6]&0x0F) + '0';
  49.                                 showbuf[4] = '-';
  50.                                 showbuf[5] = '-';
  51.                                 showbuf[6] = (buf[5]>>4) + '0';
  52.                                 showbuf[7] = (buf[5]&0x0F) + '0';
  53.                                 showbuf[8] = '-';
  54.                                 showbuf[9] = '-';
  55.                                 showbuf[10] = (buf[4]>>4) + '0';
  56.                                 showbuf[11] = (buf[4]&0x0F) + '0';
  57.                                 showbuf[12] = '\0';
  58.                                 SendString(showbuf);
  59.                                 SendString("\r\n");
  60.                                 LcdShowStr(1,2,showbuf);
  61.                                 
  62.                                 showbuf[0] = (buf[3]>>4) + '0';
  63.                                 showbuf[1] = (buf[3]&0x0F) + '0';
  64.                                 showbuf[2] = '\0';
  65.                                 SendString(showbuf);
  66.                                 SendString("\r\n");
  67.                                 LcdShowStr(7,3,showbuf);
  68.                                 
  69.                                 showbuf[0] = (buf[2]>>4) + '0';
  70.                                 showbuf[1] = (buf[2]&0x0F) + '0';
  71.                                 showbuf[2] = ':';
  72.                                 showbuf[3] = ':';
  73.                                 showbuf[4] = (buf[1]>>4) + '0';
  74.                                 showbuf[5] = (buf[1]&0x0F) + '0';
  75.                                 showbuf[6] = ':';
  76.                                 showbuf[7] = ':';
  77.                                 showbuf[8] = (buf[0]>>4) + '0';
  78.                                 showbuf[9] = (buf[0]&0x0F) + '0';
  79.                                 showbuf[10] = '\0';
  80.                                 SendString(showbuf);
  81.                                 SendString("\r\n");
  82.                                 LcdShowStr(1,3,showbuf);
  83.                                 psec=buf[0];
  84.                         }
  85.                 }
  86.         
  87.                 if(flag2s&&flagset)
  88.                 {        
  89.                 flag2s = 0;
  90.                
  91.                 ack=ReadSensor(buf,&flag);
  92.                 switch (flag)
  93.                         {
  94.                         case 0: SendString("传感器A发送开始信号\r\n");break;
  95.                         case 1: SendString("传感器A收到起始信号\r\n");break;
  96.                         case 2: SendString("传感器A起始1信号超时\r\n");break;
  97.                         case 3: SendString("传感器A起始2信号超时\r\n");break;
  98.                         case 4: SendString("传感器A未收到起始信号\r\n");break;
  99.                         default : break;
  100.                         }
  101.                 if(ack)
  102.                         {
  103.                                 ack=0;
  104.                                 addr = buf[0]+buf[1]+buf[2]+buf[3];
  105.                                 if(addr==buf[4])
  106.                                 {
  107.                                         humidity=buf[0]*256+buf[1];
  108.                                         showbuf[0] = '0'+((humidity/100)%10);
  109.                                         showbuf[1] = '0'+((humidity/10)%10);
  110.                                         showbuf[2] = '.';
  111.                                         showbuf[3] = '0'+(humidity%10);
  112.                                         showbuf[4] = '\0';
  113.                                         SendString(showbuf);
  114.                                         SendString("\r\n");
  115.                                         LcdShowStr(2,0,showbuf);
  116.                                        
  117.                                         temperature = (buf[2]*256)+buf[3];        
  118.                                         showbuf[0] = '0'+((temperature/100)%10);
  119.                                         showbuf[1] = '0'+((temperature/10)%10);
  120.                                         showbuf[2] = '.';
  121.                                         showbuf[3] = '0'+(temperature%10);
  122.                                         showbuf[4] = '\0';
  123.                                         SendString(showbuf);
  124.                                         SendString("\r\n");
  125.                                         LcdShowStr(2,1,showbuf);
  126.                                        
  127.                                 }
  128.                                 else
  129.                                         SendString("传感器A效验错误\r\n");
  130.                                 }
  131.                 else
  132.                         SendString("传感器A无返回值\r\n");
  133.                 ack=ReadSensorb(buf,&flag);
  134.                 switch (flag)
  135.                 {
  136.                         case 0: SendString("传感器B发送开始信号\r\n");break;
  137.                         case 1: SendString("传感器B收到起始信号\r\n");break;
  138.                         case 2: SendString("传感器B起始1信号超时\r\n");break;
  139.                         case 3: SendString("传感器B起始2信号超时\r\n");break;
  140.                         case 4: SendString("传感器B未收到起始信号\r\n");break;
  141.                         default : break;
  142.                 }
  143.                 if(ack)
  144.                 {
  145.                         ack=0;
  146.                
  147.                         addr = buf[0]+buf[1]+buf[2]+buf[3];
  148.                         if(addr==buf[4])
  149.                         {
  150.                                 humidity=buf[0]*256+buf[1];
  151.                                 showbuf[0] = '0'+((humidity/100)%10);
  152.                                 showbuf[1] = '0'+((humidity/10)%10);
  153.                                 showbuf[2] = '.';
  154.                                 showbuf[3] = '0'+(humidity%10);
  155.                                 showbuf[4] = '\0';
  156.                                 SendString(showbuf);
  157.                                 SendString("\r\n");
  158.                                 LcdShowStr(6,0,showbuf);
  159.                                 temperature = (buf[2]*256)+buf[3];        
  160.                                 showbuf[0] = '0'+((temperature/100)%10);
  161.                                 showbuf[1] = '0'+((temperature/10)%10);
  162.                                 showbuf[2] = '.';
  163.                                 showbuf[3] = '0'+(temperature%10);
  164.                                 showbuf[4] = '\0';
  165.                                 SendString(showbuf);
  166.                                 SendString("\r\n");
  167.                                 LcdShowStr(6,1,showbuf);
  168.                         }
  169.                         else
  170.                                 SendString("传感器B效验错误\r\n");
  171.                 }
  172.                 else
  173.                         SendString("传感器B无返回值\r\n");
  174.                 }
  175.         }
  176. }

  177. void ConfigTimer0()
  178. {
  179.         AUXR |= 0x80;
  180.         TMOD &= 0xF0;
  181.         TH0 = (65536-33177600/1000)>>8;
  182.         TL0 = 65536-33177600/1000;
  183.         TR0 = 1;
  184.         ET0 = 1;
  185.         EA = 1;
  186. }
  187. void InterruptTimer0() interrupt 1
  188. {
  189.         static unsigned int time2s = 0;
  190.         static unsigned char time200ms = 0;
  191.         time2s++;
  192.         time200ms++;
  193.         if(time200ms >= 200)
  194.         {
  195.                 time200ms = 0;
  196.                 flag200ms = 1;
  197.         }
  198.         if(time2s >= 2000)
  199.         {
  200.                 time2s = 0;
  201.                 flag2s = 1;
  202.         }
  203.         KeyScan();
  204. }
复制代码

所有程序资料51hei提供下载:
15w.rar (94.58 KB, 下载次数: 50)



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:601639 发表于 2020-6-30 18:59 | 只看该作者
很好,功能强大。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表