慕容雪花 发表于 2025-1-11 23:10

【MCXA156开发板测评】-5-LPADC中断模式快速配置

本帖最后由 慕容雪花 于 2025-1-12 11:11 编辑

<p>上期测评:【MCXA156开发板测评】-4-LPI2C驱动OLED屏幕&nbsp;&nbsp;<a href="https://bbs.eeworld.com.cn/thread-1304186-1-1.html" target="_blank">https://bbs.eeworld.com.cn/thread-1304186-1-1.html</a></p>

<p>本次体验使用恩智浦MCUXPRESSO内置的CONFIGTOOL来配置LPADC的中断模式。LPADC的FIFO Control Register (FCTRL0)可以设置&ldquo;Watermark Level Selection&rdquo;。</p>

<p style="text-align: center;"></p>

<p>&nbsp;</p>

<p>当ADC转换结果数量大于设置的阈值FIFO 0 watermark后:</p>

<ul>
        <li>触发ISR(在FWMIE置位的情况下)</li>
        <li>或者进行DMA请求(在FWMDE置位的情况下)</li>
</ul>

<p style="text-align: center;"> &nbsp;</p>

<p>在IRQ服务程序中可以设置一个转换完成标志,方便在主程序中获取转换结果。</p>

<p style="text-align: center;"> &nbsp;</p>

<p>&nbsp;</p>

<p><strong><span style="font-size:18px;">一、CONFIGTOOL配置</span></strong></p>

<p>设置FIFO0WATERMARK为0:</p>

<p style="text-align: center;"> &nbsp;</p>

<p>选择中断源,并使能中断向量。</p>

<p style="text-align: center;"> &nbsp;</p>

<p><strong><span style="font-size:18px;">二、代码适配</span></strong></p>

<p>初始化代码:</p>

<pre>
<code>/***********************************************************************************************************************
* ADC1 initialization code
**********************************************************************************************************************/
/* clang-format off */
/* TEXT BELOW IS USED AS SETTING FOR TOOLS *************************************
instance:
- name: 'ADC1'
- type: 'lpadc'
- mode: 'LPADC'
- custom_name_enabled: 'false'
- type_id: 'lpadc_2.8.1'
- functional_group: 'BOARD_InitPeripherals'
- peripheral: 'ADC1'
- config_sets:
- fsl_lpadc:
    - lpadcConfig:
      - clockSource: 'AsynchronousFunctionClock'
      - clockSourceFreq: 'ClocksTool_DefaultInit'
      - enableInDozeMode: 'false'
      - conversionAverageMode: 'kLPADC_ConversionAverage8'
      - offsetCalibration: 'no'
      - autoCalibrate: 'true'
      - enableAnalogPreliminary: 'true'
      - powerUpDelay: '0x80'
      - referenceVoltageSource: 'kLPADC_ReferenceVoltageAlt3'
      - powerLevelMode: 'kLPADC_PowerLevelAlt4'
      - triggerPriorityPolicy: 'kLPADC_ConvPreemptImmediatelyNotAutoResumed'
      - enableConvPause: 'false'
      - convPauseDelay: '0'
      - FIFOWatermark: '0'
      - FIFOWatermarkDMA: 'false'
    - lpadcConvCommandConfig:
      - 0:
      - user_commandId: ''
      - commandId: '1'
      - chainedNextCommandNumber: '0'
      - sampleChannelMode: 'kLPADC_SampleChannelSingleEndSideA'
      - channelNumber: 'A.21'
      - enableAutoChannelIncrement: 'false'
      - loopCount: '0'
      - hardwareAverageMode: 'kLPADC_HardwareAverageCount1'
      - sampleTimeMode: 'kLPADC_SampleTimeADCK3'
      - hardwareCompareMode: 'kLPADC_HardwareCompareDisabled'
      - hardwareCompareValueHigh: '0'
      - hardwareCompareValueLow: '0'
      - conversionResoultuionMode: 'kLPADC_ConversionResolutionHigh'
      - enableWaitTrigger: 'false'
    - lpadcConvTriggerConfig:
      - 0:
      - user_triggerId: ''
      - triggerId: '0'
      - targetCommandId: '1'
      - delayPower: '0'
      - priority: 'false'
      - enableHardwareTrigger: 'false'
    - IRQ_cfg:
      - interrupt_type: 'kLPADC_FIFO0WatermarkInterruptEnable'
      - enable_irq: 'true'
      - adc_interrupt:
      - IRQn: 'ADC1_IRQn'
      - enable_interrrupt: 'enabled'
      - enable_priority: 'true'
      - priority: '0'
      - enable_custom_name: 'false'
* BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/
/* clang-format on */
const lpadc_config_t ADC1_config = {
.enableInDozeMode = false,
.conversionAverageMode = kLPADC_ConversionAverage8,
.enableAnalogPreliminary = true,
.powerUpDelay = 0x80UL,
.referenceVoltageSource = kLPADC_ReferenceVoltageAlt3,
.powerLevelMode = kLPADC_PowerLevelAlt4,
.triggerPriorityPolicy = kLPADC_ConvPreemptImmediatelyNotAutoResumed,
.enableConvPause = false,
.convPauseDelay = 0UL,
.FIFOWatermark = 0UL,
};
lpadc_conv_command_config_t ADC1_commandsConfig = {
{
    .sampleChannelMode = kLPADC_SampleChannelSingleEndSideA,
    .channelNumber = 21U,
    .chainedNextCommandNumber = 0,
    .enableAutoChannelIncrement = false,
    .loopCount = 0UL,
    .hardwareAverageMode = kLPADC_HardwareAverageCount1,
    .sampleTimeMode = kLPADC_SampleTimeADCK3,
    .hardwareCompareMode = kLPADC_HardwareCompareDisabled,
    .hardwareCompareValueHigh = 0UL,
    .hardwareCompareValueLow = 0UL,
    .conversionResolutionMode = kLPADC_ConversionResolutionHigh,
    .enableWaitTrigger = false
}
};
lpadc_conv_trigger_config_t ADC1_triggersConfig = {
{
    .targetCommandId = 1,
    .delayPower = 0UL,
    .priority = 1,
    .enableHardwareTrigger = false
}
};

static void ADC1_init(void) {
/* Initialize LPADC converter */
LPADC_Init(ADC1_PERIPHERAL, &amp;ADC1_config);
/* Perform auto calibration */
LPADC_DoAutoCalibration(ADC1_PERIPHERAL);
/* Configure conversion command 1. */
LPADC_SetConvCommandConfig(ADC1_PERIPHERAL, 1, &amp;ADC1_commandsConfig);
/* Configure trigger 0. */
LPADC_SetConvTriggerConfig(ADC1_PERIPHERAL, 0, &amp;ADC1_triggersConfig);
/* Interrupt vector ADC1_IRQn priority settings in the NVIC. */
NVIC_SetPriority(ADC1_IRQN, ADC1_IRQ_PRIORITY);
/* Enable interrupts from LPADC */
LPADC_EnableInterrupts(ADC1_PERIPHERAL, (kLPADC_FIFO0WatermarkInterruptEnable));
/* Enable interrupt ADC1_IRQN request in the NVIC */
EnableIRQ(ADC1_IRQN);
}</code></pre>

<p>设置转换完成标志位。</p>

<pre>
<code>/* ADC1_IRQn interrupt handler */
void ADC1_IRQHANDLER(void) {
/* Place your code here */
if (LPADC_GetConvResult(ADC1_PERIPHERAL, &amp;mLpadcResultConfigStruct))
{
          g_LpadcConversionCompletedFlag = true;
}

#if defined __CORTEX_M &amp;&amp; (__CORTEX_M == 4U)
    __DSB();
#endif
}</code></pre>

<p>在主程序中:</p>

<pre>
<code>      if(g_LpadcConversionCompletedFlag)
      {
              PRINTF("%d\r\n", ((mLpadcResultConfigStruct.convValue)));
              g_LpadcConversionCompletedFlag = false;
      }</code></pre>

<p><strong><span style="font-size:18px;">三、实物测试</span></strong></p>

<p>MIKROBUS P3_30(功能:LPADC1_A21)引脚接入模拟声音传感器。</p>

<p style="text-align: center;"> &nbsp;</p>

<p>传感器型号:DFROBOT SEN0487。</p>

<p>这是一款超小体积的MEMS麦克风。放大器增益为66,当没有检测到声音时,输出电压在1.5V左右浮动,在说话时通过ADC采样可以很容易看到声音的波形。兼容3.3V/5V,可以很轻松的用在各类主控上。引脚说明:</p>

<p style="text-align: center;"> &nbsp;</p>

<p style="text-align: center;">&nbsp;</p>

<p style="text-align: center;"> &nbsp;</p>

<p>&nbsp;</p>

<p>串口输出采集到的声音传感器数据并发送到上位机:</p>

<div style="text-align: center;"></div>

<p><strong><span style="font-size:18px;">四、总结</span></strong></p>

<p>使用恩智浦MCUXPRESSO以及MCUCONFIGTOOL非常方便的配置LPADC的各种工作模式与参数,自动化生成代码节省了大量的时间,方便快速上述ADC模块。当然LPADC还有低功耗模式等多个特点有待探索。</p>
页: [1]
查看完整版本: 【MCXA156开发板测评】-5-LPADC中断模式快速配置