標(biāo)題:
stm32f4單片機(jī)超聲波模塊測(cè)距程序
[打印本頁(yè)]
作者:
高大尚
時(shí)間:
2019-3-18 20:46
標(biāo)題:
stm32f4單片機(jī)超聲波模塊測(cè)距程序
注意定時(shí)器管腳與模塊Ech腳相連,由于時(shí)間緊迫,沒(méi)試過(guò)不使用定時(shí)器對(duì)應(yīng)的腳與模塊Ech腳相連,會(huì)不會(huì)達(dá)到同樣的現(xiàn)象。
單片機(jī)源程序如下:
#include "Ultrasonicwave.h"
#include "sys.h"
//#define HCSR04_PORT GPIOA
//#define HCSR04_CLK RCC_APB2Periph_GPIOA
//#define HCSR04_TRIG GPIO_Pin_5
//#define HCSR04_ECHO GPIO_Pin_1
#define TRIG_Send PAout(5)
#define ECHO_Reci PBin(6)
u16 msHcCount = 0;
void Hcsr04Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通輸出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//50MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_5;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_TIM4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_6);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_Period = (1000-1);
TIM_TimeBaseStructure.TIM_Prescaler =(84-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);
}
//
static void OpenTimerForHc()
{
TIM_SetCounter(TIM4,0);
msHcCount = 0;
TIM_Cmd(TIM4, ENABLE);
}
static void CloseTimerForHc()
{
TIM_Cmd(TIM4, DISABLE);
}
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);
}
void TIM4_IRQHandler(void)
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update );
msHcCount++;
}
}
u32 GetEchoTimer(void)
{
u32 t = 0;
t = msHcCount*1000;
t += TIM_GetCounter(TIM4);
TIM4->CNT = 0;
delay_ms(50);
return t;
}
float Hcsr04GetLength(void )
{
u32 t = 0;
int i = 0;
float lengthTemp = 0;
float sum = 0;
while(i!=5)//循環(huán)5次消除余震
{
TRIG_Send = 1;
delay_us(20);
TRIG_Send = 0;
while(ECHO_Reci == 0);
OpenTimerForHc();
i = i + 1;
while(ECHO_Reci == 1);
CloseTimerForHc(); //關(guān)閉定時(shí)器
t = GetEchoTimer(); //獲取US級(jí)時(shí)間
lengthTemp = ((float)t/58.0);//cm
sum = lengthTemp + sum ;
}
lengthTemp = sum/5.0;
return lengthTemp;
}
復(fù)制代碼
所有資料51hei提供下載:
基于stm32f4zgt6超聲波模塊測(cè)距.7z
(303.96 KB, 下載次數(shù): 55)
2019-3-18 22:41 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
hujj
時(shí)間:
2019-3-19 09:13
很好,正在搜集這方面的資料,謝謝樓主!
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1