/*
* humidity.h
*
* Created on: 2016年11月5日
* Author: Administrator
*/
#ifndef HUMIDITY_H_
#define HUMIDITY_H_
#include "lcd1602.h"
uchar Read_dht11_byte();
void Get_dat();
uchar Read_dht11_byte()
{
uchar i = 0,dat = 0,k;
P2DIR &= ~BIT1;
while(!(P2IN & BIT1));
for(i=0;i<8;i++)
{
delay_us(40);
if((BIT1 & P2IN) == 1)
k = 1;
else
k = 0;
dat <<= 1;
dat |= k;
}
return dat;
}
void Get_dat()
{
uint R_H,R_L,T_H,T_L,RH,RL,TH,TL,check;
uchar H[2],T[2];
P2DIR |= BIT1;
P2OUT &= ~BIT1;
P2OUT |= BIT1;
P2OUT &= ~BIT1;
delay_ms(18);
P2OUT |= BIT1;
delay_us(40);
P2DIR &= ~BIT1;
while(P2IN & BIT1);//等待dht11反应电平
delay_us(80);
while(!(P2IN & BIT1));//准备传送数据
R_H = Read_dht11_byte();
R_L = Read_dht11_byte();
T_H = Read_dht11_byte();
T_L = Read_dht11_byte();
check = Read_dht11_byte();
if((R_H+R_L+T_H+T_L) == check)
{
RH = R_H;
RL = R_L;
TH = T_H;
TL = T_L;
}
H[0] = (char)(0x30 + RH %10);
H[1] = (char)(0x30 + RL /10);
T[0] = (char)(0x30 + TH %10);
T[1] = (char)(0x30 + TL /10);
LCDShowStr(0,0,"H:");
LCDShowStr(2,0,H);
LCDShowStr(0,1,"T:");
LCDShowStr(2,1,T);
}
#endif /* HUMIDITY_H_ */
发一段正确代码
#include
#include "lcd1602.h"
/*
* main.c
*/
#define CPU_F ((double)1000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0)) //微秒延时程序
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
uchar table[] = {" s: %RH "};
uchar table1[] ={" w: "};
uchar table2[] ={"SH: % WH: C "};
uchar able3[] ={"SL: % WL: C "};
uint com_data,respond,temp,untemp;
uint TH_data,TL_data,RH_data,RL_data,CK_data;
uint TH_temp,TL_temp,RH_temp,RL_temp,CK_temp;
uchar s1num,num,shis[1],shig[1],wens[1],weng[1];
//****************************************************************************/
//收发信号检测,数据读取
/****************************************************************************/
char receive()
{
uint i;
com_data=0;
P2DIR &=~ BIT1;
for(i=0;i<=7;i++)
{
respond=2;
while((!(P2IN&BIT1))&&respond++);
delay_us(20);
if(P2IN&BIT1)
{
temp=1;
respond=2;
while((P2IN&BIT1)&&respond++);
}
else
temp=0;
com_data<<=1;
com_data|=temp;
}
return(com_data);
}
/****************************************************************************/
//湿度读取子程序
//温度高8位== TL_data
//温度低8位== TH_data
//湿度高8位== RH_data
//湿度低8位== RH_data
//检验位 == CK_data
/***************************************************************************/
void GET_TRH()
{
//主机拉低18MS
P2DIR |=BIT1;
P2OUT &=~BIT1;
delay_ms(18);
P2OUT |=BIT1;
//DATA总线由上啦电阻拉高 主机延时20us
delay_us(20);
//主机设为输入 判断从机的响应信号
P2DIR &=~ BIT1;
//判断DHT11 是否有低电平响应信号
if(!(P2IN&BIT1))
{
respond=2;
//判断DHT12发出80us的低电平响应信号是否结束
while((!(P2IN&BIT1))&& respond++);
respond=2;
//判断从机是否发出80us的高电平
while((P2IN&BIT1) && respond++);
//数据接收状态
RH_temp = receive();
RL_temp = receive();
TH_temp = receive();
TL_temp = receive();
CK_temp = receive();
P2DIR |=BIT1;
P2OUT |=BIT1;
//数据检验
untemp=(RH_temp+RL_temp+TH_temp+TL_temp);
if(untemp==CK_temp)
{
RH_data = RH_temp;
RL_data = RL_temp;
TH_data = TH_temp;
TL_data = TL_temp;
CK_data = CK_temp;
}
}
//湿度整数部分
//s=(int)(RH_data/10);
//g=(int)(RH_data%10);
shis[0]= (char)(0X30+RH_data/10); //湿度的十位
shig[0]= (char)(0X30+RH_data%10); //湿度的个位
//温度整数部分
wens[0]= (char)(0X30+TH_data/10); //温度的十位
weng[0]= (char)(0X30+TH_data%10); //温度的个位
LCDShowStr(0,0,"H:");
LCDShowStr(2,0,shis);
LCDShowStr(3,0,shig);
LCDShowStr(4,0,"%RH");
LCDShowStr(0,1,"T:");
LCDShowStr(2,1,wens);
LCDShowStr(3,1,weng);
LCDShowStr(4,1,"C");
/* delay_ms(500);
LCDShowStr(2,0," ");
LCDShowStr(3,0," ");*/
}
int main(void) {
WDTCTL=WDTPW+WDTHOLD; //关闭看门狗
BCSCTL1=CALBC1_1MHZ; //BCSCTL1 Calibration Data for 1MHz
DCOCTL=CALDCO_1MHZ; //DCOCTL Calibration Data for 1MHz
BCSCTL2=SELM_1+DIVM_0; //设置MCLK为1MHZ
P2SEL |= 0x00;
P1DIR |=(BIT0+BIT1+BIT2+BIT4+BIT5+BIT6+BIT7);
P1OUT &= ~(BIT0|BIT1|BIT2|BIT4|BIT5|BIT6|BIT7);
LCD_Init();
while(1)
{
GET_TRH();
}
}
|