久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

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

QQ登錄

只需一步,快速開始

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

單片機(jī)51+藍(lán)牙+l298

[復(fù)制鏈接]
ID:252744 發(fā)表于 2017-11-23 17:02 | 顯示全部樓層 |閱讀模式
#include<reg52.h>

sbit PWM1 = P2^0;//右后輪電機(jī)驅(qū)動(dòng)使能
sbit PWM2 = P2^1;//左后輪電機(jī)驅(qū)動(dòng)使能
sbit PWM3 = P2^2;//右前輪電機(jī)驅(qū)動(dòng)使能
sbit PWM4 = P2^3;//左前輪電機(jī)驅(qū)動(dòng)使能

sbit motor_control_1 = P0^0;//右后輪后退
sbit motor_control_2 = P0^1;//右后輪前進(jìn)
sbit motor_control_3 = P0^2;//左后輪后退
sbit motor_control_4 = P0^3;//左后輪前進(jìn)
sbit motor_control_5 = P0^4;//右前輪后退
sbit motor_control_6 = P0^5;//右前輪前進(jìn)
sbit motor_control_7 = P0^6;//左前輪后退
sbit motor_control_8 = P0^7;//左前輪前進(jìn)
unsigned char ucBluetoothData = 230;//設(shè)置初始速度最大值為255最小為125

unsigned char ucLock = 0;//互斥量,俗稱原子鎖


unsigned int uiPWMCnt1 = 0;
unsigned int uiPWM1 = 230;
unsigned int uiPWMCnt2 = 0;
unsigned int uiPWM2 = 230;
unsigned int uiPWMCnt3 = 0;
unsigned int uiPWM3 = 230;
unsigned int uiPWMCnt4 = 0;
unsigned int uiPWM4 = 230;

unsigned char ucTempPWM;//設(shè)置中間變量



void initial_myself();
void initial_peripheral();
void T0_time();
void usart_service(void);
//void usart_send(unsigned char ucSendData);
void delay_long(unsigned int uiDelayLong);
void go_forward(void);//前進(jìn)
void fall_back(void);//后退
void turn_left(void);//左轉(zhuǎn)
void turn_right(void);//右轉(zhuǎn)
void stop();//剎車

void main()
{
        initial_myself();
        delay_long(100);
        initial_peripheral();
        while(1)
        {
                usart_service();

//                delay_long(500);


        }

}
//串口服務(wù)函數(shù)
void usart_service()
{

                switch(ucBluetoothData)
                {
                        case 0x04://前進(jìn)
                                ucBluetoothData = 0x02;//避免一直觸發(fā)
                                go_forward();
                                ucLock = 1;
                                uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;
                                ucLock = 0;
                                break;
                        case 0x05://左轉(zhuǎn)
                            ucBluetoothData = 0x02;//避免一直觸發(fā)
                                turn_left();
                                ucLock = 1;
                                uiPWM2 = uiPWM4= ucTempPWM / 4;
                                uiPWM3 =uiPWM1 = ucTempPWM;
                                ucLock = 0;
                                break;
                        case 0x06://右轉(zhuǎn)
                                ucBluetoothData = 0x02;//避免一直觸發(fā)
                                turn_right();
                                ucLock = 1;
                                uiPWM2 = uiPWM4 = ucTempPWM;
                                uiPWM3 =uiPWM1 = ucTempPWM / 4;
                                ucLock = 0;
                                break;
                        case 0x07://后退
                                ucBluetoothData = 0x02;//避免一直觸發(fā)
                                fall_back();
                                ucLock = 1;
                                uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;
                                ucLock = 0;
                                break;
                        case 0x01:
                                ucBluetoothData = 0x02;//避免一直觸發(fā)
                                stop();        
                                ucLock = 1;
                                uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;
                                ucLock = 0;
                                break;
                        case 0x00:
                                ucBluetoothData = 0x02;//避免一直觸發(fā)
                                stop();
                                ucLock = 1;
                                uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;
                                ucLock = 0;
                                break;
                        default :
                                break;

                }
                delay_long(100);
//                usart_send(ucBluetoothData);
                //速度調(diào)節(jié)的數(shù)據(jù)
                if(ucBluetoothData!=0x00&&ucBluetoothData!=0x01 && ucBluetoothData!=0x02 &&  ucBluetoothData!=0x04 && ucBluetoothData!=0x05 && ucBluetoothData!=0x06 && ucBluetoothData!=0x07)
                {
                        ucLock = 1;
                        ucTempPWM = uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucBluetoothData;
                        ucLock = 0;
                }

}
void T0_time() interrupt 1
{
        TF0 = 0;//清除中斷標(biāo)志
        TR0 = 0;//關(guān)定時(shí)器
        uiPWMCnt1 ++;
        uiPWMCnt2 ++;
        uiPWMCnt3 ++;
        uiPWMCnt4 ++;
        if(ucLock == 0)
        {
                if(uiPWMCnt1 > 255)
                {
                        uiPWMCnt1 = 0;
                }
                if(uiPWMCnt1 < uiPWM1)
                {
                        PWM1 = 1;

                }
                else
                {
                        PWM1 = 0;
                }

                if(uiPWMCnt2 > 255)
                {
                        uiPWMCnt2 = 0;
                }
                if(uiPWMCnt2 < uiPWM2)
                {
                        PWM2 = 1;

                }
                else
                {
                        PWM2 = 0;
                }

                if(uiPWMCnt3 > 255)
                {
                        uiPWMCnt3 = 0;
                }
              //  if(uiPWMCnt3 < uiPWM3)
              //  {
              //          PWM3 = 1;

              //  }
              //    else
              //    {
              //            PWM3 = 0;
              //   }

                if(uiPWMCnt4 > 255)
                {
                        uiPWMCnt4 = 0;
                }
               //  if(uiPWMCnt4 < uiPWM4)
               // {
               //         PWM4 = 1;

               // }
              // else
              // {
              //            PWM4 = 0;
              //  }


      }

        TH0 = 0xff;
        TL0 = 0x28;
        TR0 = 1;///開定時(shí)器

}

void initial_myself()
{
        TMOD = 0x01;//設(shè)置定時(shí)器0為工作方式1
        TH0 = 0xff;
        TL0 = 0x28;

        //配置串口
        SCON = 0x50;
        TMOD = 0x21;
        TH1 = TL1 = -(11095200L/12/32/9600);
        IP = 0x10;
        stop();
        PWM1 = 1;
        PWM2 = 1;        
                PWM3 = 1;
        PWM4 = 1;

}

void initial_peripheral()
{
        EA = 1;//開總中斷
        ES = 1;//允許串口中斷
        ET0 = 1;//允許定時(shí)器中斷
        TR0 = 1;//啟動(dòng)定時(shí)器
        TR1 = 1;//
}
void usart_receive(void) interrupt 4
{

        if(RI == 1)
        {
                RI = 0;
                ucBluetoothData = SBUF;
//                uiSendCnt = 0;
        }

        else
        {
                TI = 0;
        }

}
//void usart_send(unsigned char ucSendData)
//{
//        ES = 0;
//        TI = 0;
//        SBUF = ucSendData;
//        TI = 0;
//        ES = 1;
//
//}
void delay_long(unsigned int uiDelayLong)
{
        unsigned int i;
        unsigned int j;
        for(i = 0 ; i < uiDelayLong ; i++)
        {
                for(j = 0; j < 500; j++);

        }

}
void stop()//停止
{
        motor_control_1 = 0;
        motor_control_2 = 0;
        motor_control_3 = 0;
        motor_control_4 = 0;
        motor_control_5 = 0;
        motor_control_6 = 0;
        motor_control_7 = 0;
        motor_control_8 = 0;
}
void fall_back()
{
        motor_control_1 = 1;
        motor_control_2 = 0;
        motor_control_3 = 1;
        motor_control_4 = 0;
        motor_control_5 = 1;
        motor_control_6 = 0;
        motor_control_7 = 1;
        motor_control_8 = 0;
}
void go_forward()
{
        motor_control_1 = 0;
        motor_control_2 = 1;
        motor_control_3 = 0;
        motor_control_4 = 1;
        motor_control_5 = 0;
        motor_control_6 = 1;
        motor_control_7 = 0;
        motor_control_8 = 1;
}
void turn_left()//左轉(zhuǎn)
{
        motor_control_1 = 0;
        motor_control_2 = 1;
        motor_control_3 = 0;
        motor_control_4 = 1;
        motor_control_5 = 0;
        motor_control_6 = 1;
        motor_control_7 = 0;
        motor_control_8 = 1;

}
void turn_right()//右轉(zhuǎn)
{
        motor_control_1 = 0;
        motor_control_2 = 1;
        motor_control_3 = 0;
        motor_control_4 = 1;
        motor_control_5 = 0;
        motor_control_6 = 1;
        motor_control_7 = 0;
        motor_control_8 = 1;
}
回復(fù)

使用道具 舉報(bào)

ID:243748 發(fā)表于 2017-12-9 00:27 來自觸屏版 | 顯示全部樓層
歷害了樓主
回復(fù)

使用道具 舉報(bào)

ID:74784 發(fā)表于 2017-12-19 21:09 | 顯示全部樓層
上位機(jī)代碼在哪?
回復(fù)

使用道具 舉報(bào)

ID:265197 發(fā)表于 2017-12-22 10:43 來自觸屏版 | 顯示全部樓層
前來學(xué)習(xí)了
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 国产欧美日韩 | 国产精品成人一区二区 | 成人国产在线观看 | 日韩黄色大片 | 中国美女乱淫免费看视频 | 国产日韩精品在线 | 99热这里| 国产视频一区二区三区四区 | 蜜桃一区二区 | 操操av | 亚洲精品乱码久久久久 | 狠狠干夜夜操 | 欧美视频久久 | 亚洲综合另类 | 久久久综合视频 | 黄色a一级片 | 国产蜜臀av| 午夜精品在线 | 黄色片在线免费观看 | 天天视频黄 | 欧美精品在线播放 | 久久夜色精品国产欧美乱极品 | 一级黄色网 | 日本中文字幕在线播放 | 国产麻豆91 | 一级片黄色片 | 国产精品久久久久久久免费看 | 婷色 | 日韩一级大片 | 久久99精品久久久久久水蜜桃 | 日本黄色三级视频 | 久久动态图 | 天天曰天天操 | 91福利在线视频 | 五月天毛片 | 免费观看一区二区 | 午夜免费观看视频 | www.黄色av| 国产最新av | 免费黄色片视频 | 日韩 欧美|