stm32两路超声波测速代码
时间: 2023-07-02 17:04:22 浏览: 116
以下是一个使用 STM32 的两路超声波测速的代码示例,其中使用了定时器和中断来实现:
```c
#include "stm32f10x.h"
#define TRIGGER1_PIN GPIO_Pin_0
#define TRIGGER1_PORT GPIOA
#define ECHO1_PIN GPIO_Pin_1
#define ECHO1_PORT GPIOA
#define TRIGGER2_PIN GPIO_Pin_2
#define TRIGGER2_PORT GPIOA
#define ECHO2_PIN GPIO_Pin_3
#define ECHO2_PORT GPIOA
#define SOUND_SPEED 340 // 声速,单位:m/s
volatile uint32_t time1_start = 0;
volatile uint32_t time1_end = 0;
volatile uint32_t time2_start = 0;
volatile uint32_t time2_end = 0;
void delay_us(uint32_t us)
{
uint32_t count = us * (SystemCoreClock / 1000000);
while(count--);
}
void EXTI0_IRQHandler()
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
time1_start = TIM2->CNT;
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void EXTI1_IRQHandler()
{
if(EXTI_GetITStatus(EXTI_Line1) != RESET)
{
time1_end = TIM2->CNT;
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
void EXTI2_IRQHandler()
{
if(EXTI_GetITStatus(EXTI_Line2) != RESET)
{
time2_start = TIM2->CNT;
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
void EXTI3_IRQHandler()
{
if(EXTI_GetITStatus(EXTI_Line3) != RESET)
{
time2_end = TIM2->CNT;
EXTI_ClearITPendingBit(EXTI_Line3);
}
}
int main()
{
GPIO_InitTypeDef gpio_init_struct;
EXTI_InitTypeDef exti_init_struct;
NVIC_InitTypeDef nvic_init_struct;
TIM_TimeBaseInitTypeDef tim_init_struct;
// 配置引脚
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
gpio_init_struct.GPIO_Mode = GPIO_Mode_Out_PP;
gpio_init_struct.GPIO_Pin = TRIGGER1_PIN | TRIGGER2_PIN;
gpio_init_struct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(TRIGGER1_PORT, &gpio_init_struct);
GPIO_Init(TRIGGER2_PORT, &gpio_init_struct);
gpio_init_struct.GPIO_Mode = GPIO_Mode_IPD;
gpio_init_struct.GPIO_Pin = ECHO1_PIN | ECHO2_PIN;
GPIO_Init(ECHO1_PORT, &gpio_init_struct);
GPIO_Init(ECHO2_PORT, &gpio_init_struct);
// 配置定时器2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
tim_init_struct.TIM_ClockDivision = TIM_CKD_DIV1;
tim_init_struct.TIM_CounterMode = TIM_CounterMode_Up;
tim_init_struct.TIM_Period = 0xFFFF;
tim_init_struct.TIM_Prescaler = 71;
TIM_TimeBaseInit(TIM2, &tim_init_struct);
TIM_Cmd(TIM2, ENABLE);
// 配置外部中断
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource3);
exti_init_struct.EXTI_Line = EXTI_Line0;
exti_init_struct.EXTI_LineCmd = ENABLE;
exti_init_struct.EXTI_Mode = EXTI_Mode_Interrupt;
exti_init_struct.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_Init(&exti_init_struct);
exti_init_struct.EXTI_Line = EXTI_Line1;
EXTI_Init(&exti_init_struct);
exti_init_struct.EXTI_Line = EXTI_Line2;
EXTI_Init(&exti_init_struct);
exti_init_struct.EXTI_Line = EXTI_Line3;
EXTI_Init(&exti_init_struct);
nvic_init_struct.NVIC_IRQChannel = EXTI0_IRQn;
nvic_init_struct.NVIC_IRQChannelCmd = ENABLE;
nvic_init_struct.NVIC_IRQChannelPreemptionPriority = 0;
nvic_init_struct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&nvic_init_struct);
nvic_init_struct.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_Init(&nvic_init_struct);
nvic_init_struct.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_Init(&nvic_init_struct);
nvic_init_struct.NVIC_IRQChannel = EXTI3_IRQn;
NVIC_Init(&nvic_init_struct);
// 循环测量距离
while(1)
{
// 触发超声波测距 1
GPIO_SetBits(TRIGGER1_PORT, TRIGGER1_PIN);
delay_us(10);
GPIO_ResetBits(TRIGGER1_PORT, TRIGGER1_PIN);
// 等待测距完成
while(GPIO_ReadInputDataBit(ECHO1_PORT, ECHO1_PIN) == RESET);
// 开始测距
while(GPIO_ReadInputDataBit(ECHO1_PORT, ECHO1_PIN) == SET);
while(GPIO_ReadInputDataBit(ECHO1_PORT, ECHO1_PIN) == RESET);
while(GPIO_ReadInputDataBit(ECHO1_PORT, ECHO1_PIN) == SET);
uint32_t time1 = time1_end - time1_start;
float distance1 = time1 * SOUND_SPEED / 2.0f / 1000000.0f; // 测距公式:distance = time * 声速 / 2
// 触发超声波测距 2
GPIO_SetBits(TRIGGER2_PORT, TRIGGER2_PIN);
delay_us(10);
GPIO_ResetBits(TRIGGER2_PORT, TRIGGER2_PIN);
// 等待测距完成
while(GPIO_ReadInputDataBit(ECHO2_PORT, ECHO2_PIN) == RESET);
// 开始测距
while(GPIO_ReadInputDataBit(ECHO2_PORT, ECHO2_PIN) == SET);
while(GPIO_ReadInputDataBit(ECHO2_PORT, ECHO2_PIN) == RESET);
while(GPIO_ReadInputDataBit(ECHO2_PORT, ECHO2_PIN) == SET);
uint32_t time2 = time2_end - time2_start;
float distance2 = time2 * SOUND_SPEED / 2.0f / 1000000.0f; // 测距公式:distance = time * 声速 / 2
// 打印结果
printf("Distance1: %.2f cm\r\n", distance1 * 100);
printf("Distance2: %.2f cm\r\n", distance2 * 100);
// 延时一段时间
delay_us(500000);
}
}
```
需要注意的是,这是一个简化的代码示例,实际应用中还需要根据具体情况进行优化和修改。
阅读全文