高中生
最后登录1970-1-1
在线时间 小时
注册时间2017-2-28
|
分享一个stm32使用HC-SR04超声波模块测距的代码
bsp_sonar.h
#ifndef __BSP_SONAR_H
#define __BSP_SONAR_H
#include "stm32f10x.h"
#define GENERAL_TIM5 TIM5
#define GENERAL_TIM5_CLK RCC_APB1Periph_TIM5
#define GENERAL_TIM_PERIOD 0XFFFF
#define GENERAL_TIM5_CH1_GPIO_CLK RCC_APB2Periph_GPIOA
#define GENERAL_TIM5_CH1_PORT GPIOA
#define GENERAL_TIM5_CH1_PIN GPIO_Pin_0
#define GENERAL_TIM_CHANNEL_x TIM_Channel_1
#define GENERAL_TIM_IT_CCx TIM_IT_CC1
#define GENERAL_TIM5_IRQ TIM5_IRQn
#define GENERAL_TIM5_INT_FUN TIM5_IRQHandler
#define GENERAL_TIM_GetCapturex_FUN TIM_GetCapture1
#define GENERAL_TIM_OCxPolarityConfig_FUN TIM_OC1PolarityConfig
#define GENERAL_TIM_STRAT_ICPolarity TIM_ICPolarity_Rising
#define GENERAL_TIM_END_ICPolarity TIM_ICPolarity_Falling
typedef struct
{
uint8_t Capture_FinishFlag;
uint8_t Capture_StartFlag;
uint16_t Capture_CcrValue;
uint16_t Capture_Period;
}TIM_ICUserValueTypeDef;
extern TIM_ICUserValueTypeDef TIM_ICUserValueStructure;
void GENERAL_TIM5_Init(void);
void TRIG_GPIO_Config(void);
void UltrasonicWave_StartMeasure(void);
#endif
bsp_sonar.c
#include "bsp_Sonar.h"
#include "bsp_GeneralTim.h"
void TRIG_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void UltrasonicWave_StartMeasure(void)
{
GPIO_SetBits(GPIOB,GPIO_Pin_5);
Delay(20);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
}
TIM_ICUserValueTypeDef TIM_ICUserValueStructure = {0,0,0,0};
static void GENERAL_TIM5_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure5;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure5.NVIC_IRQChannel = GENERAL_TIM5_IRQ ;
NVIC_InitStructure5.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure5.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure5.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure5);
}
static void GENERAL_TIM5_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure5;
RCC_APB2PeriphClockCmd(GENERAL_TIM5_CH1_GPIO_CLK, ENABLE);
GPIO_InitStructure5.GPIO_Pin = GENERAL_TIM5_CH1_PIN;
GPIO_InitStructure5.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GENERAL_TIM5_CH1_PORT, &GPIO_InitStructure5);
}
static void GENERAL_TIM5_Mode_Config(void)
{
GENERAL_TIM_APBxClock_FUN(GENERAL_TIM5_CLK,ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure5;
TIM_TimeBaseStructure5.TIM_Period=GENERAL_TIM_PERIOD;
TIM_TimeBaseStructure5.TIM_Prescaler= GENERAL_TIM_PSC;
TIM_TimeBaseStructure5.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure5.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure5.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(GENERAL_TIM5, &TIM_TimeBaseStructure5);
TIM_ICInitTypeDef TIM_ICInitStructure5;
TIM_ICInitStructure5.TIM_Channel = GENERAL_TIM_CHANNEL_x;
TIM_ICInitStructure5.TIM_ICPolarity = GENERAL_TIM_STRAT_ICPolarity;
TIM_ICInitStructure5.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure5.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure5.TIM_ICFilter = 0;
TIM_ICInit(GENERAL_TIM5, &TIM_ICInitStructure5);
TIM_ClearFlag(GENERAL_TIM5, TIM_FLAG_Update|GENERAL_TIM_IT_CCx);
TIM_ITConfig (GENERAL_TIM5, TIM_IT_Update | GENERAL_TIM_IT_CCx, ENABLE );
TIM_Cmd(GENERAL_TIM5, ENABLE);
}
void GENERAL_TIM5_Init(void)
{
GENERAL_TIM5_GPIO_Config();
GENERAL_TIM5_NVIC_Config();
GENERAL_TIM5_Mode_Config();
}
main.c
int main(void)
{
uint32_t time;
uint32_t TIM_PscCLK = 72000000 / (GENERAL_TIM_PSC+1);
GENERAL_TIM5_Init();
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
UltrasonicWave_StartMeasure();
USART_Config();
while(1)
{
if(TIM_ICUserValueStructure.Capture_FinishFlag == 1)
{
time = TIM_ICUserValueStructure.Capture_Period * (GENERAL_TIM_PERIOD+1) +
(TIM_ICUserValueStructure.Capture_CcrValue+1);
TIM_ICUserValueStructure.Capture_FinishFlag = 0;
}
}
中断函数
void GENERAL_TIM5_INT_FUN(void)
{
if ( TIM_GetITStatus ( GENERAL_TIM5, TIM_IT_Update) != RESET )
{
TIM_ICUserValueStructure.Capture_Period ++;
TIM_ClearITPendingBit ( GENERAL_TIM5, TIM_FLAG_Update );
}
if ( TIM_GetITStatus (GENERAL_TIM5, GENERAL_TIM_IT_CCx ) != RESET)
{
if ( TIM_ICUserValueStructure.Capture_StartFlag == 0 )
{
TIM_SetCounter ( GENERAL_TIM5, 0 );
TIM_ICUserValueStructure.Capture_Period = 0;
TIM_ICUserValueStructure.Capture_CcrValue = 0;
GENERAL_TIM_OCxPolarityConfig_FUN(GENERAL_TIM5, TIM_ICPolarity_Falling);
TIM_ICUserValueStructure.Capture_StartFlag = 1;
}
else
{
TIM_ICUserValueStructure.Capture_CcrValue =
GENERAL_TIM_GetCapturex_FUN (GENERAL_TIM5);
GENERAL_TIM_OCxPolarityConfig_FUN(GENERAL_TIM5, TIM_ICPolarity_Rising);
TIM_ICUserValueStructure.Capture_StartFlag = 0;
TIM_ICUserValueStructure.Capture_FinishFlag = 1;
}
TIM_ClearITPendingBit (GENERAL_TIM5,GENERAL_TIM_IT_CCx);
}
}
但是程序执行起来存在一定的问题,测得的高电平时间明显不准,而且随着距离的变化高电平时间基本不发生变化。
有兴趣的同学可以帮忙看一下
|
|