找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1931|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

stm32控制超聲波模塊最大距離就顯示168cm,用例程代碼255多,搞不明白

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:751817 發(fā)表于 2020-7-6 23:17 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
#include "hc_sr04.h"

float Distance;

void hcgpio_init(void)
{
        GPIO_InitTypeDef        gpio_struct;
        NVIC_InitTypeDef        nvic_struct;
        EXTI_InitTypeDef        exti_struct;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOA |
                                RCC_APB2Periph_AFIO, ENABLE);

        gpio_struct.GPIO_Pin            =       GPIO_Pin_0;
        gpio_struct.GPIO_Mode           =       GPIO_Mode_Out_PP;
        gpio_struct.GPIO_Speed          =       GPIO_Speed_50MHz;
        GPIO_Init(GPIOC,&gpio_struct);

        gpio_struct.GPIO_Pin            =       GPIO_Pin_1;
        gpio_struct.GPIO_Mode           =       GPIO_Mode_IPD;
        gpio_struct.GPIO_Speed          =       GPIO_Speed_50MHz;
        GPIO_Init(GPIOC,&gpio_struct);

        GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource1);

        exti_struct.EXTI_Line           =       EXTI_Line1;
        exti_struct.EXTI_Mode           =       EXTI_Mode_Interrupt;
        exti_struct.EXTI_Trigger        =       EXTI_Trigger_Rising;
        exti_struct.EXTI_LineCmd        =       ENABLE;
        EXTI_Init(&exti_struct);

        nvic_struct.NVIC_IRQChannel     =       EXTI1_IRQn;
        nvic_struct.NVIC_IRQChannelCmd  =       ENABLE;
        nvic_struct.NVIC_IRQChannelPreemptionPriority   =       2;
        nvic_struct.NVIC_IRQChannelSubPriority          =       2;
        NVIC_Init(&nvic_struct);

}

void EXTI1_IRQHandler(void)
{
        delay_us(10);

        if(EXTI_GetITStatus(EXTI_Line1) !=RESET)
        {
                TIM_SetCounter(TIM2,0);
                TIM_Cmd(TIM2,ENABLE);
               
                while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1));  //μè′yμíμçÆ½
               
                TIM_Cmd(TIM2,DISABLE);
               
                Distance=TIM_GetCounter(TIM2)*340/200.0;
               
                if(Distance>0)
                {
                        printf("Distance:%f cm\r\n",Distance);
                }
                       
                EXTI_ClearITPendingBit(EXTI_Line1);


        }
}

void hc_sr04(void)
{
        GPIO_SetBits(GPIOC,GPIO_Pin_0);
        delay_us(20);
        GPIO_ResetBits(GPIOC,GPIO_Pin_0);

}

#include "hc_time.h"

void hctime_init(void)
{
        TIM_TimeBaseInitTypeDef         time_struct;
        NVIC_InitTypeDef                nvic_struct;

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 ,ENABLE);

        time_struct.TIM_Period          =       5000;
        time_struct.TIM_Prescaler       =       7199;
        time_struct.TIM_CounterMode     =       TIM_CounterMode_Up;
        time_struct.TIM_ClockDivision   =       0;
        //tim2
        TIM_TimeBaseInit(TIM2,&time_struct);

        TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
        TIM_ITConfig(TIM2,TIM_IT_Trigger,ENABLE);

        nvic_struct.NVIC_IRQChannel     =       TIM2_IRQn;
        nvic_struct.NVIC_IRQChannelCmd  =       ENABLE;
        nvic_struct.NVIC_IRQChannelPreemptionPriority   =  1;
        nvic_struct.NVIC_IRQChannelSubPriority          =  0;

        NVIC_Init(&nvic_struct);
}

void TIM2_IRQHandler(void)
{
        if(TIM_GetITStatus(TIM2,TIM_IT_Update) != RESET)
        {
                TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
        }

}





分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:476215 發(fā)表于 2020-7-7 00:12 | 只看該作者
是不是超聲波模塊信號多次反射了,可以降低超聲波的發(fā)射頻率試一下;還有一種可能是你計時的定時器出了問題,對比這查看下
回復(fù)

使用道具 舉報

板凳
ID:764088 發(fā)表于 2020-7-7 10:33 | 只看該作者
這個例程 有這些毛。航邮辗答伝夭,中斷處理開頭就延時,處理內(nèi)用過雜, 過多,甚至打印。
本來超聲波模塊是通過 發(fā)送波及回波間隔,來測試 距離的,
回復(fù)

使用道具 舉報

地板
ID:751817 發(fā)表于 2020-7-7 10:58 | 只看該作者
qee654 發(fā)表于 2020-7-7 00:12
是不是超聲波模塊信號多次反射了,可以降低超聲波的發(fā)射頻率試一下;還有一種可能是你計時的定時器出了問題 ...

頻率減低了,也不行;定時器的問題不清楚
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表