用stm32实现五路循迹模块的功能,控制小车循迹的代码
时间: 2023-06-16 12:05:36 浏览: 1479
循迹模块一般使用红外线传感器,可以检测黑线和白线的反差,从而实现对小车行驶方向的控制。以下是使用STM32实现五路循迹的代码示例:
```c
#include "stm32f10x.h"
#define Trig1 GPIO_Pin_0
#define Echo1 GPIO_Pin_1
#define Trig2 GPIO_Pin_2
#define Echo2 GPIO_Pin_3
#define Trig3 GPIO_Pin_4
#define Echo3 GPIO_Pin_5
#define Trig4 GPIO_Pin_6
#define Echo4 GPIO_Pin_7
#define Trig5 GPIO_Pin_8
#define Echo5 GPIO_Pin_9
void delay_us(u32 n)
{
u32 i;
while(n--)
{
i = 10;
while(i--);
}
}
void Ultrasonic_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = Trig1 | Trig2 | Trig3 | Trig4 | Trig5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Echo1 | Echo2 | Echo3 | Echo4 | Echo5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseInitStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2, ENABLE);
TIM_Cmd(TIM3, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Ultrasonic_Trigger(u16 GPIO_Pin)
{
GPIO_SetBits(GPIOA, GPIO_Pin);
delay_us(10);
GPIO_ResetBits(GPIOA, GPIO_Pin);
}
u16 Ultrasonic_GetDistance(u16 GPIO_Pin)
{
u16 distance = 0;
u32 counter = 0;
TIM3->CNT = 0;
Ultrasonic_Trigger(GPIO_Pin);
while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin) == RESET);
TIM_Cmd(TIM3, ENABLE);
while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin) == SET)
{
counter++;
if(counter > 60000) break;
}
TIM_Cmd(TIM3, DISABLE);
distance = (float)counter / 58.0;
return distance;
}
int main(void)
{
u16 distance[5];
Ultrasonic_Init();
while(1)
{
distance[0] = Ultrasonic_GetDistance(Echo1);
distance[1] = Ultrasonic_GetDistance(Echo2);
distance[2] = Ultrasonic_GetDistance(Echo3);
distance[3] = Ultrasonic_GetDistance(Echo4);
distance[4] = Ultrasonic_GetDistance(Echo5);
if(distance[0] > 10 && distance[0] < 30) //左前
{
//向右转
}
else if(distance[1] > 10 && distance[1] < 30) //左
{
//向右转
}
else if(distance[2] > 10 && distance[2] < 30) //中
{
//直行
}
else if(distance[3] > 10 && distance[3] < 30) //右
{
//向左转
}
else if(distance[4] > 10 && distance[4] < 30) //右前
{
//向左转
}
else //没有检测到黑线
{
//停止
}
}
}
```
以上代码中,使用了定时器TIM3作为超声波模块的计时器,通过捕获定时器的计数值来计算超声波的回波时间,并进而计算出距离。通过检测五路超声波模块返回的距离来判断小车的行驶方向,从而实现对小车的控制。
阅读全文