|
#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);
vary=getdata;
press=((10.0/23.0)*vary)+9.3;
temp=(int)(press*10);
LCD_disp_char(10,1,temp/1000+'0');
LCD_disp_char(11,1,(temp%1000)/100+'0');
LCD_disp_char(12,1,(temp%100)/10+'0');
LCD_disp_char(14,1,(temp%10)+'0');
if(getdata<15||getdata>232)
p=0;
error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
if(error!=0)
s_connectionreset();
else
{
t=1;
humi_val.f=(float)humi_val.i;
temp_val.f=(float)temp_val.i;
calc_dht90(&humi_val.f,&temp_val.f);
wendu=10*temp_val.f;
if(temp_val.f<-10||temp_val.f>30)
t=0;
h=1;
LCD_disp_char(2,1,wendu/1000+'0');
LCD_disp_char(3,1,(wendu%1000)/100+'0');
LCD_disp_char(4,1,(wendu%100)/10+'0');
LCD_disp_char(6,1,(wendu%10)+'0');
shidu=10*humi_val.f;
if(humi_val.f<40||humi_val.f>60)
h=0;
LCD_disp_char(6,2,shidu/1000+'0');
LCD_disp_char(7,2,(shidu%1000)/100+'0');
LCD_disp_char(8,2,(shidu%100)/10+'0');
LCD_disp_char(10,2,(shidu%10)+'0');
}
delay_n10us(8000);
}
}
|
|