基於STM32F103C8T6的超聲波測距示例
整個工程檔案:
一、所需材料:
1、麵包板:
https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-17409464480.12.942e3961LSzalG&id=566952992749
2、STM32F103C8T6核心板
https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-17409464480.12.69693961YaTulR&id=567200541464
3、超聲波模組
https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-17409464480.12.306c3961DPKmG1&id=567318723493
4、串列埠工具
https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-17409464480.21.7dd33961iRVAkv&id=567201073239
5、STlink 下載程式,或用串列埠下載亦可
二、超聲波原理分析
網上一大堆,以後待補
備註:HC-SR04在距離3cm內無法準確測量,近處3CM外測量誤差0.7cm左右。
三、硬體連線
實際效果圖:
四、原始碼分析
#include "delay.h"
#include "sys.h"
#include "usart.h"
/***************************************************
大海電子專營各種電子模組,歡迎光臨
硬體連線
串列埠1:RX PA10 TX PA9
超聲波:Trig PB11 ECHG PB10
備註:以下程式碼非原創,只是經過修改適配成C8T6
若侵權,請告知。
***************************************************/
//超聲波硬體介面定義
#define HCSR04_PORT GPIOB
#define HCSR04_CLK RCC_APB2Periph_GPIOB
#define HCSR04_TRIG GPIO_Pin_11
#define HCSR04_ECHO GPIO_Pin_10
//超聲波計數
u16 msHcCount = 0;
//定時器4設定
void hcsr04_NVIC()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//IO口初始化 及其他初始化
void Hcsr04Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(HCSR04_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin =HCSR04_TRIG;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(HCSR04_PORT, &GPIO_InitStructure);
GPIO_ResetBits(HCSR04_PORT,HCSR04_TRIG);
GPIO_InitStructure.GPIO_Pin = HCSR04_ECHO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(HCSR04_PORT, &GPIO_InitStructure);
GPIO_ResetBits(HCSR04_PORT,HCSR04_ECHO);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = (1000-1);
TIM_TimeBaseStructure.TIM_Prescaler =(72-1);
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
hcsr04_NVIC();
TIM_Cmd(TIM4,DISABLE);
}
//開啟定時器4
static void OpenTimerForHc()
{
TIM_SetCounter(TIM4,0);
msHcCount = 0;
TIM_Cmd(TIM4, ENABLE);
}
//關閉定時器4
static void CloseTimerForHc()
{
TIM_Cmd(TIM4, DISABLE);
}
//定時器4終中斷
void TIM4_IRQHandler(void)
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update );
msHcCount++;
}
}
//獲取定時器4計數器值
u32 GetEchoTimer(void)
{
u32 t = 0;
t = msHcCount*1000;
t += TIM_GetCounter(TIM4);
TIM4->CNT = 0;
delay_ms(50);
return t;
}
//通過定時器4計數器值推算距離
float Hcsr04GetLength(void )
{
u32 t = 0;
int i = 0;
float lengthTemp = 0;
float sum = 0;
while(i!=5)
{
TRIG_Send = 1;
delay_us(20);
TRIG_Send = 0;
while(ECHO_Reci == 0);
OpenTimerForHc();
i = i + 1;
while(ECHO_Reci == 1);
CloseTimerForHc();
t = GetEchoTimer();
lengthTemp = ((float)t/58.0);//cm
sum = lengthTemp + sum ;
}
lengthTemp = sum/5.0;
return lengthTemp;
}
//測試主函式
int main(void)
{
float length;
delay_init(); //延時函式初始化
NVIC_Configuration(); //設定NVIC中斷分組2:2位搶佔優先順序,2位響應優先順序
uart_init(9600); //串列埠初始化為9600
Hcsr04Init();
printf("大海電子串列埠測試\r\n");
Hcsr04Init();
printf("超聲波初始化成功!\n");
while(1)
{
length = Hcsr04GetLength();
printf("距離為:%.3fcm\n",length);
delay_ms(1000);
}
}