標題: 自做藍牙小車,代碼源程序 [打印本頁]

作者: kirin    時間: 2017-4-10 21:59
標題: 自做藍牙小車,代碼源程序
簡單實現(xiàn)前后左右

#include<AT89x51.H>

//HL-1小車驅(qū)動接線定義
#define A_left_moto_go  {P2_0 = 1,P2_1 = 0;}
#define A_left_moto_back {P2_0 = 0,P2_1 = 1;}
#define A_left_moto_stop {P2_0 = 0,P2_1 = 0;}
#define A_right_moto_go {P2_2 = 1,P2_3 = 0;}
#define A_right_moto_back {P2_2 = 0,P2_3 = 1;}
#define A_right_moto_stop {P2_2 = 0,P2_3 = 0;}

#define B_left_moto_go {P1_0 = 1,P1_1 = 0;}
#define B_left_moto_back {P1_0 = 0,P1_1 = 1;}
#define B_left_moto_stop {P1_0 = 0,P1_1 = 0;}
#define B_right_moto_go {P1_2 = 1,P1_3 = 0;}
#define B_right_moto_back {P1_2 = 0,P1_3 = 1;}
#define B_right_moto_stop {P1_2 = 0,P1_3 = 0;}                   

#define uint    unsigned int                          //重定義無符號整數(shù)類型
#define uchar   unsigned char                         //重定義無符號字符類型

#define led P0_0

/************************************************************************/       
//延時函數(shù)       
   void delay(unsigned int k)
{   
     unsigned int x,y;
         for(x=0;x<k;x++)
           for(y=0;y<2000;y++);
}

/************************************************************************/
void run()
{
        A_left_moto_go;
        A_right_moto_go;
        B_left_moto_go;
        B_right_moto_go;
}

void back_run()
{
        A_left_moto_back;
        A_right_moto_back;
        B_left_moto_back;
        B_right_moto_back;
}

void stop_run()
{
        A_left_moto_stop;
        A_right_moto_stop;
        B_left_moto_stop;
        B_right_moto_stop;
}

void left_run()
{
        A_left_moto_back;
        A_right_moto_go;
        B_left_moto_back;
        B_right_moto_go;
}

void right_run()
{
        A_left_moto_go;
        A_right_moto_back;
        B_left_moto_go;
        B_right_moto_back;
}



char uart_data; // 函數(shù)變量
char temp;

void main(void) //主函數(shù)
{       
        SCON=0X50; //設(shè)置串口方式為1 方式1
        TMOD=0X20;//設(shè)置為定時器1,
        TH1= 0x0FD; //賦初值
        TL1= 0x0FD;
        ES=1; //開串口中斷
        EA=1; //開中中斷
        TR1=1; //啟動定時器1
        led = 1; //p2.0置1,燈滅
        while(1);
}


void serial_IT(void) interrupt 4 //中斷程序
{
        if(RI==1) //R1置1.向cpu 申請中斷,循環(huán)
{
        RI=0; //軟件清零,取消申請
        uart_data = SBUF; //藍牙輸入數(shù)據(jù)賦值
        SBUF = uart_data; //返回終端
        temp = uart_data; //賦值
        if(temp == 0x00 ) led = ~led;
        switch(temp)
        {
                case 'a'         :        run();                 break;// 前進
                case 'b'        :         back_run();        break;//后退
                case 'c'        :        left_run();        break;//左轉(zhuǎn)
                case 'd'        :        right_run();break;//右轉(zhuǎn)
                case 'e'        :        stop_run();        break;//停止
        }
        }
        else T1 = 0; //中斷關(guān)閉

}
         


作者: 357337895    時間: 2018-1-15 19:45
有藍牙接收代碼嗎




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1