超声波传感器stm32例程
时间: 2023-09-21 19:06:11 浏览: 38
以下是超声波传感器(HC-SR04)在STM32上的例程:
```c
#include "stm32f10x.h"
#include <stdio.h>
#define TRIG_PIN GPIO_Pin_8
#define ECHO_PIN GPIO_Pin_9
#define TRIG_PORT GPIOB
#define ECHO_PORT GPIOB
__IO uint32_t TimingDelay;
void Delay(__IO uint32_t nTime);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void TIM_Configuration(void);
void USART_Configuration(void);
void SendString(char *s);
float GetDistance(void);
int main(void)
{
char buffer[16];
float distance;
GPIO_Configuration();
NVIC_Configuration();
TIM_Configuration();
USART_Configuration();
while (1)
{
distance = GetDistance();
sprintf(buffer, "%.2f cm\r\n", distance);
SendString(buffer);
Delay(1000);
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
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_IN_FLOATING;
GPIO_Init(ECHO_PORT, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
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 = 0x0;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_Cmd(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_CC4, ENABLE);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void SendString(char *s)
{
while (*s)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
USART_SendData(USART1, *s++);
}
}
float GetDistance(void)
{
GPIO_SetBits(TRIG_PORT, TRIG_PIN);
Delay(10);
GPIO_ResetBits(TRIG_PORT, TRIG_PIN);
while (GPIO_ReadInputDataBit(ECHO_PORT, ECHO_PIN) == RESET)
;
TIM_SetCounter(TIM2, 0);
while (GPIO_ReadInputDataBit(ECHO_PORT, ECHO_PIN) == SET)
;
return TIM_GetCapture4(TIM2) * 0.017;
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
}
}
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while (TimingDelay != 0)
;
}
void SysTick_Handler(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
```
其中,`GPIO_Configuration()`函数用于配置超声波传感器的引脚,`NVIC_Configuration()`函数用于配置TIM2中断,`TIM_Configuration()`函数用于配置TIM2计数器和输入捕获,`USART_Configuration()`函数用于配置USART1通信,`SendString()`函数用于向串口发送字符串,`GetDistance()`函数用于获取超声波传感器测得的距离。`Delay()`函数用于实现延时。
在主函数中,使用`while (1)`循环不断获取距离并通过串口发送。