所需模块:
1.stm32f103c8t6核心板
2.TB6612电机驱动模块1个
3.5个红外循迹传感器
4.4个电机
通过左右电机产生差速来控制方向
main.c
#include "system.h" #include "stm32f10x.h" #include "SysTick.h" #include "motor.h" #include "Tracing.h" #include "pwm.h" int main() { PWM_Init(); motor_Init(); Infra_Module_Init(); u8 IR1,IR2,IR3,IR4,IR5; u8 time_start; while(1) { IR1=IR_1(); IR2=IR_2(); IR3=IR_3(); IR4=IR_4(); IR5=IR_5(); if(IR1==0&&IR2==0&&IR3==1&&IR4==0&&IR5==0) { Car_Up(); }else if(IR1==0&&IR2==1&&IR3==0&&IR4==1&&IR5==0){ Car_Up(); }else if(IR1==0&&IR2==1&&IR3==1&&IR4==1&&IR5==0){ Car_Up(); }else if(IR1==0&&IR2==0&&IR3==0&&IR4==1&&IR5==0){ Car_TurnRight_S(); }else if(IR1==0&&IR2==0&&IR3==1&&IR4==1&&IR5==0){ Car_TurnRight_S(); }else if(IR1==0&&IR2==0&&IR3==0&&IR4==0&&IR5==1){ Car_TurnRight_B(); }else if(IR1==0&&IR2==0&&IR3==0&&IR4==1&&IR5==1){ Car_TurnRight_B(); }else if(IR1==0&&IR2==0&&IR3==1&&IR4==0&&IR5==1){ Car_TurnRight_B(); }else if(IR1==0&&IR2==1&&IR3==0&&IR4==0&&IR5==0){ Car_TurnLeft_S(); }else if(IR1==1&&IR2==1&&IR3==0&&IR4==0&&IR5==0){ Car_TurnLeft_S(); }else if(IR1==1&&IR2==0&&IR3==0&&IR4==0&&IR5==0){ Car_TurnLeft_B(); }else if(IR1==1&&IR2==1&&IR3==0&&IR4==0&&IR5==0){ Car_TurnLeft_B(); }else if(IR1==1&&IR2==0&&IR3==1&&IR4==0&&IR5==0){ Car_TurnLeft_B(); }else if(IR1==1&&IR2==1&&IR3==1&&IR4==1&&IR5==1){ Car_Stop(); } } }pwm.c
pwm.h
////////此处使用PA2 PA7作为pwm信号输出口////////////////////// #include "pwm.h" #include "system.h" void PWM_Init(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); TIM_InternalClockConfig(TIM3); TIM_InternalClockConfig(TIM2); TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 100 - 1; //ARR TIM_TimeBaseInitStructure.TIM_Prescaler = 36 - 1; //PSC TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure); TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure); TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCStructInit(&TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; //CCR TIM_OC3Init(TIM2, &TIM_OCInitStructure); TIM_OC2Init(TIM3, &TIM_OCInitStructure); TIM_Cmd(TIM3, ENABLE); TIM_Cmd(TIM2, ENABLE); } void PWM12_SetCompare1(uint16_t Compare) { TIM_SetCompare3(TIM2, Compare); } // void PWM12_SetCompare2(uint16_t Compare) { TIM_SetCompare2(TIM3, Compare); } ////////////////////////////////////pwm.c #ifndef _pwm_H #define _pwm_H #include "system.h" void PWM_Init(void); void PWM12_SetCompare1(uint16_t Compare); void PWM12_SetCompare2(uint16_t Compare); #endifTrcing.c
Trcing.h
这边使用的是PB3-7作为红外循迹输入
#include "Tracing.h" #include "system.h" #include "motor.h" #include "SysTick.h" //红外模块初始化 void Infra_Module_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(INFRAs_PORT_RCC,ENABLE); ///关闭关闭 JTAG RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);// GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(INFRAs_PORT,&GPIO_InitStructure); } u8 IR_1(void) { u8 n; n=GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3); return n; } u8 IR_2(void) { u8 n; n=GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4); return n; } u8 IR_3(void) { u8 n; n=GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5); return n; } u8 IR_4(void) { u8 n; n=GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6); return n; } u8 IR_5(void) { u8 n; n=GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7); return n; } Trcing.h部分 //////////////////////////////////////////////////////////////////////// #ifndef __Tracing_H #define __Tracing_H #include "system.h" //红外模块管脚定义 #define INFRAs_PORT GPIOB #define INFRAs_PIN GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 #define INFRAs_PORT_RCC RCC_APB2Periph_GPIOB void Infra_Module_Init(void); u8 IR_1(void); u8 IR_2(void); u8 IR_3(void); u8 IR_4(void); u8 IR_5(void); #endifmotor.c
motor.h
#include "motor.h" #include "system.h" #include "pwm.h" void motor_Init(void) { GPIO_InitTypeDef lee; lee.GPIO_Mode=GPIO_Mode_Out_PP; lee.GPIO_Speed=GPIO_Speed_50MHz; lee.GPIO_Pin=motor_RB1_PIN; RCC_APB2PeriphClockCmd(motor_RB1_RCC,ENABLE); GPIO_Init(motor_RB1_PORT,&lee); lee.GPIO_Pin=motor_RB2_PIN; RCC_APB2PeriphClockCmd(motor_RB2_RCC,ENABLE); GPIO_Init(motor_RB2_PORT,&lee); lee.GPIO_Pin=motor_LA1_PIN; RCC_APB2PeriphClockCmd(motor_LA1_RCC,ENABLE); GPIO_Init(motor_LA1_PORT,&lee); lee.GPIO_Pin=motor_LA2_PIN; RCC_APB2PeriphClockCmd(motor_LA2_RCC,ENABLE); GPIO_Init(motor_LA2_PORT,&lee); } /******************************Motor电机模块************************** 电机初始化设置 以及电机PWM设置 *********************************************************************/ //设置右路电机速度 PWM void MotorR_SetSpeed(int8_t Speed) { if (Speed >= 0)//Speed值为正 { GPIO_SetBits(GPIOA, GPIO_Pin_5);//电机正转 GPIO_ResetBits(GPIOA, GPIO_Pin_6); PWM12_SetCompare1(Speed);//设置Speed转速 } else//Speed值为负 { GPIO_ResetBits(GPIOA, GPIO_Pin_5);//电机反转 GPIO_SetBits(GPIOA, GPIO_Pin_6); PWM12_SetCompare1(-Speed);//设为-Speed转速 } } //设置左路电机PWM 速度 void MotorL_SetSpeed(int8_t Speed) { if (Speed >= 0) { GPIO_SetBits(GPIOA, GPIO_Pin_3); GPIO_ResetBits(GPIOA, GPIO_Pin_4); PWM12_SetCompare2(Speed); } else { GPIO_ResetBits(GPIOA, GPIO_Pin_3); GPIO_SetBits(GPIOA, GPIO_Pin_4); PWM12_SetCompare2(-Speed); } } ////////运动状态 void Car_Stop(void)//小车停止 { MotorR_SetSpeed(0); MotorL_SetSpeed(0); } void Car_Up(void)//小车前进 { MotorR_SetSpeed(45); MotorL_SetSpeed(45); } void Car_Down(void)//小车后退 { MotorR_SetSpeed(-20); MotorL_SetSpeed(-20); } void Car_TurnRight_S(void)//小车右转 { MotorR_SetSpeed(-10); MotorL_SetSpeed(35);////大 } void Car_TurnRight_B(void)//小车右转 { MotorR_SetSpeed(-10); MotorL_SetSpeed(45);//大 } void Car_TurnLeft_S(void)//小车左转 { MotorR_SetSpeed(35);//大 MotorL_SetSpeed(-15); } void Car_TurnLeft_B(void)//小车左转 { MotorR_SetSpeed(45);//大 MotorL_SetSpeed(-10); } void Car_Spin(void)//小车旋转 { MotorR_SetSpeed(-40); MotorL_SetSpeed(40); } /////////////////////////////////////////////////////////////////////////////// #ifndef _motor_H #define _motor_H #include "system.h" //右边电机**************接BO口 #define motor_RB2_PORT GPIOA #define motor_RB2_PIN GPIO_Pin_6 #define motor_RB2_RCC RCC_APB2Periph_GPIOA #define motor_RB1_PORT GPIOA #define motor_RB1_PIN GPIO_Pin_5 #define motor_RB1_RCC RCC_APB2Periph_GPIOA ///左边电机**************接AO口 #define motor_LA2_PORT GPIOA #define motor_LA2_PIN GPIO_Pin_3 #define motor_LA2_RCC RCC_APB2Periph_GPIOA #define motor_LA1_PORT GPIOA #define motor_LA1_PIN GPIO_Pin_4 #define motor_LA1_RCC RCC_APB2Periph_GPIOA void motor_Init(void); void MotorR_SetSpeed(int8_t Speed); void MotorL_SetSpeed(int8_t Speed); void Car_Stop(void);//小车停止 void Car_Up(void);//小车前进 void Car_Down(void);//小车后退 void Car_TurnRight_S(void);//小车右转 void Car_TurnRight_B(void); void Car_TurnLeft_S(void);//小车左转 void Car_TurnLeft_B(void); void Car_Spin(void);//小车旋转 #endif电机1和4并联,2和3并联
!!!!!!!!!!!
注意!:vm,和vcc触点很近,使用万用表测量触点电压时不能接触两个点,12v与3v3串通,电压过高会烧坏链接在一起的所有芯片(c8t6和tb6612一定烧,如果链接电脑可能也会波及)