氣象參數仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
abc0528d395559abcfd66afbc6ea6d93.png (35.69 KB, 下載次數: 47)
下載附件
2021-6-11 14:48 上傳
單片機氣象參數檢測源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #include <math.h>
- #include <stdio.h>
- #define LCD_DB P0
- sbit LCD_RS=P2^0;
- sbit LCD_RW=P2^1;
- sbit LCD_E=P2^2;
- sbit ADCS =P3^4;
- sbit ADDI =P3^7;
- sbit ADDO =P3^7;
- sbit ADCLK =P3^6;
- sbit SCK = P2^6;
- sbit DATA = P2^7;
- sbit h=P1^0;
- sbit t=P1^1;
- sbit p=P1^2;
- #define noACK 0
- #define ACK 1
- #define STATUS_REG_W 0x06
- #define STATUS_REG_R 0x07
- #define MEASURE_TEMP 0x03
- #define MEASURE_HUMI 0x05
- #define RESET 0x1e
- #define uchar unsigned char
- #define uint unsigned int
- uint temp;
- uchar getdata;
- typedef union
- { unsigned int i;
- float f;
- } value;
- enum {TEMP,HUMI};
- void s_transstart(void);
- void s_connectionreset(void);
- char s_write_byte(unsigned char value);
- char s_read_byte(unsigned char ack);
- char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
- void calc_dht90(float *p_humidity ,float *p_temperature);
- void LCD_init(void);
- void LCD_write_command(uchar command);
- void LCD_write_data(uchar dat);
- void LCD_disp_char(uchar x,uchar y,uchar dat);
- void LCD_disp_str(uchar x,uchar y,uchar *str);
- void delay_n10us(uint n);
- void LCD_init(void)
- {
- delay_n10us(10);
- LCD_write_command(0x38);
- delay_n10us(10);
- LCD_write_command(0x0c);
- delay_n10us(10);
- LCD_write_command(0x06);
- delay_n10us(10);
- LCD_write_command(0x01);
- delay_n10us(1000);
- }
- void LCD_write_command(uchar dat)
- {
- delay_n10us(10);
- LCD_RS=0;
- LCD_RW=0;
- LCD_E=1;
- LCD_DB=dat;
- delay_n10us(10);
- LCD_E=0;
- delay_n10us(10);
- }
- void LCD_write_data(uchar dat)
- {
- delay_n10us(10);
- LCD_RS=1;
- LCD_RW=0;
- LCD_E=1;
- LCD_DB=dat;
- delay_n10us(10);
- LCD_E=0;
- delay_n10us(10);
- }
- 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);
- }
- 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++;
- }
- }
- void delay_n10us(uint n)
- {
- uint i;
- for(i=n;i>0;i--)
- {
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- }
- }
- void s_transstart(void)
- {
- DATA=1; SCK=0;
- _nop_();
- SCK=1;
- _nop_();
- DATA=0;
- _nop_();
- SCK=0;
- _nop_();_nop_();_nop_();
- SCK=1;
- _nop_();
- DATA=1;
- _nop_();
- SCK=0;
- }
- void s_connectionreset(void)
- {
- unsigned char i;
- DATA=1; SCK=0;
- for(i=0;i<9;i++)
- {
- SCK=1;
- SCK=0;
- }
- s_transstart();
- }
- char s_write_byte(unsigned char value)
- {
- unsigned char i,error=0;
- for (i=0x80;i>0;i/=2)
- {
- if (i & value) DATA=1;
- else DATA=0;
- SCK=1;
- _nop_();_nop_();_nop_();
- SCK=0;
- }
- DATA=1;
- SCK=1;
- error=DATA;
- _nop_();_nop_();_nop_();
- SCK=0;
- DATA=1;
- return error;
- }
-
- char s_read_byte(unsigned char ack)
- {
- unsigned char i,val=0;
- DATA=1;
- for (i=0x80;i>0;i/=2)
- { SCK=1;
- if (DATA) val=(val | i);
- _nop_();_nop_();_nop_();
- SCK=0;
- }
- if(ack==1)DATA=0;
- else DATA=1;
- _nop_();_nop_();_nop_();
- SCK=1;
- _nop_();_nop_();_nop_();
- SCK=0;
- _nop_();_nop_();_nop_();
- DATA=1;
- return val;
- }
-
- char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
- {
- unsigned error=0;
- unsigned int i;
-
- s_transstart();
- switch(mode){
- 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;
- if(DATA) error+=1;
- *(p_value) =s_read_byte(ACK);
- *(p_value+1)=s_read_byte(ACK);
- *p_checksum =s_read_byte(noACK);
- return error;
- }
-
- void calc_dht90(float *p_humidity ,float *p_temperature)
- { const float C1=-4.0;
- const float C2=+0.0405;
- const float C3=-0.0000028;
- const float T1=+0.01;
- const float T2=+0.00008;
- float rh=*p_humidity;
- float t=*p_temperature;
- float rh_lin;
- float rh_true;
- float t_C;
- t_C=t*0.01 - 40;
- rh_lin=C3*rh*rh + C2*rh + C1;
- rh_true=(t_C-25)*(T1+T2*rh)+rh_lin-3;
- if(rh_true>100)rh_true=100;
- if(rh_true<0.1)rh_true=0.1;
- *p_temperature=t_C;
- *p_humidity=rh_true;
- }
- unsigned int Adc0832(unsigned char channel)
- {
- uchar i=0;
- uchar j;
- uint dat=0;
- uchar ndat=0;
- if(channel==0)channel=2;
- if(channel==1)channel=3;
- ADDI=1;
- _nop_();
- _nop_();
- ADCS=0;
- _nop_();
- _nop_();
- ADCLK=1;
- _nop_();
- _nop_();
- ADCLK=0;
- _nop_();
- _nop_();
- ADCLK=1;
- ADDI=channel&0x1;
- _nop_();
- _nop_();
- ADCLK=0;
- _nop_();
- _nop_();
- ADCLK=1;
- ADDI=(channel>>1)&0x1;
- _nop_();
- _nop_();
- ADCLK=0;
- ADDI=1;
- _nop_();
- _nop_();
- dat=0;
- for(i=0;i<8;i++)
- {
- dat|=ADDO;
- ADCLK=1;
- _nop_();
- _nop_();
- ADCLK=0;
- _nop_();
- _nop_();
- dat<<=1;
- if(i==7)dat|=ADDO;
- }
- for(i=0;i<8;i++)
- {
- j=0;
- j=j|ADDO;
- ADCLK=1;
- _nop_();
- _nop_();
- ADCLK=0;
- _nop_();
- _nop_();
- j=j<<7;
- ndat=ndat|j;
- if(i<7)ndat>>=1;
- }
- ADCS=1;
- ADCLK=0;
- ADDO=1;
- dat<<=8;
- dat|=ndat;
- return(dat);
- }
- void main(void)
- {
- value humi_val,temp_val;
- int vary;
- unsigned char error,checksum;
- unsigned int wendu,shidu;
- unsigned int temp;
- float press;
- LCD_init();
- s_connectionreset();
- LCD_disp_str(0,1,"TE");
- LCD_disp_str(9,1,"P");
- LCD_disp_str(4,2,"RH");
- LCD_disp_str(2,1,"TTT.TC");
- LCD_disp_str(6,2,"RRR.R%");
- LCD_disp_str(10,1,"PPP.Pk");
- delay_n10us(2000);
- while(1)
- {
- p=1;
- getdata=Adc0832(0);
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
51hei.png (11.05 KB, 下載次數: 49)
下載附件
2021-6-11 15:45 上傳
所有資料51hei提供下載:
氣象參數.rar
(111.09 KB, 下載次數: 74)
2021-6-11 14:04 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|