|
現(xiàn)做了一個用手機控制51單片機的窗簾自動控制系統(tǒng),其中在手機手機上分為自動模式和控制模式。
自動模式下單片機可以根據(jù)光敏傳感器模塊來打開或關(guān)閉窗簾和LED燈,其中還要顯示當(dāng)前窗簾和LED燈的狀態(tài)。
發(fā)送到手機上采用的是ES8266。當(dāng)時預(yù)想是根據(jù)光敏傳感器模塊來判斷燈和窗簾的狀態(tài),但是一旦加上發(fā)送后自動模式就會失去控制。
代碼如下:
/*
通信波特率: 9600Mbps
引腳定義: led1=2.0
*/
#include<reg51.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
/*以下是51單片機的晶振大小 */
#define FOSC_110592M
/*引腳定義 */
sbit led=P1^1;//燈
sbit guang=P1^0;//光敏傳感器
sbit xianwei1=P1^2; //限位位動開關(guān)
sbit xianwei2=P1^3;
sbit led1=P0^0;// 第一個電機轉(zhuǎn)動提示燈
sbit led2=P0^1;//
unsigned char Key_Value=0;
unsigned char code RUN1[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};//正轉(zhuǎn)
unsigned char code RUN2[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1};//反轉(zhuǎn)
//變量定義
uchar cankao;
uchar shezhi;//燈亮滅程度設(shè)置
uchar count;
char *strx=0;//
unsigned char x=1,y=0,Rxbuff[50],Rxnum;
unsigned char Tx_Buf[1];
unsigned char LE,aa;
//串行口連續(xù)發(fā)送char型數(shù)組,遇到終止號/0將停止
void Uart1Sends(uchar *str)
{
while(*str!='\0')
{
SBUF=*str;
while(!TI);//等待發(fā)送完成信號(TI=1)出現(xiàn)
TI=0;
str++;
}
}
/******延時函數(shù)****************/
void delaybj(unsigned int t)
{
unsigned int k;
while(t--)
{
for(k=0; k<60; k++)//用for的空循環(huán)延長程序的執(zhí)行時間
{ }
}
}
void delayms(unsigned int x)
{
unsigned int i;
while(x--)
for(i=125;i>0;i--);
}
void Clear_Buffer(void)//清空緩存
{
int i;
for(i=0;i<50;i++)
Rxbuff[i]=0;//
Rxnum=0;
}
void UART_send_byte(char dat)//發(fā)送一個字節(jié)
{
SBUF = dat;
while (TI == 0);
TI = 0;
}
void Send_Str(unsigned char *buf) // 發(fā)送字符串
{
while (*buf != '\0')
{
UART_send_byte(*buf++);
}
}
void Send_DATA(uchar *buffer) //發(fā)送數(shù)據(jù)
{
//Uart1Sends("AT+CIPSEND=0,1\r\n");
Send_Str("AT+CIPSEND=0,1\r\n");
delayms(300);
Send_Str(buffer);//發(fā)送數(shù)據(jù)
// Uart1Sends(buffer);
delayms(100);
Clear_Buffer();
}
//步進(jìn)電機驅(qū)動 正轉(zhuǎn)
void motor_ffw()
{
unsigned char i;
for (i=0; i<8; i++) //一個周期轉(zhuǎn)3.75*8=30度
{
P2 = RUN1[i]&0x1f; //取數(shù)據(jù)
delaybj(2); //調(diào)節(jié)轉(zhuǎn)速
}
}
//步進(jìn)電機驅(qū)動 反轉(zhuǎn)
void motor_ffz()
{
unsigned char i;
for (i=0; i<8; i++) //一個周期轉(zhuǎn)3.75*8=30度
{
P2 = RUN2[i]&0x1f; //取數(shù)據(jù)
delaybj(2); //調(diào)節(jié)轉(zhuǎn)速
}
}
void guangkong() //光敏控制
{
if(guang==1)//有光的時候電機轉(zhuǎn)動
{
motor_ffz();
led1=1;
led2=0;
shezhi=10;
}
if(guang==0)//無光的時候電機關(guān)閉
{
motor_ffw();
led1=0;
led2=1;
shezhi=0;
LE=0; //發(fā)送數(shù)據(jù)到手機
Tx_Buf[0]=LE+0x30;
Send_DATA(Tx_Buf);
}
}
//串口初始化函數(shù)
void init()
{
TMOD=0x21;
SM0=0;
SM1=1;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
TR0=1;
IE=0X82;
cankao=10; //設(shè)置的參考電平時間
TH1=0xfd;
TL1=0xfd;
SCON=0x50;
PCON=0x00;
IP=0x10;
TR1=1;
ES = 1;
EA=1;
}
void fs()
{
if(led1==0)
{
//
// LE=led1;
// Tx_Buf[0]=LE+0x30;
// Send_DATA(Tx_Buf);
}
}
void delay(uint ttt)
{
while(ttt--);
}
//ESP8266上電初始化
void esp8266_init()
{
Uart1Sends("AT+CIPMUX=1\r\n");
delay(50000);
Uart1Sends("AT+CIPSERVER=1,8080\r\n");
}
//主函數(shù)
void main()
{
delay(50000);
delay(500);
init();
esp8266_init();
while(1)
{
if(Key_Value==1)//正轉(zhuǎn)
{
motor_ffw();
}
if(Key_Value==2)//反轉(zhuǎn)
{
motor_ffz();
}
if(Key_Value==3)//停止電機
{
P2=0xf0;
// P0=0xff;
}
if(xianwei1==0||xianwei2==0)
{
Key_Value=3;
}
if(shezhi<0)//調(diào)光亮度極限值的控制
{
shezhi=1;
}
if(shezhi>10)
{
shezhi=10;
}
if(Key_Value==4)//光控
{
guangkong();
}
}
}
void timer() interrupt 1 //PWM占空比調(diào)節(jié)服務(wù)函數(shù)
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
if (count==shezhi)
{
led=1;
}
count++;
if(count ==cankao)
{
count=0;
if(shezhi!=0)
led=0;
}
}
//串口服務(wù)的函數(shù)
void time() interrupt 4
{
//手機控制的數(shù)據(jù)
if(RI)
{
RI=0;
switch(SBUF)
{
case 'a':shezhi++;break;//燈亮度加
case 'b':shezhi--;break; // 燈亮度減
case 'c':shezhi=10;break;// 打開燈
case 'd':shezhi=0;break; // 關(guān)閉燈
case 'e':Key_Value=1;led1=0;led2=1;break; //正轉(zhuǎn)
case 'f':Key_Value=2;led1=1;led2=0;break; //反轉(zhuǎn)
case 'i':Key_Value=3;led1=1;led2=1;break; //停止轉(zhuǎn)動
case 'g':led=1;Key_Value=3;led1=1;led2=1;break; //手動模式
case 'h':led=1;Key_Value=4;break; //自動模式
}
}
}
問一下,發(fā)送數(shù)據(jù)應(yīng)該怎么放?,求大佬講一下
|
|