当前位置: 首页 > news >正文

5路红外循迹小车

所需模块:

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); #endif

Trcing.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); #endif

motor.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一定烧,如果链接电脑可能也会波及)

http://www.cnnetsun.cn/news/62271.html

相关文章:

  • 4、VXLAN BGP EVPN基础解析
  • 22、VXLAN BGP EVPN 多 Pod 与多 Fabric 部署方案解析
  • 2025年十大项目管理系统排名:综合功能、场景与用户口碑的权威榜单
  • Docker部署前端项目,收藏这篇就够了
  • Python+Vue的大学生就业信息管理系统 Pycharm django flask
  • 15 分钟获现场信号,30 分钟建指挥链路!分布式系统赋能救援 “秒响应”
  • 自动化测试中的常见陷阱与规避
  • 6-4 WPS JS宏 不重复随机取值应用
  • 《余行论》第七篇:历史篇
  • 《余行论》第九篇:证验篇
  • 构建动态响应式动画架构:lottie-ios与现代数据流技术融合实践
  • 起薪 15K+!网安领跑 2025IT 转行 6 大榜,政策红利 + 百万缺口,路径直接抄
  • 小程序商城搭建 自带拼团砍价功能 快速引爆销量
  • 海外网红营销:超越促销,用“圣诞故事”绑定品牌情感
  • Qwen3-32B双模式大模型:重构企业AI效率的范式革命
  • 9、深入探索AppStack:创建、分配、测试与管理全流程
  • 12、ThinApp与App Volumes结合应用及Horizon View集成指南
  • 16、在 Citrix XenApp 环境中部署 App Volumes
  • 国开(铸造学院开放大学)25秋《生产与运作管理》形考任务1【标准答案】
  • DETR 2025新突破:从农业到工业的实时检测革命
  • 深度学习优化参数深度解析:揭秘学习率调度的实战指南
  • 不写一行代码,把大模型变成安全编码专家 | LLaMA-Factory 微调实战
  • Vue2 - VDOM 和双端Diff算法
  • 40、Sendmail 配置与规则详解
  • 44、网络新闻:Usenet与C News深度剖析
  • 46、C News系统使用与维护全解析
  • OptiScaler游戏画质优化工具深度解析
  • 16、Yocto项目开发工具与流程详解
  • 25、深入解析Linux相关技术:从CGL到汽车级Linux
  • Nature同款 | 跟着顶刊学配色第 26 期