|
這個(gè)是stm32尋跡控制輪子程序但是我到現(xiàn)在都沒(méi)讀懂他到底定義是哪四個(gè)io口,PA6,PA7 PB8,PB9,OR,PC6,PC7,PC8,PC9,求大神指點(diǎn)
- #include "motor_control.h"
- #include "SysTick.h"
- #include "UltrasonicWave.h"
- #include "pwm_output.h"
- #include "usart1.h"
- #define ONWARD 1
- #define BACKWARD 0
- extern vu8 count;
- extern vu32 averge_distance;
- vu8 left,right,middle;
- vu32 S2;
- vu32 S4;
- vu32 S1;
- vu32 timer;
- //u16 Pwm_Right;
- //u16 Pwm_Left;
- /**************************************
- 函數(shù)名 :Control_GPIO_Config
- 函數(shù)描述:配置Control小車(chē)用到的I/O口
- 輸入?yún)?shù):無(wú)
- 輸出參數(shù):無(wú)
- **************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIOA_InitStructure,GPIOB_InitStructure; //定義兩個(gè)GPIO_InitTypeDef類(lèi)型的結(jié)構(gòu)體
-
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC,ENABLE);//開(kāi)啟GPIOA外設(shè)時(shí)鐘
- GPIOA_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; //選擇要控制的GPIOA引腳
- GPIOA_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //設(shè)置引腳模式為推挽式輸出
- GPIOA_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //設(shè)置引腳速度為50Mhz
- GPIO_Init(GPIOA,&GPIOA_InitStructure); //調(diào)用庫(kù)函數(shù)初始化 GPIOA
- //剛開(kāi)始停止電機(jī)轉(zhuǎn)動(dòng)
- GPIO_SetBits(GPIOC,GPIO_Pin_8|GPIO_Pin_9);
-
-
- //RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC,ENABLE); //開(kāi)啟GPIOB的時(shí)鐘
-
- GPIOB_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //選擇要控制的GPIOB引腳
- GPIOB_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //配置引腳模式為推挽式輸出
- GPIOB_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //設(shè)置引腳速度為50MHz
-
- GPIO_Init(GPIOC,&GPIOB_InitStructure); //調(diào)用初始化GPIOB
- GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7); //啟動(dòng)關(guān)閉電機(jī)
-
- }
- /*****************************************
- 函數(shù)名 : Control_GPIOE_Config
- 輸入描述 : 對(duì)傳感器輸入信號(hào)的管腳配置
- 輸入?yún)?shù) :無(wú)
- 輸出參數(shù) :無(wú)
- ******************************************/
- void Control_GPIOE_Config(void)
- {
- GPIO_InitTypeDef GPIOB_InitStructure; //定義GPIOE管腳結(jié)構(gòu)體變量
-
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB,ENABLE);//開(kāi)啟GPIOE外設(shè)時(shí)鐘
-
- GPIOB_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_3|GPIO_Pin_4;
- GPIOB_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //輸入采用浮空輸入
-
- GPIO_Init(GPIOB,&GPIOB_InitStructure);
- }
- /********************************************
- * 函數(shù)名 :TIM3_GPIO_Config
- * 描述 :配置TIM3復(fù)用輸出PWM時(shí)用到的I/O
- * 輸入 :無(wú)
- * 輸出 :無(wú)
- * 調(diào)用 :內(nèi)部調(diào)用
- *********************************************/
- void TIM3_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //配置GPIOC的管腳
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //開(kāi)啟TIM3時(shí)鐘
- /**配置pwm輸出的對(duì)應(yīng)GPIO管腳****/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽式輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- /*****************************************************************************
- *文件名 : TIM_Configuration
- *描述 : TIM4 初始化
- * 輸入 : 無(wú)
- * 輸出 : 無(wú)
- * 返回 : 無(wú)
- ******************************************************************************/
- void TIM3_Configuration(u16 CCR_Val_12,u16 CCR_Val_34)
- {
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
- //CCR_Val_12 = 900;
- //CCR_Val_34 = 900;
- /* -----------------------------------------------------------------------
- TIM3 Configuration: generate 2 PWM signals with 2 different duty cycles:
- TIM3CLK = 36 MHz, Prescaler = 0x0, TIM3 counter clock = 36 MHz
- TIM3 ARR Register = 999 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
- TIM3 Frequency = 36 KHz.
- TIM3 Channel1 duty cycle = (CCR_Val_12/ TIM3_ARR)* 100 = 50%
- TIM3 Channel1 duty cycle = (CCR_Val_34/ TIM3_ARR)* 100 = 50%
- ----------------------------------------------------------------------- */
- /***通道1的配置*****/
- TIM_TimeBaseStructure.TIM_Period = 999; //設(shè)定總周期值
- TIM_TimeBaseStructure.TIM_Prescaler = 0; //設(shè)置預(yù)分頻:不預(yù)分頻,即為36MHz
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //設(shè)置時(shí)鐘分頻系數(shù):不分頻
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計(jì)數(shù)模式
-
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置為PWM模式1
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR_Val_12; //設(shè)置跳變值,當(dāng)計(jì)數(shù)器計(jì)數(shù)到這個(gè)值時(shí),電平發(fā)生跳變
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //當(dāng)定時(shí)器計(jì)數(shù)值小于CCR_Val時(shí)為高電平
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR_Val_34; //設(shè)置通道3的電平跳變值,輸出另外一個(gè)占空比的PWM
-
- TIM_OC1Init(TIM3, &TIM_OCInitStructure); //使能通道1
- TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
-
- TIM_OC2Init(TIM3, &TIM_OCInitStructure); //使能通道2
- TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
- /***通道3的配置*****/
-
- TIM_ARRPreloadConfig(TIM3, ENABLE); // 使能TIM3重載寄存器ARR
- TIM_Cmd(TIM3, ENABLE); //使能定時(shí)器3
-
- // TIM_OC3Init(TIM3, &TIM_OCInitStructure); //使能通道3
- // TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
- // TIM_OC4Init(TIM3, &TIM_OCInitStructure); //使能通道4
- // TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
- //
-
- }
- /*************壁障前進(jìn)***********/
- void Forward_run(void)
- {
- /*左側(cè)輪子停轉(zhuǎn)*/
- // Pwm_Left = 900;
- // Pwm_Right = 900;
-
- TIM3_Configuration(700,700);
-
-
- GPIO_ResetBits(GPIOC,GPIO_Pin_8);//0
- GPIO_SetBits(GPIOC,GPIO_Pin_9);//1
-
- GPIO_ResetBits(GPIOC,GPIO_Pin_6);//0
- GPIO_SetBits(GPIOC,GPIO_Pin_7);//1
-
- }
- /*************壁障后退***********/
- void Backward_run(void)
- {
-
- /*左側(cè)輪子停轉(zhuǎn)*/
- // Pwm_Left = 900;
- // Pwm_Right = 900;
-
- TIM3_Configuration(700,700);
-
- GPIO_SetBits(GPIOC,GPIO_Pin_8);//1
- GPIO_ResetBits(GPIOC,GPIO_Pin_9);//0
-
- GPIO_SetBits(GPIOC,GPIO_Pin_6);//1
- GPIO_ResetBits(GPIOC,GPIO_Pin_7);//0
-
- }
- /*************壁障左轉(zhuǎn)***********/
- void Left_turn(void)
- {
- /*左側(cè)輪子停轉(zhuǎn)*/
- // Pwm_Left = 900;
- // Pwm_Right = 900;
-
- TIM3_Configuration(700,700);
-
- // GPIO_ResetBits(GPIOB,GPIO_Pin_1);//0 //右電機(jī)轉(zhuǎn)動(dòng)
- // GPIO_SetBits(GPIOB,GPIO_Pin_0);//1
-
- GPIO_SetBits(GPIOC,GPIO_Pin_8); //左電機(jī)停止
- GPIO_SetBits(GPIOC,GPIO_Pin_9);
-
- GPIO_ResetBits(GPIOC,GPIO_Pin_6);//0 //右電機(jī)轉(zhuǎn)動(dòng)
- GPIO_SetBits(GPIOC,GPIO_Pin_7);//1
- }
- /*************壁障右轉(zhuǎn)***********/
- void Right_turn(void)
- {
- /***右側(cè)停轉(zhuǎn)*/
- // Pwm_Left = 900;
- // Pwm_Right = 900;
-
- TIM3_Configuration(700,700);
-
- GPIO_ResetBits(GPIOC,GPIO_Pin_8);//0 //左電機(jī)轉(zhuǎn)動(dòng)
- GPIO_SetBits(GPIOC,GPIO_Pin_9);//1
-
- GPIO_SetBits(GPIOC,GPIO_Pin_6); //右電機(jī)停止
- GPIO_SetBits(GPIOC,GPIO_Pin_7);
-
- // GPIO_SetBits(GPIOB,GPIO_Pin_2);//1
- // GPIO_ResetBits(GPIOB,GPIO_Pin_4);//0
- }
- /*************停止**********/
- void Stop(void)
- {
- /*左側(cè)輪子停轉(zhuǎn)*/
- // Pwm_Left = 0;
- // Pwm_Right = 0;
-
- TIM3_Configuration(0,0);
-
-
- GPIO_SetBits(GPIOC,GPIO_Pin_8);
- GPIO_SetBits(GPIOC,GPIO_Pin_9);
-
- GPIO_SetBits(GPIOC,GPIO_Pin_6);
- GPIO_SetBits(GPIOC,GPIO_Pin_7);
- }
- /*************循跡前進(jìn)***********/
- void Xunji_Forward_run(void)
- {
- /*左側(cè)輪子停轉(zhuǎn)*/
- // Pwm_Left = 900;
- // Pwm_Right = 900;
-
- TIM3_Configuration(400,400);
-
-
- GPIO_ResetBits(GPIOC,GPIO_Pin_8);//0
- GPIO_SetBits(GPIOC,GPIO_Pin_9);//1
-
-
- GPIO_ResetBits(GPIOC,GPIO_Pin_6);//0
- GPIO_SetBits(GPIOC,GPIO_Pin_7);//1
-
- }
- /*************循跡左轉(zhuǎn)***********/
- void Xunji_Left_turn(void)
- {
- /*左側(cè)輪子停轉(zhuǎn)*/
- // Pwm_Left = 900;
- // Pwm_Right = 900;
-
- TIM3_Configuration(400,400);
-
- // GPIO_ResetBits(GPIOB,GPIO_Pin_1);//0 //右電機(jī)轉(zhuǎn)動(dòng)
- // GPIO_SetBits(GPIOB,GPIO_Pin_0);//1
-
- GPIO_SetBits(GPIOC,GPIO_Pin_8); //左電機(jī)停止
- GPIO_SetBits(GPIOC,GPIO_Pin_9);
-
- GPIO_ResetBits(GPIOC,GPIO_Pin_6);//0 //右電機(jī)轉(zhuǎn)動(dòng)
- GPIO_SetBits(GPIOC,GPIO_Pin_7);//1
- }
- /*************循跡右轉(zhuǎn)***********/
- void Xunji_Right_turn(void)
- {
- /***右側(cè)停轉(zhuǎn)*/
- // Pwm_Left = 900;
- // Pwm_Right = 900;
-
- TIM3_Configuration(400,400);
-
- GPIO_ResetBits(GPIOC,GPIO_Pin_8);//0 //左電機(jī)轉(zhuǎn)動(dòng)
- GPIO_SetBits(GPIOC,GPIO_Pin_9);//1
-
- GPIO_SetBits(GPIOC,GPIO_Pin_6); //右電機(jī)停止
- GPIO_SetBits(GPIOC,GPIO_Pin_7);
-
- // GPIO_SetBits(GPIOB,GPIO_Pin_2);//1
- // GPIO_ResetBits(GPIOB,GPIO_Pin_4);//0
- }
- /*************循跡停止***********/
- void Xunji_Stop(void)
- {
- /*左側(cè)輪子停轉(zhuǎn)*/
- // Pwm_Left = 0;
- // Pwm_Right = 0;
-
- TIM3_Configuration(400,400);
-
-
- GPIO_SetBits(GPIOC,GPIO_Pin_8);
- GPIO_SetBits(GPIOC,GPIO_Pin_9);
-
- GPIO_SetBits(GPIOC,GPIO_Pin_6);
- GPIO_SetBits(GPIOC,GPIO_Pin_7);
- }
- void COMM(void) //方向函數(shù)
- {
- Degree(20,1);
- // TIM_SetCompare1(TIM2,11);
- Delay_ms(700);
- Average_Distance();
- S2=averge_distance;
-
- Degree(150,1);
- // TIM_SetCompare1(TIM2,4);
- Delay_ms(700);
- Average_Distance();
- S4=averge_distance;
-
- Degree(85,1);
- // TIM_SetCompare1(TIM2,8);
- Delay_ms(700);
- S1=averge_distance;
-
- if(S2<30||S4<30) //只要左右各有距離小于20CM小車(chē)后退
- {
- Backward_run(); //后退
- Delay_ms(700);
- }
-
- if(S2>S4)
- {
- Right_turn(); //車(chē)的左邊比車(chē)的右邊距離小,右轉(zhuǎn)
- Delay_ms(700);
- }
- else
- {
- Left_turn(); //車(chē)的左邊比車(chē)的右邊距離大,左轉(zhuǎn)
- Delay_ms(700);
- }
- }
- /***************************************************
- * 函數(shù)名 : Auto_run
- * 描述 : 避障程序
- * 輸入 : 無(wú)
- * 輸出 : 無(wú)
- * 返回 : 無(wú)
- ****************************************************/
- void Auto_run(void) //檢測(cè)障礙
- {
- Average_Distance();
- if(averge_distance<30)
- {
- Stop();//小車(chē)停止
- COMM(); //方向函數(shù)
- }
-
- else
- {
- if(averge_distance>30)
- {
- Forward_run();
-
- }
- }
- }
- /***************************************************
- * 函數(shù)名 : 循跡程序
- * 輸入 : 無(wú)
- * 輸出 : 無(wú)
- * 返回 : 無(wú)
- ****************************************************/
- void Xunji_run(void) //檢測(cè)障礙
- {
- //四路尋跡傳感器有信號(hào)(白線)為0 沒(méi)有信號(hào)(黑線)為1
- if(Out1==0 && Out2==0&&Out3==0 &&Out4==0 )
- {
- Xunji_Forward_run();//實(shí)現(xiàn)小車(chē)的前進(jìn)
- }
- else if(Out1==1 && Out2==0&&Out3==0 &&Out4==1 )
- {
- Xunji_Stop();//實(shí)現(xiàn)小車(chē)的停止
- }
- else if(Out1==0 && Out2==1&&Out3==1 &&Out4==0 )
- {
- Xunji_Forward_run();//實(shí)現(xiàn)小車(chē)的前進(jìn)
- }
- else if(Out1==0 && Out2==1&&Out3==0 &&Out4==0 )
- {
- // TurnRight();//實(shí)現(xiàn)小車(chē)的左轉(zhuǎn)
- Xunji_Left_turn();
- }
- else if(Out1==1 && Out2==0&&Out3==0 &&Out4==0 )
- {
- // TurnRight();//實(shí)現(xiàn)小車(chē)的左轉(zhuǎn)
- Xunji_Left_turn();
- }
- else if(Out1==0 && Out2==0&&Out3==1 &&Out4==1 )
- {
- Xunji_Right_turn();//實(shí)現(xiàn)小車(chē)的前進(jìn)
- }
- else if(Out1==0 && Out2==1&&Out3==1 &&Out4==1 )
- {
- // TurnRight();//實(shí)現(xiàn)小車(chē)的左轉(zhuǎn)
- Xunji_Left_turn();
- }
- else if(Out1==0 && Out2==0&&Out3==0 &&Out4==1 )
- {
- // TurnLeft();//實(shí)現(xiàn)小車(chē)的右轉(zhuǎn)
- Xunji_Right_turn();//實(shí)現(xiàn)小車(chē)的左轉(zhuǎn)
- }
- else if(Out1==0 && Out2==0&&Out3==1 &&Out4==0 )
- {
- // TurnLeft();//實(shí)現(xiàn)小車(chē)的右轉(zhuǎn)
- Xunji_Right_turn();//實(shí)現(xiàn)小車(chē)的左轉(zhuǎn)
-
- }
- else if(Out1==1 && Out2==1&&Out3==0 &&Out4==0 )
- {
- // RunToForward();//實(shí)現(xiàn)小車(chē)的前進(jìn)
- Xunji_Left_turn();
- }
- else if(Out1==1 && Out2==1&&Out3==1 &&Out4==0 )
- {
- // TurnLeft();//實(shí)現(xiàn)小車(chē)的右轉(zhuǎn)
- Xunji_Right_turn();//實(shí)現(xiàn)小車(chē)的左轉(zhuǎn)
- }
- else
- {
- Xunji_Stop();//實(shí)現(xiàn)小車(chē)的停止
- }
- }
復(fù)制代碼
|
|