【MCXN947开发板测评】A/D数据采集与应用
[复制链接]
MCXN947配有 2个16位 ADC,用它可以实现高精度的模拟量数据采集。
对A/D数据采集功能的采集结果如图1所示,所输入的模拟信号由 J8接口的28号引脚 (ADC0_A2)输入。
在完成一次数据采集后,需在发送一个字符后才能获得一个新的数据采集值。
图1 随机采集
图2 极值采集
此外,在进行数值变换的情况下,还可得到检测的电压值,见图3所示。
图3 显示电压值
在按图4进行连接的情况下,实现数据检测与显示的主程序是:
int main(void)
{
lpadc_config_t mLpadcConfigStruct;
lpadc_conv_trigger_config_t mLpadcTriggerConfigStruct;
lpadc_conv_command_config_t mLpadcCommandConfigStruct;
lpadc_conv_result_t mLpadcResultConfigStruct;
vref_config_t vrefConfig;
CLOCK_SetClkDiv(kCLOCK_DivFlexcom4Clk, 1U);
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
CLOCK_SetClkDiv(kCLOCK_DivAdc0Clk, 1U);
CLOCK_AttachClk(kFRO_HF_to_ADC0);
SPC_EnableActiveModeAnalogModules(DEMO_SPC_BASE, kSPC_controlVref);
VREF_GetDefaultConfig(&vrefConfig);
vrefConfig.bufferMode = kVREF_ModeBandgapOnly;
VREF_Init(DEMO_VREF_BASE, &vrefConfig);
BOARD_InitPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
Lcd_config();
Lcd_Init();
LCD_Clear(RED);
LCD_ShowString(20,10,"MCXN947",YELLOW);
LCD_ShowString(20,30,"A/D Test",YELLOW);
LCD_ShowString(20,50,"ADC=",YELLOW);
LPADC_GetDefaultConfig(&mLpadcConfigStruct);
mLpadcConfigStruct.enableAnalogPreliminary = true;
#if defined(DEMO_LPADC_VREF_SOURCE)
mLpadcConfigStruct.referenceVoltageSource = DEMO_LPADC_VREF_SOURCE;
#endif
#if defined(FSL_FEATURE_LPADC_HAS_CTRL_CAL_AVGS) && FSL_FEATURE_LPADC_HAS_CTRL_CAL_AVGS
mLpadcConfigStruct.conversionAverageMode = kLPADC_ConversionAverage128;
#endif
LPADC_Init(DEMO_LPADC_BASE, &mLpadcConfigStruct);
#if defined(FSL_FEATURE_LPADC_HAS_CTRL_CALOFSMODE) && FSL_FEATURE_LPADC_HAS_CTRL_CALOFSMODE
LPADC_SetOffsetCalibrationMode(DEMO_LPADC_BASE, DEMO_LPADC_OFFSET_CALIBRATION_MODE);
#endif #if defined(FSL_FEATURE_LPADC_HAS_CTRL_CALOFS) && FSL_FEATURE_LPADC_HAS_CTRL_CALOFS
#if defined(DEMO_LPADC_DO_OFFSET_CALIBRATION) && DEMO_LPADC_DO_OFFSET_CALIBRATION
LPADC_DoOffsetCalibration(DEMO_LPADC_BASE);
#else
#if defined(FSL_FEATURE_LPADC_HAS_OFSTRIM) && FSL_FEATURE_LPADC_HAS_OFSTRIM
#if defined(FSL_FEATURE_LPADC_OFSTRIM_COUNT) && (FSL_FEATURE_LPADC_OFSTRIM_COUNT == 2U)
LPADC_SetOffsetValue(DEMO_LPADC_BASE,DEMO_LPADC_OFFSET_VALUE_A, DEMO_LPADC_OFFSET_VALUE_B);
#elif defined(FSL_FEATURE_LPADC_OFSTRIM_COUNT) && (FSL_FEATURE_LPADC_OFSTRIM_COUNT == 1U)
LPADC_SetOffsetValue(DEMO_LPADC_BASE, DEMO_LPADC_OFFSET_VALUE);
#endif
#else
if (DEMO_LPADC_OFFSET_CALIBRATION_MODE == kLPADC_OffsetCalibration12bitMode)
{
LPADC_SetOffset12BitValue(DEMO_LPADC_BASE,DEMO_LPADC_OFFSET_VALUE_A, DEMO_LPADC_OFFSET_VALUE_B);
}
else
{
LPADC_SetOffset16BitValue(DEMO_LPADC_BASE,DEMO_LPADC_OFFSET_VALUE_A, DEMO_LPADC_OFFSET_VALUE_B);
}
#endif
#endif
#endif
#if defined(FSL_FEATURE_LPADC_HAS_CTRL_CAL_REQ) && FSL_FEATURE_LPADC_HAS_CTRL_CAL_REQ
LPADC_DoAutoCalibration(DEMO_LPADC_BASE);
#endif
#if (defined(FSL_FEATURE_LPADC_HAS_CFG_CALOFS) && FSL_FEATURE_LPADC_HAS_CFG_CALOFS)
LPADC_DoAutoCalibration(DEMO_LPADC_BASE);
#endif
LPADC_GetDefaultConvCommandConfig(&mLpadcCommandConfigStruct);
mLpadcCommandConfigStruct.channelNumber = DEMO_LPADC_USER_CHANNEL;
#if defined(DEMO_LPADC_USE_HIGH_RESOLUTION) && DEMO_LPADC_USE_HIGH_RESOLUTION
mLpadcCommandConfigStruct.conversionResolutionMode = kLPADC_ConversionResolutionHigh;
#endif
LPADC_SetConvCommandConfig(DEMO_LPADC_BASE,DEMO_LPADC_USER_CMDID, &mLpadcCommandConfigStruct);
LPADC_GetDefaultConvTriggerConfig(&mLpadcTriggerConfigStruct);
mLpadcTriggerConfigStruct.targetCommandId = DEMO_LPADC_USER_CMDID;
mLpadcTriggerConfigStruct.enableHardwareTrigger = false;
LPADC_SetConvTriggerConfig(DEMO_LPADC_BASE, 0U, &mLpadcTriggerConfigStruct);
#if defined(FSL_FEATURE_LPADC_HAS_CMDL_CSCALE) && FSL_FEATURE_LPADC_HAS_CMDL_CSCALE
if (kLPADC_SampleFullScale == mLpadcCommandConfigStruct.sampleScaleMode)
{
PRINTF("Full channel scale (Factor of 1).\r\n");
}
else if (kLPADC_SamplePartScale == mLpadcCommandConfigStruct.sampleScaleMode)
{
PRINTF("Divided input voltage signal. (Factor of 30/64).\r\n");
}
#endif
while (1)
{
LPADC_DoSoftwareTrigger(DEMO_LPADC_BASE, 1U); #if (defined(FSL_FEATURE_LPADC_FIFO_COUNT) && (FSL_FEATURE_LPADC_FIFO_COUNT == 2U))
while (!LPADC_GetConvResult(DEMO_LPADC_BASE, &mLpadcResultConfigStruct, 0U))
#else
while (!LPADC_GetConvResult(DEMO_LPADC_BASE, &mLpadcResultConfigStruct))
#endif
LCD_ShowNum(52,50,((mLpadcResultConfigStruct.convValue) >> g_LpadcResultShift),4,YELLOW);
delay();
}
}
经程序的编译和下载,其显示效果如图5所示。
图4 电路连接
图5 显示效果
|