在推荐的这家店里买了些排线,把超声波模块跟逻辑分析仪接起来,不触发也经常看到逻辑分析仪上有信号输入,判断线不好使,换了跟之前的线,没有了,仔细看这家的排线,针比较细,估计接触不好吧。
换成之前的线后,
超声波模块触发用5伏触发收到的波形,用3伏触发没收到波形,做完的同学,你们是怎么搭的电路呢?
先展示下计算的超声波的距离
first cap =16085205
second cap =16090035
164.000000
first cap =32218205
second cap =32222445
144.000000
first cap =48351202
second cap =48355913
160.000000
first cap =64484204
second cap =64488860
158.000000
first cap =80617201
second cap =80621833
157.000000
first cap =96750205
second cap =96754840
157.000000
first cap =112883205
second cap =112886013
95.000000
first cap =129017203
second cap =129022074
165.000000
之前是接了3伏电源,用手触发超声波,有时候不能触发,现在接线,用F410去触发,每次都触发了,并且测量出了距离。如上图所示。
距离变化较大的是我用书挡住了测出的距离,用卷尺比对,两个距离大体正确。
现在发布源码
HAL_TIM_IC_Start_IT(&htim5,TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim5,TIM_CHANNEL_2);
启动捕获后,只要触发就自动计算出距离
HAL_TIM_IC_Start(&htim5,TIM_CHANNEL_1);
HAL_GPIO_WritePin(GPIOA, trigL_Pin, GPIO_PIN_SET);
delay_us(30);
HAL_GPIO_WritePin(GPIOA, trigL_Pin, GPIO_PIN_RESET);
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(TIM5==htim->Instance)
{
if(HAL_TIM_ACTIVE_CHANNEL_1==htim->Channel)
{
if(HAL_GPIO_ReadPin(GPIOA,echoL_Pin))
{
cntL1=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1);
// HAL_UART_Transmit(&huart1,"up\r\n",4,50);
printf("first cap =%d\r\n",cntL1);
}
else
{
cntL2=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1);
// HAL_UART_Transmit(&huart1,"down\r\n",6,50);
printf("second cap =%d\r\n",cntL2);
if(cntL2>cntL1)
{
dist_L=(cntL2-cntL1)*340/10000;
}
else
{
dist_L=(0xffffffff-cntL1+cntL2)*340/10000;
}
printf("%f\r\n",dist_L);
}
}
else if(HAL_TIM_ACTIVE_CHANNEL_2==htim->Channel)
{
if(HAL_GPIO_ReadPin(GPIOB,echoR_Pin))
{
cntR1=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2);
// HAL_UART_Transmit(&huart1,"up\r\n",4,50);
}
else
{
cntR2=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2);
// HAL_UART_Transmit(&huart1,"down\r\n",6,50);
if(cntR2>cntR1)
{
dist_R=(cntR1-cntR2)*340/10000;
}
else
{
dist_R=(0xffffffff-cntR1+cntR2)*340/10000;
}
}
}
}
}
我测试超声波的稳定性似乎没有说明书里那么好,更不要说精度了,大家的稳定性拿出来晒晒,对比下啊
1=8036169 2=8039842 cnt=3673, dist=124.882004
1=9044167 2=9047884 cnt=3717, dist=126.377998
1=10052168 2=10056110 cnt=3942, dist=134.028000
1=12063168 2=12066912 cnt=3744, dist=127.295998
1=13072168 2=13076084 cnt=3916, dist=133.143997
1=14081169 2=14085232 cnt=4063, dist=138.141998
1=15090168 2=15093911 cnt=3743, dist=127.262001
1=17101168 2=17104720 cnt=3552, dist=120.767998
1=21115168 2=21119012 cnt=3844, dist=130.695999
1=22124168 2=22128060 cnt=3892, dist=132.328003
1=23133168 2=23137035 cnt=3867, dist=131.477997
1=24142167 2=24145765 cnt=3598, dist=122.332001
1=25151167 2=25154308 cnt=3141, dist=106.793999
1=26160167 2=26164468 cnt=4301, dist=146.233994
1=27169167 2=27173304 cnt=4137, dist=140.658005
1=28178170 2=28181989 cnt=3819, dist=129.845993
1=29187168 2=29191059 cnt=3891, dist=132.294006
1=32200168 2=32203912 cnt=3744, dist=127.295998
1=33209166 2=33212696 cnt=3530, dist=120.019997
1=35219166 2=35223205 cnt=4039, dist=137.326004
1=36228167 2=36232234 cnt=4067, dist=138.278000
1=38239168 2=38242989 cnt=3821, dist=129.914001
1=39248168 2=39252257 cnt=4089, dist=139.026001
1=40257168 2=40261258 cnt=4090, dist=139.059998
1=43270168 2=43274181 cnt=4013, dist=136.442001
1=44279169 2=44284663 cnt=5494, dist=186.796005
1=45288167 2=45293844 cnt=5677, dist=193.018005
1=46297168 2=46301715 cnt=4547, dist=154.598007
1=48307168 2=48311304 cnt=4136, dist=140.623993
1=50318167 2=50322703 cnt=4536, dist=154.223999
1=52328169 2=52332941 cnt=4772, dist=162.248001
1=54339166 2=54342961 cnt=3795, dist=129.029999
1=56350168 2=56354947 cnt=4779, dist=162.485992
1=58361169 2=58365558 cnt=4389, dist=149.225998
1=59370168 2=59374824 cnt=4656, dist=158.304001
1=60379168 2=60383421 cnt=4253, dist=144.602005
1=61388167 2=61392821 cnt=4654, dist=158.235992
1=62397169 2=62401508 cnt=4339, dist=147.526001
1=63406167 2=63410775 cnt=4608, dist=156.671997
1=64415168 2=64419729 cnt=4561, dist=155.074005
1=65423169 2=65427747 cnt=4578, dist=155.651993
1=67434166 2=67439015 cnt=4849, dist=164.865997
1=69445169 2=69449999 cnt=4830, dist=164.220001
1=70454168 2=70457982 cnt=3814, dist=129.675995
1=71463168 2=71467689 cnt=4521, dist=153.714005
1=72471169 2=72475039 cnt=3870, dist=131.580002
1=74482168 2=74486785 cnt=4617, dist=156.977997
1=75491168 2=75495779 cnt=4611, dist=156.774002
1=76500168 2=76505076 cnt=4908, dist=166.871994
1=79513167 2=79515406 cnt=2239, dist=76.125999
1=80522168 2=80524220 cnt=2052, dist=69.767998
1=81531168 2=81533323 cnt=2155, dist=73.269997
1=82540167 2=82541963 cnt=1796, dist=61.063999
1=85553169 2=85555168 cnt=1999, dist=67.966003
1=89568170 2=89570199 cnt=2029, dist=68.986000
1=90577169 2=90579316 cnt=2147, dist=72.998001
1=91586168 2=91588289 cnt=2121, dist=72.113998
1=92595168 2=92597263 cnt=2095, dist=71.230003
1=93604167 2=93608004 cnt=3837, dist=130.457993
1=94613168 2=94615484 cnt=2316, dist=78.744003
1=96624169 2=96626310 cnt=2141, dist=72.793999
1=97633169 2=97635139 cnt=1970, dist=66.980003
1=99644169 2=99647324 cnt=3155, dist=107.269997
1=102657167 2=102660942 cnt=3775, dist=128.350006
1=103666168 2=103670035 cnt=3867, dist=131.477997
1=104675169 2=104678720 cnt=3551, dist=120.734001
1=105684169 2=105687692 cnt=3523, dist=119.781998
1=106693169 2=106696822 cnt=3653, dist=124.202003
1=109706168 2=109709817 cnt=3649, dist=124.066002
1=110715167 2=110719572 cnt=4405, dist=149.770004
1=111725169 2=111729211 cnt=4042, dist=137.427994
1=112734170 2=112737425 cnt=3255, dist=110.669998
1=114745170 2=114748995 cnt=3825, dist=130.050003
1=116756168 2=116759754 cnt=3586, dist=121.924004
1=117765169 2=117768826 cnt=3657, dist=124.337997
1=118774169 2=118777828 cnt=3659, dist=124.405998
1=122789168 2=122792575 cnt=3407, dist=115.837997
1=123799168 2=123802790 cnt=3622, dist=123.148003
1=124808167 2=124811746 cnt=3579, dist=121.685997
1=128823170 2=128826786 cnt=3616, dist=122.944000
1=129832170 2=129835956 cnt=3786, dist=128.723999
1=130841170 2=130845082 cnt=3912, dist=133.007996
1=132852169 2=132856169 cnt=4000, dist=136.000000
1=134863169 2=134863884 cnt=715, dist=24.309999
1=135872167 2=135872861 cnt=694, dist=23.596001
1=136881168 2=136881863 cnt=695, dist=23.629999
1=137890169 2=137890864 cnt=695, dist=23.629999
1=138899168 2=138899842 cnt=674, dist=22.916000
1=140910169 2=140910830 cnt=661, dist=22.474001
1=141919169 2=141919831 cnt=662, dist=22.507999
1=144932168 2=144937060 cnt=4892, dist=166.328003
1=145941167 2=145942035 cnt=868, dist=29.511999
1=146950168 2=146950814 cnt=646, dist=21.964001
1=147959168 2=147959853 cnt=685, dist=23.290001
1=149970168 2=149970937 cnt=769, dist=26.146000
1=151981168 2=151981889 cnt=721, dist=24.514000
1=152990167 2=152990939 cnt=772, dist=26.247999
1=153999167 2=154003137 cnt=3970, dist=134.979996
1=155008169 2=155012070 cnt=3901, dist=132.634003
1=156017168 2=156022060 cnt=4892, dist=166.328003
1=157026168 2=157031056 cnt=4888, dist=166.192001
1=159037170 2=159042109 cnt=4939, dist=167.925995
1=161048168 2=161052472 cnt=4304, dist=146.335999
1=162057168 2=162057915 cnt=747, dist=25.398001
1=163066169 2=163070554 cnt=4385, dist=149.089996
1=164076167 2=164076867 cnt=700, dist=23.799999
1=166087169 2=166087868 cnt=699, dist=23.766001
1=167096168 2=167096916 cnt=748, dist=25.431999
1=170109168 2=170109868 cnt=700, dist=23.799999
1=171118169 2=171118893 cnt=724, dist=24.615999
1=173129169 2=173129845 cnt=676, dist=22.983999
1=176142169 2=176142869 cnt=700, dist=23.799999
1=178153169 2=178153844 cnt=675, dist=22.950001
1=180164168 2=180164844 cnt=676, dist=22.983999
1=181173169 2=181173872 cnt=703, dist=23.902000
有奖活动 | |
---|---|
【有奖活动——B站互动赢积分】活动开启啦! | |
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |