這個是stm32尋跡控制輪子程序但是我到現在都沒讀懂他到底定義是哪四個io口,PA6,PA7 PB8,PB9,OR,PC6,PC7,PC8,PC9,求大神指點
- #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;
- /**************************************
- 函數名 :Control_GPIO_Config
- 函數描述:配置Control小車用到的I/O口
- 輸入參數:無
- 輸出參數:無
- **************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIOA_InitStructure,GPIOB_InitStructure; //定義兩個GPIO_InitTypeDef類型的結構體
-
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC,ENABLE);//開啟GPIOA外設時鐘
- GPIOA_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; //選擇要控制的GPIOA引腳
- GPIOA_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //設置引腳模式為推挽式輸出
- GPIOA_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //設置引腳速度為50Mhz
- GPIO_Init(GPIOA,&GPIOA_InitStructure); //調用庫函數初始化 GPIOA
- //剛開始停止電機轉動
- GPIO_SetBits(GPIOC,GPIO_Pin_8|GPIO_Pin_9);
-
-
- //RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC,ENABLE); //開啟GPIOB的時鐘
-
- 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; //設置引腳速度為50MHz
-
- GPIO_Init(GPIOC,&GPIOB_InitStructure); //調用初始化GPIOB
- GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7); //啟動關閉電機
-
- }
- /*****************************************
- 函數名 : Control_GPIOE_Config
- 輸入描述 : 對傳感器輸入信號的管腳配置
- 輸入參數 :無
- 輸出參數 :無
- ******************************************/
- void Control_GPIOE_Config(void)
- {
- GPIO_InitTypeDef GPIOB_InitStructure; //定義GPIOE管腳結構體變量
-
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB,ENABLE);//開啟GPIOE外設時鐘
-
- 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);
- }
- /********************************************
- * 函數名 :TIM3_GPIO_Config
- * 描述 :配置TIM3復用輸出PWM時用到的I/O
- * 輸入 :無
- * 輸出 :無
- * 調用 :內部調用
- *********************************************/
- void TIM3_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //配置GPIOC的管腳
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //開啟TIM3時鐘
- /**配置pwm輸出的對應GPIO管腳****/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽式輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- /*****************************************************************************
- *文件名 : TIM_Configuration
- *描述 : TIM4 初始化
- * 輸入 : 無
- * 輸出 : 無
- * 返回 : 無
- ******************************************************************************/
- 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; //設定總周期值
- TIM_TimeBaseStructure.TIM_Prescaler = 0; //設置預分頻:不預分頻,即為36MHz
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //設置時鐘分頻系數:不分頻
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計數模式
-
- 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; //設置跳變值,當計數器計數到這個值時,電平發生跳變
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //當定時器計數值小于CCR_Val時為高電平
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR_Val_34; //設置通道3的電平跳變值,輸出另外一個占空比的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); //使能定時器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);
- //
-
- }
- /*************壁障前進***********/
- void Forward_run(void)
- {
- /*左側輪子停轉*/
- // 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)
- {
-
- /*左側輪子停轉*/
- // 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
-
- }
- /*************壁障左轉***********/
- void Left_turn(void)
- {
- /*左側輪子停轉*/
- // Pwm_Left = 900;
- // Pwm_Right = 900;
-
- TIM3_Configuration(700,700);
-
- // GPIO_ResetBits(GPIOB,GPIO_Pin_1);//0 //右電機轉動
- // GPIO_SetBits(GPIOB,GPIO_Pin_0);//1
-
- GPIO_SetBits(GPIOC,GPIO_Pin_8); //左電機停止
- GPIO_SetBits(GPIOC,GPIO_Pin_9);
-
- GPIO_ResetBits(GPIOC,GPIO_Pin_6);//0 //右電機轉動
- GPIO_SetBits(GPIOC,GPIO_Pin_7);//1
- }
- /*************壁障右轉***********/
- void Right_turn(void)
- {
- /***右側停轉*/
- // 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_SetBits(GPIOC,GPIO_Pin_6); //右電機停止
- GPIO_SetBits(GPIOC,GPIO_Pin_7);
-
- // GPIO_SetBits(GPIOB,GPIO_Pin_2);//1
- // GPIO_ResetBits(GPIOB,GPIO_Pin_4);//0
- }
- /*************停止**********/
- void Stop(void)
- {
- /*左側輪子停轉*/
- // 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);
- }
- /*************循跡前進***********/
- void Xunji_Forward_run(void)
- {
- /*左側輪子停轉*/
- // 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
-
- }
- /*************循跡左轉***********/
- void Xunji_Left_turn(void)
- {
- /*左側輪子停轉*/
- // Pwm_Left = 900;
- // Pwm_Right = 900;
-
- TIM3_Configuration(400,400);
-
- // GPIO_ResetBits(GPIOB,GPIO_Pin_1);//0 //右電機轉動
- // GPIO_SetBits(GPIOB,GPIO_Pin_0);//1
-
- GPIO_SetBits(GPIOC,GPIO_Pin_8); //左電機停止
- GPIO_SetBits(GPIOC,GPIO_Pin_9);
-
- GPIO_ResetBits(GPIOC,GPIO_Pin_6);//0 //右電機轉動
- GPIO_SetBits(GPIOC,GPIO_Pin_7);//1
- }
- /*************循跡右轉***********/
- void Xunji_Right_turn(void)
- {
- /***右側停轉*/
- // 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_SetBits(GPIOC,GPIO_Pin_6); //右電機停止
- GPIO_SetBits(GPIOC,GPIO_Pin_7);
-
- // GPIO_SetBits(GPIOB,GPIO_Pin_2);//1
- // GPIO_ResetBits(GPIOB,GPIO_Pin_4);//0
- }
- /*************循跡停止***********/
- void Xunji_Stop(void)
- {
- /*左側輪子停轉*/
- // 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) //方向函數
- {
- 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小車后退
- {
- Backward_run(); //后退
- Delay_ms(700);
- }
-
- if(S2>S4)
- {
- Right_turn(); //車的左邊比車的右邊距離小,右轉
- Delay_ms(700);
- }
- else
- {
- Left_turn(); //車的左邊比車的右邊距離大,左轉
- Delay_ms(700);
- }
- }
- /***************************************************
- * 函數名 : Auto_run
- * 描述 : 避障程序
- * 輸入 : 無
- * 輸出 : 無
- * 返回 : 無
- ****************************************************/
- void Auto_run(void) //檢測障礙
- {
- Average_Distance();
- if(averge_distance<30)
- {
- Stop();//小車停止
- COMM(); //方向函數
- }
-
- else
- {
- if(averge_distance>30)
- {
- Forward_run();
-
- }
- }
- }
- /***************************************************
- * 函數名 : 循跡程序
- * 輸入 : 無
- * 輸出 : 無
- * 返回 : 無
- ****************************************************/
- void Xunji_run(void) //檢測障礙
- {
- //四路尋跡傳感器有信號(白線)為0 沒有信號(黑線)為1
- if(Out1==0 && Out2==0&&Out3==0 &&Out4==0 )
- {
- Xunji_Forward_run();//實現小車的前進
- }
- else if(Out1==1 && Out2==0&&Out3==0 &&Out4==1 )
- {
- Xunji_Stop();//實現小車的停止
- }
- else if(Out1==0 && Out2==1&&Out3==1 &&Out4==0 )
- {
- Xunji_Forward_run();//實現小車的前進
- }
- else if(Out1==0 && Out2==1&&Out3==0 &&Out4==0 )
- {
- // TurnRight();//實現小車的左轉
- Xunji_Left_turn();
- }
- else if(Out1==1 && Out2==0&&Out3==0 &&Out4==0 )
- {
- // TurnRight();//實現小車的左轉
- Xunji_Left_turn();
- }
- else if(Out1==0 && Out2==0&&Out3==1 &&Out4==1 )
- {
- Xunji_Right_turn();//實現小車的前進
- }
- else if(Out1==0 && Out2==1&&Out3==1 &&Out4==1 )
- {
- // TurnRight();//實現小車的左轉
- Xunji_Left_turn();
- }
- else if(Out1==0 && Out2==0&&Out3==0 &&Out4==1 )
- {
- // TurnLeft();//實現小車的右轉
- Xunji_Right_turn();//實現小車的左轉
- }
- else if(Out1==0 && Out2==0&&Out3==1 &&Out4==0 )
- {
- // TurnLeft();//實現小車的右轉
- Xunji_Right_turn();//實現小車的左轉
-
- }
- else if(Out1==1 && Out2==1&&Out3==0 &&Out4==0 )
- {
- // RunToForward();//實現小車的前進
- Xunji_Left_turn();
- }
- else if(Out1==1 && Out2==1&&Out3==1 &&Out4==0 )
- {
- // TurnLeft();//實現小車的右轉
- Xunji_Right_turn();//實現小車的左轉
- }
- else
- {
- Xunji_Stop();//實現小車的停止
- }
- }
復制代碼
|