stm32小车红外避障Keil5全部代码
时间: 2023-08-19 14:06:01 浏览: 111
以下是基于STM32的小车红外避障代码,包括所有的头文件和初始化函数,使用Keil5编写:
头文件:
```
#ifndef __INFRARED_H
#define __INFRARED_H
#include "stm32f10x.h"
#define LEFT_INFRARED 1
#define RIGHT_INFRARED 2
void Infrared_InitConfig(void);
int Infrared_GetDistance(int infrared);
#endif /* __INFRARED_H */
```
初始化函数:
```
#include "infrared.h"
void Infrared_InitConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); // 使能GPIO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // 使能TIM3时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // PA6、PA7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_10 | GPIO_Pin_11; // PB0、PB1、PB10、PB11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4; // PC0~PC4
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入
GPIO_Init(GPIOC, &GPIO_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 60000 - 1; // 计数器周期为60000
TIM_TimeBaseStructure.TIM_Prescaler = 71; // 分频系数为72
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 时钟分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; // PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // 使能输出
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // 输出极性为高电平
TIM_OCInitStructure.TIM_Pulse = 0; // 初始占空比为0
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); // 输出比较1预装载使能
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); // 输出比较2预装载使能
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); // 输出比较3预装载使能
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); // 输出比较4预装载使能
TIM_Cmd(TIM3, ENABLE); // TIM3使能
}
```
红外测距函数:
```
#include "infrared.h"
int Infrared_GetDistance(int infrared)
{
int distance = 0;
switch(infrared)
{
case LEFT_INFRARED:
if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0) == 0)
{
distance = 10;
}
else if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1) == 0)
{
distance = 20;
}
else if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_2) == 0)
{
distance = 30;
}
else if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3) == 0)
{
distance = 40;
}
else if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4) == 0)
{
distance = 50;
}
else
{
distance = 100;
}
break;
case RIGHT_INFRARED:
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == 0)
{
distance = 10;
}
else if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) == 0)
{
distance = 20;
}
else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 0)
{
distance = 30;
}
else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
{
distance = 40;
}
else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_10) == 0)
{
distance = 50;
}
else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0)
{
distance = 60;
}
else
{
distance = 100;
}
break;
default:
break;
}
return distance;
}
```
主函数:
```
#include "stm32f10x.h"
#include "delay.h"
#include "usart.h"
#include "infrared.h"
#include "motor.h"
int main(void)
{
int left_distance, right_distance;
SystemInit();
delay_init();
USART_InitConfig();
Infrared_InitConfig();
Motor_InitConfig();
while(1)
{
left_distance = Infrared_GetDistance(LEFT_INFRARED);
right_distance = Infrared_GetDistance(RIGHT_INFRARED);
if(left_distance < 100 && right_distance < 100) // 左右两侧均有障碍物,后退并转向
{
Motor_SetSpeed(-800, -800);
delay_ms(500);
Motor_SetSpeed(800, -800);
delay_ms(1000);
}
else if(left_distance < 100) // 左侧有障碍物,右转
{
Motor_SetSpeed(800, -800);
}
else if(right_distance < 100) // 右侧有障碍物,左转
{
Motor_SetSpeed(-800, 800);
}
else // 左右两侧均无障碍物,向前行驶
{
Motor_SetSpeed(800, 800);
}
delay_ms(100);
}
}
```
需要注意的是,该代码中的头文件、初始化函数和主函数需要在不同的文件中进行编写,且各部分的初始化函数需要根据具体硬件电路进行修改。
阅读全文