標(biāo)題: 跪求基于51單片機(jī)藍(lán)牙控制小車的源程序 [打印本頁]

作者: why1111    時(shí)間: 2017-3-28 19:38
標(biāo)題: 跪求基于51單片機(jī)藍(lán)牙控制小車的源程序
迫切的需要。!跪求!
作者: 雨林木木風(fēng)    時(shí)間: 2017-12-5 15:52
簡(jiǎn)單實(shí)現(xiàn)前后左右

#include<AT89x51.H>

//HL-1小車驅(qū)動(dòng)接線定義
#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                          //重定義無符號(hào)整數(shù)類型
#define uchar   unsigned char                         //重定義無符號(hào)字符類型

#define led P0_0

/************************************************************************/        
//延時(shí)函數(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è)置為定時(shí)器1,
        TH1= 0x0FD; //賦初值
        TL1= 0x0FD;
        ES=1; //開串口中斷
        EA=1; //開中中斷
        TR1=1; //啟動(dòng)定時(shí)器1
        led = 1; //p2.0置1,燈滅
        while(1);
}


void serial_IT(void) interrupt 4 //中斷程序
{
        if(RI==1) //R1置1.向cpu 申請(qǐng)中斷,循環(huán)
{
        RI=0; //軟件清零,取消申請(qǐng)
        uart_data = SBUF; //藍(lán)牙輸入數(shù)據(jù)賦值
        SBUF = uart_data; //返回終端
        temp = uart_data; //賦值
        if(temp == 0x00 ) led = ~led;
        switch(temp)
        {
                case 'a'         :        run();                 break;// 前進(jìn)
                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)閉






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