以msp430f5529作为主控,连接了一些传感器,可以将传感器的信息通过GSM模块发送到别的手机上
GSM模块中要插上sim卡
整个工程就在这里了
单片机源程序如下:
- /*************************************************
- TXD 4.4 RST 76
- RXD 4.5 TEST 71
- CO2传感器 MGT811 5v P6.1 20.32
- 光强度传感器 BH1750 3.3v SDA-P3.1 SCL-P3.3 宽度 15.33
- 温湿度传感器 DHT11 3.3v P1.4 14.27
- 电扇 P75 水泵 P77
- 加湿器 P76 窗帘 P62
- ************************************************/
- #include "msp430f5529.h"
- #include "strings.h"
- #include "BH1750.h"
- #include "AT.h"
- #include "iic.h"
- #include "math.h"
- #define CPU (1000000)
- #define delay_us(x) (__delay_cycles((double)x*CPU/1000000.0))
- #define delay_ms(x) (__delay_cycles((double)x*CPU/1000.0))
- #define HIGH P1OUT|=BIT4;
- #define LOW P1OUT&=~BIT4;
- #define FANOFF P7OUT|=BIT5;
- #define FANON P7OUT&=~BIT5;
- #define PUMPON P7OUT|=BIT7;
- #define PUMPOFF P7OUT&=~BIT7;
- char temph,templ,humdh,humdl,check;
- #define Max 200
- char Buff[Max];
- int light_flag,mode_flag;
- int getco2[2];
- extern unsigned char BUF[2];
- extern char content[];
- char high[6]="(high)";
- char cold[6]="(cold)";
- char hot[5]="(hot)";
- char dry[5]="(dry)";
- char damp[6]="(damp)";
- char normal[8]="(normal)";
- char light[10]="Light: ";
- char air[10] = "\r\nCO2: ";
- char temp[15]= "\r\ntempture: ";
- char wet[15] = "\r\nhumidity: ";
- void water_get(void);
- char receive(void);
- void ADCinit();
- void ATinit(void);
- void InitbSys(void);
- void light_get(void);
- void co2_get(void);
- void convert(void);
- void Delay(unsigned int time)
- {
- unsigned int i,k;
- for(i=0;i<255;i++)
- for(k=0;k<time;k++)
- _NOP();
- }
- //****************** 本地变量声明 *************************//
- int ss[2];
- int flag1=0,i=0;
- int flag2=0,flag3=0,flag4=0; //读取短信内容用到的 flag
- u8 Times=0,First_Int = 0,shijian=0;
- vu8 Timer0_start;
- char NBUF[5],TBUF[5];
- char CBUF[5],T2BUF[5];
- char HBUF[5],T3BUF[5];
- char TEBUF[5],T4BUF[5];
- //***************** 开始吧!!!! *************************//
- void roll(int p)
- {
- for(i=1;i<p;i++)
- {
- P6OUT |= BIT2;;
- delay_us(500);
- P6OUT &= ~BIT2;
- delay_ms(20);
- }
- }
- void rollback(int p)
- {
- for(i=1;i<p;i++)
- {
- P6OUT |= BIT2;;
- delay_us(2500);
- P6OUT &=~BIT2;
- delay_ms(20);
- }
- }
- void humon(void)
- {
- P7OUT &=~ BIT6;
- delay_ms(100);
- P7OUT |= BIT6;
- }
- void send_buf( char *p)
- {
- while(*p !='\0') //C语言里字符串末尾自动加“\0”(ASCII码值为0)
- {
- while (!(UCA1IFG&UCTXIFG));
- UCA1TXBUF=*p; //发送字符对应的ASCII码,12864指针可设置自动指向下一个显示地址
- *p++;
- Delay(40);
- }
- }
- void main(void)
- {
- WDTCTL = WDTPW + WDTHOLD; // Stop WDT
- P2DIR|=BIT3+BIT2+BIT1;
- P2OUT|=BIT3+BIT2+BIT1;
- P7DIR |= BIT4+BIT5+BIT6+BIT7;
- P7OUT &=~(BIT4+BIT5+BIT6+BIT7);
- P7OUT |= BIT6;
- P6DIR|=BIT2;
- FANOFF;
- InitbSys();
- ATinit();
- ADCinit();
- TA0CCTL0 = CCIE;
- TA0CCR0 = 50000;
- TA0CTL = TASSEL_2 + MC_1 + TACLR;
- _EINT();
- wait();
- Set_Text_Mode();
- Second_AT_Command("AT+CMGD=1,4","OK",3);
- Delay(2000);
- mode_flag=1;
- light_flag=0;
- while(1)
- {
- if(mode_flag==1)
- {
- P2OUT &=~ BIT1;
- if((Find("C")||Find("M")||Find("T")||Find("I")))
- {
- P2OUT |= BIT1;
- send_buf("AT+CMGR=1\r\n");
- Delay(2000);
- if(Find("aaa"))
- {
- roll(300);
- light_flag=1;
- }
- else if(Find("bbb"))
- {
- rollback(270);
- light_flag=0;
- }
- else if(Find("ccc"))
- {
- FANON;
- }
- else if(Find("ddd"))
- {
- FANOFF;
- }
- else if(Find("eee"))
- {
- PUMPON;
- /* Delay(5000);
- Delay(5000);
- Delay(5000);
- PUMPOFF;*/
- }
- else if(Find("fff"))
- {
- PUMPOFF;
- }
- else if(Find("ggg"))
- {
- humon();
- }
- else if(Find("hhh"))
- {
- water_get();
- co2_get();
- light_get();
- convert();
- Send((char*)content);
- }
- else if(Find("000"))
- {
- mode_flag=0;
- P2OUT &=~ BIT2;
- }
- else{}
- Clear();
- Second_AT_Command("AT+CMGD=1,4","OK",3);
- Delay(2000);
- Second_AT_Command("AT+CMGD=1,4","OK",3);
- Delay(2000);
- Clear();
- }
- }
- else
- {
- P2OUT &=~ BIT2;
- P2OUT ^= BIT3;
- light_get();
- if((BUF[0]*255+BUF[1]>=300)&&(light_flag==0))
- {
- roll(300);
- light_flag=1;
- }
- else if((BUF[0]*255+BUF[1]<=200)&&(light_flag==1))
- {
- rollback(300);
- light_flag=0;
- }
- else{;}
- water_get();
- Delay(5000);
- if((humdh*255+humdl>=9000)||(getco2[0]*100+getco2[1]>2300))
- {
- FANON;
- }
- else
- {
- FANOFF;
- }
- co2_get();
- }
- }
- // __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
- }
- #pragma vector=TIMER0_A0_VECTOR
- __interrupt void TIMER0_A0_ISP(void)
- {
- if(Timer0_start)
- Times++;
- if(Times > (10*shijian))
- {
- Timer0_start = 0;
- Times = 0;
- }
- }
- #pragma vector=USCI_A1_VECTOR
- __interrupt void USCI_A1_ISR(void)
- {
- switch(__even_in_range(UCA1IV,4))
- {
- case 0:break; // Vector 0 - no interrupt
- case 2: // Vector 2 - RXIFG
- Buff[First_Int] = UCA1RXBUF;
- First_Int++;
- if(First_Int >= Max)
- First_Int = 0;
- break;
- case 4:break; // Vector 4 - TXIFG
- default: break;
- }
- }
- #pragma vector=PORT2_VECTOR
- __interrupt void Key_interrput()
- {
- if(P2IFG&BIT7)
- {
- Delay(200);
- if(P2IFG&BIT7)
- {
- while((P2IN&BIT7)==0);//松手检测
- P2IFG &= ~BIT7; //清除中断标志位
- if(mode_flag==0)
- {
- P2OUT |= BIT2+BIT3;
- mode_flag=1; //暗淡状态短信控制
- }
- else
- {
- P2OUT &=~ BIT2;
- mode_flag=0; //发光状态自动控制
- }
- } } }
- #pragma vector=ADC12_VECTOR
- __interrupt void ADC12_ISR(void)
- {
- switch(__even_in_range(ADC12IV,34))
- {
- case 0:break;
- case 2:break;
- case 4:break;
- case 6:
- getco2[0]=ADC12MEM0/100;
- getco2[1]=ADC12MEM0%100;
- //__bic_SR_register_on_exit(LPM0_bits);
- case 8:break; case 9:break; case 10:break; case 11:break;
- case 12:break; case 13:break; case 14:break; case 15:break;
- case 16:break; case 17:break; case 18:break; case 19:break;
- case 20:break; case 21:break;
- case 22:break; case 23:break; case 24:break; case 25:break;
- case 26:break; case 27:break; case 28:break;
- case 29:break; case 30:break; case 31:break; case 32:break;
- case 33:break; case 34:break;
- default:break;
- }
- }
- void convert(void)
- {
- int A,B,reall,realc,realh,realt;
- for(i=0;i<200;i++)
- content[i]=0;
- ss[0]=getco2[0];
- ss[1]=getco2[1];
- //*******************光强度获取值转换**********************//
- reall=BUF[0]*255+BUF[1];
- A=0;
- for(i=0;i<5;i++)
- {
- NBUF[i] = (reall-A)/(pow(10,4-i));
- A+=NBUF[i]*(pow(10,4-i));
- }
- for(i=0;i<5;i++)
- NBUF[i]=NBUF[i]+48;
- B=0;
- for(i=0;i<5;i++)
- {
- if(NBUF[i]!=48)
- {
- TBUF[B]=NBUF[i];
- B++;
- }
- }
- //*******************co2获取值转换**********************//
- realc=getco2[0]*100+getco2[1];
- A=0;
- for(i=0;i<5;i++)
- {
- CBUF[i] = (realc-A)/(pow(10,4-i));
- A+=CBUF[i]*(pow(10,4-i));
- }
- for(i=0;i<5;i++)
- CBUF[i]=CBUF[i]+48;
- B=0;
- for(i=0;i<5;i++)
- {
- if(CBUF[i]!=48)
- {
- T2BUF[B]=CBUF[i];
- B++;
- }
- }
- //*******************湿度获取值转换**********************//
- realh=humdh*255+humdl;
- A=0;
- for(i=0;i<5;i++)
- {
- HBUF[i] = (realh-A)/(pow(10,4-i));
- A+=HBUF[i]*(pow(10,4-i));
- }
- for(i=0;i<5;i++)
- HBUF[i]=HBUF[i]+48;
- B=0;
- for(i=0;i<5;i++)
- {
- if(HBUF[i]!=48)
- {
- T3BUF[B]=HBUF[i];
- B++;
- }
- }
- //*******************温度获取值转换**********************//
- realt=temph*255+templ;
- A=0;
- for(i=0;i<5;i++)
- {
- TEBUF[i] = (realt-A)/(pow(10,4-i));
- A+=TEBUF[i]*(pow(10,4-i));
- }
- for(i=0;i<5;i++)
- TEBUF[i]=TEBUF[i]+48;
- B=0;
- for(i=0;i<5;i++)
- {
- if(TEBUF[i]!=48)
- {
- T4BUF[B]=TEBUF[i];
- B++;
- }
- }
- //******************************************************//
- strcat(content,(char*)light);
- strcat(content,(char*)TBUF);
- strcat(content,(char*)air);
- strcat(content,(char*)T2BUF);
- //if(realc>2500) strcat(content,(char*)high);
- //else strcat(content,(char*)normal);
- strcat(content,(char*)wet);
- strcat(content,(char*)T3BUF);
- // if(realh>600) strcat(content,(char*)damp);
- // else if(realh<100) strcat(content,(char*)dry);
- // else strcat(content,(char*)normal);
- strcat(content,(char*)temp);
- strcat(content,(char*)T4BUF);
- // if(realt>1800) strcat(content,(char*)hot);
- // else if(realt<400) strcat(content,(char*)cold);
- // else strcat(content,(char*)normal);
- }
- void ATinit(void)
- {
- P4SEL |=BIT4+BIT5 ; // P5.6,7 = USCI_A1 TXD/RXD
- UCA1CTL1 |= UCSWRST; // **Put state machine in reset**
- UCA1CTL1 |= UCSSEL_2; // SMCLK
- UCA1BR0 = 9; // 1MHz 115200 (see User's Guide)
- UCA1BR1 = 0; // 1MHz 115200
- UCA1MCTL |= UCBRS_1 + UCBRF_0; // Modulation UCBRSx=1, UCBRFx=0
- UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
- UCA1IE |= UCRXIE; // Enable USCI_A1 RX interrupt
- }
- void InitbSys()
- {
- P2IE |= BIT7; //设置p2.7可以中断
- P2IES |= BIT7; //设置p2.7为下降沿中断
- P2IFG &= ~BIT7;//设置p2.7为0 无中断请求
- P2REN |= BIT7; //设置p2.7为上下拉电阻使能
- P2OUT |= BIT7;
- UCSCTL4 |= SELS__DCOCLK ;
- InitIIC();
- WriteBH1750(0x01,1);
- P4DIR|=BIT7;
- }
- void ADCinit()
- {
- ADC12CTL0 = ADC12SHT02 + ADC12ON;
- ADC12CTL1 = ADC12SHP;
- ADC12IE = 0X01;
- ADC12CTL0 |= ADC12ENC;
- P6SEL |= 0X01;
- }
- void light_get()
- {
- WriteBH1750(0x01,1);
- WriteBH1750(0x10,1);
- delay_ms(180);
- ReadBH1750Seq(0x01);
- }
- void co2_get()
- {
- ADC12CTL0 |= ADC12SC;
- //__bis_SR_register(LPM0_bits + GIE);
- }
- char receive(void)
- {
- unsigned char num=0,tem,cnt; //临时变量用于存储接受数据
- for(cnt=0;cnt<8;cnt++)
- {
- tem=0;
- while(!(P1IN&BIT4)); //等待14us的低电平结束
- delay_us(26);
- if((P1IN&BIT4)) /////////长于30us定义为1
- {
- tem=1;
- while((P1IN&BIT4)); //等待一位的采集结束
- }
- num<<=1;
- num|=tem;
- }
- return num;
- }
- void water_get(void)
- {
- P1DIR|=BIT4;
- LOW;
- delay_ms(20);
- HIGH;
- delay_us(30); //20--40
- P1DIR &=~BIT4;
- if(!(P1IN&BIT4)); //从机发出响应信号
- {
- while(!(P1IN&BIT4)); //等待响应响应结束
- while((P1IN&BIT4)); //开始采集
- humdh=receive();
- humdl=receive();
- temph=receive();
- templ=receive();
- check=receive();
- }
- }
复制代码
所有资料51hei提供下载:
Z8 LH.rar
(725.19 KB, 下载次数: 32)
|