蓝桥杯电子类超声波程序
时间: 2023-10-07 11:04:49 浏览: 73
超声波测距一般需要用到超声波模块和单片机,以下是一个基于STM32的超声波测距程序,可以参考:
```c
#include "stm32f10x.h"
#define TRIG_PIN GPIO_Pin_0
#define TRIG_PORT GPIOB
#define ECHO_PIN GPIO_Pin_1
#define ECHO_PORT GPIOB
// 定义计数器的最大值和最小值
#define MAX_CNT 50000
#define MIN_CNT 3
// 定义超声波模块的工作频率
#define FREQ 40
// 定义超声波测距的时间参数
#define TIME1 10
#define TIME2 5800
// 定义超声波测距的公式
#define DISTANCE (340 * TIME1 * TIME2 / (2 * FREQ * 1000000))
uint16_t time_cnt;
void delay_us(uint16_t us) {
TIM2->ARR = us; // 设置计数器的自动重载值
TIM2->CNT = 0; // 清零计数器的计数值
TIM2->SR &= ~TIM_SR_UIF; // 清除定时器的溢出标志
TIM2->CR1 |= TIM_CR1_CEN; // 使能计数器
while ((TIM2->SR & TIM_SR_UIF) == 0); // 等待计数器溢出
TIM2->CR1 &= ~TIM_CR1_CEN; // 关闭计数器
}
void TIM3_IRQHandler() {
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET) {
if (GPIO_ReadInputDataBit(ECHO_PORT, ECHO_PIN)) {
// 如果IO口为高电平,计数器开始计数
TIM_SetCounter(TIM2, 0);
} else {
// 如果IO口为低电平,计数器停止计数
time_cnt = TIM_GetCounter(TIM2);
}
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
}
}
int main() {
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // 使能GPIOB的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // 使能TIM3的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // 使能TIM2的时钟
// 配置PB0和PB1的GPIO口
GPIO_InitStructure.GPIO_Pin = TRIG_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(TRIG_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ECHO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ECHO_PORT, &GPIO_InitStructure);
// 配置TIM3的时间基准
TIM_TimeBaseInitStructure.TIM_Period = 65535;
TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
// 配置TIM3的输入捕获通道
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 = 0x0F;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
// 配置TIM2的时间基准
TIM_TimeBaseInitStructure.TIM_Period = MAX_CNT;
TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
// 配置TIM3的中断
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);
while (1) {
GPIO_SetBits(TRIG_PORT, TRIG_PIN); // 发送超声波信号
delay_us(10);
GPIO_ResetBits(TRIG_PORT, TRIG_PIN);
TIM_SetCounter(TIM2, 0); // 清零计数器的计数值
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); // 清除TIM3的中断标志
TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE); // 使能TIM3的中断
while (TIM_GetFlagStatus(TIM3, TIM_FLAG_CC1) == RESET); // 等待输入捕获中断
TIM_ITConfig(TIM3, TIM_IT_CC1, DISABLE); // 关闭TIM3的中断
if (time_cnt < MIN_CNT || time_cnt > MAX_CNT) {
// 如果计数值不在范围内,说明距离太远或太近
continue;
}
printf("Distance: %d cm\n", DISTANCE);
delay_us(500000); // 延时500ms
}
}
```
需要注意的是,这是一个简单的程序,仅供参考。实际应用中,需要根据具体的超声波模块和单片机进行调整。
阅读全文