本帖最后由 wenyangzeng 于 2019-9-22 11:51 编辑
NUCLEO_G431RB测评 (3)过采样16位ADC
NUCLEO_G431的一个亮点是通过硬件过采样技术使得ADC转换位数达到16位,值得进行评测。
硬件外接1只电位器,将AVDD分压送ADC通道0,OLED以毫伏为单位显示采样的电压值。
OLED驱动请参考上一贴《NUCLEO_G431RB测评 (2)驱动OLED》
软件设定ADC过采样128次,右移3位,得到16位分辨率。相关代码如下:
#include "adc.h"
ADC_HandleTypeDef hadc1;
void MX_ADC1_Init(void)
{
ADC_MultiModeTypeDef multimode = {0};
ADC_ChannelConfTypeDef sConfig = {0};
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.GainCompensation = 0;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
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_OVERWRITTEN;
hadc1.Init.OversamplingMode = ENABLE;
hadc1.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_128;
hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_3;
hadc1.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;
hadc1.Init.Oversampling.OversamplingStopReset = ADC_REGOVERSAMPLING_CONTINUED_MODE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
multimode.Mode = ADC_MODE_INDEPENDENT;
if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
{
Error_Handler();
}
if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_92CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_ADC_Start(&hadc1) != HAL_OK)
{
Error_Handler();
}
}
void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(adcHandle->Instance==ADC1)
{
__HAL_RCC_ADC12_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC1_Init();
Disp_buf[4]='m';
Disp_buf[5]='V';
LCD_Init();
LCD_Print(24, 8,"STM32G431RB",TYPE16X16,TYPE8X16);
LCD_Print(36, 24, "Nucleo64",TYPE16X16,TYPE8X16);
while (1)
{
if (HAL_ADC_PollForConversion(&hadc1, 1) != HAL_OK)
{
Error_Handler();
}
uwConvertedValue = HAL_ADC_GetValue(&hadc1);
uwInputVoltage =uwConvertedValue * 3300;
uwInputVoltage = uwInputVoltage / 0xFFF0;
Disp_buf[0]=uwInputVoltage%10000/1000+0x30;
Disp_buf[1]=uwInputVoltage%1000/100+0x30;
Disp_buf[2]=uwInputVoltage%100/10+0x30;
Disp_buf[3]=uwInputVoltage%10+0x30;
LCD_Print(40, 48,Disp_buf,TYPE16X16,TYPE8X16);
}
}
运行结果如图。
评测过程发现NUCLEO_G431的硬件16位过采样ADC转换结果非常稳定,转换后未经任何软件滤波处理,显示的读数只在最后1位偶尔跳动1个数字。可见NUCLEO_G431确实是一款性能优异的STM32芯片。
感谢社区,感谢ST提供本次评测机会。
此内容由EEWORLD论坛网友wenyangzeng原创,如需转载或用于商业用途需征得作者同意并注明出处