希望用过MSP430控制DHT11以及感兴趣的坛友看一下
[复制链接]
最近在做实验:用MSP430F149控制DHT11测量温湿度然后在1602上显示。不知道为什么湿度和温度都总是显示00.0,希望有经验的你们看一下程序有什么问题,谢谢。程序如下:
#include "msp430x14x.h" // 包含头文件
#define uchar unsigned char #define uint unsigned int #define DHT11_OUTPUT P2DIR|=BIT6
#define DHT11_INPUT P2DIR&=~BIT6 #define DHT11_H P2OUT|=BIT6 #define DHT11_L P2OUT&=~BIT6 #define DHT11_IN (P2IN&BIT6)
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 delay_ms(uchar ms) { uchar i; while(ms--) { for(i = 0; i< 150; i++) { _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); } } }
void delay_us() { uchar i; i--; i--; i--; i--; i--; i--; }
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++); delay_us(); delay_us(); delay_us(); 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 //调用的程序有 delay();, Delay_5us();,RECEIVE(); void read_TRH() { DHT11_OUTPUT;
DHT11_H; ////输出高 delay_ms(10); DHT11_L; delay_ms(18); DHT11_H; //DATA总线由上拉电阻拉高 主机延时20us DHT11_INPUT; delay_us(); delay_us(); delay_us(); delay_us(); 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; DHT11_H; 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; } } //湿度整数部分 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(); delay_us(); 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++;
} //延时 delay_ms(1000) ; } }
[ 本帖最后由 兜揣阳光 于 2012-3-29 22:52 编辑 ]
|