dvacos 发表于 2024-9-6 20:11

[B-G431B-ESC1评测] 七,板载旋钮ADC采样

<div class='showpostmsg'> 本帖最后由 dvacos 于 2024-9-6 20:17 编辑

<div>在论坛里看见有的小伙伴使用B-G431B-ESC1开发板的ADC采样旋钮的电压一直都是0V,于是我挑战一下。</div>

<div>&nbsp;</div>

<div><span style="font-size:20px;"><strong>一 、硬件确认</strong></span></div>

<div>&nbsp;</div>

<div></div>

<div>&nbsp;</div>

<div>3D PCB 确认引脚</div>

<div></div>

<div></div>

<div>经过万用表测量,确认电压会随着旋钮的变化而变化,由0V-3.3V之间变化。</div>

<div>&nbsp;</div>

<div><span style="font-size:20px;"><strong>二 、软件初始化</strong></span></div>

<div>&nbsp;</div>

<div><strong>Cube Mx配置</strong></div>

<div>&nbsp;</div>

<div>配置PB12 为ADC,默认分配为ADC1 11通道</div>

<div></div>

<div>&nbsp;</div>

<div>ADC只有一根线,所以配置为单端</div>

<div></div>

<div>&nbsp;</div>

<div>ADC配置</div>

<div></div>

<div>&nbsp;</div>

<div>以上Cube Mx配置ADC初始化完毕</div>

<div>&nbsp;</div>

<div><strong>代码配置</strong></div>

<div>由于初始化的时候选择的是软件触发,所以需要添加一些代码,才能开始采样。开始采样与获取采样值API如下</div>

<div>HAL_ADC_Start(&amp;hadc1);</div>

<div>HAL_ADC_GetValue(&amp;hadc1);</div>

<div>&nbsp;</div>

<div><span style="font-size:20px;"><strong>三、 实验</strong></span></div>

<div> &nbsp;</div>

<div>&nbsp;</div>

<div><span style="font-size:20px;"><strong>四 、完整ADC代码</strong></span></div>

<div>
<pre>
<code class="language-cpp">/**
* @brief ADC1 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC1_Init(void)
{

/* USER CODE BEGIN ADC1_Init 0 */

/* USER CODE END ADC1_Init 0 */

ADC_MultiModeTypeDef multimode = {0};
ADC_ChannelConfTypeDef sConfig = {0};

/* USER CODE BEGIN ADC1_Init 1 */

/* USER CODE END ADC1_Init 1 */

/** Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_LEFT;
hadc1.Init.GainCompensation = 0;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&amp;hadc1) != HAL_OK)
{
    Error_Handler();
}

/** Configure the ADC multi-mode
*/
multimode.Mode = ADC_MODE_INDEPENDENT;
if (HAL_ADCEx_MultiModeConfigChannel(&amp;hadc1, &amp;multimode) != HAL_OK)
{
    Error_Handler();
}

/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_11;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&amp;hadc1, &amp;sConfig) != HAL_OK)
{
    Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */

/* USER CODE END ADC1_Init 2 */

}




int main(void)
{

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */
SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_ADC1_Init();
MX_USART2_UART_Init();

/* Initialize interrupts */
/* USER CODE BEGIN 2 */

/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
      char printf_data;
                uint16_t get_adc=0;
                double voltage=0;
               
                HAL_ADC_Start(&amp;hadc1);
                get_adc=HAL_ADC_GetValue(&amp;hadc1);
                voltage = get_adc*(3.3/65535);
      
                sprintf(printf_data,"Get_Adc:%dVoltage:%.2f \r\n",get_adc,voltage);
                HAL_UART_Transmit(&amp;huart2,(uint8_t *)printf_data,strlen(printf_data),50);
                HAL_Delay(100);
      
      
}
/* USER CODE END 3 */
}</code></pre>

<p>&nbsp;</p>
</div>
</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]
查看完整版本: [B-G431B-ESC1评测] 七,板载旋钮ADC采样