“打造自己的C2000 LaunchPad 项目文件“的基础上 实现 ADC
[复制链接]
#include "DSP28x_Project.h"
interrupt void adc_isr(void);
Uint16 LoopCount;
Uint16 ConversionCount;
Uint16 Voltage1[10];
Uint16 Voltage2[10];
//单通道多次转换
void main(void)
{
InitSysCtrl();
DINT; //关闭CPU中断
InitPieCtrl(); //初始化中断向量
IER = 0x0000; //禁止 CPU 中断
IFR = 0x0000; //清除 CPU 中断标志
InitPieVectTable(); //初始化中断向量表
EALLOW;
PieVectTable.ADCINT1 = &adc_isr;
EDIS;
InitAdc();
PieCtrlRegs.PIEIER1.bit.INTx1 = 1; //使能PIE模块中的 INT1.1 (ADCINT1)
IER |= M_INT1; //开CPU中断1,IER最低位置1,最低位为中断1使能
EINT; //使能全局中断
ERTM; //使能实时中断
LoopCount = 0;
ConversionCount = 0;
//配置 ADC
EALLOW;
AdcRegs.ADCCTL1.bit.INTPULSEPOS = 1; //中断脉冲在ADC结果锁存到结果寄存器的一个周期前产生
AdcRegs.INTSEL1N2.bit.INT1E = 1; //INTSEL1N2:中断选择1和2寄存器
AdcRegs.INTSEL1N2.bit.INT1CONT = 0; //禁止 ADCINT1 连续模式使能
AdcRegs.INTSEL1N2.bit.INT1SEL = 2; //ACINT1 的触发源为 EOS2
AdcRegs.ADCSOC0CTL.bit.CHSEL = 4; //SOC0 通道配置为 ADCINA4
AdcRegs.ADCSOC1CTL.bit.CHSEL = 4; //SOC1 通道配置为 ADCINA4
AdcRegs.ADCSOC2CTL.bit.CHSEL = 2; //SOC2 通道配置为 ADCINA2
AdcRegs.ADCSOC0CTL.bit.TRIGSEL = 5; //SOC0 触发源为 EPWM1A, 轮循转换 先SOC0 然后SOC1
AdcRegs.ADCSOC1CTL.bit.TRIGSEL = 5; //SOC1 触发源为 EPWM1A, 轮循转换 先SOC0 然后SOC1
AdcRegs.ADCSOC2CTL.bit.TRIGSEL = 5; //SOC2 触发源为 EPWM1A, 轮循转换 先SOC0 然后SOC1,最后SOC2
AdcRegs.ADCSOC0CTL.bit.ACQPS = 6; //设置 SOC0 采样保持时间为 7 个 ADC 时钟周期 (6+1个时钟周期)
AdcRegs.ADCSOC1CTL.bit.ACQPS = 6; //设置 SOC1 采样保持时间为 7 个 ADC 时钟周期 (6+1个时钟周期)
AdcRegs.ADCSOC2CTL.bit.ACQPS = 6; //设置 SOC2 采样保持时间为 7 个 ADC 时钟周期 (6+1个时钟周期)
EDIS;
//假定ePWM1的时钟已经在InitSysCtrl()中被启用
EPwm1Regs.ETSEL.bit.SOCAEN = 1; // Enable SOC on A group
EPwm1Regs.ETSEL.bit.SOCASEL = 4; // Select SOC from from CPMA on upcount
EPwm1Regs.ETPS.bit.SOCAPRD = 1; // Generate pulse on 1st event
EPwm1Regs.CMPA.half.CMPA = 0x0080; // Set compare A value
EPwm1Regs.TBPRD = 0xFFFF; // Set period for ePWM1
EPwm1Regs.TBCTL.bit.CTRMODE = 0; // count up and start
// 等待ADC中断
for(;;)
{
LoopCount++;
}
}
interrupt void adc_isr(void)
{
Voltage1[ConversionCount] = AdcResult.ADCRESULT1;
Voltage2[ConversionCount] = AdcResult.ADCRESULT2;
//如果已经记录了20次转换,则重新开始
if(ConversionCount == 9)
{
ConversionCount = 0;
}
else ConversionCount++;
AdcRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; //清除 ADCINT1 中断标志 为下一个 SOC 重新初始化
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; // Acknowledge interrupt to PIE
return;
}