找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4292|回复: 2
收起左侧

奇葩温度计完工啦! 分享代码给大家看看

  [复制链接]
ID:518902 发表于 2021-3-21 22:36 | 显示全部楼层 |阅读模式
前几天做好了板子 最近几天一直在写程序
经过几天的奋斗(并不)  总算写完了!
俗话说: 电路简单 程序就麻烦 我算是体会到了 555
以后再也不图省事了 软件可比硬件让人头大多啦!
这个温度计所有的显示部分都是一个IO来控制 用的型号是STC15W104  扩展用的2片 74HC595
当时看中它的程序空间大 结果其实也没那么大,嘎嘎 结果EEPROM只有1K了

温度

温度

湿度

湿度

最大最小值显示,这里显示的是最大温度

最大最小值显示,这里显示的是最大温度


好了 进入正题 我先自吹自擂一下 (啊?! 谁扔的臭鸡蛋!)
从左到右 三个按钮 1 2 3
按钮1:短按 查看最大最小温湿度 每按一次切换显示1个  这些数据全部存储在内部的FlashRom中 会自动更新
长按 清除内部FlashRom存储的数据 并用当前的测量值全部填充
按钮2:显示模式轮换,三个模式:温湿度自动轮换 仅温度 仅湿度
其中温湿度自动轮换时间可调  显示的数值是每次切换温湿度的时候测量的
仅温度 仅湿度 这两个的数据是 每半秒更新一次(不可调 但是可以在编译前调好,我设置的是半秒)
这2个模式貌似每次更新的时候 LED会闪一下 没办法 设计缺陷555 谁叫他1个IO干完全部的显示呢?
按钮3:切换自动轮换的时间 ,我设置了3个挡位 2.5s 5s 10s ,同时这个还会自动存储在FlashROM里,只需要设定时间一次~~

P.S 最后加点感想: 这次又学到了很多东西,还专门写了2个C文件模块,模块化编程好啊~ 赞美C语言!
昨天晚上写这个程序,写的头很大,来来回回删了又写写了又删 ,起码删了300行,可谓是绞尽脑汁,我那10毫升的大脑都要变成浆糊啦!
今晚折腾出来了 我甚开心,赶紧拍下照片发帖留念了,MCU的世界真是美妙....我永远喜欢单片机!

单片机源程序如下:
  1. #include "stc15f2k60s2.h"
  2. #include "dht11.h"
  3. #include "iaprom.h"

  4. #define u8 unsigned char
  5. #define u16 unsigned int

  6. u8 code SMGtable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};//最后这个是负号

  7. void IOinit();
  8. void LEDsDisplay(u8 Number,u8 position);
  9. void Timer0Init();
  10. void hc595activate();
  11. void hc595send (u8 sbyte);
  12. void Delay10ms();
  13. void AllDisplay(u8 Integer,u8 Decimal);
  14. void keyscan();
  15. void CheckMaxMin();
  16. void NormalMode();
  17. void TemponlyMode();
  18. void HumionlyMode();
  19. void ModeInit();

  20. void ModeSelect();                   //选择3种模式
  21. void ViewMaxMin();         //查看最大最小温湿度

  22. sbit LCHCK=P3^2;                   //595的时钟
  23. sbit SFTCK=P3^3;

  24. sbit sb1=P3^4;                        //MaxMin按钮1          检查max min,4 组数据切换,长按清空flash存储区
  25. sbit sb2=P3^5;                        //mode按钮sb2   切换模式 湿度温度自动切换 长温度 长湿度
  26. sbit sb3=P3^0;                        //切换时间sb3        自动切换时间调整   2.5S  5S 10S 三档 50 100 200

  27. extern bit TempPoOrNe;         //温度正负位
  28. bit TempHumiSelect=1; //1=Temp 0=Humi

  29. int Temp,Humi;

  30. u8 Time50ms=0;                 //50ms标志位
  31. u8 SetTimes=50;                 //设定的时间 分别是 50(默认) 100 200
  32. u8 SetMode=0x00;        //模式标志位 分别为 0x00 0x11 0x22   自动切换 长温度 长湿度
  33. u8 ConditionLeds=0x0f;          //后4位控制4个LED灯泡的亮灭 0x01 MAX ,0x02 Min ,0x04 Temp ,0x08 Humi


  34. void main()
  35. {
  36.         IOinit();
  37.         Delay1000ms();//延时1s 等待传感器就位
  38.         Timer0Init();          //定时器0初始化
  39.         EA=1;ET0=1;                  //开中断,开定时器0中断
  40.         GetTempAndHumi();  // 先读取一次温湿度
  41.         ModeInit();       //从ROM中读取mode 进行初始化
  42.         while(1)
  43.         {
  44.                 ModeSelect();                //选择模式 每个模式也负责读取温度
  45.                 CheckMaxMin();
  46.                 keyscan();
  47.         }       
  48. }

  49. void keyscan()
  50. {
  51.         if(sb1==0||sb2==0||sb3==0)
  52.         {
  53.                 Delay10ms();
  54.                 if(sb1==0)
  55.                 {
  56.                         Timer0Init();
  57.                         Time50ms=0;
  58.                         while(!sb1);
  59.                         Delay10ms();
  60.                         if(Time50ms>20)          //>1000ms
  61.                         {
  62.                                 IapEraseSector(0x0000);
  63.                                 IapProgramWord(0x0000,Temp);
  64.                                 IapProgramWord(0x0002,Humi);
  65.                                 IapProgramWord(0x0004,Temp);
  66.                                 IapProgramWord(0x0006,Humi);
  67.                         }
  68.                         else
  69.                         {
  70.                                 ViewMaxMin();        //短按操作
  71.                         }
  72.                         Timer0Init();                 //复位中断
  73.                         Time50ms=0;
  74.                 }
  75.                 if(sb2==0)                                         //切换模式
  76.                 {
  77.                         if(SetMode==0x00) SetMode=0x11;
  78.                         else if(SetMode==0x11) SetMode=0x22;
  79.                         else if(SetMode==0x22) SetMode=0x00;
  80.                         else SetMode=0x00;
  81.                         while(!sb2);
  82.                         Delay10ms();
  83.                         IapEraseSector(0x0200);                    //擦除扇区
  84.                         IapProgramByte(0x0200,SetMode);        //写入更改后的模式
  85.                         Time50ms=0;
  86.                 }
  87.                 if(sb3==0&&SetMode==0x00)                                 //调整自动切换时间 2.5s 5s 10s
  88.                 {
  89.                         while(!sb3);
  90.                         Delay10ms();
  91.                         if(SetTimes==50) SetTimes=100;
  92.                         else if(SetTimes==100) SetTimes=200;
  93.                         else if(SetTimes==200) SetTimes=50;
  94.                         else SetTimes=50;
  95.                 }
  96.         }
  97. }
  98. void CheckMaxMin()         //将读取到的温度湿度和存储的最大最小值对比,如果有更大/更小的,就写入
  99. {
  100.         int MaxTemp,MaxHumi,MinTemp,MinHumi;
  101.         Temp=TempInteger*10+TempDecimal;                //把温度转化带正负的Int 如24.4 -> 244
  102.         if(TempPoOrNe!=1) Temp=Temp*-1;                        //判断温度正负
  103.         Humi=HumiInteger*10+HumiDecimal;                //湿度转化 ,其实可以用无符号 因为湿度是不可能是负数.但是我偷懒了
  104.         MaxTemp=IapReadWord(0x0000);                        //从Rom中读取一些参数        做个留存保护
  105.         MaxHumi=IapReadWord(0x0002);
  106.         MinTemp=IapReadWord(0x0004);
  107.         MinHumi=IapReadWord(0x0006);
  108.         if(Temp>MaxTemp||Temp<MinTemp||Humi>MaxHumi||Humi<MinHumi) //如果需要更新
  109.         {
  110.                 IapEraseSector(0x0000);
  111.                 if(Temp>MaxTemp) IapProgramWord(0x0000,Temp); else IapProgramWord(0x0000,MaxTemp);
  112.                 if(Temp<MinTemp) IapProgramWord(0x0004,Temp); else IapProgramWord(0x0004,MinTemp);
  113.                 if(Humi>MaxHumi) IapProgramWord(0x0002,Humi); else IapProgramWord(0x0002,MaxHumi);
  114.                 if(Humi<MinHumi) IapProgramWord(0x0006,Humi); else IapProgramWord(0x0006,MinHumi);
  115.         }

  116.                
  117. }
  118. void ModeInit()
  119. {
  120.          SetMode=IapReadByte(0x0200);
  121. }
  122. void ViewMaxMin()                        //查看最大最小
  123. {
  124.          int cache;
  125.          u8 cache1,cache2;
  126.          Time50ms=0;
  127.          while(sb1)                        //有按下就会继续往下执行 否则return
  128.          {
  129.                  ConditionLeds=0x05;                                           //最大温度灯亮
  130.                 cache=IapReadWord(0x0000);
  131.                 if(cache<0)
  132.                 {
  133.                 TempPoOrNe=0;
  134.                 cache=cache*-1;
  135.                 }
  136.                 else TempPoOrNe=1;
  137.                 cache1=cache/10;cache2=cache%10;
  138.                 AllDisplay(cache1,cache2);//这是最大温度
  139.                 if(Time50ms>100)
  140.                 {
  141.                         Time50ms=0;
  142.                         GetTempAndHumi();
  143.                         TempHumiSelect=1;
  144.                         return;     //五秒不再按按钮就会自动退出
  145.                 }
  146.          }
  147.          Delay10ms(); //按键消抖
  148.          while(!sb1); //等你松手
  149.          Delay10ms(); //按键消抖
  150.          Time50ms=0;
  151.          while(sb1)        //等你继续按下
  152.          {
  153.                 ConditionLeds=0x09;                           //最大湿度灯亮
  154.                 cache=IapReadWord(0x0002);
  155.                 cache1=cache/10;cache2=cache%10;
  156.                 AllDisplay(cache1,cache2);//这是最大温度
  157.                 if(Time50ms>100)
  158.                 {
  159.                         Time50ms=0;
  160.                         GetTempAndHumi();
  161.                         TempHumiSelect=1;
  162.                         return;     //五秒不再按按钮就会自动退出
  163.                 }
  164.          }
  165.          Delay10ms(); //按键消抖
  166.          while(!sb1); //等你松手
  167.          Delay10ms(); //按键消抖
  168.          Time50ms=0;
  169.          while(sb1)        //等你继续按下
  170.          {
  171.                 ConditionLeds=0x06;                                          //最小温度
  172.                 cache=IapReadWord(0x0004);
  173.                 if(cache<0)
  174.                 {
  175.                 TempPoOrNe=0;
  176.                 cache=cache*-1;
  177.                 }
  178.                 else TempPoOrNe=1;
  179.                 cache1=cache/10;cache2=cache%10;
  180.                 AllDisplay(cache1,cache2);//这是最小温度
  181.                 if(Time50ms>100)
  182.                 {
  183.                         Time50ms=0;
  184.                         GetTempAndHumi();
  185.                         TempHumiSelect=1;
  186.                         return;     //五秒不再按按钮就会自动退出
  187.                 }
  188.          }
  189.          Delay10ms(); //按键消抖
  190.          while(!sb1); //等你松手
  191.          Delay10ms(); //按键消抖
  192.          Time50ms=0;
  193.          while(sb1)        //等你继续按下
  194.          {
  195.                 ConditionLeds=0x0a;
  196.                 cache=IapReadWord(0x0006);
  197.                 cache1=cache/10;cache2=cache%10;
  198.                 AllDisplay(cache1,cache2);//这是最小湿度
  199.                 if(Time50ms>100)
  200.                 {
  201.                         Time50ms=0;
  202.                         GetTempAndHumi();
  203.                         TempHumiSelect=1;
  204.                         return;     //五秒不再按按钮就会自动退出
  205.                 }
  206.          }
  207.          Delay10ms(); //按键消抖
  208.          while(!sb1); //等你松手
  209.          GetTempAndHumi();
  210.          Delay10ms(); //按键消抖
  211.          TempHumiSelect=1;
  212.          Time50ms=0;
  213. }
  214. void NormalMode()                        //常规模式
  215. {
  216.         if(TempHumiSelect)
  217.         {
  218.                 ConditionLeds=0x04;                //亮温度灯
  219.                 AllDisplay(TempInteger,TempDecimal);       
  220.         }
  221.         else
  222.         {
  223.                 ConditionLeds=0x08;          //亮湿度灯
  224.                 AllDisplay(HumiInteger,HumiDecimal);
  225.         }
  226.         if(Time50ms>SetTimes)
  227.         {
  228.                 Time50ms=0;
  229.                 TempHumiSelect=~TempHumiSelect;
  230.                 GetTempAndHumi();//每次切换时读取一次温湿度
  231.         }
  232. }
  233. void TemponlyMode()                   //仅温度
  234. {
  235.         ConditionLeds=0x04;                //亮温度灯
  236.         AllDisplay(TempInteger,TempDecimal);
  237.         if(Time50ms>10)                   //500ms
  238.         {
  239.                 Time50ms=0;
  240.                 GetTempAndHumi();//每次切换时读取一次温湿度
  241.         }
  242. }
  243. void HumionlyMode()                        //仅湿度
  244. {
  245.         ConditionLeds=0x08;          //亮湿度灯
  246.         AllDisplay(HumiInteger,HumiDecimal);
  247.         if(Time50ms>10)                 //500ms
  248.         {
  249.                 Time50ms=0;
  250.                 GetTempAndHumi();//每次切换时读取一次温湿度
  251.         }
  252. }
  253. void ModeSelect()
  254. {
  255.         if(SetMode==0x00) NormalMode();
  256.         else if(SetMode==0x11) TemponlyMode();
  257.         else if(SetMode==0x22) HumionlyMode();
  258.         else NormalMode();
  259. }
  260. void IOinit(void)
  261. {
  262. //P3 0  1  2  3  4  5  x  x
  263. //         阻 默 默 默 阻 阻 x  x
  264. //M1 1  0  0  0  1  1  x  x
  265. //M0 0  0  0  0  0  0  x  x
  266. P3M1=0x31;
  267. P3M0=0x00;       
  268. }

  269. void LEDsDisplay(u8 Number,u8 position)         //在单个数码管和所有led灯珠显示内容 ,会自动在第三位上加上小数点
  270. {
  271.         u8 i=0,j=0xf0;
  272.         i=(clrbit(j,position+3))+ConditionLeds;         //        计算需要输出的位选+led指示灯亮灭情况
  273.         hc595send(i);           //发送位选和状态显示灯状态位
  274.         if(position==3) hc595send(SMGtable[Number]|0x80);           //第三位加个小数点
  275.         else hc595send(SMGtable[Number]);
  276.         hc595activate();                       
  277. }

  278. void AllDisplay(u8 Integer,u8 Decimal)                 //扫描显示所有4个数码管和led灯珠        //也进行显示正负
  279. {
  280.         if(TempPoOrNe!=1)
  281.         {
  282.         LEDsDisplay(10,1);         //显示负号位 ,否则不显示
  283.         }
  284.         if(Integer/10%10!=0)          //判断十位是否需要消隐
  285.         {
  286.         LEDsDisplay(Integer/10%10,2);         //十位
  287.         }
  288.         LEDsDisplay(Integer/1%10,3);         //个位
  289.         LEDsDisplay(Decimal/1%10,4);         //小数点后1位
  290. }

  291. void Timer0Init()                //        50ms @12Mhz 计时器0初始化
  292. {
  293.         AUXR &= 0x7F;                //12T
  294.         TMOD &= 0xF0;       
  295.         TL0 = 0xB0;               
  296.         TH0 = 0x3C;               
  297.         TF0 = 0;               
  298.         TR0 = 1;
  299. }

  300. void Timer0 () interrupt 1          //计时器0中断
  301. {
  302.         Time50ms++;
  303. }

  304. void hc595send (u8 sbyte)        //移位输出8位数据至移位寄存器中          高位先进入移位寄存器
  305. {
  306.         u8 i=0;
  307.         for(i=0;i<8;i++)
  308.         {
  309.                 SFTCK=0;
  310.                 dat11=getbit(sbyte,7-i);
  311.                 SFTCK=1;
  312.         }
  313. }

  314. void hc595activate()                //让595输出寄存器输出移位寄存器保存的结果
  315. {
  316.         LCHCK=0;
  317.         LCHCK=1;
  318.         LCHCK=0;       
  319. }


  320. void Delay10ms()                //@12.000MHz  软延时10ms
  321. {
  322.         unsigned char i, j;

  323.         i = 117;
  324.         j = 184;
  325.         do
  326.         {
  327.                 while (--j);
  328.         } while (--i);
  329. }
复制代码
51hei.png
最后就是程序 仅供学习
禁止其他任何用途(如作业等)
不然与本人初衷不符
全部资料51hei下载地址:
温湿度计.zip (6.15 KB, 下载次数: 31)

评分

参与人数 2黑币 +115 收起 理由
1339337425 + 15 很给力!
admin + 100 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:915897 发表于 2021-6-15 22:09 | 显示全部楼层
木有电路图吗?
回复

使用道具 举报

ID:300473 发表于 2021-6-16 15:52 | 显示全部楼层
大佬厉害,真希望我也能做到!!!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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