S32K144之ADC
S32K144有两个12位ADC模块,分别是ADC0和ADC1。不同封装的ADC模块具有不同的通道数,下面通过表格列出:
1、ADC硬件触发源
PDB触发:默认设置PDB触发模式,两个PDB 为 两个ADC 产生触发和预触发(ADC 和 PDB 成对工作,如 PDB0触发 ADC0和 PDB1触发ADC1)。
TRGMUX触发:支持许多触发器源。参考手册中以LPIT为例子。LPIT最多支持4个通道,每个通道都有一个触发器和一个预触发器。
2、DMA支持ADC
手册中有说到可能ADC需要(4K采样/秒)进行连续采样,CPU会有很有负载,所以ADC支持DMA请求功能以获得更高的性能,ADC可以在转换完成时触发DMA。
3、ADC硬件通道交错
有几个特殊的ADC通道支持两个ADC之间的通道交错。例如ADC0_SE4和ADC1_SE14,这两个通道可以独立工作,但也可以进行如下图所示的硬件交错。在硬件交错模式下,可以同时用ADC0和ADC1对引脚PTB0上的信号进行采样。交错模式由SIM_CHIPCTL [ADC_INTERLEAVE_EN]位启用。
ADC0_SE4 和 ADC1_SE14 通道在 PTB0 引脚上交错
ADC0_SE5 和 ADC1_SE15 通道在 PTB1 引脚上交错;
ADC1_SE8 和 ADC0_SE8 通道在 PTB13 引脚上交错;
ADC1_SE9 和 ADC0_SE9 通道在 PTB14 引脚上交错。
4、ADC工程之采集电压值
下面来演示一下如何通过Processor Expert来配置ADC模块,示例为使用ADC1的通道14,复用到PTB0引脚,去采集3.3V电压。
首先我们从官方示例代码中导入一个ADC示例代码,然后打开PE组件的ADC模块,进行ADC模块选择,我这里配置成ADC1。
分别配置ADC1的时钟,频率,精度,触发条件以及是否使用DMA功能,如需多个通道,在配置list中添加相应的数量,这里我全部选择默认配置。
然后进行通道配置,是否需要中断使能以及使能相应的通道,我这里选择channel 14。
最后,我们把相应的通道复用到外部引脚,我选择了PTB0,然后单击如图3这个图标,生成相应的配置代码,可在Geberated Code文件夹中进行查看。
main.c的代码如下:
/*!
** @file main.c
** @version 01.00
** @brief
** Main module.
** This module contains user's application code.
*/
/*!
** @addtogroup main_module main module documentation
** @{
*/
/* MODULE main */
/* Including necessary module. Cpu.h contains other modules needed for compiling.*/
#include "Cpu.h"
volatile int exit_code = 0;
/* User includes (#include below this line is not maintained by Processor Expert) */
#include "printf.h"
#include "stdbool.h"
/*!
\brief The main function for the project.
\details The startup initialization sequence is the following:
* - startup asm routine
* - main()
*/
int main(void)
{
/* Write your local variable definition here */
uint16_t ADC_result = 0;
uint16_t i,j;
/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
#ifdef PEX_RTOS_INIT
PEX_RTOS_INIT(); /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of Processor Expert internal initialization. ***/
/* Write your code here */
/*
* 系统初始化
*/
CLOCK_SYS_Init(g_clockManConfigsArr,
CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr,
CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
/*
* 引脚初始化
*/
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
/*
* 串口初始化
*/
LPUART_DRV_Init(INST_LPUART1, &lpuart1_State, &lpuart1_InitConfig0);
/*
* DMA初始化
*/
EDMA_DRV_Init(&dmaController1_State,
&dmaController1_InitConfig0,
edmaChnStateArray,
edmaChnConfigArray,
EDMA_CONFIGURED_CHANNELS_COUNT);
/*
* ADC初始化
*/
ADC_DRV_ConfigConverter(INST_ADCONV1, &adConv1_ConvConfig0);
while(1)
{
/*
* ADC通道配置,软件触发
*/
ADC_DRV_ConfigChan(INST_ADCONV1,0, &adConv1_ChnConfig0);
/*
* 等待ADC转换完成
*/
ADC_DRV_WaitConvDone(INST_ADCONV1);
/*
* ADC转换延时
*/
for(i = 0; i<0xFFFF; i++)
{
for(j = 0; j < 5; j++);
}
/*
* ADC转换成功则利用串口输出ADC转换结果
*/
ADC_DRV_GetChanResult(INST_ADCONV1, 0, &ADC_result);
PRINTF("ADC result : %d\r\n",ADC_result);
}
/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
/*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
#ifdef PEX_RTOS_START
PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of RTOS startup code. ***/
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for(;;) {
if(exit_code != 0) {
break;
}
}
return exit_code;
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
/* END main */
/*!
** @}
*/
/*
** ###################################################################
**
** This file was created by Processor Expert 10.1 [05.21]
** for the Freescale S32K series of microcontrollers.
**
** ###################################################################
*/
通过串口打印出ADC采集到的电压值:
完整工程百度网盘链接:链接:https://pan.baidu.com/s/1cWGszQibulHaagmUuMG6Og 提取码:dkk6