最近在做MSP430F149驱动DHT11采集温湿度然后在LCD1602上显示的实验,但硬件连接好后总是显示00.0,好像DHT11没工作,看了好久找不出程序问题,希望有经验的你帮下忙,谢谢。。。原程序如下:
#include "msp430x14x.h" // 包含头文件
#define uchar unsigned char
#define uint unsigned int
#define DHT11_OUTPUT P2DIR|=BIT0
#define DHT11_INPUT P2DIR&=~BIT0
#define DHT11_H P2OUT|=BIT0
#define DHT11_L P2OUT&=~BIT0
#define DHT11_IN (P2IN&BIT0)
uchar str1[]={" "};
uchar str2[]={" "};
uchar dis1[] = {"TRH RH: "}; //定义数组
uchar dis2[] = {"TRH T: "};
uchar TH_data,TL_data,RH_data,RL_data,CK_data;
uchar TH_temp,TL_temp,RH_temp,RL_temp,CK_temp;
uchar com_data,untemp,temp;
uchar respond;
void Delayus(int n)
{
CCR0 = n;
TACTL |= (MC_1 + TACLR + TAIE); //增计数到CCR0,计数器清零,定时器A中断打开
TACTL |= TACLR;
TACTL |= MC_1;
while(!(TACTL & BIT0)); //等待
TACTL &= ~(MC_1 + TAIE + BIT0); //停止计数,关中断,清除中断标志
TACTL &= ~MC_1; //停止计数
TACTL &= ~BIT0; //清除中断标志
}
void delay(uint z)
{
uint t,y;
for(t=z;t>0;t--)
{
for(y=110;y>0;y--);
}
}
void write_com(uchar com)
{
P3OUT&=~BIT0;//RS
P3OUT&=~BIT1; //RW
P5OUT=com;
delay(5);
P3OUT|=BIT2; //E
delay(5);
P3OUT&=~BIT2;
}
void write_data(uchar date)
{
P3OUT|=BIT0;
P3OUT&=~BIT1;
P5OUT=date;
delay(5);
P3OUT|=BIT2;
delay(5);
P3OUT&=~BIT2;
}
void LCD_set_xy( uchar x, uchar y )
{
uchar address;
if (y == 0)
address = 0x80 + x;
else
address = 0xc0 + x;
write_com(address);
}
void LCD_write_char( uchar x,uchar y,uchar dat)
{
LCD_set_xy( x, y );
write_data(dat);
}
void init()
{
P3DIR=0xff;
P3SEL=0;
P3OUT=0x00;
P3OUT&=~BIT2;
P3OUT&=~BIT0;
P5DIR=0XFF;
P5SEL=0;
P5OUT=0X00;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
//设定LCD显示位置
void lcd_dis_pos(uchar pos)
{
write_com(pos | 0x80); //数据指针=80+地址变量
}
//收发信号检测,数据读取
char receive()
{
uchar i;
com_data=0;
for(i=0;i<=7;i++)
{
respond=2;
while((!DHT11_IN)&&respond++);
Delayus(40);
Delayus(40);
if(DHT11_IN)
{
temp=1;
respond=2;
while((DHT11_IN)&&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
//校验 8位 == CK_data
void read_TRH()
{
DHT11_OUTPUT; //设置P2.0输出
DHT11_H; ////输出高
Delayus(10);
DHT11_L;
Delayus(20000);
DHT11_H;
//DATA总线由上拉电阻拉高 主机延时20us
Delayus(40);
DHT11_INPUT;
Delayus(60);
Delayus(60);
if(!(DHT11_IN))
{
respond=2;
//判断DHT11发出 80us 的低电平响应信号是否结束
while((!DHT11_IN)&&respond++);
respond=2;
//判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
while((DHT11_IN)&&respond++);
//数据接收状态
RH_temp = receive();
RL_temp = receive();
TH_temp = receive();
TL_temp = receive();
CK_temp = receive();
DHT11_OUTPUT; //设置P2.0输出
DHT11_H; //P2.0输出高,释放总线
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;
}
}
// RH_data = 21;
// RL_data = 13;
// TH_data =36;
// TL_data = 40;
//湿度整数部分
str1[0] =(char)0X30+RH_data/10;
str1[1] =(char) 0X30+RH_data%10;
str1[2] =0x2e; //小数点
//湿度小数部分
str1[3] =(char) 0X30+RL_data/10;
str1[5] = 0X25; //"%"
str1[6] = 0X52; //"R"
str1[7] = 0X48; //"H"
//温度整数部分
str2[0] =(char) 0X30+TH_data/10;
str2[1] =(char) 0X30+TH_data%10;
str2[2] =(char) 0x2e; //小数点
//温度小数部分
str2[3] = 0X30+TL_data/10;
str2[5] = 0X27; //"'"
str2[6] = 0X43; //"C"
}
void main (void)
{
uchar i,n=0x40,m;
WDTCTL = WDTPW + WDTHOLD;
TACTL|= TASSEL_2 + ID_3;
init();
while(1)
{
read_TRH();
//写字符
for(i=0;i<=7;i++)
{
lcd_dis_pos(i); //显示字符
write_data(dis1
);
lcd_dis_pos(n+i); //显示字符
write_data(dis2);
}
//写湿度数据
m=0x08;
for(i=0;i<=7;i++)
{
lcd_dis_pos(m);
write_data(str1);
m++;
}
//写温度数据
m=0x48;
for(i=0;i<=7;i++)
{
lcd_dis_pos(m);
write_data(str2);
m++;
}
}
}
此帖出自小平头技术问答
u16 Get_SHT11(void)
{ static u16 step=0,dey;
static value humi_val,temp_val;
float dew_point;
static unsigned char error =0;
unsigned char Hbyte,Lbyte,crc;
OsTaskStart();
s_transstart(); //transmission start
//send command to sensor
error+=s_write_byte(MEASURE_TEMP);
OsDelay(30,1);
//for (i=0;i<65535;i++) if((READ_SDA())==0) break;//wait until sensor has finished the measurement
if(READ_SDA()) error+=1; // or timeout (~2 sec.) is reached
Hbyte =s_read_byte(ACK); //read the first byte (MSB)
Lbyte =s_read_byte(ACK); //read the second byte (LSB)
crc=s_read_byte(noACK); //read checksum
temp_val.i=0;
temp_val.i |= Hbyte<<8; //read the first byte (MSB)
temp_val.i |= Lbyte; //read the second byte (LSB)
temp_val.crc =crc; //read checksum
s_transstart(); //transmission start
//send command to sensor
error+=s_write_byte(MEASURE_HUMI);
OsDelay(30,2);
//for (i=0;i<65535;i++) if((READ_SDA())==0) break;//wait until sensor has finished the measurement
if(READ_SDA()) error+=1; // or timeout (~2 sec.) is reached
Hbyte =s_read_byte(ACK); //read the first byte (MSB)
Lbyte =s_read_byte(ACK); //read the second byte (LSB)
crc=s_read_byte(noACK); //read checksum
humi_val.i=0;
humi_val.i |= Hbyte<<8; //read the first byte (MSB)
humi_val.i |= Lbyte; //read the second byte (LSB)
humi_val.crc =crc; //read checksum
if(error!=0) s_connectionreset(); //in case of an error: connection reset
else
{ humi_val.f=(float)humi_val.i; //converts integer to float
temp_val.f=(float)temp_val.i; //converts integer to float
calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
printf("SHT11: temp:%5.1fC, humi:%5.1f%%, dew point:%5.1fC ",temp_val.f,humi_val.f,dew_point);
}
error=0;
OsDelay(50,3);
OsTaskReloop();
}
//----------------------------------------------------------------
void iicDelay(u16 us)
{ u16 i;
for(i=0;i<us;i++)
{
}
}
//----------------------------------------------------------------
void s_connectionreset(void)
{ unsigned char i;
//配置端口PB6,7为OD模式
// GPIOB->CRL &=~(0x88000000);
iicDelay(1);
GPIO_SetBits(GPIOB, GPIO_Pin_7);//SDA=1;
GPIO_ResetBits(GPIOB, GPIO_Pin_6);//SCL=0;
iicDelay(1);
for(i=0;i<9;i++) //9 SCK cycles
{ GPIO_SetBits(GPIOB, GPIO_Pin_6);//SCK=1;
iicDelay(1);
GPIO_ResetBits(GPIOB, GPIO_Pin_6);//SCK=0;
iicDelay(1);
}
GPIO_SetBits(GPIOB, GPIO_Pin_6);//SCK=1;
iicDelay(1);
GPIO_ResetBits(GPIOB, GPIO_Pin_7);//SDA=0;
iicDelay(1);
GPIO_ResetBits(GPIOB, GPIO_Pin_6);//SCK=0;
iicDelay(1);
GPIO_SetBits(GPIOB, GPIO_Pin_6);//SCK=1;
iicDelay(1);
GPIO_SetBits(GPIOB, GPIO_Pin_7);//SDA=1;
iicDelay(1);
//配置端口PB6.7为复用OD模式
//GPIOB->CRL |=0x88000000 ;
iicDelay(1);
}
//----------------------------------------------------------------
char s_write_byte(unsigned char value)
{ unsigned char i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking
{ if (i & value) SDA_H(); //masking value with i , write to SENSI-BUS
else SDA_L();
iicDelay(1);
SCL_H(); //clk for SENSI-BUS
iicDelay(1); //pulswith approx. 5 us
SCL_L();
iicDelay(1);
}
SDA_H(); //release DATA-line
iicDelay(1);
SCL_H(); //clk #9 for ack
iicDelay(1);
error=READ_SDA(); //check ack (DATA will be pulled down by SHT11)
SCL_L();
iicDelay(1);
return error; //error=1 in case of no acknowledge
}
//----------------------------------------------------------------------------------
char s_read_byte(unsigned char ack)
{
unsigned char i,val=0;
SDA_H();
iicDelay(1); //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{
SCL_H(); //clk for SENSI-BUS
iicDelay(1);
if (READ_SDA()) val=(val | i); //read bit
SCL_L();
iicDelay(1);
}
if(ack)SDA_L();else SDA_H(); //in case of "ack==1" pull down DATA-Line
iicDelay(1);
SCL_H(); //clk #9 for ack
iicDelay(1); //pulswith approx. 5 us
SCL_L();
iicDelay(1);
SDA_H(); //release DATA-line
iicDelay(1);
return val;
}
//----------------------------------------------------------------
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
void s_transstart(void)
{
iicDelay(1);
SCL_H();//SCK=1;
iicDelay(1);
SDA_L();//DATA=0;
iicDelay(1);
SCL_L();//SCK=0;
iicDelay(1);
SCL_H();//SCK=1;
iicDelay(1);
SDA_H();//DATA=1;
iicDelay(1);
SCL_L();//SCK=0;
iicDelay(1);
}
//----------------------------------------------------------------
// resets the sensor by a softreset
char s_softreset(void)
{ unsigned char error=0;
s_connectionreset(); //reset communication
error+=s_write_byte(RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
}
//----------------------------------------------------------------
// reads the status register with checksum (8-bit)
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
{ unsigned char error=0;
s_transstart(); //transmission start
error=s_write_byte(STATUS_REG_R); //send command to sensor
*p_value=s_read_byte(ACK); //read status register (8-bit)
*p_checksum=s_read_byte(noACK); //read checksum (8-bit)
return error; //error=1 in case of no response form the sensor
}
//----------------------------------------------------------------
// writes the status register with checksum (8-bit)
char s_write_statusreg(unsigned char *p_value)
{ unsigned char error=0;
s_transstart(); //transmission start
error+=s_write_byte(STATUS_REG_W);//send command to sensor
error+=s_write_byte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}
//----------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
char s_measure(u16 *p_value, unsigned char *p_checksum, unsigned char mode)
{ unsigned error=0;
u32 i;
unsigned char Hbyte,Lbyte,crc;
s_transstart(); //transmission start
switch(mode){ //send command to sensor
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*100;i++) if((READ_SDA())==0) break; //wait until sensor has finished the measurement
if(READ_SDA()) error+=1; // or timeout (~2 sec.) is reached
Hbyte =s_read_byte(ACK); //read the first byte (MSB)
Lbyte =s_read_byte(ACK); //read the second byte (LSB)
crc=s_read_byte(noACK); //read checksum
*p_value=0;
*p_value |= Hbyte<<8; //read the first byte (MSB)
*p_value |= Lbyte; //read the second byte (LSB)
*p_checksum =crc; //read checksum
return error;
}
//----------------------------------------------------------------
// calculates temperature and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp
void calc_sth11(float *p_humidity ,float *p_temperature)
{ const float C1=-4.0; // for 12 Bit
const float C2=+0.0405; // for 12 Bit
const float C3=-0.0000028; // for 12 Bit
const float T1=+0.01; // for 14 Bit @ 5V
const float T2=+0.00008; // for 14 Bit @ 5V
float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature
t_C=t*0.01 - 40; //calc. temperature from ticks to
rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
if(rh_true>100)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range
*p_temperature=t_C; //return temperature
*p_humidity=rh_true; //return humidity[%RH]
}
//----------------------------------------------------------------
// 计算露点
// input: humidity [%RH], temperature
// output: dew point
float calc_dewpoint(float h,float t)
{ float logEx,dew_point;
logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
}
一周热门 更多>