stm32循迹小车详细制作代码
时间: 2023-10-16 17:08:50 浏览: 912
循迹小车是一种基于传感器技术实现的自动导航小车,可以通过识别地面上的黑线或白线进行自动行驶。下面是一个基于STM32微控制器的循迹小车详细制作代码。
硬件材料:
1. STM32F103C8T6微控制器板
2. L298N电机驱动板
3. 红外传感器模块
4. 4WD小车底盘
5. 18650锂电池及电池盒
6. 面包板、杜邦线等
软件准备:
1. Keil uVision 5集成开发环境
2. ST-Link V2下载器
3. ST-LINK Utility下载工具
4. STM32F103C8T6的库文件
代码实现:
1. 定义引脚和变量:
```
#define Left_forward PBout(12)
#define Left_backward PBout(13)
#define Right_forward PBout(14)
#define Right_backward PBout(15)
#define IR1 PAin(0)//定义红外传感器引脚
#define IR2 PAin(1)
#define IR3 PAin(2)
#define IR4 PAin(3)
#define IR5 PAin(4)
int left_speed,right_speed;
```
2. 初始化引脚:
```
void GPIO_Configuration()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void PWM_Configuration()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 2000-1;
TIM_TimeBaseStructure.TIM_Prescaler = 72-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_Cmd(TIM1, ENABLE);
}
void Motor_Configuration(void)
{
GPIO_Configuration();
PWM_Configuration();
}
```
3. 电机控制:
```
void Motor_Control(int left_speed,int right_speed)
{
if (left_speed > 0)
{
Left_forward = 1;
Left_backward = 0;
TIM_SetCompare1(TIM1, left_speed);
}
else if (left_speed < 0)
{
Left_forward = 0;
Left_backward = 1;
TIM_SetCompare1(TIM1, -left_speed);
}
else
{
Left_forward = 0;
Left_backward = 0;
TIM_SetCompare1(TIM1, 0);
}
if (right_speed > 0)
{
Right_forward = 1;
Right_backward = 0;
TIM_SetCompare2(TIM1, right_speed);
}
else if (right_speed < 0)
{
Right_forward = 0;
Right_backward = 1;
TIM_SetCompare2(TIM1, -right_speed);
}
else
{
Right_forward = 0;
Right_backward = 0;
TIM_SetCompare2(TIM1, 0);
}
}
```
4. 循迹控制:
```
void Tracking_Control(void)
{
if (IR1 == 0 && IR2 == 0 && IR3 == 1 && IR4 == 0 && IR5 == 0)//匀速直行
{
left_speed = 1000;
right_speed = 1000;
Motor_Control(left_speed,right_speed);
}
else if (IR1 == 0 && IR2 == 1 && IR3 == 1 && IR4 == 0 && IR5 == 0)//左偏
{
left_speed = 200;
right_speed = 800;
Motor_Control(left_speed,right_speed);
}
else if (IR1 == 0 && IR2 == 0 && IR3 == 1 && IR4 == 1 && IR5 == 0)//右偏
{
left_speed = 800;
right_speed = 200;
Motor_Control(left_speed,right_speed);
}
else if (IR1 == 0 && IR2 == 1 && IR3 == 0 && IR4 == 0 && IR5 == 0)//左转弯
{
left_speed = -200;
right_speed = 800;
Motor_Control(left_speed,right_speed);
}
else if (IR1 == 0 && IR2 == 0 && IR3 == 0 && IR4 == 1 && IR5 == 0)//右转弯
{
left_speed = 800;
right_speed = -200;
Motor_Control(left_speed,right_speed);
}
else if (IR1 == 1 && IR2 == 1 && IR3 == 0 && IR4 == 1 && IR5 == 1)//十字路口
{
left_speed = 0;
right_speed = 0;
Motor_Control(left_speed,right_speed);
}
}
```
5. 主函数:
```
int main(void)
{
SystemInit();
Motor_Configuration();
while (1)
{
Tracking_Control();
}
}
```
这是一个简单的循迹小车代码,你可以根据自己的实际需求进行改进和优化。
阅读全文