前幾天做好了板子 最近幾天一直在寫程序
經過幾天的奮斗(并不) 總算寫完了!
俗話說: 電路簡單 程序就麻煩 我算是體會到了 555
以后再也不圖省事了 軟件可比硬件讓人頭大多啦!
這個溫度計所有的顯示部分都是一個IO來控制 用的型號是STC15W104 擴展用的2片 74HC595
當時看中它的程序空間大 結果其實也沒那么大,嘎嘎 結果EEPROM只有1K了
1.jpg (137.99 KB, 下載次數: 60)
下載附件
溫度
2021-3-21 22:25 上傳
3.jpg (114.71 KB, 下載次數: 77)
下載附件
濕度
2021-3-21 22:25 上傳
2.jpg (129.3 KB, 下載次數: 62)
下載附件
最大最小值顯示,這里顯示的是最大溫度
2021-3-21 22:25 上傳
好了 進入正題 我先自吹自擂一下 (啊?! 誰扔的臭雞蛋!)
從左到右 三個按鈕 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的世界真是美妙....我永遠喜歡單片機!
單片機源程序如下:
- #include "stc15f2k60s2.h"
- #include "dht11.h"
- #include "iaprom.h"
- #define u8 unsigned char
- #define u16 unsigned int
- u8 code SMGtable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};//最后這個是負號
- void IOinit();
- void LEDsDisplay(u8 Number,u8 position);
- void Timer0Init();
- void hc595activate();
- void hc595send (u8 sbyte);
- void Delay10ms();
- void AllDisplay(u8 Integer,u8 Decimal);
- void keyscan();
- void CheckMaxMin();
- void NormalMode();
- void TemponlyMode();
- void HumionlyMode();
- void ModeInit();
- void ModeSelect(); //選擇3種模式
- void ViewMaxMin(); //查看最大最小溫濕度
- sbit LCHCK=P3^2; //595的時鐘
- sbit SFTCK=P3^3;
- sbit sb1=P3^4; //MaxMin按鈕1 檢查max min,4 組數據切換,長按清空flash存儲區
- sbit sb2=P3^5; //mode按鈕sb2 切換模式 濕度溫度自動切換 長溫度 長濕度
- sbit sb3=P3^0; //切換時間sb3 自動切換時間調整 2.5S 5S 10S 三檔 50 100 200
- extern bit TempPoOrNe; //溫度正負位
- bit TempHumiSelect=1; //1=Temp 0=Humi
- int Temp,Humi;
- u8 Time50ms=0; //50ms標志位
- u8 SetTimes=50; //設定的時間 分別是 50(默認) 100 200
- u8 SetMode=0x00; //模式標志位 分別為 0x00 0x11 0x22 自動切換 長溫度 長濕度
- u8 ConditionLeds=0x0f; //后4位控制4個LED燈泡的亮滅 0x01 MAX ,0x02 Min ,0x04 Temp ,0x08 Humi
- void main()
- {
- IOinit();
- Delay1000ms();//延時1s 等待傳感器就位
- Timer0Init(); //定時器0初始化
- EA=1;ET0=1; //開中斷,開定時器0中斷
- GetTempAndHumi(); // 先讀取一次溫濕度
- ModeInit(); //從ROM中讀取mode 進行初始化
- while(1)
- {
- ModeSelect(); //選擇模式 每個模式也負責讀取溫度
- CheckMaxMin();
- keyscan();
- }
- }
- void keyscan()
- {
- if(sb1==0||sb2==0||sb3==0)
- {
- Delay10ms();
- if(sb1==0)
- {
- Timer0Init();
- Time50ms=0;
- while(!sb1);
- Delay10ms();
- if(Time50ms>20) //>1000ms
- {
- IapEraseSector(0x0000);
- IapProgramWord(0x0000,Temp);
- IapProgramWord(0x0002,Humi);
- IapProgramWord(0x0004,Temp);
- IapProgramWord(0x0006,Humi);
- }
- else
- {
- ViewMaxMin(); //短按操作
- }
- Timer0Init(); //復位中斷
- Time50ms=0;
- }
- if(sb2==0) //切換模式
- {
- if(SetMode==0x00) SetMode=0x11;
- else if(SetMode==0x11) SetMode=0x22;
- else if(SetMode==0x22) SetMode=0x00;
- else SetMode=0x00;
- while(!sb2);
- Delay10ms();
- IapEraseSector(0x0200); //擦除扇區
- IapProgramByte(0x0200,SetMode); //寫入更改后的模式
- Time50ms=0;
- }
- if(sb3==0&&SetMode==0x00) //調整自動切換時間 2.5s 5s 10s
- {
- while(!sb3);
- Delay10ms();
- if(SetTimes==50) SetTimes=100;
- else if(SetTimes==100) SetTimes=200;
- else if(SetTimes==200) SetTimes=50;
- else SetTimes=50;
- }
- }
- }
- void CheckMaxMin() //將讀取到的溫度濕度和存儲的最大最小值對比,如果有更大/更小的,就寫入
- {
- int MaxTemp,MaxHumi,MinTemp,MinHumi;
- Temp=TempInteger*10+TempDecimal; //把溫度轉化帶正負的Int 如24.4 -> 244
- if(TempPoOrNe!=1) Temp=Temp*-1; //判斷溫度正負
- Humi=HumiInteger*10+HumiDecimal; //濕度轉化 ,其實可以用無符號 因為濕度是不可能是負數.但是我偷懶了
- MaxTemp=IapReadWord(0x0000); //從Rom中讀取一些參數 做個留存保護
- MaxHumi=IapReadWord(0x0002);
- MinTemp=IapReadWord(0x0004);
- MinHumi=IapReadWord(0x0006);
- if(Temp>MaxTemp||Temp<MinTemp||Humi>MaxHumi||Humi<MinHumi) //如果需要更新
- {
- IapEraseSector(0x0000);
- if(Temp>MaxTemp) IapProgramWord(0x0000,Temp); else IapProgramWord(0x0000,MaxTemp);
- if(Temp<MinTemp) IapProgramWord(0x0004,Temp); else IapProgramWord(0x0004,MinTemp);
- if(Humi>MaxHumi) IapProgramWord(0x0002,Humi); else IapProgramWord(0x0002,MaxHumi);
- if(Humi<MinHumi) IapProgramWord(0x0006,Humi); else IapProgramWord(0x0006,MinHumi);
- }
-
- }
- void ModeInit()
- {
- SetMode=IapReadByte(0x0200);
- }
- void ViewMaxMin() //查看最大最小
- {
- int cache;
- u8 cache1,cache2;
- Time50ms=0;
- while(sb1) //有按下就會繼續往下執行 否則return
- {
- ConditionLeds=0x05; //最大溫度燈亮
- cache=IapReadWord(0x0000);
- if(cache<0)
- {
- TempPoOrNe=0;
- cache=cache*-1;
- }
- else TempPoOrNe=1;
- cache1=cache/10;cache2=cache%10;
- AllDisplay(cache1,cache2);//這是最大溫度
- if(Time50ms>100)
- {
- Time50ms=0;
- GetTempAndHumi();
- TempHumiSelect=1;
- return; //五秒不再按按鈕就會自動退出
- }
- }
- Delay10ms(); //按鍵消抖
- while(!sb1); //等你松手
- Delay10ms(); //按鍵消抖
- Time50ms=0;
- while(sb1) //等你繼續按下
- {
- ConditionLeds=0x09; //最大濕度燈亮
- cache=IapReadWord(0x0002);
- cache1=cache/10;cache2=cache%10;
- AllDisplay(cache1,cache2);//這是最大溫度
- if(Time50ms>100)
- {
- Time50ms=0;
- GetTempAndHumi();
- TempHumiSelect=1;
- return; //五秒不再按按鈕就會自動退出
- }
- }
- Delay10ms(); //按鍵消抖
- while(!sb1); //等你松手
- Delay10ms(); //按鍵消抖
- Time50ms=0;
- while(sb1) //等你繼續按下
- {
- ConditionLeds=0x06; //最小溫度
- cache=IapReadWord(0x0004);
- if(cache<0)
- {
- TempPoOrNe=0;
- cache=cache*-1;
- }
- else TempPoOrNe=1;
- cache1=cache/10;cache2=cache%10;
- AllDisplay(cache1,cache2);//這是最小溫度
- if(Time50ms>100)
- {
- Time50ms=0;
- GetTempAndHumi();
- TempHumiSelect=1;
- return; //五秒不再按按鈕就會自動退出
- }
- }
- Delay10ms(); //按鍵消抖
- while(!sb1); //等你松手
- Delay10ms(); //按鍵消抖
- Time50ms=0;
- while(sb1) //等你繼續按下
- {
- ConditionLeds=0x0a;
- cache=IapReadWord(0x0006);
- cache1=cache/10;cache2=cache%10;
- AllDisplay(cache1,cache2);//這是最小濕度
- if(Time50ms>100)
- {
- Time50ms=0;
- GetTempAndHumi();
- TempHumiSelect=1;
- return; //五秒不再按按鈕就會自動退出
- }
- }
- Delay10ms(); //按鍵消抖
- while(!sb1); //等你松手
- GetTempAndHumi();
- Delay10ms(); //按鍵消抖
- TempHumiSelect=1;
- Time50ms=0;
- }
- void NormalMode() //常規模式
- {
- if(TempHumiSelect)
- {
- ConditionLeds=0x04; //亮溫度燈
- AllDisplay(TempInteger,TempDecimal);
- }
- else
- {
- ConditionLeds=0x08; //亮濕度燈
- AllDisplay(HumiInteger,HumiDecimal);
- }
- if(Time50ms>SetTimes)
- {
- Time50ms=0;
- TempHumiSelect=~TempHumiSelect;
- GetTempAndHumi();//每次切換時讀取一次溫濕度
- }
- }
- void TemponlyMode() //僅溫度
- {
- ConditionLeds=0x04; //亮溫度燈
- AllDisplay(TempInteger,TempDecimal);
- if(Time50ms>10) //500ms
- {
- Time50ms=0;
- GetTempAndHumi();//每次切換時讀取一次溫濕度
- }
- }
- void HumionlyMode() //僅濕度
- {
- ConditionLeds=0x08; //亮濕度燈
- AllDisplay(HumiInteger,HumiDecimal);
- if(Time50ms>10) //500ms
- {
- Time50ms=0;
- GetTempAndHumi();//每次切換時讀取一次溫濕度
- }
- }
- void ModeSelect()
- {
- if(SetMode==0x00) NormalMode();
- else if(SetMode==0x11) TemponlyMode();
- else if(SetMode==0x22) HumionlyMode();
- else NormalMode();
- }
- void IOinit(void)
- {
- //P3 0 1 2 3 4 5 x x
- // 阻 默 默 默 阻 阻 x x
- //M1 1 0 0 0 1 1 x x
- //M0 0 0 0 0 0 0 x x
- P3M1=0x31;
- P3M0=0x00;
- }
- void LEDsDisplay(u8 Number,u8 position) //在單個數碼管和所有led燈珠顯示內容 ,會自動在第三位上加上小數點
- {
- u8 i=0,j=0xf0;
- i=(clrbit(j,position+3))+ConditionLeds; // 計算需要輸出的位選+led指示燈亮滅情況
- hc595send(i); //發送位選和狀態顯示燈狀態位
- if(position==3) hc595send(SMGtable[Number]|0x80); //第三位加個小數點
- else hc595send(SMGtable[Number]);
- hc595activate();
- }
- void AllDisplay(u8 Integer,u8 Decimal) //掃描顯示所有4個數碼管和led燈珠 //也進行顯示正負
- {
- if(TempPoOrNe!=1)
- {
- LEDsDisplay(10,1); //顯示負號位 ,否則不顯示
- }
- if(Integer/10%10!=0) //判斷十位是否需要消隱
- {
- LEDsDisplay(Integer/10%10,2); //十位
- }
- LEDsDisplay(Integer/1%10,3); //個位
- LEDsDisplay(Decimal/1%10,4); //小數點后1位
- }
- void Timer0Init() // 50ms @12Mhz 計時器0初始化
- {
- AUXR &= 0x7F; //12T
- TMOD &= 0xF0;
- TL0 = 0xB0;
- TH0 = 0x3C;
- TF0 = 0;
- TR0 = 1;
- }
- void Timer0 () interrupt 1 //計時器0中斷
- {
- Time50ms++;
- }
- void hc595send (u8 sbyte) //移位輸出8位數據至移位寄存器中 高位先進入移位寄存器
- {
- u8 i=0;
- for(i=0;i<8;i++)
- {
- SFTCK=0;
- dat11=getbit(sbyte,7-i);
- SFTCK=1;
- }
- }
- void hc595activate() //讓595輸出寄存器輸出移位寄存器保存的結果
- {
- LCHCK=0;
- LCHCK=1;
- LCHCK=0;
- }
- void Delay10ms() //@12.000MHz 軟延時10ms
- {
- unsigned char i, j;
- i = 117;
- j = 184;
- do
- {
- while (--j);
- } while (--i);
- }
復制代碼
51hei.png (3.83 KB, 下載次數: 75)
下載附件
2021-3-21 22:58 上傳
最后就是程序 僅供學習
禁止其他任何用途(如作業等)
不然與本人初衷不符
全部資料51hei下載地址:
溫濕度計.zip
(6.15 KB, 下載次數: 31)
2021-3-21 22:35 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|