【NUCLEO-WL55JC2测评】+ ADC采集转发
<div class='showpostmsg'><p>【NUCLEO-WL55JC2测评】+ ADC采集转发</p><p>1、功能说明</p>
<p>我要实现的功能就是采集4~20MA电流然后转发给主机,那第一个功能就是ADC采集250欧姆电阻上的电压,然后发送。</p>
<p> </p>
<p>2.了解WL55JC的ADC</p>
<p>WL55JC资源中只有ADC1,同时它包含12个ADC通道 ADC1_IN0~ ADC1_IN11,每个通道12bit,同时内部4通道包含</p>
<p>temperature sensor<br />
voltage reference<br />
VBAT monitoring<br />
DAC output</p>
<p>每个通道对应管脚如下:</p>
<p> ADC1_IN0 PB13<br />
ADC1_IN1 PB14<br />
ADC1_IN2 PB3<br />
ADC1_IN3 PB4<br />
ADC1_IN4 PB2 <br />
ADC1_IN5 PB1</p>
<p> ADC1_IN6 PA10<br />
ADC1_IN7 PA11<br />
ADC1_IN8 PA12<br />
ADC1_IN9 PA13 JTMS-SWDIO<br />
ADC1_IN10 PA14 JTCK-SWCLK,<br />
ADC1_IN11 PA15</p>
<p>在NUCLEO-WL55JC2板子上的 CN8对外扩展口上的 A0~A5</p>
<p>A0~A5对应ADC通道号及管脚</p>
<p> 管脚 ADC1 通道</p>
<p>A0 PB1 ADC1_IN5<br />
A1 PB2 ADC1_IN4 <br />
A2 PA10 ADC1_IN6<br />
A3 PB4 ADC1_IN3<br />
A4 PB14 ADC1_IN1<br />
A5 PB13 ADC1_IN0</p>
<p>3、管脚初始化</p>
<p>程序中对相应管脚初始化</p>
<p>/* Enable GPIOs clock */</p>
<p> __HAL_RCC_GPIOB_CLK_ENABLE();</p>
<p> </p>
<p> GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; //Analog Mode<br />
GPIO_InitStruct.Pull = GPIO_NOPULL; //No Pull-up or Pull-down activation <br />
GPIO_InitStruct.Pin = GPIO_PIN_All & (~(GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_4 | GPIO_PIN_13 | GPIO_PIN_14));<br />
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);</p>
<p> </p>
<p> /* Disable GPIOs clock */ <br />
__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 />
uint32_t ADCxConvertedValues = 0;<br />
#ifdef HAL_ADC_MODULE_ENABLED<br />
ADC_ChannelConfTypeDef sConfig = {0};</p>
<p> MX_ADC1_Init();<br />
/** Configure Regular Channel */<br />
sConfig.Channel = channel;<br />
sConfig.Rank = ADC_REGULAR_RANK_1;<br />
sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;<br />
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)<br />
{<br />
Error_Handler();<br />
}</p>
<p> if (HAL_ADC_Start(&hadc1) != HAL_OK)<br />
{<br />
/* Start Error */<br />
Error_Handler();<br />
}<br />
/** Wait for end of conversion */<br />
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);</p>
<p> /* Workaround for tempsensor value*/<br />
if (channel == ADC_CHANNEL_TEMPSENSOR)<br />
{<br />
if (HAL_ADC_Start(&hadc1) != HAL_OK)<br />
{<br />
/* Start Error */<br />
Error_Handler();<br />
}<br />
/** Wait for end of conversion */<br />
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);<br />
}</p>
<p> /** Wait for end of conversion */<br />
HAL_ADC_Stop(&hadc1) ; /* it calls also ADC_Disable() */</p>
<p> ADCxConvertedValues = HAL_ADC_GetValue(&hadc1);</p>
<p> HAL_ADC_DeInit(&hadc1);</p>
<p>#endif /* HAL_ADC_MODULE_ENABLED */<br />
return ADCxConvertedValues;<br />
}</p>
<p>5、LoRafa发送</p>
<p>这里直接参考例程PingPong</p>
<p>在发起端不断定时读取ADC然后发送给从端</p>
<p>if (isMaster == true)<br />
{<br />
if(counter++>9){counter=0;}<br />
<br />
/*to be replaced */<br />
APP_LOG(TS_ON, VLEVEL_L, "counter= %d\n\r", counter);<br />
<br />
TEMP= GetTemperatureLevel();<br />
BatteryLevel= GetBatteryLevel();<br />
<br />
ADC0=SYS_GetADCLevel(); //读ADC0 <br />
APP_LOG(TS_ON, VLEVEL_L, "ADC0= %d\n\r", ADC0); <br />
<br />
Buffer = counter; <br />
Buffer = (ADC0) >> 8;<br />
Buffer = (ADC0); <br />
Buffer = BatteryLevel;<br />
<br />
for (i = 4; i < BufferSize; i++)<br />
{<br />
Buffer = i - 4;<br />
}<br />
APP_LOG(TS_ON, VLEVEL_L, "Master Tx start\n\r");</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]