代碼附上來了
前燈 PB12
右燈pb13
zuo pb14
蜂鳴器 PB15
- /*
- 硬件鏈接:
- 左電機控制端口:PA3、PA4
- 右電機控制端口:PA1、PA2
- */
- #include "stm32f10x.h"//聲明頭文件
- #include "stm32f10x_exti.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_usart.h"
- #define uchar unsigned char//宏定義
- #define uint unsigned int//宏定義
- #define AA 35//左輪速度調節值
- #define BB 35//右輪速度調節值
- uchar a=0;//定義一個變量a,用來讀取串口的數據
- /*有參數延時函數*/
- void delay(__IO uint32_t nCount)
- {
- for (; nCount != 0; nCount--);
- }
- unsigned int temp=0;
- unsigned char rebuf[5];
- unsigned int start=0;
- void GPIO_DengConfiguration()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* 初始化LED的GPIO管腳,配置為推挽輸出 */ //設置端口
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; //車燈GPIO_Pin12,13,14端口 15蜂鳴器端口
- //設置對端口的模式
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //設置為 推挽(Push-Pull)輸出
- //
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //最大輸出速度為50MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- //前進子程序
- void go1()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //左邊輪子正轉
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子正轉
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //關閉右邊輪子
- delay(500000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
-
- //前進子程序
- void go2()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //左邊輪子正轉
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子正轉
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //關閉右邊輪子
- delay(400000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //前進子程序
- void go3()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //左邊輪子正轉
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子正轉
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //關閉右邊輪子
- delay(300000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //前進子程序
- void go4()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //左邊輪子正轉
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子正轉
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //關閉右邊輪子
- delay(200000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //前進子程序
- void go5()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //左邊輪子正轉
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子正轉
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //關閉右邊輪子
- delay(100000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //前進子程序
- void go6()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //左邊輪子正轉
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子正轉
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //關閉右邊輪子
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //小車后退的子程序
- void hui1()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_3);
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_3); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_1);
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_1); //關閉右邊輪子
- delay(500000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //小車后退的子程序
- void hui2()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_3);
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_3); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_1);
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_1); //關閉右邊輪子
- delay(400000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
-
- //小車后退的子程序
- void hui3()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_3);
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_3); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_1);
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_1); //關閉右邊輪子
- delay(300000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
-
- //小車后退的子程序
- void hui4()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_3);
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_3); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_1);
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_1); //關閉右邊輪子
- delay(200000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
-
- //小車后退的子程序
- void hui5()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_3);
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_3); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_1);
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_1); //關閉右邊輪子
- delay(100000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
-
- //小車后退的子程序
- void hui6()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_3);
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_3); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_1);
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_1); //關閉右邊輪子
-
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //小車左轉彎子程序
- void turnleft()
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_13);//轉向燈打開
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //左邊輪子正轉
-
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子停止
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子停止
- delay(250000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //小車右轉彎子程序
- void turnright()
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_14);//轉向燈打開
-
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子停止
-
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_ResetBits(GPIOA,GPIO_Pin_2); //右邊輪子正轉
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子停止
- delay(250000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //停止子程序
- void down()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2);
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左右輪全部停止
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- int main()//主程序
- {
- while(1)//死循環
- {
- if(a==0x14||a==0x15||a==0x54||a==0x55||a==0x64||a==0x65)//如果是就前進
- go1();
- if(a==0x16||a==0x17||a==0x56||a==0x57||a==0x66||a==0x67)//如果是就前進
- go2();
- if(a==0x18||a==0x19||a==0x58||a==0x59||a==0x68||a==0x69)//如果是就前進
- go3();
- if(a==0x1a||a==0x1b||a==0x5a||a==0x5b||a==0x6a||a==0x6b)//如果是就前進
- go4();
- if(a==0x1c||a==0x1d||a==0x5c||a==0x5d||a==0x6c||a==0x6d)//如果是就前進
- go5();
- if(a==0x1e||a==0x1f||a==0x5e||a==0x5f||a==0x6e||a==0x6f)//如果是就前進
- go6();
- if(a==0x24||a==0x25||a==0x74||a==0x75||a==0x84||a==0x85)//如果是就后退
- hui1();
- if(a==0x26||a==0x27||a==0x76||a==0x77||a==0x86||a==0x87)//如果是就后退
- hui2();
- if(a==0x28||a==0x29||a==0x78||a==0x79||a==0x88||a==0x89)//如果是就后退
- hui3();
- if(a==0x2a||a==0x2b||a==0x7a||a==0x7b||a==0x8a||a==0x8b)//如果是就后退
- hui4();
- if(a==0x2c||a==0x2d||a==0x7c||a==0x7d||a==0x8c||a==0x8d)//如果是就后退
- hui5();
- if(a==0x2e||a==0x2f||a==0x7e||a==0x7d||a==0x8e||a==0x8f)//如果是就后退
- hui6();
- if(a==0x3f)//如果是0x3f就左轉
- turnleft();
- if(a==0x4f)//如果是0x4f就右轉
- turnright();
- if(a==0x00)//如果是0x00就停止
- down();
- if(a==0xa0)//如果是0xa0車燈打開
- GPIO_ResetBits(GPIOB,GPIO_Pin_12); //打開轉向燈
- if(a==0xb0)//如果是0xb0車燈關閉
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- if(a==0xc0)//如果是0xc0蜂鳴器鳴響
- GPIO_ResetBits(GPIOB,GPIO_Pin_15); //打開蜂鳴器
- if(a==0xd0)//如果是0xd0蜂鳴器關
- GPIO_SetBits(GPIOB,GPIO_Pin_15); //關閉蜂鳴器
- else
- down();
- }
- }
- void (unsigned char DataToSend) //串口中斷服務程序
- {
- // USART_SendData(USART1, (unsigned char) DataToSend);
- // a= ;//讀取單片機串口接受的藍牙模塊發送的數據
- a = DataToSend;
- }
- /*
- 硬件鏈接:
- 左電機控制端口:PA3、PA4
- 右電機控制端口:PA1、PA2
- */
- #include "stm32f10x.h"//聲明頭文件
- #include "stm32f10x_exti.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_usart.h"
- #define uchar unsigned char//宏定義
- #define uint unsigned int//宏定義
- #define AA 35//左輪速度調節值
- #define BB 35//右輪速度調節值
- uchar a=0;//定義一個變量a,用來讀取串口的數據
- /*有參數延時函數*/
- void delay(__IO uint32_t nCount)
- {
- for (; nCount != 0; nCount--);
- }
- unsigned int temp=0;
- unsigned char rebuf[5];
- unsigned int start=0;
- void GPIO_DengConfiguration()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* 初始化LED的GPIO管腳,配置為推挽輸出 */ //設置端口
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; //車燈GPIO_Pin12,13,14端口 15蜂鳴器端口
- //設置對端口的模式
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //設置為 推挽(Push-Pull)輸出
- //
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //最大輸出速度為50MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- //前進子程序
- void go1()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //左邊輪子正轉
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子正轉
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //關閉右邊輪子
- delay(500000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
-
- //前進子程序
- void go2()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //左邊輪子正轉
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子正轉
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //關閉右邊輪子
- delay(400000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //前進子程序
- void go3()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //左邊輪子正轉
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子正轉
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //關閉右邊輪子
- delay(300000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //前進子程序
- void go4()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //左邊輪子正轉
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子正轉
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //關閉右邊輪子
- delay(200000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //前進子程序
- void go5()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //左邊輪子正轉
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子正轉
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //關閉右邊輪子
- delay(100000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //前進子程序
- void go6()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //左邊輪子正轉
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子正轉
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //關閉右邊輪子
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //小車后退的子程序
- void hui1()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_3);
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_3); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_1);
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_1); //關閉右邊輪子
- delay(500000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //小車后退的子程序
- void hui2()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_3);
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_3); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_1);
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_1); //關閉右邊輪子
- delay(400000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
-
- //小車后退的子程序
- void hui3()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_3);
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_3); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_1);
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_1); //關閉右邊輪子
- delay(300000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
-
- //小車后退的子程序
- void hui4()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_3);
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_3); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_1);
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_1); //關閉右邊輪子
- delay(200000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
-
- //小車后退的子程序
- void hui5()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_3);
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_3); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_1);
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_1); //關閉右邊輪子
- delay(100000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
-
- //小車后退的子程序
- void hui6()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_3);
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_3); //關閉左邊輪子
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子倒轉
- GPIO_ResetBits(GPIOA,GPIO_Pin_1);
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_1); //關閉右邊輪子
-
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //小車左轉彎子程序
- void turnleft()
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_13);//轉向燈打開
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //左邊輪子正轉
-
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子停止
- delay(AA);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子停止
- delay(250000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //小車右轉彎子程序
- void turnright()
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_14);//轉向燈打開
-
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左邊輪子停止
-
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_ResetBits(GPIOA,GPIO_Pin_2); //右邊輪子正轉
- delay(BB);
- GPIO_SetBits(GPIOA,GPIO_Pin_2); //右邊輪子停止
- delay(250000);
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- //停止子程序
- void down()
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- GPIO_SetBits(GPIOA,GPIO_Pin_2);
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_SetBits(GPIOA,GPIO_Pin_4); //左右輪全部停止
- GPIO_SetBits(GPIOB,GPIO_Pin_13);
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- }
- int main()//主程序
- {
- while(1)//死循環
- {
- if(a==0x14||a==0x15||a==0x54||a==0x55||a==0x64||a==0x65)//如果是就前進
- go1();
- if(a==0x16||a==0x17||a==0x56||a==0x57||a==0x66||a==0x67)//如果是就前進
- go2();
- if(a==0x18||a==0x19||a==0x58||a==0x59||a==0x68||a==0x69)//如果是就前進
- go3();
- if(a==0x1a||a==0x1b||a==0x5a||a==0x5b||a==0x6a||a==0x6b)//如果是就前進
- go4();
- if(a==0x1c||a==0x1d||a==0x5c||a==0x5d||a==0x6c||a==0x6d)//如果是就前進
- go5();
- if(a==0x1e||a==0x1f||a==0x5e||a==0x5f||a==0x6e||a==0x6f)//如果是就前進
- go6();
- if(a==0x24||a==0x25||a==0x74||a==0x75||a==0x84||a==0x85)//如果是就后退
- hui1();
- if(a==0x26||a==0x27||a==0x76||a==0x77||a==0x86||a==0x87)//如果是就后退
- hui2();
- if(a==0x28||a==0x29||a==0x78||a==0x79||a==0x88||a==0x89)//如果是就后退
- hui3();
- if(a==0x2a||a==0x2b||a==0x7a||a==0x7b||a==0x8a||a==0x8b)//如果是就后退
- hui4();
- if(a==0x2c||a==0x2d||a==0x7c||a==0x7d||a==0x8c||a==0x8d)//如果是就后退
- hui5();
- if(a==0x2e||a==0x2f||a==0x7e||a==0x7d||a==0x8e||a==0x8f)//如果是就后退
- hui6();
- if(a==0x3f)//如果是0x3f就左轉
- turnleft();
- if(a==0x4f)//如果是0x4f就右轉
- turnright();
- if(a==0x00)//如果是0x00就停止
- down();
- if(a==0xa0)//如果是0xa0車燈打開
- GPIO_ResetBits(GPIOB,GPIO_Pin_12); //打開轉向燈
- if(a==0xb0)//如果是0xb0車燈關閉
- GPIO_SetBits(GPIOB,GPIO_Pin_14); //關閉轉向燈
- if(a==0xc0)//如果是0xc0蜂鳴器鳴響
- GPIO_ResetBits(GPIOB,GPIO_Pin_15); //打開蜂鳴器
- if(a==0xd0)//如果是0xd0蜂鳴器關
- GPIO_SetBits(GPIOB,GPIO_Pin_15); //關閉蜂鳴器
- else
- down();
- }
- }
- void (unsigned char DataToSend) //串口中斷服務程序
- {
- // USART_SendData(USART1, (unsigned char) DataToSend);
- // a= ;//讀取單片機串口接受的藍牙模塊發送的數據
- a = DataToSend;
- }
復制代碼
|