【MCXA156开发板测评】-5-LPI2C中断模式快速配置
[复制链接]
本帖最后由 慕容雪花 于 2025-1-11 23:37 编辑
上期测评:【MCXA156开发板测评】-4-LPI2C驱动OLED屏幕 https://bbs.eeworld.com.cn/thread-1304186-1-1.html
本次体验使用恩智浦MCUXPRESSO内置的CONFIGTOOL来配置LPI2C3的中断模式。LPADC的FIFO Control Register (FCTRL0)可以设置“Watermark Level Selection”。
当ADC转换结果数量大于设置的阈值FIFO 0 watermark后:
- 触发ISR(在FWMIE置位的情况下)
- 或者进行DMA请求(在FWMDE置位的情况下)
在IRQ服务程序中可以设置一个转换完成标志,方便在主程序中获取转换结果。
一、CONFIGTOOL配置
设置FIFO0WATERMARK为0:
选择中断源,并使能中断向量。
二、代码适配
初始化代码:
/***********************************************************************************************************************
* 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[1] = {
{
.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[1] = {
{
.targetCommandId = 1,
.delayPower = 0UL,
.priority = 1,
.enableHardwareTrigger = false
}
};
static void ADC1_init(void) {
/* Initialize LPADC converter */
LPADC_Init(ADC1_PERIPHERAL, &ADC1_config);
/* Perform auto calibration */
LPADC_DoAutoCalibration(ADC1_PERIPHERAL);
/* Configure conversion command 1. */
LPADC_SetConvCommandConfig(ADC1_PERIPHERAL, 1, &ADC1_commandsConfig[0]);
/* Configure trigger 0. */
LPADC_SetConvTriggerConfig(ADC1_PERIPHERAL, 0, &ADC1_triggersConfig[0]);
/* 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);
}
设置转换完成标志位。
/* ADC1_IRQn interrupt handler */
void ADC1_IRQHANDLER(void) {
/* Place your code here */
if (LPADC_GetConvResult(ADC1_PERIPHERAL, &mLpadcResultConfigStruct))
{
g_LpadcConversionCompletedFlag = true;
}
#if defined __CORTEX_M && (__CORTEX_M == 4U)
__DSB();
#endif
}
在主程序中:
if(g_LpadcConversionCompletedFlag)
{
PRINTF("%d\r\n", ((mLpadcResultConfigStruct.convValue)));
g_LpadcConversionCompletedFlag = false;
}
三、实物测试
MIKROBUS P3_30(功能:LPADC1_A21)引脚接入模拟声音传感器。
传感器型号:DFROBOT SEN0487。
这是一款超小体积的MEMS麦克风。放大器增益为66,当没有检测到声音时,输出电压在1.5V左右浮动,在说话时通过ADC采样可以很容易看到声音的波形。兼容3.3V/5V,可以很轻松的用在各类主控上。引脚说明:
串口输出采集到的声音传感器数据并发送到上位机:
四、总结
使用恩智浦MCUXPRESSO以及MCUCONFIGTOOL非常方便的配置LPADC的各种工作模式与参数,自动化生成代码节省了大量的时间,方便快速上述ADC模块。当然LPADC还有低功耗模式等多个特点有待探索。
|