// Start ePWM
EPwm2Regs.TBCTL.bit.CTRMODE = 0; // Un-freeze and enter up-count mode
do {
GpioDataRegs.GPADAT.bit.GPIO31 = 0; // Turn on LED
DELAY_US(1000 * 500); // ON delay
GpioDataRegs.GPADAT.bit.GPIO31 = 1; // Turn off LED
DELAY_US(1000 * 500); // OFF delay
} while(1);
}
// Write ADC configurations and power up the ADC for both ADC A and ADC B
void ConfigureADC(void)
{
EALLOW;
AdcaRegs.ADCCTL2.bit.PRESCALE = 6; // Set ADCCLK divider to /4
AdcaRegs.ADCCTL2.bit.RESOLUTION = 0; // 12-bit resolution
AdcaRegs.ADCCTL2.bit.SIGNALMODE = 0; // Single-ended channel conversions (12-bit mode only)
AdcaRegs.ADCCTL1.bit.INTPULSEPOS = 1; // Set pulse positions to late
AdcaRegs.ADCCTL1.bit.ADCPWDNZ = 1; // Power up the ADC
DELAY_US(1000); // Delay for 1ms to allow ADC time to power up
EDIS;
}
void ConfigureEPWM(void)
{
EALLOW;
// Assumes ePWM clock is already enabled
EPwm2Regs.TBCTL.bit.CTRMODE = 3; // Freeze counter
EPwm2Regs.TBCTL.bit.HSPCLKDIV = 0; // TBCLK pre-scaler = /1
EPwm2Regs.TBPRD = 0x07D0; // Set period to 2000 counts (50kHz)
EPwm2Regs.ETSEL.bit.SOCAEN = 0; // Disable SOC on A group
EPwm2Regs.ETSEL.bit.SOCASEL = 2; // Select SOCA on period match
EPwm2Regs.ETSEL.bit.SOCAEN = 1; // Enable SOCA
EPwm2Regs.ETPS.bit.SOCAPRD = 1; // Generate pulse on 1st event
EDIS;
}
void ConfigureDAC(void)
{
EALLOW;
DacbRegs.DACCTL.bit.DACREFSEL = 1; // Use ADC references
DacbRegs.DACCTL.bit.LOADMODE = 0; // Load on next SYSCLK
DacbRegs.DACVALS.all = 0x0800; // Set mid-range
DacbRegs.DACOUTEN.bit.DACOUTEN = 1; // Enable DAC
EDIS;
}
void SetupADCEpwm(void)
{
EALLOW;
AdcaRegs.ADCSOC0CTL.bit.CHSEL = 0; // SOC0 will convert pin A0
AdcaRegs.ADCSOC0CTL.bit.ACQPS = 14; // Sample window is 100 SYSCLK cycles
AdcaRegs.ADCSOC0CTL.bit.TRIGSEL = 7; // Trigger on ePWM2 SOCA/C
AdcaRegs.ADCINTSEL1N2.bit.INT1SEL = 0; // End of SOC0 will set INT1 flag
AdcaRegs.ADCINTSEL1N2.bit.INT1E = 1; // Enable INT1 flag
AdcaRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; // Make sure INT1 flag is cleared
EDIS;
}
interrupt void adca1_isr(void)
{
// Read the ADC result and store in circular buffer
AdcaResults[resultsIndex++] = AdcaResultRegs.ADCRESULT0;
if(RESULTS_BUFFER_SIZE <= resultsIndex)
{
resultsIndex = 0;
}
// Toggle GPIO18 so we can read it with the ADC
if (ToggleCount++ >= 15)
{
GpioDataRegs.GPATOGGLE.bit.GPIO18 = 1;
ToggleCount = 0;
}
// Write to DACB to create input to ADC-A0
if (sineEnable != 0)
{
dacOutput = dacOffset + ((QuadratureTable[resultsIndex % 0x20] ^ 0x8000) >> 5);
}
else
{
dacOutput = dacOffset;
}
DacbRegs.DACVALS.all = dacOutput;
// Return from interrupt
AdcaRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; // Clear ADC INT1 flag
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; // Acknowledge PIE group 1 to enable further interrupts
}