欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 3152|回復(fù): 3
收起左側(cè)

51單片機(jī)發(fā)送數(shù)據(jù)給手機(jī)

[復(fù)制鏈接]
ID:640802 發(fā)表于 2019-12-28 15:53 | 顯示全部樓層 |閱讀模式
現(xiàn)做了一個(gè)用手機(jī)控制51單片機(jī)的窗簾自動(dòng)控制系統(tǒng),其中在手機(jī)手機(jī)上分為自動(dòng)模式和控制模式。
自動(dòng)模式下單片機(jī)可以根據(jù)光敏傳感器模塊來(lái)打開或關(guān)閉窗簾和LED燈,其中還要顯示當(dāng)前窗簾和LED燈的狀態(tài)。
發(fā)送到手機(jī)上采用的是ES8266。當(dāng)時(shí)預(yù)想是根據(jù)光敏傳感器模塊來(lái)判斷燈和窗簾的狀態(tài),但是一旦加上發(fā)送后自動(dòng)模式就會(huì)失去控制。
代碼如下:
/*
  通信波特率: 9600Mbps
  引腳定義:         led1=2.0               
*/
#include<reg51.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char

/*以下是51單片機(jī)的晶振大小        */
#define FOSC_110592M

/*引腳定義 */
sbit led=P1^1;//燈
sbit guang=P1^0;//光敏傳感器


sbit xianwei1=P1^2;        //限位位動(dòng)開關(guān)
sbit xianwei2=P1^3;

sbit led1=P0^0;// 第一個(gè)電機(jī)轉(zhuǎn)動(dòng)提示燈
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ù)組,遇到終止號(hào)/0將停止
void Uart1Sends(uchar *str)
{
        while(*str!='\0')
        {
                SBUF=*str;
                while(!TI);//等待發(fā)送完成信號(hào)(TI=1)出現(xiàn)
                TI=0;
                str++;
        }
}
/******延時(shí)函數(shù)****************/
void delaybj(unsigned int t)
{                           
   unsigned int k;
   while(t--)
   {
     for(k=0; k<60; k++)//用for的空循環(huán)延長(zhǎng)程序的執(zhí)行時(shí)間
     { }
   }
}

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ā)送一個(gè)字節(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)電機(jī)驅(qū)動(dòng)         正轉(zhuǎn)
void  motor_ffw()
{
   unsigned char i;

      for (i=0; i<8; i++)       //一個(gè)周期轉(zhuǎn)3.75*8=30度
        {
          P2 = RUN1[i]&0x1f;     //取數(shù)據(jù)
          delaybj(2);             //調(diào)節(jié)轉(zhuǎn)速

        }
  }
  //步進(jìn)電機(jī)驅(qū)動(dòng)  反轉(zhuǎn)
void  motor_ffz()
{
   unsigned char i;

      for (i=0; i<8; i++)       //一個(gè)周期轉(zhuǎn)3.75*8=30度
        {
          P2 = RUN2[i]&0x1f;     //取數(shù)據(jù)
          delaybj(2);             //調(diào)節(jié)轉(zhuǎn)速

        }
  }



void guangkong() //光敏控制
         {

if(guang==1)//有光的時(shí)候電機(jī)轉(zhuǎn)動(dòng)
{
   motor_ffz();
          led1=1;
                  led2=0;
                  shezhi=10;
               
}

if(guang==0)//無(wú)光的時(shí)候電機(jī)關(guān)閉          
{
      motor_ffw();
          led1=0;
                  led2=1;
                  shezhi=0;

          LE=0;                                 //發(fā)送數(shù)據(jù)到手機(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è)置的參考電平時(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)//停止電機(jī)
         {
          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
{                                                                                                                                                                                             
//手機(jī)控制的數(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)動(dòng)

case 'g':led=1;Key_Value=3;led1=1;led2=1;break; //手動(dòng)模式

case 'h':led=1;Key_Value=4;break;        //自動(dòng)模式

                  
}


}
}
         
  問(wèn)一下,發(fā)送數(shù)據(jù)應(yīng)該怎么放?,求大佬講一下               


回復(fù)

使用道具 舉報(bào)

ID:617449 發(fā)表于 2019-12-28 20:50 | 顯示全部樓層
你好!
1、中斷不能有沖突
2、初始化的AT指令要對(duì)
3、程序控制部分的命令要對(duì)
4、自動(dòng)和手動(dòng),設(shè)置一個(gè)標(biāo)志位
回復(fù)

使用道具 舉報(bào)

ID:640802 發(fā)表于 2019-12-29 15:04 | 顯示全部樓層
來(lái)51學(xué)習(xí)1 發(fā)表于 2019-12-28 20:50
你好!
1、中斷不能有沖突
2、初始化的AT指令要對(duì)

代碼上是這些問(wèn)題嗎?
回復(fù)

使用道具 舉報(bào)

無(wú)效樓層,該帖已經(jīng)被刪除
ID:358382 發(fā)表于 2020-4-24 16:40 | 顯示全部樓層
cankao和count的初始值是多少都沒設(shè)置出來(lái)
回復(fù)

使用道具 舉報(bào)

6#
無(wú)效樓層,該帖已經(jīng)被刪除

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表