各个模块与MSP430F5309连接图:作者:Edison.NO1116各个模块实物图:DHT11温湿度模块DHT11是数字的湿度传感器....6元一个买的...与它的通信方式类似于I2C总线通信.串行数据一次返回40位.数据格式:8bit湿度整数数据+8bit湿度小数数据+8bi温度整数数据+8bit温度小数数据+8bit校验和数据传送正确时校验和数据等于“8bit湿度整数数据+8bit湿度小数数据+8bi温度整数数据+8bit温度小数数据”所得结果的末8位。如果模块离芯片距离近,建议在数据线接一5k电阻上拉.时序图如下:第一次拉低要求至少18ms,之后拉高20-40us,之后DHT拉低80us后再拉高80us..之后进入数据阶段.每个数据,0,1以高电平的时间长短区分.0数据高电平时间为26-28us,1为70us..不论1或者0,之前都先输出50us的低电平..数据传输完成后拉高总线.TSL245光照模块MSP430程序代码:#includemsp430.h#includemsp430F5309.h//P2.1----dht11温湿度测量接口//P2.2-----illuvaluereadenabledtsl245片选、供电接口//P2.3-----soilresivaluereadenabled土壤电阻率测量片选、供电接口//P2.4-----soilfrequentandillufrequentinputtsl245和土壤电阻率模块频率输入口#defineHIGHP2OUT|=BIT1;#defineLOWP2OUT&=~BIT1;longintcount,freqvalue;inti,temp16,humd16,freqmeasureenabled;chartemph,templ,humdh,humdl,check,cal;voidDelayNus(intn){TA1CCR0=n;//sybchangedTA1CCTL0|=MC_1;//增计数到CCR0while(!(TA1CTL&BIT0));//等待TA1CTL&=~MC_1;//停止计数TA1CTL&=~BIT0;//清除中断标志}charreceive(void)//接受函数{chartem,cnt=0;//临时变量用于存储接受数据while(cnt8){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++;}returntem;}intdht11read(void)//dht11温湿度读取函数,读取数据成功返回值1,不成功返回0,读取的数据保存在temph,templ,humdh,humdl,humd16,temp16中{P2DIR|=BIT1;P2OUT|=BIT1;//1Mclock------------------------------------------------------------------------UCSCTL0=0x0000;//SetlowestpossibleDCOx,MODxUCSCTL1=DCORSEL_2;//SelectDCOrange2MHzoperationUCSCTL2=FLLD_1|31;//SetDCOMultiplierfor1MHz//(N+1)*FLLRef=Fdco//(31+1)*32768=1MHz//SetFLLDiv=fDCOCLK/2//1Mclock------------------------------------------------------------------------TA1CCTL0&=~CCIE;//CCR0interruptenabled-------------------?????????????iamnotsureitisrightTA1CTL=TASSEL_2|TACLR;//SMCLK,contmode,clearTARLOW;DelayNus(18000);HIGH;DelayNus(30);P2DIR&=~BIT1;while(P2IN&BIT1);while(!(P2IN&BIT1));while(P2IN&BIT1);//Datacomeshumdh=receive();humdl=receive();temph=receive();templ=receive();check=receive();cal=humdh+humdl+temph+templ;if(check==cal){temp16=templ+temph*256;humd16=humdl+humdh*256;return1;}else{temp16=0;humd16=0;return0;}}//--------------------------------------------------frequentreadstart----------------------------------------------------------------longintfrequentread(void){freqvalue=0;count=0;freqmeasureenabled=1;P2DIR&=~BIT4;TA1CCTL0=CCIE;//CCR0interruptenabledTA1CCR0=60000;//8Mclock------------------------------------------------------------------------UCSCTL0=0x0000;//SetlowestpossibleDCOx,MODxUCSCTL1=DCORSEL_5;//SelectDCOrange16MHzoperationUCSCTL2=FLLD_1|243;//SetDCOMultiplierfor8MHz//(N+1)*FLLRef=Fdco//(243+1)*32768=8MHz//SetFLLDiv=fDCOCLK/2//8Mclock------------------------------------------------------------------------TA1CTL=TASSEL_2|MC_2|TACLR|ID_3;//SMCLK,contmode,clearTAR,8-divider(0.2stime)__bis_SR_register(GIE);//EnterLPM0,enableinterruptswhile(freqmeasureenabled){if(i==(P2IN&BIT4)){i=!i;count=count+1;}}//1Mclock------------------------------------------------------------------------UCSCTL0=0x0000;//SetlowestpossibleDCOx,MODxUCSCTL1=DCORSEL_2;//SelectDCOrange2MHzoperationUCSCTL2=FLLD_1|31;//SetDCOMultiplierfor1MHz//(N+1)*FLLRef=Fdco//(31+1)*32768=1MHz//SetFLLDiv=fDCOCLK/2//1Mclock------------------------------------------------------------------------returnfreqvalue;//freqvalue=0;}//--------------------------------------------------frequentreadend-----------------------------------------------------------------//--------------------------------------------------illuandsoilresireadstart------------------------------------------------------intilluread()//光照强度读取函数,函数直接返回光照强度{intii;P2DIR|=BIT2;ii=frequentread()/333;P2DIR&=~BIT2;returnii;}intsoilresiread()//土壤电阻率读取函数,函数直接返回土壤电阻率值{intii;P2DIR|=BIT3;ii=0.5*(1/(0.7*0.00001*frequentread())-1);//频率换算成电阻值单位KOhmP2DIR&=~BIT3;returnii;}//--------------------------------------------------illuandsoilresireadend--------------------------------------------------------main(){//inttestsoilresivalue,testilluvalue,testhumdvalue,testtempvalue;WDTCTL=WDTPW+WDTHOLD;for(i=0;i5;i++){if(dht11read()==1){//testhumdvalue=humd16;//空气湿度值//testtempvalue=temp16;//空气温度值break;}}//testilluvalue=illuread();//光照强度值//testsoilresivalue=soilresiread();//土壤电阻率值单位:千欧姆}//Timer1interruptserviceroutine#pragmavector=TIMER1_A0_VECTOR__interruptvoidTIMER1_A0_ISR(void){freqvalue=count*8.3;count=0;freqmeasureenabled=0;TA1CCTL0&=~CCIE;//CCR0interruptdisabled}