|
附有代碼和仿真程序以供參考
51hei.gif (93.47 KB, 下載次數(shù): 55)
下載附件
2021-6-24 17:31 上傳
下載 (1).png (137.52 KB, 下載次數(shù): 62)
下載附件
仿真圖
2021-6-24 16:29 上傳
下載.png (72.8 KB, 下載次數(shù): 56)
下載附件
軟件流程
2021-6-24 16:28 上傳
單片機(jī)源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #include <math.h> //Keil library
- #include <stdio.h> //Keil library
- //*********************第一部分LCD1602設(shè)置
- //START****************************************
- #define LCD_DB P0
- sbit LCD_RS=P1^0; //P2^0是p2.0的意思;LCD_RS
- sbit LCD_RW=P1^1; //P2^1是p2.1的意思
- sbit LCD_E=P1^2; //P2^2是p2.2的意思
- /******定義函數(shù)****************/
- #define uchar unsigned char
- #define uint unsigned int
- typedef unsigned long U32;
- typedef signed long S32; /* defined for signed 32-bits integer variable 有符號(hào)32位整型變量 */
- typedef float F32;
- void LCD_init(void); //初始化函數(shù)
- void LCD_write_command(uchar command); //寫(xiě)指令函數(shù)
- void LCD_write_data(uchar dat); //寫(xiě)數(shù)據(jù)函數(shù)
- void LCD_disp_char(uchar x,uchar y,uchar dat);//在某個(gè)屏幕位置上顯示一個(gè)字符,X(0-15),y(1-2)
- void LCD_disp_str(uchar x,uchar y,uchar *str); //LCD1602顯示字符串函數(shù)
- void delay_n10us(uint n); //延時(shí)函數(shù)
- /*--------------------------------------
- ;模塊名稱:LCD_init();
- ;功 能:初始化LCD1602
- ;占用資源:--
- ;參數(shù)說(shuō)明:--
- ;創(chuàng)建日期:2008.08.15
- ;版 本:FV1.0(函數(shù)版本Function Version)
- ;修改日期:--
- ;修改說(shuō)明:--
- ;-------------------------------------*/
- void LCD_init(void)
- {
- delay_n10us(10);
- LCD_write_command(0x38);//設(shè)置8位格式,2行,5x7
- delay_n10us(10);
- LCD_write_command(0x0c);//整體顯示,關(guān)光標(biāo),不閃爍
- delay_n10us(10);
- LCD_write_command(0x06);//設(shè)定輸入方式,增量不移位
- delay_n10us(10);
- LCD_write_command(0x01);//清除屏幕顯示
- delay_n10us(100); //延時(shí)清屏,延時(shí)函數(shù),延時(shí)約n個(gè)10us
- }
- /*--------------------------------------
- ;模塊名稱:LCD_write_command();
- ;功 能:LCD1602寫(xiě)指令函數(shù)
- ;占用資源: P2.0--RS(LCD_RS),P2.1--RW(LCD_RW),P2.2--E(LCD_E).
- ;參數(shù)說(shuō)明:dat為寫(xiě)命令參數(shù)
- ;創(chuàng)建日期:2008.08.15
- ;版 本:FV1.0(函數(shù)版本Function Version)
- ;修改日期:--
- ;修改說(shuō)明:--
- ;-------------------------------------*/
- void LCD_write_command(uchar dat)
- {
- delay_n10us(10);
- LCD_RS=0; //指令
- LCD_RW=0; //寫(xiě)入
- LCD_E=1; //允許
- LCD_DB=dat;
- delay_n10us(10); //實(shí)踐證明,我的LCD1602上,用for循環(huán)1次就能完成普通寫(xiě)指令。
- LCD_E=0;
- delay_n10us(10); //實(shí)踐證明,我的LCD1602上,用for循環(huán)1次就能完成普通寫(xiě)指令。
- }
- /*--------------------------------------
- ;模塊名稱:LCD_write_data();
- ;功 能:LCD1602寫(xiě)數(shù)據(jù)函數(shù)
- ;占用資源: P2.0--RS(LCD_RS),P2.1--RW(LCD_RW),P2.2--E(LCD_E).
- ;參數(shù)說(shuō)明:dat為寫(xiě)數(shù)據(jù)參數(shù)
- ;創(chuàng)建日期:2008.08.15
- ;版 本:FV1.0(函數(shù)版本Function Version)
- ;修改日期:--
- ;修改說(shuō)明:--
- ;-------------------------------------*/
- void LCD_write_data(uchar dat)
- {
- delay_n10us(10);
- LCD_RS=1; //數(shù)據(jù)
- LCD_RW=0; //寫(xiě)入
- LCD_E=1; //允許
- LCD_DB=dat;
- delay_n10us(10);
- LCD_E=0;
- delay_n10us(10);
- }
- /*--------------------------------------
- ;模塊名稱:LCD_disp_char();
- ;功 能:LCD1602顯示一個(gè)字符函數(shù),在某個(gè)屏幕位置上顯示一個(gè)字符
- ,X(0-15),y(1-2)。
- ;占用資源:--
- ;參數(shù)說(shuō)明:X為1602的列值(取值范圍是0-15),y為1602的行值(取值范圍
- 是1-2),dat為所要顯示字符對(duì)應(yīng)的地址參數(shù)。
- ;創(chuàng)建日期:2008.08.15
- ;版 本:FV1.0(函數(shù)版本Function Version)
- ;修改日期:--
- ;修改說(shuō)明:--
- ;-------------------------------------*/
- void LCD_disp_char(uchar x,uchar y,uchar dat)
- {
- uchar address;
- if(y==1)
- address=0x80+x;
- else
- address=0xc0+x;
- LCD_write_command(address);
- LCD_write_data(dat);
- }
- /*--------------------------------------
- ;模塊名稱:LCD_disp_str();
- ;功 能:LCD1602顯示字符串函數(shù),在某個(gè)屏幕起始位置{X(0-15),y
- (1-2)}上顯示一個(gè)字符串。
- ;占用資源:--
- ;參數(shù)說(shuō)明:X為1602的列值(取值范圍是0-15),y為1602的行值(取值范圍
- 是1-2),str為所要顯示字符串對(duì)應(yīng)的指針參數(shù)。
- ;創(chuàng)建日期:2008.08.16
- ;版 本:FV1.0(函數(shù)版本Function Version)
- ;修改日期:--
- ;修改說(shuō)明:--
- ;-------------------------------------*/
- void LCD_disp_str(uchar x,uchar y,uchar *str)
- {
- uchar address;
- if(y==1)
- address=0x80+x;
- else
- address=0xc0+x;
- LCD_write_command(address);
- while(*str!='\0')
- {
- LCD_write_data(*str);
- str++;
- }
- }
- /*--------------------------------------
- ;模塊名稱:delay_n10us();
- ;功 能:延時(shí)函數(shù),延時(shí)約n個(gè)10us
- ;占用資源:--
- ;參數(shù)說(shuō)明:--
- ;創(chuàng)建日期:2008.08.15
- ;版 本:FV1.1(函數(shù)版本Function Version)
- ;修改日期:2008.08.16
- ;修改說(shuō)明:修改為較精確的延時(shí)函數(shù)
- ;-------------------------------------*/
- void delay_n10us(uint n)
- {
- uint i;
- for(i=n;i>0;i--)
- {
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); //延時(shí)10us@12M晶振
- }
- }
- //*********************第一部分LCD1602設(shè)置
- //END****************************************
- //*********************第二部分DHT90設(shè)置
- //START****************************************
- bit set_temp_up=0;
- bit set_temp_down=0;
- bit set_humidity_up=0;
- bit set_humidity_down=0;
-
- sbit SCK = P3^2; //定義通訊時(shí)鐘端口
- sbit DATA = P3^3; //定義通訊數(shù)據(jù)端口
- sbit D1=P3^4; //定義溫度報(bào)警端口
- sbit D2=P3^5; //定義濕度報(bào)警端口
- sbit D3=P3^6; //定義溫度報(bào)警端口
- sbit D4=P3^7; //定義濕度報(bào)警端口
- sbit key_set=P1^3;//設(shè)置功能選擇鍵
- sbit key_up=P1^4;//數(shù)字鍵加+
- sbit key_down=P1^5;//數(shù)字鍵減-
- uchar selectnum=0,downnum=0,checknum;
- uchar value_shi,value_ge,downnum_shi,downnum_ge;
- uchar shidu_shi,shidu_ge,wendu_shi,wendu_ge;
- sbit PWMZ = P2^0; //定義調(diào)速端口
- sbit PWMF = P2^1; //定義調(diào)速端口
- sbit PWMZ2 = P2^3; //定義調(diào)速端口
- sbit PWMF2 = P2^4; //定義調(diào)速端口
- sbit Alarm = P2^5;
- bit temp_alarm_flag=1;
- bit rh_alarm_flag=1;
- unsigned char CYCLE; //定義周期 該數(shù)字X基準(zhǔn)定時(shí)時(shí)間 如果是10 則周期是10 x 0.1ms
- unsigned char PWM_ON ;//定義高電平時(shí)間
- uchar flag;
- unsigned char CYCLE2; //定義周期 該數(shù)字X基準(zhǔn)定時(shí)時(shí)間 如果是10 則周期是10 x 0.1ms
- unsigned char PWM_ON2 ;//定義高電平時(shí)間
- uchar flag2;
- uchar temp_uplimit,temp_lowlimit,humidity_uplimit,humidity_lowlimit;
- unsigned int Alarm_temp_up=260,Alarm_temp_low=240,Alarm_humidity_up=700,Alarm_humidity_low=500;
- unsigned int wendu,shidu;
- typedef union
- {
- unsigned int i; //定義了兩個(gè)共用體
- float f;
- } value;
- enum {TEMP,HUMI}; //TEMP=0,HUMI=1
-
- #define noACK 0 //用于判斷是否結(jié)束通訊
- #define ACK 1 //結(jié)束數(shù)據(jù)傳輸
- //adr command r/w
- #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
- /****************定義函數(shù)****************/
- void s_transstart(void); //啟動(dòng)傳輸函數(shù)
- void s_connectionreset(void); //連接復(fù)位函數(shù)
- char s_write_byte(unsigned char value);//DHT90寫(xiě)函數(shù)
- char s_read_byte(unsigned char ack); //DHT90讀函數(shù)
- char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);//測(cè)量溫濕度函數(shù)
- void calc_dht90(float *p_humidity ,float *p_temperature);//溫濕度補(bǔ)償
- /*--------------------------------------
- ;模塊名稱:s_transstart();
- ;功 能:啟動(dòng)傳輸函數(shù)
- ;占用資源:--
- ;參數(shù)說(shuō)明:--
- ;創(chuàng)建日期:2008.08.15
- ;版 本:FV1.0(函數(shù)版本Function Version)
- ;修改日期:--
- ;修改說(shuō)明:--
- ;-------------------------------------*/
- void s_transstart(void)
- // generates a transmission start
- // _____ ________
- // DATA: |_______|
- // ___ ___
- // SCK : ___| |___| |______
- {
- DATA=1; SCK=0; // 初始化狀態(tài)
- // 對(duì)DATA SCK高低電平變化
- _nop_();
- SCK=1;
- _nop_();
- DATA=0;
- _nop_();
- SCK=0;
- _nop_();_nop_();_nop_();
- SCK=1;
- _nop_();
- DATA=1;
- _nop_();
- SCK=0;
- }
- /*--------------------------------------
- ;模塊名稱:s_connectionreset();
- ;功 能:連接復(fù)位函數(shù)
- ;占用資源:--
- ;參數(shù)說(shuō)明:--
- ;創(chuàng)建日期:2008.08.15
- ;版 本:FV1.0(函數(shù)版本Function Version)
- ;修改日期:--
- ;修改說(shuō)明:--
- ;-------------------------------------*/
- void s_connectionreset(void)
- // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
- // _____________________________________________________________
- // DATA: |_______|
- // _ _ _ _ _ _ _ _ _ ___ ___
- // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
- {
- unsigned char i;
- DATA=1; SCK=0; //初始信號(hào)狀態(tài)
- for(i=0;i<9;i++) //執(zhí)行9個(gè)時(shí)鐘信號(hào)
- {
- SCK=1;
- SCK=0;
- }
- s_transstart(); // 調(diào)用啟動(dòng)傳輸函數(shù)
- }
- void delay1ms(uint z) //這是一個(gè)毫秒級(jí)別的顯示函數(shù)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- /*--------------------------------------
- ;模塊名稱:s_write_byte();
- ;功 能:DHT90寫(xiě)函數(shù)
- ;占用資源:--
- ;參數(shù)說(shuō)明:--
- ;創(chuàng)建日期:2008.08.15
- ;版 本:FV1.0(函數(shù)版本Function Version)
- ;修改日期:--
- ;修改說(shuō)明:--
- ;-------------------------------------*/
- 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_();_nop_(); //pulswith approx. 5 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個(gè)上升沿將被DHT90自動(dòng)下拉為低電平。
- _nop_();_nop_();_nop_();
- SCK=0;
- DATA=1; //release DATA-line
- return error; //error=1 in case of no acknowledge //返回:0成功,1失敗
- }
-
- /*--------------------------------------
- ;模塊名稱:s_read_byte();
- ;功 能:DHT90讀函數(shù)
- ;占用資源:--
- ;參數(shù)說(shuō)明:--
- ;創(chuàng)建日期:2008.08.15
- ;版 本:FV1.0(函數(shù)版本Function Version)
- ;修改日期:--
- ;修改說(shuō)明:--
- ;-------------------------------------*/
- 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; //初始化
- for (i=0x80;i>0;i/=2) //開(kāi)始讀取數(shù)據(jù)
- {
- SCK=1;
- if (DATA) val=(val | i);
- _nop_();_nop_();_nop_();
- SCK=0;
- }
- if(ack==1)DATA=0; //如果是校驗(yàn)(ack==0),表示還沒(méi)讀取數(shù)據(jù)完成
- else DATA=1; //如果是校驗(yàn)(ack==0),讀取完后結(jié)束通訊
- _nop_();_nop_();_nop_(); //延時(shí) 5 us
- SCK=1; //SCK拉高
- _nop_();_nop_();_nop_(); //延時(shí) 5 us
- SCK=0; //SCK拉低
- _nop_();_nop_();_nop_(); //pulswith approx. 5 us
- DATA=1; //返回初始狀態(tài)
- return val;
- }
-
- /*--------------------------------------
- ;模塊名稱:s_measure();
- ;功 能:測(cè)量溫濕度函數(shù)
- ;占用資源:--
- ;參數(shù)說(shuō)明:--
- ;創(chuàng)建日期:2008.08.15
- ;版 本:FV1.0(函數(shù)版本Function Version)
- ;修改日期:--
- ;修改說(shuō)明:--
- ;-------------------------------------*/
- char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
- // makes a measurement (humidity/temperature) with checksum
- {
- unsigned error=0;
- unsigned int i;
-
- s_transstart(); //啟動(dòng)傳輸函數(shù)
- switch(mode)
- { //發(fā)送命令到傳感器
- 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; //直到測(cè)量溫度 濕度完畢
- if(DATA) error+=1; // 判斷是否在測(cè)量過(guò)程中發(fā)送錯(cuò)誤
- *(p_value) =s_read_byte(ACK); //讀取第一個(gè)字節(jié)
-
- *(p_value+1)=s_read_byte(ACK); //讀取第2個(gè)字節(jié)
- *p_checksum =s_read_byte(noACK); //讀取校驗(yàn)碼
- return error; //返回錯(cuò)誤 標(biāo)志
- }
-
- /*--------------------------------------
- ;模塊名稱:calc_dht90();
- ;功 能:溫濕度補(bǔ)償函數(shù)
- ;占用資源:--
- ;參數(shù)說(shuō)明:--
- ;創(chuàng)建日期:2008.08.15
- ;版 本:FV1.0(函數(shù)版本Function Version)
- ;修改日期:--
- ;修改說(shuō)明:--
- ;-------------------------------------*/
- void calc_dht90(float *p_humidity ,float *p_temperature)
- {
- const float C1=-4.0; // 定義C1為浮點(diǎn)數(shù)類型
- const float C2=+0.0405; // 定義C2為浮點(diǎn)數(shù)類型
- const float C3=-0.0000028; // 定義C3為浮點(diǎn)數(shù)類型
- const float T1=+0.01; // 定義T1為浮點(diǎn)數(shù)類型
- const float T2=+0.00008; // 定義T1為浮點(diǎn)數(shù)類型
- float rh=*p_humidity; // 定義rh為浮點(diǎn)數(shù)類型
- float t=*p_temperature; // 定義t為浮點(diǎn)數(shù)類型
- float rh_lin; // 定義rh_lin為浮點(diǎn)數(shù)類型
- float rh_true; // 定義rh_true為浮點(diǎn)數(shù)類型
- float t_C; // 定義t_C為浮點(diǎn)數(shù)類型
- t_C=t*0.01 - 40; //溫度補(bǔ)償
- rh_lin=C3*rh*rh + C2*rh + C1; //濕度補(bǔ)償
- rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //計(jì)算濕度值
- if(rh_true>100)rh_true=100; //如果測(cè)量到的數(shù)據(jù)大于100,取值為100
- if(rh_true<0.1)rh_true=0.1; //確定測(cè)量精度為一位小數(shù)點(diǎn)
- *p_temperature=t_C; //返回溫度值
- *p_humidity=rh_true; //返回濕度值
- }
- void Key_function_scan()
- {
- if(key_set==0)
- {
- delay1ms(10);
- if(key_set==0)
- {
- TR0 = 0;
- TR1 = 0;
-
- LCD_disp_str(0,1," ");
- LCD_disp_str(0,2," ");//清屏
- selectnum++;
- if(selectnum==1)
- {
- set_temp_up=1;//設(shè)置溫度上限位
- set_temp_down=0;
- set_humidity_up=0;
- set_humidity_down=0;
- LCD_disp_str(0,1," Set_Temp_Hight ");
- LCD_disp_char(5,2,(Alarm_temp_up%1000)/100+'0');
- LCD_disp_char(6,2,(Alarm_temp_up%100)/10+'0');
- LCD_disp_char(7,2,'.');
- LCD_disp_char(8,2,(Alarm_temp_up%10)+'0');
-
- }
- if(selectnum==2)
- {
- set_temp_down=1;//設(shè)置溫度下限位
- set_temp_up=0;
- set_humidity_up=0;
- set_humidity_down=0;
- LCD_disp_str(0,1," Set_Temp_Low ");
- LCD_disp_char(5,2,(Alarm_temp_low%1000)/100+'0'); //顯示溫度十位
- LCD_disp_char(6,2,(Alarm_temp_low%100)/10+'0'); //顯示溫度個(gè)位
- LCD_disp_char(7,2,'.');
- LCD_disp_char(8,2,(Alarm_temp_low%10)+'0'); //顯示溫度小數(shù)點(diǎn)后第一位
-
- }
- if(selectnum==3)
- {
- set_humidity_up=1;//設(shè)置濕度上限位
- set_humidity_down=0;
- set_temp_down=0;
- set_temp_up=0;
- LCD_disp_str(0,1," Set_Hum_Hight ");
- LCD_disp_char(5,2,(Alarm_humidity_up%1000)/100+'0'); //顯示濕度十位
- LCD_disp_char(6,2,(Alarm_humidity_up%100)/10+'0'); //顯示濕度個(gè)位
- LCD_disp_char(7,2,'.');
- LCD_disp_char(8,2,(Alarm_humidity_up%10)+'0'); //顯示濕度小數(shù)點(diǎn)后第一位
- }
- if(selectnum==4)
- {
- set_humidity_down=1;//設(shè)置濕度下限位
- set_humidity_up=0;
- set_temp_down=0;
- set_temp_up=0;
- LCD_disp_str(0,1," Set_Hum_Low ");
- LCD_disp_char(5,2,(Alarm_humidity_low%1000)/100+'0'); //顯示濕度十位
- LCD_disp_char(6,2,(Alarm_humidity_low%100)/10+'0'); //顯示濕度個(gè)位
- LCD_disp_char(7,2,'.');
- LCD_disp_char(8,2,(Alarm_humidity_low%10)+'0'); //顯示濕度小數(shù)點(diǎn)后第一位
- }
- if(selectnum==5)//返回測(cè)試溫度和濕度界面
- {
- LCD_disp_str(0,1," ");
- LCD_disp_str(0,2," ");//清屏
-
- selectnum=0;
- set_humidity_up=0;
- set_humidity_down=0;
- set_temp_down=0;
- set_temp_up=0;
- LCD_disp_str(0,1,"Temper: ");
- LCD_disp_str(0,2,"Humdity: ");
- }
- while(!key_set);//等待按鍵釋放
- }
- }
- //////////////////////////////////////////
- if(key_up==0)
- {
- delay1ms(10);
- if(key_up==0)
- {
- if(set_temp_up==1)
- { //溫度上限加
- Alarm_temp_up++;
- if(Alarm_temp_up==999)Alarm_temp_up=0;
- LCD_disp_char(5,2,(Alarm_temp_up%1000)/100+'0'); //顯示溫度十位
- LCD_disp_char(6,2,(Alarm_temp_up%100)/10+'0'); //顯示溫度個(gè)位
- LCD_disp_char(7,2,'.');
- LCD_disp_char(8,2,(Alarm_temp_up%10)+'0'); //顯示溫度小數(shù)點(diǎn)后第一位
- }
- if(set_humidity_up==1)
- { //濕度上限加
- Alarm_humidity_up++;
- if(Alarm_humidity_up==999)Alarm_humidity_up=0;
- LCD_disp_char(5,2,(Alarm_humidity_up%1000)/100+'0'); //顯示濕度十位
- LCD_disp_char(6,2,(Alarm_humidity_up%100)/10+'0'); //顯示濕度個(gè)位
- LCD_disp_char(7,2,'.');
- LCD_disp_char(8,2,(Alarm_humidity_up%10)+'0'); //顯示濕度小數(shù)點(diǎn)后第一位
- }
- if(set_temp_down==1)
- {//溫度下限
- Alarm_temp_low++;
- if(Alarm_temp_low==999)Alarm_temp_low=0;
- LCD_disp_char(5,2,(Alarm_temp_low%1000)/100+'0'); //顯示溫度十位
- LCD_disp_char(6,2,(Alarm_temp_low%100)/10+'0'); //顯示溫度個(gè)位
- LCD_disp_char(7,2,'.');
- LCD_disp_char(8,2,(Alarm_temp_low%10)+'0'); //顯示溫度小數(shù)點(diǎn)后第一位
-
- }
- if(set_humidity_down==1)
- {//濕度下限
- Alarm_humidity_low++;
- if(Alarm_humidity_low==999)Alarm_humidity_low=0;
- LCD_disp_char(5,2,(Alarm_humidity_low%1000)/100+'0'); //顯示濕度十位
- LCD_disp_char(6,2,(Alarm_humidity_low%100)/10+'0'); //顯示濕度個(gè)位
- LCD_disp_char(7,2,'.');
- LCD_disp_char(8,2,(Alarm_humidity_low%10)+'0'); //顯示濕度小數(shù)點(diǎn)后第一位
-
- }
- while(!key_up);//等待按鍵釋放
- }
-
- }
- //////////////////////////////////
- if(key_down==0)
- {
- delay1ms(10);
- if(key_down==0)
- {
- if(set_temp_down==1)
- {//溫度下限
- Alarm_temp_low--;
- if(Alarm_temp_low==0)Alarm_temp_low=999;
-
- LCD_disp_char(5,2,(Alarm_temp_low%1000)/100+'0'); //顯示溫度十位
- LCD_disp_char(6,2,(Alarm_temp_low%100)/10+'0'); //顯示溫度個(gè)位
- LCD_disp_char(7,2,'.');
- LCD_disp_char(8,2,(Alarm_temp_low%10)+'0'); //顯示溫度小數(shù)點(diǎn)后第一位
- }
- if(set_humidity_down==1)
- {//濕度下限
- Alarm_humidity_low--;
- if(Alarm_humidity_low==0)Alarm_humidity_low=999;
-
- LCD_disp_char(5,2,(Alarm_humidity_low%1000)/100+'0'); //顯示濕度十位
- LCD_disp_char(6,2,(Alarm_humidity_low%100)/10+'0'); //顯示濕度個(gè)位
- LCD_disp_char(7,2,'.');
- LCD_disp_char(8,2,(Alarm_humidity_low%10)+'0'); //顯示濕度小數(shù)點(diǎn)后第一位
- }
- if(set_temp_up==1)
- {//溫度
- Alarm_temp_up--;
- if(Alarm_temp_up==0)Alarm_temp_up=999;
-
- LCD_disp_char(5,2,(Alarm_temp_up%1000)/100+'0'); //顯示溫度十位
- LCD_disp_char(6,2,(Alarm_temp_up%100)/10+'0'); //顯示溫度個(gè)位
- LCD_disp_char(7,2,'.');
- LCD_disp_char(8,2,(Alarm_temp_up%10)+'0'); //顯示溫度小數(shù)點(diǎn)后第一位
-
- }
- if(set_humidity_up==1)
- {//濕度
- Alarm_humidity_up--;
- if(Alarm_humidity_up==0)Alarm_humidity_up=999;
-
- LCD_disp_char(5,2,(Alarm_humidity_up%1000)/100+'0'); //顯示濕度十位
- LCD_disp_char(6,2,(Alarm_humidity_up%100)/10+'0'); //顯示濕度個(gè)位
- LCD_disp_char(7,2,'.');
- LCD_disp_char(8,2,(Alarm_humidity_up%10)+'0'); //顯示濕度小數(shù)點(diǎn)后第一位
- }
- while(!key_down);//等待按鍵釋放
- }
-
- }
- /////////////////////////
- }
- void Alarm_Limit()
- {
- if(wendu<=Alarm_temp_low)//判斷溫度值是否超出設(shè)定范圍,如超出LED亮
- {
- D1=0;
- D2=1;
- temp_alarm_flag=0;
- TR0=1;
- flag=0; //反轉(zhuǎn)
- PWMZ = 0;
- }
- else
- {
-
- }
- if(wendu>=Alarm_temp_up)//判斷溫度值是否超出設(shè)定范圍,如超出LED亮
- {
- D2=0;
- D1=1;
- temp_alarm_flag=0;
- TR0=1;
- flag=1; //正轉(zhuǎn)
- PWMF = 0;
- }
- else
- {
-
-
- }
- if(wendu>Alarm_temp_low&&wendu<Alarm_temp_up) //溫度在范圍內(nèi)
- {
- D1=1;
- D2=1;
- temp_alarm_flag=1;
- TR0=0;
- PWMZ = 0;
- PWMF = 0;
- }
- if(shidu<=Alarm_humidity_low)//判斷濕度值是否超出設(shè)定范圍,如超出LED亮
- {
- D3=0;D4=1;
- rh_alarm_flag=0;
- TR1=1;
- flag2=0; //反轉(zhuǎn)
- PWMZ2 = 0;
- }
- else
- {
-
- }
- if(shidu>=Alarm_humidity_up)//判斷濕度值是否超出設(shè)定范圍,如超出LED亮
- {
- D4=0;D3=1;
- rh_alarm_flag=0;
- TR1=1;
- flag2=1; //正轉(zhuǎn)
- PWMF2 = 0;
- }
- else
- {
-
- }
- if(shidu>Alarm_humidity_low&&shidu<Alarm_humidity_up) //濕度在范圍內(nèi)
- {
- D3=1;
- D4=1;
- rh_alarm_flag=1;
- TR1=0;
- PWMZ2 = 0;
- PWMF2 = 0;
- }
-
- if(temp_alarm_flag==0||rh_alarm_flag==0)
- {
- Alarm=0;
- delay1ms(10);
- Alarm=1;
- }
- else
- {
- Alarm=1;
- }
- }
- //*********************第二部分DHT90設(shè)置
- //END****************************************
- void SysInit_two(void)
- {
- // TMOD |=0x01;//定時(shí)器設(shè)置 1ms in 12M crystal
- //TH0=(65536-1000)/256;
- // TL0=(65536-1000)%256;//定時(shí)1mS
- //IE= 0x82; //打開(kāi)中斷
- //TR0=1;
- TMOD=0X11; //T0 T1都工作在方式1(16位計(jì)數(shù)器)
- TH0=0x3c; //50ms
- TL0=0xb0;
- TR0=0;
- ET0=1;
- TH1=0x3C; //50ms
- TL1=0xB0;
- TR1=0;
- ET1=1;
- EA=1;
- }
- //*********主函數(shù)*****************
- unsigned char time_ms1;
- value humi_val,temp_val;
- unsigned char error,checksum;
- void dis()
- {
- error=0;
- error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity 測(cè)量濕度
- error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature 測(cè)量溫度
- if(error!=0) s_connectionreset(); //in case of an error: connection reset 判斷校驗(yàn)是否正確
- else
- {
- humi_val.f=(float)humi_val.i; //converts integer to float 濕度轉(zhuǎn)換成浮點(diǎn)數(shù)
- temp_val.f=(float)temp_val.i; //converts integer to float 溫度轉(zhuǎn)換成浮點(diǎn)數(shù)
- calc_dht90(&humi_val.f,&temp_val.f); //calculate humidity, temperature 計(jì)算出溫度 濕度值
- wendu=10*temp_val.f;
-
-
- LCD_disp_char(8,1,(wendu%1000)/100+'0'); //顯示溫度十位
- LCD_disp_char(9,1,(wendu%100)/10+'0'); //顯示溫度個(gè)位
- LCD_disp_str(10,1,".");
- LCD_disp_char(11,1,(wendu%10)+'0'); //顯示溫度小數(shù)點(diǎn)后第一位
- LCD_disp_char(12,1,0xdf);
- LCD_disp_str(13,1,"C");
- shidu=10*humi_val.f;
- LCD_disp_char(8,2,(shidu%1000)/100+'0'); //顯示濕度十位
- LCD_disp_char(9,2,(shidu%100)/10+'0'); //顯示濕度個(gè)位
- LCD_disp_str(10,2,".");
- LCD_disp_char(11,2,(shidu%10)+'0'); //顯示濕度小數(shù)點(diǎn)后第一位
- LCD_disp_str(12,2,"%");
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
7濕度檢測(cè)儀.7z
(66.24 KB, 下載次數(shù): 49)
2021-6-24 16:25 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|