DSP

【DSP】TMS320F28335的ADC模块

2019-07-13 10:43发布

一、功能说明

  • 12位内建采样保持的模数转换器
  • 模拟输入电平:0~3V
  • 16个转换通道
  • 最快转换时钟频率12.5MHz(奈奎斯特定则,25MHz最高能采样12.5MHz的信号)
  • 多触发源:软件、ePWM和GPIO
  • 两种采样模式:级联和双通道模式
软件设置流程 //1.ADC时钟使能 SysCtrlRegs.PCLKCR0.bit.ADCENCLK = 1;//在InitSysCtrl()函数里 //2.时钟分频 EALLOW; SysCtrlRegs.HISPCP.all = 0x3;//HSPCLK = SYSCLKOUT/2*HISPCP = 150/(2*3) = 25.0 MHz ADC_cal(); EDIS; AdcRegs.ADCTRL3.all = 0x00E0; // Power up bandgap/reference/ADC circuits DELAY_US(ADC_usDELAY); // Delay before converting ADC channels AdcRegs.ADCTRL3.bit.ADCCLKPS = 0x0;//Fclk = HSPCLK/(2*ADCCLKPS),但当ADCCLKPS=0时,Fclk = HSPCLK AdcRegs.ADCTRL1.bit.CPS = 0x0;//ADCCLK = Fclk / (CPS+1) //3.通道设置 //4.触发源设置 //5.中断源设置

二、模式说明

2.1 级联模式 //3.通道设置 AdcRegs.ADCTRL1.bit.SEQ_CASC = 0x1;//0-dual, 1-Cascaded AdcRegs.ADCMAXCONV.all = 0x000F; // Setup 16 conv's on SEQ AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0x0; // Setup ADCINA0 as 1st SEQ conv. AdcRegs.ADCCHSELSEQ1.bit.CONV01 = 0x1; // Setup ADCINA1 as 2nd SEQ conv. AdcRegs.ADCCHSELSEQ1.bit.CONV02 = 0x2; // Setup ADCINA2 as 3rd SEQ conv. AdcRegs.ADCCHSELSEQ1.bit.CONV03 = 0x3; // Setup ADCINA3 as 4th SEQ conv. AdcRegs.ADCCHSELSEQ2.bit.CONV04 = 0x4; // Setup ADCINA4 as 5th SEQ conv. AdcRegs.ADCCHSELSEQ2.bit.CONV05 = 0x5; // Setup ADCINA5 as 6th SEQ conv. AdcRegs.ADCCHSELSEQ2.bit.CONV06 = 0x6; // Setup ADCINA6 as 7th SEQ conv. AdcRegs.ADCCHSELSEQ2.bit.CONV07 = 0x7; // Setup ADCINA7 as 8th SEQ conv. AdcRegs.ADCCHSELSEQ3.bit.CONV08 = 0x8; // Setup ADCINB0 as 9th SEQ conv. AdcRegs.ADCCHSELSEQ3.bit.CONV09 = 0x9; // Setup ADCINB1 as 10th SEQ conv. AdcRegs.ADCCHSELSEQ3.bit.CONV10 = 0xA; // Setup ADCINB2 as 11th SEQ conv. AdcRegs.ADCCHSELSEQ3.bit.CONV11 = 0xB; // Setup ADCINB3 as 12th SEQ conv. AdcRegs.ADCCHSELSEQ4.bit.CONV12 = 0xC; // Setup ADCINB4 as 13th SEQ conv. AdcRegs.ADCCHSELSEQ4.bit.CONV13 = 0xD; // Setup ADCINB5 as 14th SEQ conv. AdcRegs.ADCCHSELSEQ4.bit.CONV14 = 0xE; // Setup ADCINB6 as 15th SEQ conv. AdcRegs.ADCCHSELSEQ4.bit.CONV15 = 0xF; // Setup ADCINB7 as 16th SEQ conv. 2.2 双通道模式 //3.通道设置 AdcRegs.ADCTRL1.bit.SEQ_CASC = 0x0;//0-dual, 1-Cascaded AdcRegs.ADCMAXCONV.all = 0x0077; // Setup 8 conv's on SEQ1 and 8 conv's on SEQ2 AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0x0; // Setup ADCINA0 as 1st SEQ1 conv. AdcRegs.ADCCHSELSEQ1.bit.CONV01 = 0x1; // Setup ADCINA1 as 2nd SEQ1 conv. AdcRegs.ADCCHSELSEQ1.bit.CONV02 = 0x2; // Setup ADCINA2 as 3rd SEQ1 conv. AdcRegs.ADCCHSELSEQ1.bit.CONV03 = 0x3; // Setup ADCINA3 as 4th SEQ1 conv. AdcRegs.ADCCHSELSEQ2.bit.CONV04 = 0x4; // Setup ADCINA4 as 5th SEQ1 conv. AdcRegs.ADCCHSELSEQ2.bit.CONV05 = 0x5; // Setup ADCINA5 as 6th SEQ1 conv. AdcRegs.ADCCHSELSEQ2.bit.CONV06 = 0x6; // Setup ADCINA6 as 7th SEQ1 conv. AdcRegs.ADCCHSELSEQ2.bit.CONV07 = 0x7; // Setup ADCINA7 as 8th SEQ1 conv. AdcRegs.ADCCHSELSEQ3.bit.CONV08 = 0x8; // Setup ADCINB0 as 1st SEQ2 conv. AdcRegs.ADCCHSELSEQ3.bit.CONV09 = 0x9; // Setup ADCINB1 as 2nd SEQ2 conv. AdcRegs.ADCCHSELSEQ3.bit.CONV10 = 0xA; // Setup ADCINB2 as 3rd SEQ2 conv. AdcRegs.ADCCHSELSEQ3.bit.CONV11 = 0xB; // Setup ADCINB3 as 4th SEQ2 conv. AdcRegs.ADCCHSELSEQ4.bit.CONV12 = 0xC; // Setup ADCINB4 as 5th SEQ2 conv. AdcRegs.ADCCHSELSEQ4.bit.CONV13 = 0xD; // Setup ADCINB5 as 6th SEQ2 conv. AdcRegs.ADCCHSELSEQ4.bit.CONV14 = 0xE; // Setup ADCINB6 as 7th SEQ2 conv. AdcRegs.ADCCHSELSEQ4.bit.CONV15 = 0xF; // Setup ADCINB7 as 8th SEQ2 conv.

三、触发源

//4.触发源设置 AdcRegs.ADCTRL2.bit.SOC_SEQ1 = 1;//软触发(用于级联、双通道1#) AdcRegs.ADCTRL2.bit.SOC_SEQ2 = 1;//软触发(用于双通道2#) AdcRegs.ADCTRL2.bit.EPWM_SOCA_SEQ1 = 1;//ePWM触发(用于双通道1#) AdcRegs.ADCTRL2.bit.EPWM_SOCB_SEQ2 = 1;//ePWM触发(用于双通道2#) AdcRegs.ADCTRL2.bit.EPWM_SOCB_SEQ = 1;//ePWM触发(用于级联)

四、中断源

//5.中断源设置 AdcRegs.ADCTRL2.bit.INT_ENA_SEQ1 = 1; //用于级联、双通道1# AdcRegs.ADCTRL2.bit.INT_ENA_SEQ2 = 1; //双通道2#

五、接收转换结果

5.1 中断式 /* * AD的外部中断程序【中等优先级】 * 中断触发信号:EPWM */ interrupt void ADC_ISR(void) { Voltage[0] = AdcRegs.ADCRESULT0 >>4; Voltage[1] = AdcRegs.ADCRESULT1 >>4; Voltage[2] = AdcRegs.ADCRESULT2 >>4; Voltage[3] = AdcRegs.ADCRESULT3 >>4; Voltage[4] = AdcRegs.ADCRESULT4 >>4; Voltage[5] = AdcRegs.ADCRESULT5 >>4; Voltage[6] = AdcRegs.ADCRESULT6 >>4; Voltage[7] = AdcRegs.ADCRESULT7 >>4; Voltage[8] = AdcRegs.ADCRESULT8 >>4; Voltage[9] = AdcRegs.ADCRESULT9 >>4; Voltage[10] = AdcRegs.ADCRESULT10 >>4; Voltage[11] = AdcRegs.ADCRESULT11 >>4; Voltage[12] = AdcRegs.ADCRESULT12 >>4; Voltage[13] = AdcRegs.ADCRESULT13 >>4; Voltage[14] = AdcRegs.ADCRESULT14 >>4; Voltage[15] = AdcRegs.ADCRESULT15 >>4; // Reinitialize for next ADC sequence AdcRegs.ADCTRL2.bit.RST_SEQ1 = 1; // Reset SEQ1 AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1; // Clear INT SEQ1 bit PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; // Acknowledge interrupt to PIE return; } 5.2 查询式 while(AdcRegs.ADCST.bit.INT_SEQ1 == 0); test[0] = AdcRegs.ADCRESULT0 >>4; test[1] = AdcRegs.ADCRESULT1 >>4; test[2] = AdcRegs.ADCRESULT2 >>4; test[3] = AdcRegs.ADCRESULT3 >>4; test[4] = AdcRegs.ADCRESULT4 >>4; test[5] = AdcRegs.ADCRESULT5 >>4; test[6] = AdcRegs.ADCRESULT6 >>4; test[7] = AdcRegs.ADCRESULT7 >>4; test[8] = AdcRegs.ADCRESULT8 >>4; test[9] = AdcRegs.ADCRESULT9 >>4; test[10] = AdcRegs.ADCRESULT10 >>4; test[11] = AdcRegs.ADCRESULT11 >>4; test[12] = AdcRegs.ADCRESULT12 >>4; test[13] = AdcRegs.ADCRESULT13 >>4; test[14] = AdcRegs.ADCRESULT14 >>4; test[15] = AdcRegs.ADCRESULT15 >>4; AdcRegs.ADCTRL2.bit.RST_SEQ1 = 1;// Reset SEQ1 AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1;

六、其他设置

6.1 运行模式
运行模式分为start-stop和continuous两种模式,通过以下代码配置: AdcRegs.ADCTRL1.bit.CONT_RUN = 0x0; //0-start stop mode; 1-Continuous conversion mode start-stop模式下,AD转换结束后等待触发信号,才开始进行下一次的AD转换;
continuous模式下,AD转换结束后无需等待触发信号,立即开始下一次的AD转换。
6.2 SOC脉宽设置 AdcRegs.ADCTRL1.bit.ACQ_PS = 0xF;//Acquisition window size SOC脉宽是(ACQ_PS + 1)倍的ADCLK周期。
6.3 复位启动 AdcRegs.ADCTRL1.bit.SEQ_OVRD = 0x0; 仅用在continuous模式里面,如果该位为0,则新的SOC来时从复位状态启动,如SEQ1和级联模式下,重新从CONV00开始;如果该位为1,则不会从复位状态开始,直接从现在的转换序列开始,直到最后停止。

七、疑问

  1. 什么时候触发中断?
    答:当SEQ_CNTR = 0时,INT_SEQ = 1,且产生中断。否则,INT_SEQ = 0。
  2. ePWM触发双通道ADC,只能转换一个通道,什么原因?
    答:ACQ_PS的值太大,改小后,触发脉冲变小,可实现双通道。