這是我自己寫(xiě)的SHT10 的程序,SHT1X 系列的都適用。
0.png (37.58 KB, 下載次數(shù): 73)
下載附件
2016-12-22 05:11 上傳
幾點(diǎn)說(shuō)明: 1.主要是分以下幾個(gè)模塊寫(xiě)的:SHT10,LCD1602,主函數(shù),頭文件。 2.每支SHTxx傳感器都在25℃(77 °F)和 3.3V條件下進(jìn)行過(guò)標(biāo)定并且完全符合精度指標(biāo).因?yàn)榭紤]到實(shí)際硬件5V的電壓比較好操作,所以SHT10用的精度采用的為5V時(shí)的參數(shù)。其他的都采取默認(rèn)值(14bit濕度 , 12bit 溫度)。 3.SHT10中所以部分我都編寫(xiě)了。有的部分在本次程序中沒(méi)用到,也可以作為參考。 4.所有程序都已經(jīng)加了注釋,且有仿真圖。 5.個(gè)人認(rèn)為還可以在此基礎(chǔ)上添加個(gè)中斷。 6.程序編寫(xiě)keil 4 ,仿真 protues7.5
/***********************************************************************************************************************************************************/
頭文件(tou.h):
#ifndef __TOU_H__ #define __TOU_H__
#include<reg52.h> #include <intrins.h> //#include <math.h> //Keil library
#define uchar unsigned char enum {TEMP,HUMI};
sbit DATA = P1^7; sbit SCK = P1^6;
sbit LcdRs = P2^4; sbit LcdRw = P2^5; sbit LcdEn = P2^6; sfr DBPort = 0x80; //P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.數(shù)據(jù)端口
/******** DS1602函數(shù)聲明 ********/ void LCD_Initial(); void GotoXY(unsigned char x, unsigned chary); void Print(unsigned char *str); void LCD_Write(bit style, unsigned charinput);
/******** SHT10函數(shù)聲明 ********/ void s_connectionreset(void); char s_measure(unsigned char *p_value,unsigned char *p_checksum, unsigned char mode); void calc_sth10(float *p_humidity ,float*p_temperature); //float calc_dewpoint(float h,float t);
#endif /***********************************************************************************************************************************************************/
SHT10程序(SHT10.c):
#include<tou.h>
#define noACK 0 //繼續(xù)傳輸數(shù)據(jù),用于判斷是否結(jié)束通訊 #define ACK 1 //結(jié)束數(shù)據(jù)傳輸; //地址 命令 讀/寫(xiě) #define STATUS_REG_W 0x06 //000 0011 0 #define STATUS_REG_R 0x07 //000 0011 1 #define MEASURE_TEMP 0x03 //000 0001 1 #define MEASURE_HUMI 0x05 //000 0010 1 #define RESET 0x1e //000 1111 0
//寫(xiě)字節(jié)程序 char s_write_byte(unsigned char value) { unsignedchar i,error=0; for(i=0x80;i>0;i>>=1) //高位為1,循環(huán)右移 { if(i&value) DATA=1; //和要發(fā)送的數(shù)相與,結(jié)果為發(fā)送的位 else DATA=0; SCK=1; _nop_();_nop_();_nop_(); //延時(shí)3us SCK=0; } DATA=1; //釋放數(shù)據(jù)線 SCK=1; error=DATA; //檢查應(yīng)答信號(hào),確認(rèn)通訊正常 _nop_();_nop_();_nop_(); SCK=0; DATA=1; returnerror; //error=1 通訊錯(cuò)誤 }
//讀字節(jié)程序 char s_read_byte(unsigned char ack) //---------------------------------------------------------------------------------- { unsignedchar i,val=0; DATA=1; //釋放數(shù)據(jù)線 for(i=0x80;i>0;i>>=1) //高位為1,循環(huán)右移 { SCK=1; if(DATA) val=(val|i); //讀一位數(shù)據(jù)線的值 SCK=0; } DATA=!ack; //如果是校驗(yàn),讀取完后結(jié)束通訊; SCK=1; _nop_();_nop_();_nop_(); //延時(shí)3us SCK=0; _nop_();_nop_();_nop_(); DATA=1; //釋放數(shù)據(jù)線 returnval; }
//啟動(dòng)傳輸 void s_transstart(void) // generates a transmission start // _____ ________ // DATA: |_______| // ___ ___ // SCK : ___| |___| |______ { DATA=1; SCK=0; //準(zhǔn)備 _nop_(); SCK=1; _nop_(); DATA=0; _nop_(); SCK=0; _nop_();_nop_();_nop_(); SCK=1; _nop_(); DATA=1; _nop_(); SCK=0; }
//連接復(fù)位 void s_connectionreset(void) // communication reset: DATA-line=1 and atleast 9 SCK cycles followed by transstart // _____________________________________________________ ________ // DATA: |_______| // _ _ _ _ _ _ _ _ _ ___ ___ // SCK : __| |__| |__| |__| |__| |__| |__||__| |__| |______| |___| |______ { unsignedchar 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)傳輸 }
//軟復(fù)位程序 char s_softreset(void) // resets the sensor by a softreset { unsignedchar error=0; s_connectionreset(); //啟動(dòng)連接復(fù)位 error+=s_write_byte(RESET); //發(fā)送復(fù)位命令 returnerror; //error=1 通訊錯(cuò)誤 }
/*讀狀態(tài)寄存器 char s_read_statusreg(unsigned char*p_value, unsigned char *p_checksum) //---------------------------------------------------------------------------------- // reads the status register with checksum(8-bit) { unsignedchar error=0; s_transstart(); //transmission start error=s_write_byte(STATUS_REG_R);//send command to sensor *p_value=s_read_byte(ACK); //read status register (8-bit) *p_checksum=s_read_byte(noACK); //read checksum (8-bit) returnerror; //error=1 in case of no response form the sensor }
//寫(xiě)狀態(tài)寄存器 char s_write_statusreg(unsigned char*p_value) // writes the status register with checksum(8-bit) { unsignedchar error=0; s_transstart(); //transmission start error+=s_write_byte(STATUS_REG_W);//sendcommand to sensor error+=s_write_byte(*p_value); //send value of status register returnerror; //error>=1 in case of no response form the sensor } */
//溫濕度測(cè)量 char s_measure(unsigned char *p_value,unsigned char *p_checksum, unsigned char mode) // 進(jìn)行溫度或者濕度轉(zhuǎn)換,由參數(shù)mode決定轉(zhuǎn)換內(nèi)容; { // enum{TEMP,HUMI}; //已經(jīng)在頭文件中定義 unsignederror=0; unsignedint i;
s_transstart(); //啟動(dòng)傳輸 switch(mode) //選擇發(fā)送命令 { caseTEMP : error+=s_write_byte(MEASURE_TEMP); break; //測(cè)量溫度 case HUMI :error+=s_write_byte(MEASURE_HUMI); break; //測(cè)量濕度 default : break; } for(i=0;i<65535;i++) 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); //讀第一個(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)碼 returnerror; //error=1 通訊錯(cuò)誤 }
//溫濕度值標(biāo)度變換及溫度補(bǔ)償 void calc_sth10(float *p_humidity ,float*p_temperature) { constfloat C1=-4.0; // 12位濕度精度 修正公式 constfloat C2=+0.0405; // 12位濕度精度 修正公式 constfloat C3=-0.0000028; // 12位濕度精度 修正公式 constfloat T1=+0.01; // 14位溫度精度 5V條件 修正公式 constfloat T2=+0.00008; // 14位溫度精度 5V條件 修正公式
floatrh=*p_humidity; // rh: 12位濕度 floatt=*p_temperature; // t: 14位溫度 floatrh_lin; // rh_lin: 濕度 linear值 floatrh_true; // rh_true: 濕度 ture值 floatt_C; // 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ì)濕度對(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é)果 }
//從相對(duì)溫度和濕度計(jì)算露點(diǎn) /*float calc_dewpoint(float h,float t) { floatlogEx,dew_point; logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2); dew_point= (logEx - 0.66077)*237.3/(0.66077+7.5-logEx); returndew_point; } */
/***********************************************************************************************************************************************************/ DS1602程序(1602.c):
#include<tou.h>
//內(nèi)部等待函數(shù)************************************************************** unsigned char LCD_Wait(void) { LcdRs=0; LcdRw=1; _nop_(); LcdEn=1; _nop_(); LcdEn=0; return DBPort; } //向LCD寫(xiě)入命令或數(shù)據(jù)******************************************************** #define LCD_COMMAND 0 // Command #define LCD_DATA 1 // Data #define LCD_CLEAR_SCREEN 0x01 // 清屏 #define LCD_HOMING 0x02 // 光標(biāo)返回原點(diǎn) void LCD_Write(bit style, unsigned charinput) { LcdEn=0; LcdRs=style; LcdRw=0; _nop_(); DBPort=input; _nop_();//注意順序 LcdEn=1; _nop_();//注意順序 LcdEn=0; _nop_(); LCD_Wait(); }
//設(shè)置顯示模式************************************************************ #define LCD_SHOW 0x04 //顯示開(kāi) #define LCD_HIDE 0x00 //顯示關(guān)
#define LCD_CURSOR 0x02 //顯示光標(biāo) #define LCD_NO_CURSOR 0x00 //無(wú)光標(biāo)
#define LCD_FLASH 0x01 //光標(biāo)閃動(dòng) #define LCD_NO_FLASH 0x00 //光標(biāo)不閃動(dòng)
void LCD_SetDisplay(unsigned charDisplayMode) { LCD_Write(LCD_COMMAND, 0x08|DisplayMode); }
//設(shè)置輸入模式************************************************************ #define LCD_AC_UP 0x02 #define LCD_AC_DOWN 0x00 // default
#define LCD_MOVE 0x01 // 畫(huà)面可平移 #define LCD_NO_MOVE 0x00 //default
void LCD_SetInput(unsigned char InputMode) { LCD_Write(LCD_COMMAND, 0x04|InputMode); }
//初始化LCD************************************************************ void LCD_Initial() { LcdEn=0; LCD_Write(LCD_COMMAND,0x38); //8位數(shù)據(jù)端口,2行顯示,5*7點(diǎn)陣 LCD_Write(LCD_COMMAND,0x38); LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); //開(kāi)啟顯示, 無(wú)光標(biāo) LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏 LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); //AC遞增, 畫(huà)面不動(dòng) }
//液晶字符輸入的位置************************ void GotoXY(unsigned char x, unsigned chary) { if(y==0) LCD_Write(LCD_COMMAND,0x80|x); if(y==1) LCD_Write(LCD_COMMAND,0x80|(x-0x40)); }
//將字符輸出到液晶顯示 void Print(unsigned char *str) { while(*str!='\0') { LCD_Write(LCD_DATA,*str); str++; } }
/***********************************************************************************************************************************************************/ 主函數(shù)(main.c):
#include<tou.h>
typedef union //定義共用同類(lèi)型 { unsignedint i; floatf; } value;
//延時(shí)函數(shù) void delay(int z) //z為毫秒數(shù) { intx,y; for(x=z;x>0;x--) for(y=125;y>0;y--); }
void main() { unsignedint temp,humi; valuehumi_val,temp_val; //定義兩個(gè)共同體,一個(gè)用于濕度,一個(gè)用于溫度 // floatdew_point; //用于記錄露點(diǎn)值 unsignedchar error; //用于檢驗(yàn)是否出現(xiàn)錯(cuò)誤 unsignedchar checksum; //CRC ucharwendu[6]; //用于記錄溫度 ucharshidu[6]; //用于記錄濕度
LCD_Initial(); //初始化液晶 GotoXY(0,0); //選擇溫度顯示位置 Print("TEMP: %C"); //5格空格 GotoXY(0,1); //選擇濕度顯示位置 Print("HUMI: %RH"); //5格空格 s_connectionreset(); //啟動(dòng)連接復(fù)位 while(1) { error=0; //初始化error=0,即沒(méi)有錯(cuò)誤 error+=s_measure((unsignedchar*)&temp_val.i,&checksum,TEMP); //溫度測(cè)量 error+=s_measure((unsignedchar*)&humi_val.i,&checksum,HUMI); //濕度測(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_sth10(&humi_val.f,&temp_val.f); //修正相對(duì)濕度及溫度 // dew_point=calc_dewpoint(humi_val.f,temp_val.f);//計(jì)算e dew_point temp=temp_val.f*10; humi=humi_val.f*10; GotoXY(5,0); //設(shè)置溫度顯示位置 wendu[0]=temp/1000+'0'; //溫度百位 wendu[1]=temp%1000/100+'0'; //溫度十位 wendu[2]=temp%100/10+'0'; //溫度個(gè)位 wendu[3]=0x2E; //小數(shù)點(diǎn) wendu[4]=temp%10+'0'; //溫度小數(shù)點(diǎn)后第一位 Print(wendu); //輸出溫度 GotoXY(5,1); //設(shè)置濕度顯示位置 shidu[0]=humi/1000+'0'; //濕度百位 shidu[1]=humi%1000/100+'0'; //濕度十位 shidu[2]=humi%100/10+'0'; //濕度個(gè)位 shidu[3]=0x2E; //小數(shù)點(diǎn) shidu[4]=humi%10+'0'; //濕度小數(shù)點(diǎn)后第一位 Print(shidu); //輸出濕度 } delay(800); //等待足夠長(zhǎng)的時(shí)間,以現(xiàn)行下一次轉(zhuǎn)換 } }
/***********************************************************************************************************************************************************/
0.png (68.12 KB, 下載次數(shù): 58)
下載附件
2016-12-22 05:12 上傳
|