不知道哪里的原因 ,用oled显示时候 显示不了后面的数字, 去掉测距函数,能正常显示000.00cm 求大佬帮忙找一下原因
#include "hcsr04.h"
#include "sys.h"
#include "delay.h"
#define TRIG_Send PBout(4)
#define ECHO_Reci PBin(5)
u16 msHcCount = 0;
//float lengthTemp = 0;
void Hcsr04Init()
{
tiM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//IO¿Ú³õʼ»¯
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //·¢Ë͵çƽ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//ÍÆÍìÊä³ö
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //·µ»Øµçƽ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
//¶¨Ê±Æ÷³õʼ»¯£¬Ê¹Óûù±¾¶¨Ê±Æ÷TIM6
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //ʹÓöÔÓ¦RCCʱÖÓ
//ÅäÖö¨Ê±Æ÷
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = (1000-1); //ÉèÖö¨Ê±Æ÷
TIM_TimeBaseStructure.TIM_Prescaler =(72-1); //ÉèÖÃÓÃÀ´ÓÃÀ´×÷ΪTIMx³ýÊýµÄÔ¤·ÖƵֵ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//²»·ÖƵ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM6ÏòÉϼÆÊý
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update); //Çå³ý¸üÐÂÖжÏ
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //´ò¿ª¶¨Ê±Æ÷¸üÐÂÖжÏ
hcsr04_NVIC();
TIM_Cmd(TIM2,DISABLE);
}
void OpenTimerForHc() //´ò¿ª¶¨Ê±Æ÷
{
TIM_SetCounter(TIM2,0);//Çå³ý¼ÆÊý
msHcCount = 0;
TIM_Cmd(TIM2, ENABLE); //ʹÄÜTIMxÍâÉè
}
void CloseTimerForHc() //¹Ø±Õ¶¨Ê±Æ÷
{
TIM_Cmd(TIM2, DISABLE); //ʹÄÜTIMxÍâÉè
}
//NVICÅäÖÃ
void hcsr04_NVIC()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //Ñ¡Ôñ´®¿Ú1ÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÇÀռʽÖжÏÓÅÏȼ¶ÉèΪ1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //ÏìӦʽÖжÏÓÅÏȼ¶ÉèÖÃΪ1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÖжÏ
NVIC_Init(&NVIC_InitStructure);
}
//¶¨Ê±Æ÷6ÖжϷþÎñ
void TIM2_IRQHandler(void) //TIM6ÖжÏ
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //¼ì²éTIM6ÊÇ·ñÖжÏ
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //Çå³ýTIMxÖжϱêÖ¾
msHcCount++;
}
}
//»ñÈ¡¶¨Ê±Æ÷ʱ¼ä
u32 GetEchoTimer(void)
{
u32 t = 0;
t = msHcCount*1000;//µÃµ½MS
t += TIM_GetCounter(TIM2);
TIM2->CNT = 0; //½«TIM6¼ÆÊýÆ÷¼Ä´æÆ÷µÄ¼ÆÊýÖµÇåÁã
DelayMs(50);
return t;
}
//Ò»´Î»ñÈ¡³¬Éù²¨²â¾àÊý¾Ý£¬Á½´Î²â¾àÖ®¼äÐèÒªÏà¸ôÒ»¶¨Ê±¼ä£¬¸ô¶Ï»ØÏìÐźÅ
//È¡Îå´ÎÊý¾Ýƽ¾ùÖµ½øÐмÓȨÂ˲¨
float Hcsr04GetLength(void )
{
u32 t = 0;
int i ;
float lengthTemp = 0;
float sum = 0;
for(i=0;i<5;i++)
{
TRIG_Send = 1; //·¢ËͿڸߵçƽÊä³ö
DelayUs(20);
TRIG_Send = 0;
while(ECHO_Reci == 0); //µÈ´ý½ÓÊտڸߵçƽÊä³ö
OpenTimerForHc(); //´ò¿ª¶¨Ê±Æ÷
//i = i + 1;
while(ECHO_Reci == 1);
CloseTimerForHc(); //¹Ø±Õ¶¨Ê±Æ÷
t = GetEchoTimer(); //»ñȡʱ¼ä£¬·Ö±æÂÊΪ1US
lengthTemp = ((float)t/58.0);//cm
sum = lengthTemp + sum ;
}
lengthTemp = sum/5.0;
//DelayS(2);
return lengthTemp;
}
-
-
-
超声波测距.zip
下载积分: 积分 -1 分
2.63 MB, 下载次数: 1, 下载积分: 积分 -1 分
一周热门 更多>