溫濕度傳感器sht10程序_第1頁(yè)
溫濕度傳感器sht10程序_第2頁(yè)
溫濕度傳感器sht10程序_第3頁(yè)
溫濕度傳感器sht10程序_第4頁(yè)
溫濕度傳感器sht10程序_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上#include <reg51.h>#include <math.h>#include <intrins.h>sbit DATA=P11; /定義通訊數(shù)據(jù)端口/#define DATA_IO TRISC7 /用于設(shè)置IO狀態(tài)sbit SCK=P10; /定義通訊時(shí)鐘端口#define noACK 0 /繼續(xù)傳輸數(shù)據(jù),用于判斷是否結(jié)束通訊#define ACK 1 /結(jié)束數(shù)據(jù)傳輸; /地址 命令#define MEASURE_TEMP 0x03 /000 00011#define MEASURE_HUMI 0x05 /000 0010

2、1/void init_uart(void); /初始化void s_connectionreset(void);void s_transstart(void);char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);char s_write_byte(unsigned char value);char s_read_byte(unsigned char ack);void calc_sth11(float *p_humidity ,float *p_temperature);f

3、loat calc_dewpoint(float h,float t);void delay (unsigned int time); union unsigned int i; float f;humi_val,temp_val; /定義兩個(gè)共同體,一個(gè)用于濕度,一個(gè)用于溫度/*延時(shí) 1MS 帶參數(shù)(int)子程序*/void delay (unsigned int time) unsigned int a,b; for(a=0;a<time;a+) for(b=0;b<88;b+); /-/void init_uart(void)/-/ 端口初始化/TRISC7=0;/TRIS

4、C6=0;/-void s_connectionreset(void)/-/ 連接復(fù)位;/ _ _/ DATA: |_|/ _ _ _ _ _ _ _ _ _ _ _/ SCK : _| |_| |_| |_| |_| |_| |_| |_| |_| |_| |_| |_ unsigned char i; DATA=1; SCK=0; /準(zhǔn)備 for(i=0;i<9;i+) /DATA保持高,SCK時(shí)鐘觸發(fā)9次,發(fā)送啟動(dòng)傳輸,通迅即復(fù)位 SCK=1; SCK=0; s_transstart(); /啟動(dòng)傳輸/-void s_transstart(void)/-/ 啟動(dòng)傳輸/ _ _/ D

5、ATA: |_|/ _ _/ SCK : _| |_| |_ DATA=1;SCK=0; _nop_(); SCK=1; _nop_(); DATA=0; _nop_(); SCK=0; _nop_();_nop_();_nop_(); SCK=1; _nop_(); DATA=1; _nop_(); SCK=0; /-char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)/-/ 進(jìn)行溫度或者濕度轉(zhuǎn)換,由參數(shù)mode決定轉(zhuǎn)換內(nèi)容; unsigned error=0; unsign

6、ed char i; s_transstart(); /啟動(dòng)傳輸 switch(mode) case 02 : error+=s_write_byte(MEASURE_TEMP); break; case 01 : error+=s_write_byte(MEASURE_HUMI); break; default : break; for (i=0;i<110;i+) delay(2); if(DATA=0) break; /等待測(cè)量結(jié)束; if(DATA) error+=1; / 如果長(zhǎng)時(shí)間數(shù)據(jù)線沒(méi)有拉低,說(shuō)明測(cè)量錯(cuò)誤 *(p_value) =s_read_byte(ACK); /讀第

7、一個(gè)字節(jié),高字節(jié) (MSB) *(p_value+1)=s_read_byte(ACK); /讀第二個(gè)字節(jié),低字節(jié) (LSB) *p_checksum =s_read_byte(noACK); /read CRC校驗(yàn)碼 return error;/-char s_write_byte(unsigned char value)/-/ 寫(xiě)字節(jié)函數(shù) unsigned char i,error=0; for (i=0x80;i>0;i/=2) /高位為1,循環(huán)右移 if (i & value) DATA=1; /和要發(fā)送的數(shù)相與,結(jié)果為發(fā)送的位 else DATA=0; SCK=1; _

8、nop_();_nop_();_nop_(); SCK=0; DATA=1; /釋放數(shù)據(jù)線/ DATA_IO=1; SCK=1; error=DATA; /檢查應(yīng)答信號(hào),確認(rèn)通訊正常 SCK=0; return error; /error=1 通訊錯(cuò)誤/-char s_read_byte(unsigned char ack)/-/ 讀數(shù)據(jù); unsigned char i,val=0; / DATA_IO=0; DATA=1; /數(shù)據(jù)線為高 /DATA_IO=1; for (i=0x80;i>0;i/=2) /右移位 SCK=1; if (DATA) val=(val | i); /讀數(shù)

9、據(jù)線的值 SCK=0; / DATA_IO=0; DATA=!ack; /如果是校驗(yàn),讀取完后結(jié)束通訊; SCK=1; _nop_();_nop_();_nop_(); SCK=0; DATA=1; /釋放數(shù)據(jù)線 return val;/-void calc_sth11(float *p_humidity ,float *p_temperature)/-/ 補(bǔ)償及輸出溫度和相對(duì)濕度 const float C1=-4.0; / for 12 Bit 濕度修正公式 const float C2=+0.0405; / for 12 Bit 濕度修正公式 const float C3=-0.; /

10、for 12 Bit 濕度修正公式 const float T1=+0.01; / for 14 Bit 5V 溫度修正公式 const float T2=+0.00008; / for 14 Bit 5V 溫度修正公式 float rh=*p_humidity; float t=*p_temperature; float rh_lin; float rh_true; float t_C; t_C=t*0.01 - 40; /補(bǔ)償溫度 rh_lin=C3*rh*rh + C2*rh + C1; /相對(duì)濕度非線性補(bǔ)償 rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; /相對(duì)

11、濕度對(duì)于溫度依賴性補(bǔ)償 if(rh_true>100)rh_true=100; /濕度最大修正 if(rh_true<0.1)rh_true=0.1; /濕度最小修正 *p_temperature=t_C; /返回溫度結(jié)果 *p_humidity=rh_true; /返回濕度結(jié)果/-float calc_dewpoint(float h,float t)/-/ 計(jì)算絕對(duì)濕度值 float logEx,dew_point; logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2); dew_point = (logEx - 0.66077)*237.3/(

12、0.66077+7.5-logEx); return dew_point;/-void main()/-/ 示例程序,完成如下功能/ 1. 復(fù)位 / 2. 濕度的12測(cè)量以及溫度的14位精度測(cè)量/ 3. 補(bǔ)償及修正溫濕度/ 4. 計(jì)數(shù)并得出絕對(duì)濕度 float dew_point; unsigned char error,checksum; unsigned char HUMI,TEMP; HUMI=0X01; TEMP=0X02; / init_uart(); s_connectionreset(); while(1) error=0; error+=s_measure(unsigned char*) &humi_val.i,&checksum,HUMI); /濕度測(cè)量 error+=s_measure(unsigned char*) &temp_val.i,&checksum,TEMP); /溫度測(cè)量 if(error!=0) s_connectionreset(); /如果發(fā)生錯(cuò)誤,系統(tǒng)復(fù)位 else humi_val.f=(float)humi_val.i; /轉(zhuǎn)換為浮點(diǎn)數(shù) temp_val.f=(float)temp_val.i; /轉(zhuǎn)換為浮點(diǎn)數(shù) calc_sth11(&humi_val.f,&temp_val.f)

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論