【ST NUCLEO-U5A5ZJ-Q开发板测评】ADC体验之一
<div class='showpostmsg'><p>【前言】</p><p>stm32U5系列有比较优秀的ADC外设,U5A5有ADC12与ADC4。</p>
<p>在用户手册《RM0456》中有描述:</p>
<p> STM32U5A5除了有14位的ADC12外还有12位的ADC4,总共可以支持45个通道输入。</p>
<p> </p>
<p>本次体验只是初步体验:</p>
<p>【软件环境】STM32CubeIDE1.14.0</p>
<p>【实现步骤】</p>
<p>1、新建基于NOCLE-U5A5ZJ-Q的开发板工程。</p>
<p>2、使用默认的生成环境,环境中初始化了开发板的基础运行环境,如串口等。</p>
<p>3、开发板工程已经初始好了ADC1的AN3通道(PC2)。其基础配置参数如下:</p>
<p> 转换为单次转换,转换时间为5个时钟,14位转换。</p>
<p>生成代码后可以看到配置的代码如下:</p>
<pre>
<code>/* ADC1 init function */
void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 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_ASYNC_DIV1;
hadc1.Init.Resolution = ADC_RESOLUTION_14B;
hadc1.Init.GainCompensation = 0;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
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.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_5CYCLE;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}</code></pre>
<p>在主程序中添加测试代码如下:</p>
<pre>
<code>int main(void)
{
/* USER CODE BEGIN 1 */
uint16_t adc_val;
/* 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();
/* Configure the System Power */
SystemPower_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_ICACHE_Init();
MX_UCPD1_Init();
MX_USART1_UART_Init();
MX_USB_OTG_HS_HCD_Init();
/* USER CODE BEGIN 2 */
printf("start\r\n");
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_ADC_Start(&hadc1);
adc_val = HAL_ADC_GetValue(&hadc1);
printf("ADC VaL:%d voltage:%.3f \r\n ", adc_val, (float)adc_val*3.3/16384);
HAL_Delay(500);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}</code></pre>
<p>其代码为添加一个变量adc_val用来存放ADC的转换值,使用HAL_ADC_Start(&hadc1);开始ADC的转换,使用 adc_val = HAL_ADC_GetValue(&hadc1);来读取转换结果</p>
<p>使用printf把打印结果输出到串口。</p>
<p>【实验效果】</p>
<p>1、接入一个可调电位器到开发板上,调节电器位可以看到实实时的转换结果。</p>
<p>2、同时将万用表也接到电位器的输出端口进行对比测试。</p>
<p>我们通过看到万用表与串口输出值的对比,相差为mV级别。</p>
<p>25b705f29aeb3a25c2d4553e1b3ce59e<br />
</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> <p>将转换时间延长到814个周期,可以看到转换的稳定性相比5个周期要好很多:</p>
<p> </p>
<p>【体验过采样】</p>
<p>stm32U5可以使用ADC的过采样功能实现更高的采集精度。</p>
<p>1、设置ADC的过采样以及位移:</p>
<p> 我这里设置了左移6位,来实现20位的采集,同时采样数据也设置为左移6位的编移,生成代码后,将打印函数修正为:</p>
<p>printf("ADC VaL:%d voltage:%.4f \r\n ", adc_val, (float)adc_val*3.3/1048576);</p>
<p>【实验效果】</p>
<p> 感觉效果也不是太明显。</p>
页:
[1]