配置了三种模式,为单次模式,连续模式和DMA传输
单次模式是调用一次采集函数,在指定的某一个AD端口采集,用到的时候在去采样
连续模式是连续转换多个通道,采用级联模式,采用16路AD通道的值
DMA传输时AD采样的值直接通过DMA传输到内存中,不需要要CPU干预,持续采样,放到内存,需要用到数据的时候去读取相应的存储区即可。
单次模式
//单次模式AD初始化
void Ad_Onechanneltime_Init(void)
{
EALLOW;
SysCtrlRegs.HISPCP.all = ADC_MODCLK;// HSPCLK = SYSCLKOUT/ADC_MODCLK
EDIS;
InitAdc(); // For this example, init the ADC
// Specific ADC setup for this example:
AdcRegs.ADCTRL1.bit.ACQ_PS = ADC_SHCLK; // Sequential mode: Sample rate = 1/[(2+ACQ_PS)*ADC //clock in ns]
// = 1/(3*40ns) =8.3MHz (for 150 MHz SYSCLKOUT)
// = 1/(3*80ns) =4.17MHz (for 100 MHz SYSCLKOUT)
// If Simultaneous mode enabled: Sample rate = 1/[(3+ACQ_PS)*ADC clock in ns]
AdcRegs.ADCTRL3.bit.ADCCLKPS = ADC_CKPS;
AdcRegs.ADCTRL1.bit.SEQ_CASC = 1; // 1 级联模式
AdcRegs.ADCTRL1.bit.CONT_RUN = 0; // 启动停止模式
AdcRegs.ADCTRL1.bit.SEQ_OVRD = 1; // Enable Sequencer override feature
AdcRegs.ADCMAXCONV.bit.MAX_CONV1 = 0x0; // convert and store in 1 results registers
DELAY_US(100);
}
//单通道单次转换0~7对应A0~A7,8~15对应B0~B7
Uint16 Ad_Get(u8 n)
{
switch (n)
{
case 0: AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0x0;break;
case 1: AdcRegs.ADCCHSELSEQ1.bit.CONV01 = 0x1;break;
case 2: AdcRegs.ADCCHSELSEQ1.bit.CONV02 = 0x2;break;
case 3: AdcRegs.ADCCHSELSEQ1.bit.CONV03 = 0x3;break;
case 4: AdcRegs.ADCCHSELSEQ2.bit.CONV04 = 0x4;break;
case 5: AdcRegs.ADCCHSELSEQ2.bit.CONV05 = 0x5;break;
case 6: AdcRegs.ADCCHSELSEQ2.bit.CONV06 = 0x6;break;
case 7: AdcRegs.ADCCHSELSEQ2.bit.CONV07 = 0x7;break;
case 8: AdcRegs.ADCCHSELSEQ3.bit.CONV08 = 0x8;break;
case 9: AdcRegs.ADCCHSELSEQ3.bit.CONV09 = 0x9;break;
case 10: AdcRegs.ADCCHSELSEQ3.bit.CONV10 = 0x0A;break;
case 11: AdcRegs.ADCCHSELSEQ3.bit.CONV11 = 0x0B;break;
case 12: AdcRegs.ADCCHSELSEQ4.bit.CONV12 = 0x0C;break;
case 13: AdcRegs.ADCCHSELSEQ4.bit.CONV13 = 0x0D;break;
case 14: AdcRegs.ADCCHSELSEQ4.bit.CONV14 = 0x0E;break;
case 15: AdcRegs.ADCCHSELSEQ4.bit.CONV15 = 0x0F;break;
default :break;
}
//Start SEQ1
AdcRegs.ADCTRL2.bit.SOC_SEQ1=1;
while(AdcRegs.ADCST.bit.INT_SEQ1 == 0);
AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1;
switch (n)
{
case 0: return ( (AdcRegs.ADCRESULT0)>>4);break;
case 1: return ( (AdcRegs.ADCRESULT1)>>4);break;
case 2: return ( (AdcRegs.ADCRESULT2)>>4);break;
case 3: return ( (AdcRegs.ADCRESULT3)>>4);break;
case 4: return ( (AdcRegs.ADCRESULT4)>>4);break;
case 5: return ( (AdcRegs.ADCRESULT5)>>4);break;
case 6: return ( (AdcRegs.ADCRESULT6)>>4);break;
case 7: return ( (AdcRegs.ADCRESULT7)>>4);break;
case 8: return ( (AdcRegs.ADCRESULT8)>>4);break;
case 9: return ( (AdcRegs.ADCRESULT9)>>4);break;
case 10: return ( (AdcRegs.ADCRESULT10)>>4);break;
case 11: return ( (AdcRegs.ADCRESULT11)>>4);break;
case 12: return ( (AdcRegs.ADCRESULT12)>>4);break;
case 13: return ( (AdcRegs.ADCRESULT13)>>4);break;
case 14: return ( (AdcRegs.ADCRESULT14)>>4);break;
case 15: return ( (AdcRegs.ADCRESULT15)>>4);break;
default: break;
}
return 0;
}