|
前幾天做好了板子 最近幾天一直在寫程序
經(jīng)過幾天的奮斗(并不) 總算寫完了!
俗話說: 電路簡(jiǎn)單 程序就麻煩 我算是體會(huì)到了 555
以后再也不圖省事了 軟件可比硬件讓人頭大多啦!
這個(gè)溫度計(jì)所有的顯示部分都是一個(gè)IO來控制 用的型號(hào)是STC15W104 擴(kuò)展用的2片 74HC595
當(dāng)時(shí)看中它的程序空間大 結(jié)果其實(shí)也沒那么大,嘎嘎 結(jié)果EEPROM只有1K了
1.jpg (137.99 KB, 下載次數(shù): 60)
下載附件
溫度
2021-3-21 22:25 上傳
3.jpg (114.71 KB, 下載次數(shù): 77)
下載附件
濕度
2021-3-21 22:25 上傳
2.jpg (129.3 KB, 下載次數(shù): 62)
下載附件
最大最小值顯示,這里顯示的是最大溫度
2021-3-21 22:25 上傳
好了 進(jìn)入正題 我先自吹自擂一下 (啊?! 誰扔的臭雞蛋!)
從左到右 三個(gè)按鈕 1 2 3
按鈕1:短按 查看最大最小溫濕度 每按一次切換顯示1個(gè) 這些數(shù)據(jù)全部存儲(chǔ)在內(nèi)部的FlashRom中 會(huì)自動(dòng)更新
長按 清除內(nèi)部FlashRom存儲(chǔ)的數(shù)據(jù) 并用當(dāng)前的測(cè)量值全部填充
按鈕2:顯示模式輪換,三個(gè)模式:溫濕度自動(dòng)輪換 僅溫度 僅濕度
其中溫濕度自動(dòng)輪換時(shí)間可調(diào) 顯示的數(shù)值是每次切換溫濕度的時(shí)候測(cè)量的
僅溫度 僅濕度 這兩個(gè)的數(shù)據(jù)是 每半秒更新一次(不可調(diào) 但是可以在編譯前調(diào)好,我設(shè)置的是半秒)
這2個(gè)模式貌似每次更新的時(shí)候 LED會(huì)閃一下 沒辦法 設(shè)計(jì)缺陷555 誰叫他1個(gè)IO干完全部的顯示呢?
按鈕3:切換自動(dòng)輪換的時(shí)間 ,我設(shè)置了3個(gè)擋位 2.5s 5s 10s ,同時(shí)這個(gè)還會(huì)自動(dòng)存儲(chǔ)在FlashROM里,只需要設(shè)定時(shí)間一次~~
P.S 最后加點(diǎn)感想: 這次又學(xué)到了很多東西,還專門寫了2個(gè)C文件模塊,模塊化編程好啊~ 贊美C語言!
昨天晚上寫這個(gè)程序,寫的頭很大,來來回回刪了又寫寫了又刪 ,起碼刪了300行,可謂是絞盡腦汁,我那10毫升的大腦都要變成漿糊啦!
今晚折騰出來了 我甚開心,趕緊拍下照片發(fā)帖留念了,MCU的世界真是美妙....我永遠(yuǎn)喜歡單片機(jī)!
單片機(jī)源程序如下:
- #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};//最后這個(gè)是負(fù)號(hào)
- 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的時(shí)鐘
- sbit SFTCK=P3^3;
- sbit sb1=P3^4; //MaxMin按鈕1 檢查max min,4 組數(shù)據(jù)切換,長按清空flash存儲(chǔ)區(qū)
- sbit sb2=P3^5; //mode按鈕sb2 切換模式 濕度溫度自動(dòng)切換 長溫度 長濕度
- sbit sb3=P3^0; //切換時(shí)間sb3 自動(dòng)切換時(shí)間調(diào)整 2.5S 5S 10S 三檔 50 100 200
- extern bit TempPoOrNe; //溫度正負(fù)位
- bit TempHumiSelect=1; //1=Temp 0=Humi
- int Temp,Humi;
- u8 Time50ms=0; //50ms標(biāo)志位
- u8 SetTimes=50; //設(shè)定的時(shí)間 分別是 50(默認(rèn)) 100 200
- u8 SetMode=0x00; //模式標(biāo)志位 分別為 0x00 0x11 0x22 自動(dòng)切換 長溫度 長濕度
- u8 ConditionLeds=0x0f; //后4位控制4個(gè)LED燈泡的亮滅 0x01 MAX ,0x02 Min ,0x04 Temp ,0x08 Humi
- void main()
- {
- IOinit();
- Delay1000ms();//延時(shí)1s 等待傳感器就位
- Timer0Init(); //定時(shí)器0初始化
- EA=1;ET0=1; //開中斷,開定時(shí)器0中斷
- GetTempAndHumi(); // 先讀取一次溫濕度
- ModeInit(); //從ROM中讀取mode 進(jìn)行初始化
- while(1)
- {
- ModeSelect(); //選擇模式 每個(gè)模式也負(fù)責(zé)讀取溫度
- 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(); //復(fù)位中斷
- 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); //擦除扇區(qū)
- IapProgramByte(0x0200,SetMode); //寫入更改后的模式
- Time50ms=0;
- }
- if(sb3==0&&SetMode==0x00) //調(diào)整自動(dòng)切換時(shí)間 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() //將讀取到的溫度濕度和存儲(chǔ)的最大最小值對(duì)比,如果有更大/更小的,就寫入
- {
- int MaxTemp,MaxHumi,MinTemp,MinHumi;
- Temp=TempInteger*10+TempDecimal; //把溫度轉(zhuǎn)化帶正負(fù)的Int 如24.4 -> 244
- if(TempPoOrNe!=1) Temp=Temp*-1; //判斷溫度正負(fù)
- Humi=HumiInteger*10+HumiDecimal; //濕度轉(zhuǎn)化 ,其實(shí)可以用無符號(hào) 因?yàn)闈穸仁遣豢赡苁秦?fù)數(shù).但是我偷懶了
- MaxTemp=IapReadWord(0x0000); //從Rom中讀取一些參數(shù) 做個(gè)留存保護(hù)
- 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) //有按下就會(huì)繼續(xù)往下執(zhí)行 否則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; //五秒不再按按鈕就會(huì)自動(dòng)退出
- }
- }
- Delay10ms(); //按鍵消抖
- while(!sb1); //等你松手
- Delay10ms(); //按鍵消抖
- Time50ms=0;
- while(sb1) //等你繼續(xù)按下
- {
- ConditionLeds=0x09; //最大濕度燈亮
- cache=IapReadWord(0x0002);
- cache1=cache/10;cache2=cache%10;
- AllDisplay(cache1,cache2);//這是最大溫度
- if(Time50ms>100)
- {
- Time50ms=0;
- GetTempAndHumi();
- TempHumiSelect=1;
- return; //五秒不再按按鈕就會(huì)自動(dòng)退出
- }
- }
- Delay10ms(); //按鍵消抖
- while(!sb1); //等你松手
- Delay10ms(); //按鍵消抖
- Time50ms=0;
- while(sb1) //等你繼續(xù)按下
- {
- 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; //五秒不再按按鈕就會(huì)自動(dòng)退出
- }
- }
- Delay10ms(); //按鍵消抖
- while(!sb1); //等你松手
- Delay10ms(); //按鍵消抖
- Time50ms=0;
- while(sb1) //等你繼續(xù)按下
- {
- ConditionLeds=0x0a;
- cache=IapReadWord(0x0006);
- cache1=cache/10;cache2=cache%10;
- AllDisplay(cache1,cache2);//這是最小濕度
- if(Time50ms>100)
- {
- Time50ms=0;
- GetTempAndHumi();
- TempHumiSelect=1;
- return; //五秒不再按按鈕就會(huì)自動(dòng)退出
- }
- }
- Delay10ms(); //按鍵消抖
- while(!sb1); //等你松手
- GetTempAndHumi();
- Delay10ms(); //按鍵消抖
- TempHumiSelect=1;
- Time50ms=0;
- }
- void NormalMode() //常規(guī)模式
- {
- if(TempHumiSelect)
- {
- ConditionLeds=0x04; //亮溫度燈
- AllDisplay(TempInteger,TempDecimal);
- }
- else
- {
- ConditionLeds=0x08; //亮濕度燈
- AllDisplay(HumiInteger,HumiDecimal);
- }
- if(Time50ms>SetTimes)
- {
- Time50ms=0;
- TempHumiSelect=~TempHumiSelect;
- GetTempAndHumi();//每次切換時(shí)讀取一次溫濕度
- }
- }
- void TemponlyMode() //僅溫度
- {
- ConditionLeds=0x04; //亮溫度燈
- AllDisplay(TempInteger,TempDecimal);
- if(Time50ms>10) //500ms
- {
- Time50ms=0;
- GetTempAndHumi();//每次切換時(shí)讀取一次溫濕度
- }
- }
- void HumionlyMode() //僅濕度
- {
- ConditionLeds=0x08; //亮濕度燈
- AllDisplay(HumiInteger,HumiDecimal);
- if(Time50ms>10) //500ms
- {
- Time50ms=0;
- GetTempAndHumi();//每次切換時(shí)讀取一次溫濕度
- }
- }
- 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) //在單個(gè)數(shù)碼管和所有l(wèi)ed燈珠顯示內(nèi)容 ,會(huì)自動(dòng)在第三位上加上小數(shù)點(diǎn)
- {
- u8 i=0,j=0xf0;
- i=(clrbit(j,position+3))+ConditionLeds; // 計(jì)算需要輸出的位選+led指示燈亮滅情況
- hc595send(i); //發(fā)送位選和狀態(tài)顯示燈狀態(tài)位
- if(position==3) hc595send(SMGtable[Number]|0x80); //第三位加個(gè)小數(shù)點(diǎn)
- else hc595send(SMGtable[Number]);
- hc595activate();
- }
- void AllDisplay(u8 Integer,u8 Decimal) //掃描顯示所有4個(gè)數(shù)碼管和led燈珠 //也進(jìn)行顯示正負(fù)
- {
- if(TempPoOrNe!=1)
- {
- LEDsDisplay(10,1); //顯示負(fù)號(hào)位 ,否則不顯示
- }
- if(Integer/10%10!=0) //判斷十位是否需要消隱
- {
- LEDsDisplay(Integer/10%10,2); //十位
- }
- LEDsDisplay(Integer/1%10,3); //個(gè)位
- LEDsDisplay(Decimal/1%10,4); //小數(shù)點(diǎn)后1位
- }
- void Timer0Init() // 50ms @12Mhz 計(jì)時(shí)器0初始化
- {
- AUXR &= 0x7F; //12T
- TMOD &= 0xF0;
- TL0 = 0xB0;
- TH0 = 0x3C;
- TF0 = 0;
- TR0 = 1;
- }
- void Timer0 () interrupt 1 //計(jì)時(shí)器0中斷
- {
- Time50ms++;
- }
- void hc595send (u8 sbyte) //移位輸出8位數(shù)據(jù)至移位寄存器中 高位先進(jìn)入移位寄存器
- {
- u8 i=0;
- for(i=0;i<8;i++)
- {
- SFTCK=0;
- dat11=getbit(sbyte,7-i);
- SFTCK=1;
- }
- }
- void hc595activate() //讓595輸出寄存器輸出移位寄存器保存的結(jié)果
- {
- LCHCK=0;
- LCHCK=1;
- LCHCK=0;
- }
- void Delay10ms() //@12.000MHz 軟延時(shí)10ms
- {
- unsigned char i, j;
- i = 117;
- j = 184;
- do
- {
- while (--j);
- } while (--i);
- }
復(fù)制代碼
51hei.png (3.83 KB, 下載次數(shù): 75)
下載附件
2021-3-21 22:58 上傳
最后就是程序 僅供學(xué)習(xí)
禁止其他任何用途(如作業(yè)等)
不然與本人初衷不符
全部資料51hei下載地址:
溫濕度計(jì).zip
(6.15 KB, 下載次數(shù): 31)
2021-3-21 22:35 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|