赞
踩
开发板S32K144+S32DS+JLINK,裸机版的程序编写
S32K14x包含两个12位ADC模块,ADC0和ADC1
注意:在使用TRGMUX时,只有LPIT支持预触发器。对于其他外设,需要使用SIM_ADCOPT[ADCxSWPRETRG]配置软件预触发器
也是建议的触发方式。一个ADC和一个PDB作为一对:PDB0-ADC0, PDB1-ADC1。PDB0和PDB1的触发源可以分别通过TRGMUX_PDB0和TRGMUX_PDB1配置。
这里我们以PDB0-ADC0为例来指定触发方案。
下图说明了使用PDB触发ADC的典型案例:
LPIT最多支持4个通道,每个通道都有一个触发器和一个预触发器。
请注意对于除PDB和LPIT之外的触发器源,需要软件提供ADC预触发器。
可以选择触发启用和触发的任意组合;它们是相互独立的。但是在它被选中之后,就不能动态地更改它了。
定义了更改触发器和启用源(触发器和预触发器源)的步骤。改变触发源可以通过以下两种方法中的任何一种随时完成。
请注意如果不遵循上述限制,则可能不会报告此流程错过的触发器
无
SC1A用于软件和硬件触发模式的操作。
Field | Name | Description |
0-5 | ADCH | 输入通道选择 |
6 | AIEN | 转换完成中断使能 |
7 | COCO | 转换完成标志 |
配置寄存器1 (CFG1)选择操作方式、时钟源、时钟分频。
Field | Name | Description |
0-1 | ADICLK | 输入时钟选择 |
2-3 | MODE | 选择ADC的分辨率 |
5-6 | ADIV | 输入时钟分频 |
8 | CLRLTRG | 清除所有锁触发器 |
CFG2 (Configuration Register 2)在长采样模式下选择长采样时间
Field | Name | Description |
0-7 | SMPLTS | 采样周期选择 |
数据结果寄存器(Rn)包含通道ADC转换的结果,下表描述了不同的数据结果寄存器模式的操作。
Field | Name | Description |
0-11 | D | 数据结果 |
比较值寄存器(CV1和CV2)包含一个用于比较的比较值
Field | Name | Description |
0-15 | CV | 比较值 |
状态和控制寄存器2 (SC2)包含ADC模块的转换激活、硬件/软件触发选择、比较功能和电压参考选择
Field | Name | Description |
0-1 | REFSEL | 参考电压选择 |
2 | DMAEN | DMA使能 |
3 | ACREN | 配置compare函数,检查被监视的输入的转换结果是否在ACFGT值决定的CV1和CV2的范围之内或之外。 |
4 | ACFGT | 配置比较功能,根据ACREN的值对比CV1和CV2的转换结果 |
5 | ACFE | 比较功能使能 |
6 | ADTRG | 转换触发选择 |
7 | ADACT | 转换中标记位 |
13-14 | TRGPRNUM | 正在工作的触发器编号 |
16-19 | TRGSTLAT | 触发器状态 |
24-27 | TRGSTERR | 多路触发器请求出错 |
对比模式如下:
状态和控制寄存器3 (SC3)控制校准、连续转换、以及ADC模块的硬件平均功能
Field | Name | Description |
0-1 | AVGS | 硬件平均选择 |
2 | AVGE | 硬件平均使能 |
3 | ADCO | 连续转换模式 |
4 | CAL | ADC校准功能 |
基偏移寄存器(BASE_OFS)包含校准算法用于确定偏移校准值(OFS)的偏移值。
Field | Name | Description |
0-7 | BA_OFS | 基线偏移误差修正值 |
ADC偏移校正寄存器(OFS)包含校准生成的偏移误差校正值(OFS)
Field | Name | Description |
0-15 | OFS | 偏移误差值 |
ADC用户偏移校正寄存器(USR_OFS)包含用户定义的偏移量
Field | Name | Description |
0-7 | USR_OFS | 用户偏移误差修正值 |
ADC X偏移校正寄存器(XOFS)包含转换结果错误校正算法中使用的X偏移量
Field | Name | Description |
0-5 | XOFS | X偏移误差修正值 |
ADC Y偏移校正寄存器(YOFS)包含转换结果错误校正算法中使用的Y偏移量
Field | Name | Description |
0-5 | YOFS | Y偏移误差修正值 |
增益寄存器(G)包含整个转换的增益误差校正
Field | Name | Description |
0-10 | G | 增益误差调整因子的整体转换 |
用户增益寄存器(G)包含用户增益误差校正
Field | Name | Description |
0-9 | UG | 用户增益误差调整因子 |
通用校准值寄存器(CLPx)包含由校准函数生成的校准信息
Field | Name | Description |
0-6 | CLPS | 校准值 |
Field | Name | Description |
0-9 | CLPS3 | 校准值 |
Field | Name | Description |
0-9 | CLP2 | 校准值 |
Field | Name | Description |
0-9 | CLP1 | 校准值 |
Field | Name | Description |
0-9 | CLP0 | 校准值 |
Field | Name | Description |
0-6 | CLPX | 校准值 |
Field | Name | Description |
0-6 | CLP9 | 校准值 |
Field | Name | Description |
0-3 | CLPS_OFS | 电容偏移校正值 |
Field | Name | Description |
0-3 | CLP3_OFS | 电容偏移校正值 |
Field | Name | Description |
0-3 | CLP2_OFS | 电容偏移校正值 |
Field | Name | Description |
0-3 | CLP1_OFS | 电容偏移校正值 |
Field | Name | Description |
0-3 | CLP0_OFS | 电容偏移校正值 |
Field | Name | Description |
0-11 | CLPX_OFS | 电容偏移校正值 |
Field | Name | Description |
0-11 | CLP9_OFS | 电容偏移校正值 |
SC1A用于软件和硬件触发模式的操作。
Field | Name | Description |
0-5 | ADCH | 输入通道选择 |
6 | AIEN | 转换完成中断使能 |
7 | COCO | 转换完成标记 |
数据结果寄存器(Rn)包含通道ADC转换的结果
Field | Name | Description |
0-11 | D | 数据结果 |
此寄存器是那选择ADC触发器,在SIM章节下
Field | Name | Description |
0 | ADC0TRGSET | ADC0触发器选择 |
1-3 | ADC0SWPRETRG | ADC0软件预触发源 |
4-5 | ADC0PRETRGSEL | ADC0预触发源选择 |
ADC1从bit 8开始,内容一样
此及寄存器是触发器输入源的选择,其中
Field | Name | Description |
| SELx | 触发器输入源选择 |
31 | LK | TRGMUX寄存器上锁 |
其中SELx定义为:
再后面全是保留位
-
- #include "S32K144.h"
- #include "led.h"
- #include "key.h"
- #include "uart.h"
- #include "clocks.h"
- #include "lptmr.h"
- #include "lpit.h"
- #include "systick.h"
- #include "rtc.h"
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "ftm.h"
- #include "pdb.h"
-
- uint32_t ADC_RawResult;
- uint16_t ADC_mVResult;
-
- void adc_software_trigger(void)
- {
- PCC->PCCn[PCC_ADC0_INDEX] &=~ PCC_PCCn_CGC_MASK; /* Disable clock to change PCS */
- PCC->PCCn[PCC_ADC0_INDEX] |= PCC_PCCn_PCS(1); /* PCS=1: Select SOSCDIV2 */
- PCC->PCCn[PCC_ADC0_INDEX] |= PCC_PCCn_CGC_MASK; /* Enable bus clock in ADC */
-
- ADC0->SC1[0] |= ADC_SC1_ADCH_MASK; /* ADCH=1F: Module is disabled for conversions */
- /* AIEN=0: Interrupts are disabled */
- ADC0->CFG1 |= ADC_CFG1_ADIV_MASK
- | ADC_CFG1_MODE(1); /* ADICLK=0: Input clk=ALTCLK1=SOSCDIV2 */
- /* ADIV=0: Prescaler=1 */
- /* MODE=1: 12-bit conversion */
- ADC0->CFG2 = ADC_CFG2_SMPLTS(12); /* SMPLTS=12(default): sample time is 13 ADC clks */
- ADC0->SC2 = 0x00000000; /* ADTRG=0: SW trigger */
- /* ACFE,ACFGT,ACREN=0: Compare func disabled */
- /* DMAEN=0: DMA disabled */
- /* REFSEL=0: Voltage reference pins= VREFH, VREEFL */
- ADC0->SC3 = 0x00000000; /* CAL=0: Do not start calibration sequence */
- /* ADCO=0: One conversion performed */
- /* AVGE,AVGS=0: HW average function disabled */
- while(1){
- /* Initiate new conversion by writing to ADC0_SC1A(ADCH) */
- ADC0->SC1[0] = ADC_SC1_ADCH(2); /* ADCH = 2: External channel 2 as input */
- /* Wait for latest conversion to complete */
- while(((ADC0->SC1[0] & ADC_SC1_COCO_MASK)>>ADC_SC1_COCO_SHIFT) == 0);
- ADC_RawResult = ADC0->R[0]; /* Read ADC Data Result A (ADC0_RA) */
- ADC_mVResult = (ADC_RawResult * 5000) / (1<<12); /* Convert to mV (@VREFH = 5V) */
- }
- }
- void adc_pdb_trigger(void)
- {
- PCC->PCCn[PCC_ADC0_INDEX] &=~ PCC_PCCn_CGC_MASK; /* Disable clock to change PCS */
- PCC->PCCn[PCC_ADC0_INDEX] |= PCC_PCCn_PCS(1); /* PCS=1: Select SOSCDIV2 */
- PCC->PCCn[PCC_ADC0_INDEX] |= PCC_PCCn_CGC_MASK; /* Enable bus clock in ADC */
- /******************************************************
- * Initialize ADC0:
- * External channel 2, hardware trigger,
- * single conversion, 12-bit resolution
- *
- * NOTE: ADC0->SC1[4] corresponds to ADC0_SC1E register
- ******************************************************/
- ADC0->SC1[4] |= ADC_SC1_ADCH_MASK; /* ADCH=1F: Module is disabled for conversions */
- ADC0->CFG1 |= ADC_CFG1_ADIV_MASK
- | ADC_CFG1_MODE(1); /* ADICLK=0: Input clk=ALTCLK1=SOSCDIV2 */
- ADC0->CFG2 = ADC_CFG2_SMPLTS(12); /* SMPLTS=12(default): sample time is 13 ADC clks */
- ADC0->SC2 = ADC_SC2_ADTRG(1); /* ADTRG = 1: HW trigger */
- ADC0->SC1[4] = ADC_SC1_ADCH(2); /* ADCH = 2: External channel 2 as ADC0 input */
- ADC0->SC3 = 0x00000000; /* CAL=0: Do not start calibration sequence */
- /* ADCO=0: One conversion performed */
- /* AVGE,AVGS=0: HW average function disabled */
- /************************************************
- * Initialize PDB0:
- * 1 second period, continuous mode
- * PDB0_CH0 pre-trigger 4 output enabled
- ************************************************/
- PCC->PCCn[PCC_PDB0_INDEX] |= PCC_PCCn_CGC_MASK; /* Enable bus clock in PDB */
- PDB0->SC = PDB_SC_PRESCALER(6) /* PRESCALER = 6: clk divided by (64 x Mult factor) */
- | PDB_SC_TRGSEL(15) /* TRGSEL = 15: Software trigger selected */
- | PDB_SC_MULT(3) /* MULT = 3: Multiplication factor is 40 */
- | PDB_SC_CONT_MASK; /* CONT = 1: Enable operation in continuous mode */
- /* S32K 参考手册 1161页有写PDBn CHn对应的ADCn_SC1寄存器 */
- PDB0->MOD = 15625;
- PDB0->CH[0].C1 = (PDB_C1_TOS(0x10) /* TOS = 10h: Pre-trigger 4 asserts with DLY match */
- | PDB_C1_EN(0x10)); /* EN = 10h: Pre-trigger 4 enabled */
- PDB0->CH[0].DLY[4] = 15625; /* Delay set to half the PDB period = 9375 */
- PDB0->SC |= PDB_SC_PDBEN_MASK | PDB_SC_LDOK_MASK; /* Enable PDB. Load MOD and DLY */
- PDB0->SC |= PDB_SC_SWTRIG_MASK; /* Single initial PDB trigger */
- while(1){
- /* Wait for latest conversion to complete */
- while(((ADC0->SC1[4] & ADC_SC1_COCO_MASK)>>ADC_SC1_COCO_SHIFT) == 0);
- ADC_RawResult = ADC0->R[4]; /* Read ADC Data Result E (ADC0_RE) */
- ADC_mVResult = (ADC_RawResult * 5000) / (1<<12); /* Convert to mV (@VREFH = 5V) */
- }
- }
- void adc_trgmux_trigger(void)
- {
- PCC->PCCn[PCC_ADC0_INDEX] &=~ PCC_PCCn_CGC_MASK; /* Disable clock to change PCS */
- PCC->PCCn[PCC_ADC0_INDEX] |= PCC_PCCn_PCS(1); /* PCS=1: Select SOSCDIV2 */
- PCC->PCCn[PCC_ADC0_INDEX] |= PCC_PCCn_CGC_MASK; /* Enable bus clock in ADC */
-
- ADC0->SC1[0] |= ADC_SC1_ADCH_MASK; /* ADCH=1F: Module is disabled for conversions */
- ADC0->CFG1 |= ADC_CFG1_ADIV_MASK
- | ADC_CFG1_MODE(1); /* ADICLK=0: Input clk=ALTCLK1=SOSCDIV2 */
- ADC0->CFG2 = ADC_CFG2_SMPLTS(12); /* SMPLTS=12(default): sample time is 13 ADC clks */
- ADC0->SC2 = ADC_SC2_ADTRG(1); /* ADTRG = 1: HW trigger */
- ADC0->SC1[0] = ADC_SC1_ADCH(12); /* ADCH = 12: External channel 12 as ADC0 input */
- ADC0->SC3 = 0x00000000; /* CAL=0: Do not start calibration sequence */
- /* ADCO=0: One conversion performed */
- /* AVGE,AVGS=0: HW average function disabled */
- /************************************************
- * Configure pin PTB5 as TRGMUX_IN0
- ************************************************/
- PCC->PCCn[PCC_PORTA_INDEX] = PCC_PCCn_CGC_MASK; /* Enable clock gate for PORTB */
- PORTA->PCR[6] = PORT_PCR_MUX(6); /* Mux = 6: PTB5 as TRGMUX_IN0 */
- /* Select TRGMUX_IN0 as ADC0 Trigger Mux input source 0 */
- TRGMUX->TRGMUXn[TRGMUX_ADC0_INDEX] = TRGMUX_TRGMUXn_SEL0(2U);
-
- /************************************************
- * SIM Configurations for ADC triggering:
- * Pre-trigger source: Software pre-trigger
- * Trigger select: TRGMUX output
- ***********************************************/
- SIM->ADCOPT = SIM_ADCOPT_ADC0PRETRGSEL(2) /* ADC0PRETRGSEL = 10b: Software pretrigger */
- | SIM_ADCOPT_ADC0SWPRETRG(4) /* ADC0SWPRETRG = 100b: SW Pre-trigger 0 */
- | SIM_ADCOPT_ADC0TRGSEL(1); /* ADC0TRGSEL = 1: TRGMUX output as trigger */
- while(1){
- /* Wait for latest conversion to complete */
- while(((ADC0->SC1[0] & ADC_SC1_COCO_MASK)>>ADC_SC1_COCO_SHIFT) == 0);
- ADC_RawResult = ADC0->R[0]; /* Read ADC Data Result E (ADC0_RE) */
- ADC_mVResult = (ADC_RawResult * 5000) / (1<<12); /* Convert to mV (@VREFH = 5V) */
- }
- }
- int main(void)
- {
- WDOG_disable(); /* Disable WDOG */
- SOSC_init_8MHz(); /* Initialize system oscilator for 8 MHz xtal */
- SPLL_init_160MHz(); /* Initialize SPLL to 160 MHz with 8 MHz SOSC */
- NormalRUNmode_80MHz(); /* Init clocks: 80 MHz sysclk & core, 40 MHz bus, 20 MHz flash */
- adc_pdb_trigger();
- while(1){
-
- }
- return 0;
- }
此篇是软件触发->pdb触发->pdb触发in back-to-back mode->TRGMUX触发->LPIT触发
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。