msp430f149控制DHT11温湿度传感器的串口程序
[复制链接]
哪位大侠写过msp430f149控制DHT11温室读传感器的串口显示程序,我调了好久总是不能显示数据 ?不知道什么原因?高手帮忙啊,不胜感激之至!!
串口程序;
#include "usart.h" struct DHT_data { unsigned char DH_H; //湿度整数 unsigned char DH_L; //湿度小数 unsigned char T_H; //温度整数 unsigned char T_L; //温度小数 unsigned char Checksum; //校验和 }DHT_data; void Usart_Init(void) { P3SEL |= 0x30; // P3.4,5 = USCI_A0 TXD/RXD ME1 |= UTXE0 + URXE0; // 使能USART0收发 UCTL0 |= CHAR; // 8-bit character UTCTL0 |= SSEL0; // UCLK = ACLK UBR00 = 0x03; // 32k/9600 - 3.41 UBR10 = 0x00; // UMCTL0 = 0x4A; // Modulation UCTL0 &= ~SWRST; // 初始化UART0状态机 IE1 |= URXIE0; // 使能接收中断 _EINT(); } void TXString( char* string, int length )//发送字符串 { int pointer; for( pointer = 0; pointer < length; pointer++) { volatile int i; TXBUF0 = string[pointer]; while (!(IFG1 & UTXIFG0)); } } void my_putchar(unsigned char ch) { TXBUF0=(unsigned char) ch; while (!(IFG1 & UTXIFG0)); } void my_puts(unsigned char *s) { while(*s++) my_putchar(*s); }
DHT11 程序:
#include "dht11.h" unsigned char humidity, temperature, check_sum ; /**********************************************/ /* 开始 DHT11 温湿度计 */ /* 输入:无 */ /* 输出:应答标志 0:应答失败 1:应答成功 */ /**********************************************/ unsigned char start_DHT11(void) { unsigned char time=0; DHT11_OUTPUT; DHT11_H; DHT11_L; //拉低18ms以上 delay_ms(30);
DHT11_H; delay_us(40); DHT11_INPUT; // temp=DHT11_IN; if(!DHT11_IN) { while(!(DHT11_IN)&&time++); while(DHT11_IN&&time++); return 1; } else { return 0; } } void read_DHT11(void) { unsigned char tem[5]; //读到的五个数 unsigned char i, j, temp; for(i = 0; i < 5; i ++) { tem = 0; for(j = 0; j < 8; j ++) { tem *= 2; //不可用tem << 1 否则不移位 temp = 2; // delay_us(20); //先延20uS 再检查30uS while((!DHT11_IN) && temp ++)//避免死等 if(temp == 2) break; delay_us(32); //高电平长于26-28uS为1否为0 temp = 2; if(DHT11_IN) { while(DHT11_IN && temp ++) //当为1时等到低电平再进入下次循环。 if(temp == 2) break; tem |= 0x01; } delay_us(5); } humidity = tem[0]; temperature = tem[2]; check_sum = tem[4]; } }
主函数main:
#include <msp430x14x.h> #include "dht11.h" #include "usart.h" const char word1[] = {"\r\n**DHT11 TEST**\r\n"}; const char word2[] = {"\r\n**DHT11 CHECK**\r\n"}; const char word3[] = {"\r\n**DHT11 NOT EXIST**\r\n"}; void display(void);
void main( void ) { WDTCTL=WDTPW+WDTHOLD; //关看门狗 Usart_Init(); P2DIR |=BIT7;//点亮LED0 RED P2OUT &=~BIT7; P2DIR |=BIT5;//点亮LED1 GREEN TXString((char*)word1, sizeof word1); delay_ms(1000); delay_ms(1000); while(1) { if(start_DHT11()) //如果应答成功 { P2OUT |=BIT7; TXString((char*)word2, sizeof word2); read_DHT11(); //读温湿度 display(); //则显示温湿度数值 } else { P2OUT &=~BIT7; TXString((char*)word3, sizeof word3); } P2OUT |=BIT5; delay_ms(1000); P2OUT&=~BIT5; delay_ms(1000); } } void display(void) { unsigned char table[11]; table[0]=humidity/0x10+0x30;//湿度 table[1]=humidity%0x10+0x30; table[2]='%'; table[3]='R'; table[4]='H'; table[5]=' '; table[6]=temperature/0X10+0x30;//温度 table[7]=temperature%0X10+0x30; table[8]='C'; table[9]='\r'; table[10]='\n'; TXString((char*)table, sizeof table); }
需要的头文件:
usart.h
#ifndef _USART_H_ #define _USART_H_ #include"msp430x14x.h" void Usart_Init(void); void TXString( char* string, int length );//发送字符串 void my_putchar(unsigned char ch); void my_puts(unsigned char *s); #endif
dht11.h
#ifndef _DHT11_H_ #define _DHT11_H_ #include "msp430x14x.h" #define CPU (12000000) #define delay_us(x) (__delay_cycles((double)x*CPU/1000000.0)) #define delay_ms(x) (__delay_cycles((double)x*CPU/1000.0))
#define DHT11_OUTPUT P1DIR|=BIT0 #define DHT11_INPUT P1DIR&=~BIT0 #define DHT11_H P1OUT|=BIT0 #define DHT11_L P1OUT&=~BIT0 #define DHT11_IN (P1IN&BIT0) extern unsigned char humidity, temperature, check_sum ; unsigned char start_DHT11(void); void read_DHT11(void);
#endif
|