與SHT和液晶測溫濕的程序_第1頁
與SHT和液晶測溫濕的程序_第2頁
與SHT和液晶測溫濕的程序_第3頁
與SHT和液晶測溫濕的程序_第4頁
與SHT和液晶測溫濕的程序_第5頁
已閱讀5頁,還剩5頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、/*端口定義* P1.0-SCK (SHT10) P1.1-DATA (SHT10) P0-DB0DB7 (LCD1602) P2.0-RS (LCD1602) P2.1-RW (LCD1602) P2.2-E (LCD1602)*/#include <reg52.h>#include <intrins.h>#include <stdio.h>#include <string.h>#include <absacc.h>#include <math.h>#define uchar unsigned char#define u

2、int unsigned int/按鍵設置sbit TEM_UP = P12;sbit TEM_DOWN = P13;sbit HUM_UP = P14;sbit HUM_DOWN = P15;sbit SHEZHI_ENTER = P16;sbit SHEZHI_EXIT = P17;float f_tem_up_value = 50.0;float f_tem_down_value = -30.0;float f_hum_up_value = 10.0;float f_hum_down_value = 100.0;/1602液晶端口定義 *sbit LcdRs= P20;sbit LcdR

3、w= P21;sbit LcdEn= P22;sbit ACC0 = ACC0;sbit ACC7 = ACC7;uchar str7;/向LCD寫入命令或數據*#define LCD_COMMAND0 / Command#define LCD_DATA1 / Data#define LCD_CLEAR_SCREEN0x01 / 清屏#define LCD_HOMING 0x02 / 光標返回原點/設置顯示模式*#define LCD_SHOW0x04 /顯示開#define LCD_HIDE0x00 /顯示關 #define LCD_CURSOR0x02 /顯示光標#define LCD_N

4、O_CURSOR0x00 /無光標 #define LCD_FLASH0x01 /光標閃動#define LCD_NO_FLASH 0x00 /光標不閃動/設置輸入模式*#define LCD_AC_UP0x02#define LCD_AC_DOWN0x00 / default#define LCD_MOVE0x01 / 畫面可平移#define LCD_NO_MOVE0x00 /defaultunsigned char LCD_Wait(void);void LCD_Write(bit style, unsigned char input);/*1602液晶顯示部分子程序*/void del

5、ay(uint z)uint x,y;for(x=z;x>0;x-)for(y=110;y>0;y-);void LCD_Write(bit style, unsigned char input)LcdRs=style;P0=input; delay(5);LcdEn=1;delay(5);LcdEn=0;void LCD_SetDisplay(unsigned char DisplayMode)LCD_Write(LCD_COMMAND, 0x08|DisplayMode);void LCD_SetInput(unsigned char InputMode)LCD_Write(L

6、CD_COMMAND, 0x04|InputMode);/初始化LCD*void LCD_Initial()LcdEn=0;LCD_Write(LCD_COMMAND,0x38); /8位數據端口,2行顯示,5*7點陣LCD_Write(LCD_COMMAND,0x38);LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); /開啟顯示, 無光標LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); /清屏LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); /AC遞增, 畫面不動/液晶字符輸入的位置*void GotoX

7、Y(unsigned char x, unsigned char y)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+;void zhuanhuan(float a)/浮點數轉換成字符串函數 memset(str,0,sizeof(str);sprintf (str,"%f",

8、a); void welcome()LCD_Initial();GotoXY(0,0);Print(" Welcome! ");GotoXY(0,1);Print(" Code of sht10 ");delay(200);/*- ;模塊名稱:delay_n10us(); ;功 能:延時函數,延時約n個10us較精確的延時函數,"_nop_()"延時1us12M晶振;-*/void delay_n10us(uint n) /延時n個10us12M晶振 uint i; for(i=n;i>0;i-) _nop_();_nop_()

9、;_nop_();_nop_();_nop_();_nop_(); /*第一部分LCD1602設置 END*/*第二部分DHT90設置 START* sbit SCK = P10; /定義通訊時鐘端口sbit DATA = P11; /定義通訊數據端口typedef union unsigned int i; /定義了兩個共用體 float f; value; enum TEMP,HUMI; /TEMP=0,HUMI=1 #define noACK 0 /用于判斷是否結束通訊#define ACK 1 /結束數據傳輸 /adr command r/w #define STATUS_REG_W

10、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 /*定義函數*/void s_transstart(void); /啟動傳輸函數void s_connectionreset(void); /連接復位函數char s_write_byte(unsigned char value);/DHT90寫函數char s_read_b

11、yte(unsigned char ack); /DHT90讀函數char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);/測量溫濕度函數void calc_dht90(float *p_humidity ,float *p_temperature);/溫濕度補償/*- ;模塊名稱:s_transstart(); ;功 能:啟動傳輸函數;-*/ void s_transstart(void) / generates a transmission start / _ _ / DAT

12、A: |_| / _ _ / SCK : _| |_| |_ DATA=1; SCK=0; /Initial state _nop_(); SCK=1; _nop_(); DATA=0; _nop_(); SCK=0; _nop_();_nop_();_nop_(); SCK=1; _nop_(); DATA=1; _nop_(); SCK=0; /*- ;模塊名稱:s_connectionreset(); ;功 能:連接復位函數;-*/ void s_connectionreset(void) / communication reset: DATA-line=1 and at least 9

13、 SCK cycles followed by transstart / _ _ / DATA: |_| / _ _ _ _ _ _ _ _ _ _ _ / SCK : _| |_| |_| |_| |_| |_| |_| |_| |_| |_| |_| |_ unsigned char i; DATA=1; SCK=0; /Initial state for(i=0;i<9;i+) /9 SCK cycles SCK=1; SCK=0; s_transstart(); /transmission start /*- ;模塊名稱:s_write_byte(); ;功 能:SHT10寫函數

14、;-*/ char s_write_byte(unsigned char value) /- / writes a byte on the Sensibus and checks the acknowledge unsigned char i,error=0; for (i=0x80;i>0;i/=2) /shift bit for masking if (i & value) DATA=1; /masking value with i , write to SENSI-BUS else DATA=0; SCK=1; /clk for SENSI-BUS _nop_();_nop

15、_();_nop_(); /pulswith approx. 3 us SCK=0; DATA=1; /release DATA-line SCK=1; /clk #9 for ack error=DATA; /check ack (DATA will be pulled down by DHT90),DATA在第9個上升沿將被DHT90自動下拉為低電平。 _nop_();_nop_();_nop_(); SCK=0; DATA=1; /release DATA-line return error; /error=1 in case of no acknowledge /返回:0成功,1失敗

16、/*- ;模塊名稱:s_read_byte(); ;功 能:SHT10讀函數;-*/ char s_read_byte(unsigned char ack) / reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" unsigned char i,val=0; DATA=1; /release DATA-line for (i=0x80;i>0;i/=2) /shift bit for masking SCK=1; /clk for SENSI-BUS if (DATA)

17、val=(val | i); /read bit _nop_();_nop_();_nop_(); /pulswith approx. 3 us SCK=0; if(ack=1)DATA=0; /in case of "ack=1" pull down DATA-Line else DATA=1; /如果是校驗(ack=0),讀取完后結束通訊 _nop_();_nop_();_nop_(); /pulswith approx. 3 us SCK=1; /clk #9 for ack _nop_();_nop_();_nop_(); /pulswith approx. 3 u

18、s SCK=0; _nop_();_nop_();_nop_(); /pulswith approx. 3 us DATA=1; /release DATA-line return val; /*- ;模塊名稱:s_measure(); ;功 能:測量溫濕度函數;-*/ char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode) / makes a measurement (humidity/temperature) with checksum unsigned error=0; u

19、nsigned int i; s_transstart(); /transmission start switch(mode) /send command to sensor case TEMP : error+=s_write_byte(MEASURE_TEMP); break; case HUMI : error+=s_write_byte(MEASURE_HUMI); break; default : break; for (i=0;i<65535;i+) if(DATA=0) break; /wait until sensor has finished the measureme

20、nt if(DATA) error+=1; / or timeout (2 sec.) is reached *(p_value) =s_read_byte(ACK); /read the first byte (MSB) *(p_value+1)=s_read_byte(ACK); /read the second byte (LSB) *p_checksum =s_read_byte(noACK); /read checksum return error; /*- ;模塊名稱:calc_dht90(); ;功 能:溫濕度補償函數;-*/ void calc_dht90(float *p_h

21、umidity ,float *p_temperature)/ calculates temperature C and humidity %RH / input : humi Ticks (12 bit) / temp Ticks (14 bit)/ output: humi %RH/ temp C const float C1=-4.0; / for 12 Bit const float C2=+0.0405; / for 12 Bit const float C3=-0.0000028; / for 12 Bit const float T1=+0.01; / for 14 Bit 5V

22、 const float T2=+0.00008; / for 14 Bit 5V float rh=*p_humidity; / rh: Humidity Ticks 12 Bit float t=*p_temperature; / t: Temperature Ticks 14 Bit float rh_lin; / rh_lin: Humidity linear float rh_true; / rh_true: Temperature compensated humidity float t_C; / t_C : Temperature C t_C=t*0.01 - 40; /calc

23、. temperature from ticks to C rh_lin=C3*rh*rh + C2*rh + C1; /calc. humidity from ticks to %RH rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; /calc. temperature compensated humidity %RH if(rh_true>100)rh_true=100; /cut if the value is outside of if(rh_true<0.1)rh_true=0.1; /the physical possible range *p_temperature=t_C; /return temperature C *p_humidity=rh_true; /return humidity%RH/*主函數*void main(void)value humi_val,temp_val; unsigned char error,checksum; LcdRw=0; s_connectionreset(); welcome();/顯示歡迎畫面 delay(2000);LCD_Initial(); while(1) / shezh

溫馨提示

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

評論

0/150

提交評論