Part Number: LAUNCHXL-F28379D
Other Parts Discussed in Thread: C2000WARE
Tool/software: Code Composer Studio
Helol Team TI,
I am working on PI controller for DC/DC converter. I want to keep switching frequency close to 200kHz. But for starting purposes I am working on 100 kHz. During writing a code for PI controller I faced some problems. I completed workshop labs provided by TI C28x. However I failed to read my sensed voltage on ADC. I wrote a separate code to read sensed voltage. However after debugging ADCResultRegs.ADCRESULT0 shows no change in values. Kindly correct me in coding. I have attached my main.c file here.
#include "F28x_Project.h"
#include "math.h"
#define CPUFREQ_MHZ 100
#define DACA 1
#define DACB 2
#define DACC 3
#define REFERENCE 1
Uint32 fs = 2500000; // Sampling Frequency Hz for ADC
//Initialize variables such as time period of PWM and sensed voltage
float TBPRD = 999; // Time period (PRD=f_clk/f_pwm)-1 for sawtooth
float scaled_Vdc = 0;
volatile struct DAC_REGS* DAC_PTR[4] = {0x0,&DacaRegs,&DacbRegs,&DaccRegs};
void ConfigureADC(void);
void ConfigureEPWM(void);
void SetupADCEpwm(Uint16 channel);
interrupt void cpu_timer0_isr(void);
float PI_controller(float error);
float PI_controller1(float error_1);
void configureDAC(void);
//
// Main
//
void main(void)
{
InitSysCtrl(); // Initialise system
InitGpio(); // Initialise GPIO pins
CpuSysRegs.PCLKCR2.bit.EPWM1=1; // enable PWM
InitEPwm1Gpio(); // Initialise GPIO pins for ePWM1
DINT; // Disable all CPU interrupts
InitPieCtrl(); // Initialize the PIE control registers
IER = 0x0000; // Disable CPU interrupts and clear all CPU interrupt flags:
IFR = 0x0000;
InitPieVectTable(); // Initialize the PIE vector table with pointers to the shell Interrupt Service Routines (ISR).
configureDAC(); // Configure DAC
ConfigureADC(); // Configure the ADC and power it up
ConfigureEPWM(); // Configure the ePWM
SetupADCEpwm(2); // Setup the ADC for ePWM triggered conversions on channel 1
// Initialize the Device Peripherals
EALLOW;
ClkCfgRegs.PERCLKDIVSEL.bit.EPWMCLKDIV = 1; // EPWM clock prescale divider. Here we divide the 200 MHz sysclk by 2 and set EPWMCLK to 100 MHZ
CpuSysRegs.PCLKCR0.bit.TBCLKSYNC =0; // clock sync bit unlock
EDIS;
EALLOW;
CpuSysRegs.PCLKCR0.bit.TBCLKSYNC =1; // clock sync bit lock
EDIS;
EALLOW;
PieVectTable.TIMER0_INT = &cpu_timer0_isr; // Map Cpu Timer0 interrupt function to the PIE vector table
EDIS;
InitCpuTimers(); // Initialize Cpu Timers
ConfigCpuTimer(&CpuTimer0, CPUFREQ_MHZ, 1/2500000); // Configure Cpu Timer0 to interrupt at specified sampling frequency
CpuTimer0Regs.TCR.all = 0x4000; // Start Cpu Timer0
IER |= M_INT1; // Enable interrupt
PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global real time interrupt DBGM
for(;;)
{
asm (" NOP");
}
}
void configureDAC()
{
EALLOW;
DAC_PTR[DACA]->DACCTL.bit.DACREFSEL = REFERENCE; // To set reference for DACREFSEL
DAC_PTR[DACA]->DACOUTEN.bit.DACOUTEN = 1; // TO power up the buffered DAC
DAC_PTR[DACA]->DACVALS.all = 0; // Shadowed DAC value register
DELAY_US(10); // Delay for buffered DAC to power up
EDIS;
}
void ConfigureEPWM(void)
{
EALLOW;
// Assumes ePWM clock is already enabled
EPwm2Regs.ETSEL.bit.SOCAEN = 1; // Enable SOC on A group
EPwm2Regs.ETSEL.bit.SOCASEL = 3; // Select SOC on up-count
EPwm2Regs.ETPS.bit.SOCAPRD = 1; // Generate pulse on 1st event
EPwm2Regs.TBPRD = 39; // Set period to 2.5MHz
EPwm2Regs.TBCTL.bit.CTRMODE = 00 ; // freeze counter
EDIS;
}
void ConfigureADC(void)
{
EALLOW;
//write configurations
AdcaRegs.ADCCTL2.bit.PRESCALE = 1011; //set ADCCLK divider to /6
AdcSetMode(ADC_ADCA, ADC_RESOLUTION_12BIT, ADC_SIGNALMODE_SINGLE);
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;
}
//
// SetupADCEpwm - Setup ADC EPWM acquisition window
//
void SetupADCEpwm(Uint16 channel)
{
Uint16 acqps;
acqps = 69; //350ns
//
//Select the channels to convert and end of conversion flag
//
EALLOW;
AdcaRegs.ADCSOC0CTL.bit.CHSEL = channel; //SOC0 will convert pin A2 //Go through page no. 1411 for these register
AdcaRegs.ADCSOC0CTL.bit.ACQPS = acqps; //sample window is 70 SYSCLK cycles
AdcaRegs.ADCSOC0CTL.bit.TRIGSEL = 7; //trigger on ePWM1 SOCA/C
EDIS;
}
interrupt void cpu_timer0_isr(void)
{
DAC_PTR[DACA]->DACVALS.all = (unsigned int)((float)AdcaResultRegs.ADCRESULT0);
DAC_PTR[DACB]->DACVALS.all = (unsigned int) ((float)AdcbResultRegs.ADCRESULT0);
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; // Acknowledge this interrupt to receive more interrupts from group 1
CpuTimer1Regs.TCR.all = 0x0010; // Stop Cpu Timer1 to indicate end of interrupt
CpuTimer1Regs.TCR.all = 0x0030; // Reload Cpu Timer1
}
//
// End of file
//