DHT11湿度传感器在MSP430下的驱动
發表於 : 2012-08-31 09:15:11
http://www.cnblogs.com/sunbeam/archive/ ... 93012.html
代碼: 選擇全部
#include <msp430x14x.h>
#define HIGH P2OUT|=BIT1;
#define LOW P2OUT&=~BIT1;
void DelayNus(int n)
{
CCR0 = n;
TACTL |= MC_1; //增计数到CCR0
while(!(TACTL & BIT0)); //等待
TACTL &= ~MC_1; //停止计数
TACTL &= ~BIT0; //清除中断标志
}
char receive(void) //接受函数
{
char tem,cnt=0; //临时变量用于存储接受数据
while(cnt<8)
{
while(!(P2IN&BIT1)); //等待50us的低电平结束
DelayNus(45);
if(P2IN&BIT1) //长于30us定义为1
{
tem++;
tem<<=1;
while(P2IN&BIT1); //结束高电平
}
else
{
tem<<=1;
}
if(cnt!=7)
while(!(P2IN&BIT1)); //最后一次给函数返回留下时间
cnt++;
}
return tem;
}
int main( void )
{
int i;
char temph,templ,humdh,humdl,check,cal;
WDTCTL=WDTPW+WDTHOLD;
_DINT();
BCSCTL1&=~XT2OFF;
do
{
IFG1&=~OFIFG;
for(i=0xff;i>0;i--);
}
while(IFG1&OFIFG);
_EINT();
BCSCTL2|=SELM_2+SELS; //SMCLK 8分频
P2DIR|=BIT1;
P2OUT|=BIT1;
TACTL|=TASSEL_2+ID_3;
//inited
LOW;
DelayNus(18000);
HIGH;
DelayNus(30);
P2DIR&=~BIT1;
while(P2IN&BIT1);
while(!(P2IN&BIT1));
while(P2IN&BIT1);
//Data comes
humdh=receive();
humdl=receive();
temph=receive();
templ=receive();
check=receive();
cal=humdh+humdl+temph+templ;
if(check==cal)
{
LPM0;
}
return 0;
}