//模块:HC-05蓝牙模块//功能:手机蓝牙控制3路继电器的开关//编写:ZXC//时间:2015-5-26#includereg52.h#defineuintunsignedint#defineucharunsignedcharsbitREY1=P1^2;sbitREY2=P1^1;sbitREY3=P1^0;uchardataSendDataPoint;uchardataSendDataLength;uchardataReceiveDataPoint;uchardataReceiveByte;ucharSendAskBatVolCommandCnt0;uchartmp[64];//tmp表示需要发送数据voidstartDischarge();voidstopDischarge();bitReceiveStart;bitReceiveFlag;voidReturn()//接收数据处理程序{uchari;if(ReceiveFlag){switch(tmp[0]){case0x31://开启继电器1REY1=0;SendDataPoint=0;SendDataLength=0;tmp[0]=0x31;while(SendDataPoint=SendDataLength){SBUF=tmp[SendDataPoint];while(TI==0);//等待一个字节发送完成TI=0;SendDataPoint++;}break;case0x32://关闭继电器1REY1=1;SendDataPoint=0;SendDataLength=0;tmp[0]=0x32;while(SendDataPoint=SendDataLength){SBUF=tmp[SendDataPoint];while(TI==0);//等待一个字节发送完成TI=0;SendDataPoint++;}break;case0x33://开启继电器2REY2=0;SendDataPoint=0;SendDataLength=0;tmp[0]=0x33;while(SendDataPoint=SendDataLength){SBUF=tmp[SendDataPoint];while(TI==0);//等待一个字节发送完成TI=0;SendDataPoint++;}break;case0x34://关闭继电器2REY2=1;SendDataPoint=0;SendDataLength=0;tmp[0]=0x34;while(SendDataPoint=SendDataLength){SBUF=tmp[SendDataPoint];while(TI==0);//等待一个字节发送完成TI=0;SendDataPoint++;}break;case0x35://开启继电器3REY3=0;SendDataPoint=0;SendDataLength=0;tmp[0]=0x35;while(SendDataPoint=SendDataLength){SBUF=tmp[SendDataPoint];while(TI==0);//等待一个字节发送完成TI=0;SendDataPoint++;}break;case0x36://关闭继电器3REY3=1;SendDataPoint=0;SendDataLength=0;tmp[0]=0x36;while(SendDataPoint=SendDataLength){SBUF=tmp[SendDataPoint];while(TI==0);//等待一个字节发送完成TI=0;SendDataPoint++;}break;}ReceiveFlag=0;for(i=0;i64;i++)tmp[i]=0;}}//*************接收程序*************voiduart(void)interrupt4using2{uchara;ES=0;if(RI){a=SBUF;RI=0;tmp[ReceiveDataPoint]=a;//if(a==0x31&&!ReceiveStart)//{ReceiveStart=1;//}if(ReceiveStart){ReceiveDataPoint++;//开始接收,指针地址加1ReceiveByte=0;//接收字节数ReceiveByte=0;目的是只接收一个字节if(ReceiveDataPoint=ReceiveByte){ReceiveFlag=1;}if(ReceiveDataPoint=0){ReceiveDataPoint=0;ReceiveStart=0;}}}ES=1;}voidmain()//主程序{SCON=0x50;//设置串口工作于方式1,允许接收TMOD=0x20;//TH1=0xfD;//设置波特率为9600TL1=0xfD;TR1=1;//启动定时器1ES=1;//允许串口中断EA=1;//打开所有中断while(1){Return();}