完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
` 不知道哪里的原因 ,用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
(2.63 MB, 下载次数: 24
)
|
|
相关推荐
2个回答
|
|
大佬们帮忙看一下吧
|
|
|
|
哇 , 没有大佬哇,等的好辛苦
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1401 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1335 浏览 3 评论
2785 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1478 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1911 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-2 11:36 , Processed in 0.500693 second(s), Total 47, Slave 38 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号