蓝雨夜 发表于 2020-7-16 15:53

【NUCLEO-WL55JC2测评】+ ADC采集转发

<div class='showpostmsg'><p>【NUCLEO-WL55JC2测评】+ ADC采集转发</p>

<p>1、功能说明</p>

<p>我要实现的功能就是采集4~20MA电流然后转发给主机,那第一个功能就是ADC采集250欧姆电阻上的电压,然后发送。</p>

<p>&nbsp;</p>

<p>2.了解WL55JC的ADC</p>

<p>WL55JC资源中只有ADC1,同时它包含12个ADC通道&nbsp; ADC1_IN0~&nbsp; ADC1_IN11,每个通道12bit,同时内部4通道包含</p>

<p>temperature sensor<br />
voltage reference<br />
VBAT monitoring<br />
DAC output</p>

<p>每个通道对应管脚如下:</p>

<p>&nbsp; ADC1_IN0 &nbsp; &nbsp; &nbsp; &nbsp;PB13<br />
&nbsp; ADC1_IN1 &nbsp; &nbsp; &nbsp; &nbsp;PB14<br />
&nbsp; ADC1_IN2 &nbsp; &nbsp; &nbsp; &nbsp;PB3<br />
&nbsp; ADC1_IN3 &nbsp; &nbsp; &nbsp; &nbsp;PB4<br />
&nbsp; ADC1_IN4 &nbsp; &nbsp; &nbsp; &nbsp;PB2 &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; ADC1_IN5 &nbsp; &nbsp; &nbsp; &nbsp;PB1</p>

<p>&nbsp; ADC1_IN6 &nbsp; &nbsp; &nbsp; &nbsp;PA10<br />
&nbsp; ADC1_IN7 &nbsp; &nbsp; &nbsp; &nbsp;PA11<br />
&nbsp; ADC1_IN8 &nbsp; &nbsp; &nbsp; &nbsp;PA12<br />
&nbsp; ADC1_IN9 &nbsp; &nbsp; &nbsp; &nbsp;PA13 &nbsp; &nbsp; JTMS-SWDIO<br />
&nbsp; ADC1_IN10 &nbsp; &nbsp; &nbsp; PA14 &nbsp; &nbsp; JTCK-SWCLK,<br />
&nbsp; ADC1_IN11 &nbsp; &nbsp; &nbsp; PA15</p>

<p>在NUCLEO-WL55JC2板子上的 CN8对外扩展口上的 A0~A5</p>

<p>A0~A5对应ADC通道号及管脚</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;管脚 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ADC1 通道</p>

<p>A0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PB1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ADC1_IN5<br />
A1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PB2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ADC1_IN4 &nbsp; &nbsp; &nbsp; &nbsp;<br />
A2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PA10 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ADC1_IN6<br />
A3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PB4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ADC1_IN3<br />
A4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PB14 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ADC1_IN1<br />
A5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PB13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ADC1_IN0</p>

<p>3、管脚初始化</p>

<p>程序中对相应管脚初始化</p>

<p>/* Enable GPIOs clock */</p>

<p>&nbsp; __HAL_RCC_GPIOB_CLK_ENABLE();</p>

<p>&nbsp;</p>

<p>&nbsp; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Analog Mode<br />
&nbsp; GPIO_InitStruct.Pull = GPIO_NOPULL; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //No Pull-up or Pull-down activation &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; GPIO_InitStruct.Pin = GPIO_PIN_All &amp; (~(GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_4 | GPIO_PIN_13 | GPIO_PIN_14));<br />
&nbsp; HAL_GPIO_Init(GPIOB, &amp;GPIO_InitStruct);</p>

<p>&nbsp;</p>

<p>&nbsp; /* Disable GPIOs clock */&nbsp;<br />
&nbsp; __HAL_RCC_GPIOB_CLK_DISABLE();</p>

<p>4、读取ADC值</p>

<p>ADC_ReadChannels(ADC_CHANNEL_5); 这里ADC_CHANNEL_5 对应板上CN8的A0通道</p>

<p>static uint32_t ADC_ReadChannels(uint32_t channel)<br />
{<br />
&nbsp; uint32_t ADCxConvertedValues = 0;<br />
#ifdef HAL_ADC_MODULE_ENABLED<br />
&nbsp; ADC_ChannelConfTypeDef sConfig = {0};</p>

<p>&nbsp; MX_ADC1_Init();<br />
&nbsp; /** Configure Regular Channel */<br />
&nbsp; sConfig.Channel = channel;<br />
&nbsp; sConfig.Rank = ADC_REGULAR_RANK_1;<br />
&nbsp; sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;<br />
&nbsp; if (HAL_ADC_ConfigChannel(&amp;hadc1, &amp;sConfig) != HAL_OK)<br />
&nbsp; {<br />
&nbsp; &nbsp; Error_Handler();<br />
&nbsp; }</p>

<p>&nbsp; if (HAL_ADC_Start(&amp;hadc1) != HAL_OK)<br />
&nbsp; {<br />
&nbsp; &nbsp; /* Start Error */<br />
&nbsp; &nbsp; Error_Handler();<br />
&nbsp; }<br />
&nbsp; /** Wait for end of conversion */<br />
&nbsp; HAL_ADC_PollForConversion(&amp;hadc1, HAL_MAX_DELAY);</p>

<p>&nbsp; /* Workaround for tempsensor value*/<br />
&nbsp; if (channel == ADC_CHANNEL_TEMPSENSOR)<br />
&nbsp; {<br />
&nbsp; &nbsp; if (HAL_ADC_Start(&amp;hadc1) != HAL_OK)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; /* Start Error */<br />
&nbsp; &nbsp; &nbsp; Error_Handler();<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; /** Wait for end of conversion */<br />
&nbsp; &nbsp; HAL_ADC_PollForConversion(&amp;hadc1, HAL_MAX_DELAY);<br />
&nbsp; }</p>

<p>&nbsp; /** Wait for end of conversion */<br />
&nbsp; HAL_ADC_Stop(&amp;hadc1) ; &nbsp; /* it calls also ADC_Disable() */</p>

<p>&nbsp; ADCxConvertedValues = HAL_ADC_GetValue(&amp;hadc1);</p>

<p>&nbsp; HAL_ADC_DeInit(&amp;hadc1);</p>

<p>#endif /* HAL_ADC_MODULE_ENABLED */<br />
&nbsp; return ADCxConvertedValues;<br />
}</p>

<p>5、LoRafa发送</p>

<p>这里直接参考例程PingPong</p>

<p>在发起端不断定时读取ADC然后发送给从端</p>

<p>if (isMaster == true)<br />
&nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; if(counter++&gt;9){counter=0;}<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*to be replaced */<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; APP_LOG(TS_ON, VLEVEL_L, &quot;counter= %d\n\r&quot;, counter);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TEMP= GetTemperatureLevel();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BatteryLevel= GetBatteryLevel();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ADC0=SYS_GetADCLevel(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//读ADC0&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; APP_LOG(TS_ON, VLEVEL_L, &quot;ADC0= %d\n\r&quot;, ADC0); &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Buffer = counter;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Buffer = (ADC0) &gt;&gt; 8;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Buffer = (ADC0);&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Buffer = BatteryLevel;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; for (i = 4; i &lt; BufferSize; i++)<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Buffer = i - 4;<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; APP_LOG(TS_ON, VLEVEL_L, &quot;Master Tx start\n\r&quot;);</p>

<p>6、测试发起端</p>

<p>我们通过串口打印把ADC采样的值发送出去,同时打印出来,我测试接收端没有,所以通过ST的例程确保无线发射无误,只要把ADC采集到数据,基本就可以实现了要求</p>

<p>同时也欠缺了不能发射距离测试,功耗测试!</p>

<p></p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页: [1]
查看完整版本: 【NUCLEO-WL55JC2测评】+ ADC采集转发