|
寄存器寫(xiě)的stm32驅(qū)動(dòng)超聲波模塊,一直就在檢測(cè)上升沿電平那里出不來(lái)。用的串口獲得詳細(xì)數(shù)據(jù),
已解決,模塊電壓?jiǎn)栴},應(yīng)接5V電壓,我接成3.3V,32搞長(zhǎng)了,忘記當(dāng)初做51時(shí)是5V供電了。
- //#include "stm32f10x.h"
- #include "led.h"
- #include "sys.h"
- #include "sysclock.h"
- #include "key.h"
- #include "exti.h"
- #include "time.h"
- #include "pwm.h"
- #include "usart.h"<div class="blockcode"><blockquote>#include"hc04.h"
- u8 msHcCount = 0;//ms計(jì)數(shù)
- void Hr_hc04_Init(void)
- {
- TIM2_Init(1000-1,72-1);//分頻系數(shù)72,循環(huán)1000次1000us
-
- RCC->APB1ENR |= 1<<6; //GPIOE時(shí)鐘使能
-
- GPIOE->CRL &= 0xfffff00f;//設(shè)置位重置 E1、E2
-
- //E1推挽輸出
- GPIOE->CRL |= 0x1<<4;
- //E2浮空輸入
- GPIOE->CRL |= 0x4<<8;
-
- GPIOE->ODR|=~(1<<1);//E1一開(kāi)始低電平
- GPIOE->ODR|=~(1<<2);//E2一開(kāi)始低電平
- }
- static void OpenTimer() //打開(kāi)定時(shí)器
- {
- //清除計(jì)數(shù)器
- TIM_SetCounter(TIM2,0);
- msHcCount = 0;
- TIM_Cmd(TIM2,ENABLE);//使能定時(shí)器
- USARTx_Send_String(USART1,"定時(shí)器開(kāi)啟\r\n");
- }
-
- static void CloseTimer() //關(guān)閉定時(shí)器
- {
- // /*關(guān)閉計(jì)數(shù)器使能*/
- TIM_Cmd(TIM2,DISABLE);
- }
- //獲取定時(shí)器時(shí)間
- u32 GetEchoTimer(void)
- {
- u32 time = 0;
- /*//當(dāng)回響信號(hào)很長(zhǎng)是,計(jì)數(shù)值溢出后重復(fù)計(jì)數(shù),overCount用中斷來(lái)保存溢出次數(shù)*/
- time = msHcCount*1000;//overCount每++一次,代表overCount毫秒,time微妙
- time += TIM_GetCounter(TIM2);//獲取計(jì)TIM2數(shù)寄存器中的計(jì)數(shù)值,一邊計(jì)算回響信號(hào)時(shí)間
- TIM6->CNT = 0; //將TIM2計(jì)數(shù)寄存器的計(jì)數(shù)值清零
- delay_ms(50);
- USARTx_Send_String(USART1,"獲取定時(shí)器時(shí)間:");
- USARTx_Send_number(USART1,time);
- USARTx_Send_String(USART1,"\r\n");
- return time;
-
- }
- /*
- trig_T = 1;//trig拉高信號(hào),發(fā)出高電平
- // GPIOE->ODR |= 1<<1;
- if((GPIOE->IDR & 0x0002));//echo等待回響
- {
- USARTx_Send_String(USART1,"trig_T置高位成功\r\n");
- }
- delay_us(20);//持續(xù)時(shí)間超過(guò)10us
- // trig_T = 0;
- GPIOE->ODR |= ~(1<<1);
- USARTx_Send_String(USART1,"開(kāi)啟信號(hào)已發(fā)出456\n");
- while(!(GPIOE->IDR & 0x0004))//echo等待回響
- {
- i++;
- if(i>=10000)
- {
- USARTx_Send_String(USART1,"回響信號(hào)檢測(cè)失敗\n");
- break;
- }
- }
- USARTx_Send_String(USART1,"等待結(jié)束,計(jì)時(shí)中789\r\n");
- delay_ms(50);
-
-
- */
- float Hcsr04GetLength(void )
- {
- /*測(cè)5次數(shù)據(jù)計(jì)算一次平均值*/
- float length = 0;
- float t = 0;
- float sum = 0;
- u16 i = 0;
- u16 ttt;
- while(i != 5)
- {
-
- trig_T = 1;//trig拉高信號(hào),發(fā)出高電平
- // GPIOE->ODR |= 1<<1;
-
- if((GPIOE->IDR & 0x0002));//echo等待回響
- {
- USARTx_Send_String(USART1,"trig_T置高位成功\r\n");
- }
- delay_us(20);//持續(xù)時(shí)間超過(guò)10us
-
- // trig_T = 0;
- GPIOE->ODR |= ~(1<<1);
- USARTx_Send_String(USART1,"開(kāi)啟信號(hào)已發(fā)出\r\n");
-
- /*Echo發(fā)出信號(hào) 等待回響信號(hào)*/
- /*輸入方波后,模塊會(huì)自動(dòng)發(fā)射8個(gè)40KHz的聲波,與此同時(shí)回波引腳(echo)端的電平會(huì)由0變?yōu)?;
- (此時(shí)應(yīng)該啟動(dòng)定時(shí)器計(jì)時(shí));當(dāng)超聲波返回被模塊接收到時(shí),回波引 腳端的電平會(huì)由1變?yōu)?;
- (此時(shí)應(yīng)該停止定時(shí)器計(jì)數(shù)),定時(shí)器記下的這個(gè)時(shí)間即為
- 超聲波由發(fā)射到返回的總時(shí)長(zhǎng);*/
- // while(echo_R == 0);//echo等待回響
- while(!(GPIOE->IDR & 0x0004))//echo等待回響
- {
- ttt++;
- if(ttt>=50000)
- {
- USARTx_Send_String(USART1,"回響信號(hào)檢測(cè)失敗\r\n");
- break;
- }
- }
- USARTx_Send_String(USART1,"等待結(jié)束,計(jì)時(shí)中\(zhòng)r\n");
- /*開(kāi)啟定時(shí)器*/
- OpenTimer();
- i = i+1; //每收到一次回響信號(hào)+1,收到5次就計(jì)算均值
- // while(echo_R == 1);
- while((GPIOE->IDR & 0x0004))//echo等待回響
- {
- ttt++;
- if(ttt>=50000)
- {
- USARTx_Send_String(USART1,"等待回響失敗\r\n");
- break;
- }
- }
- /*關(guān)閉定時(shí)器*/
- CloseTimer();
- /*獲取Echo高電平時(shí)間時(shí)間*/
- t = GetEchoTimer();
- length = (float)t/58;//單位時(shí)cm
- sum += length;
- }
- length = sum/5;//五次平均值
-
- return length;
- }
- void TIM2_IRQHandler(void)
- {
- if(TIM2->SR &0X0001)//溢出中斷
- {
- msHcCount++;
- TIM2->SR &=~(1<<0);//清除中斷標(biāo)志位
- }
- }
復(fù)制代碼- #ifndef __hc04_h
- #define __hc04_h
- #include "sys.h"
- #include "stm32f10x.h"
- #include "time.h"
- #include "sysclock.h"
- #include "usart.h"
- #define trig_T PEout(1)
- #define echo_R PEin(2)
- void Hr_hc04_Init(void);
- float Hcsr04GetLength(void );
- #endif
復(fù)制代碼- #include "time.h"
- //設(shè)置 TIMx 計(jì)數(shù)器寄存器值
- void TIM_SetCounter(TIM_TypeDef* TIMx, uint16_t Counter)
- {
- /* Set the Counter Register value */
- TIMx->CNT = Counter;
- }
- //使能或者失能 TIMx 外設(shè)
- void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)
- {
-
- if (NewState != DISABLE)
- {
- /* Enable the TIM Counter */
- TIMx->CR1 |= TIM_CR1_CEN;
- }
- else
- {
- /* Disable the TIM Counter */
- TIMx->CR1 &= (uint16_t)(~((uint16_t)TIM_CR1_CEN));
- }
- }
- //獲取計(jì)時(shí)次數(shù)
- uint16_t TIM_GetCounter(TIM_TypeDef* TIMx)
- {
- return TIMx->CNT;
- }
- //自動(dòng)裝載寄存器 (TIMx_ARR)
- //預(yù)分頻器寄存器 (TIMx_PSC)
- //AHB不分頻;APB2不分頻;APB1二分頻 APB1=72/2 TIM_CLk=72
- void TIM2_Init(u16 arr,u16 psc)
- {
- RCC->APB1ENR|=1<<0; //使能TIM2時(shí)鐘
-
- TIM2->PSC=psc; //預(yù)分頻器寄存器
- TIM2->ARR=arr; //自動(dòng)裝載寄存器
-
- TIM2->DIER|=1<<0; //允許更新中斷
- TIM2->DIER|=1<<6; //使能觸發(fā)中斷
-
- // TIM2->CR1|=1<<0; //使能計(jì)數(shù)器 //外部函數(shù)TIM_Cmd 實(shí)現(xiàn)
-
- NVIC_Init(2,1,TIM2_IRQn,2);
- }
- /*超聲波中設(shè)置
- void TIM2_IRQHandler(void)
- {
- if(TIM2->SR &0X0001)//溢出中斷
- LED1=!LED1;
-
- TIM2->SR &=~(1<<0);//清除中斷標(biāo)志位
- }
- */
復(fù)制代碼- #ifndef _time_h
- #define _time_h
- #include "exti.h"
- #include "sysclock.h"
- void TIM2_Init(u16 arr,u16 psc);
- //設(shè)置 TIMx 計(jì)數(shù)器寄存器值
- void TIM_SetCounter(TIM_TypeDef* TIMx, uint16_t Counter);
- //使能或者失能 TIMx 外設(shè)
- void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState);
- //獲取計(jì)時(shí)次數(shù)
- uint16_t TIM_GetCounter(TIM_TypeDef* TIMx);
- #endif
復(fù)制代碼
#include "hc04.h"
int main()
{
int temp=0;
System_clock(9);
SysTick_init(72);
uart_init(72,9600);//時(shí)鐘頻率(Mhz) 波特率
LED_Init();
// TIM2_Init(1000-1,72-1);//分頻系數(shù)72,循環(huán)1000次1000us //放在hc04.c里了
Hr_hc04_Init();
delay_ms(50);
while(1)
{
delay_ms(500);
USARTx_Send_String(USART1,"測(cè)試距離:123");
delay_ms(10);
temp=Hcsr04GetLength();
delay_ms(10);
USARTx_Send_number(USART1,temp);
delay_ms(10);
USARTx_Send_String(USART1,"cm\r\n");
delay_ms(10);
LED4=!LED4;
delay_ms(10);
}
}
void SystemInit(void)
{
}
|
|