这个是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();//实现小车的停止
- }
- }
复制代码
|