#include <reg52.h>
#include <intrins.h>
#include <math.h>
#include <stdio.h>
unsigned int wendu,shidu;
unsigned char code table_h[20]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned char code table_l[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
sbit d1 = P3^1;
sbit d2 = P3^2;
sbit d3 = P3^3;
sbit Sp = P1^7;
#define uchar unsigned char
#define uint unsigned int
void LED_init(void);
void LED_xianshi(void);
void delay_n10us(uint n);
void LED_init(void)
{
d1 = 1;
d2 = 1;
d3=1;
P0=0xff;
P2=0xff;
}
void LED_xianshi(void)
{
uint i;
uchar a,b,c,u,v,w;
for(i=0;i<51;i++)
{
P0 = 0xff;
P2 = 0xff;
delay_n10us(200);
a = (wendu/10);
b = (wendu%10);
c = wendu-10*a-b;
u = (shidu/10);
v = (shidu%10);
w = shidu-10*u-v;
d1=1;
P0 = table_h[a];
P2 = table_l[u];
delay_n10us(1000);
d1 = 0;
d2 = 1;
P0 = table_h[b]&0x7f;
P2 = table_l[v]&0x7f;
delay_n10us(1000);
d2 =0;
d3 = 1;
P0 = table_h[c];
P2 = table_l[w];
delay_n10us(1000);
d3 = 0;
}
}
void delay_n10us(uint n)
{
uint i;
for(i=n;i>0;i--)
{
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
}
sbit SCK = P1^1;
sbit DATA = P1^0;
typedef union
{ unsigned int i;
float f;
} value;
enum {TEMP,HUMI};
#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
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_dht10(float *p_humidity ,float *p_temperature);
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_dht10(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;
if(rh_true>99)rh_true=99;
if(rh_true<0.1)rh_true=0.1;
*p_temperature=t_C;
*p_humidity=rh_true;
}
void main(void)
{
value humi_val,temp_val;
unsigned char error,checksum;
LED_init();
s_connectionreset();
while(1)
{
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)
{
P0 = 0x86;
P2 = 0xc0;
s_connectionreset();
}
else
{
humi_val.f=(float)humi_val.i;
temp_val.f=(float)temp_val.i;
calc_dht10(&humi_val.f,&temp_val.f);
wendu=temp_val.f ;
shidu=humi_val.f;
LED_xianshi();
if (wendu>40|wendu<0)
{
Sp=0;
}
}
delay_n10us(1000);
}
}
- 1
- 2
- 3
- 4
- 5
前往页