Tool/software: Code Composer Studio
Dear Manager,
I'm using TMS320F28379D with LaunchPad XL and trying to design the ADC but, when I receive the inputs, it have some oscillations.
The ADCRESULT0, ADCRESULT1 were continuous randomly changed. Is there any weird point in my code?
Also, DAC pin is A0,A1,B1 but I cannot find B1 in launchPad pin map and also if I set the ADC & DAC in same pin what problem will be occured?
Thank you
//Programed by KJH
//20191223
//
// Included Files
//
#include <stdlib.h>
#include <math.h>
#include "F28x_Project.h"
#include "ProjDepDef.h"
#define BLINKY_LED_GPIO 31
#define CPUFREQ_MHZ 200
#define SYSTEM_FREQUENCY 200 // Define the system frequency (MHz)
#define Tstep 0.000050 // 50uS
#define Tadc Tstep * 100.0
// 2500: 50uS w/updown counter -> 20 KHz (under /1 of PLLSYSCLK) -> 10000: 5KHz updown counter
// 25000: 2KHz switching
// 40000: 1.25KHz switching
// 62500: 800Hz switching
// 1250: 40KHz switching
//
// Function Prototypes
//
void ConfigureADC(void);
void ConfigureEPWM(void);
void SetupADCEpwm(void);
// debug
interrupt void adca1_isr(void);
//
// Globals
//
DB_VALUE fADC1 = 1.0;
DB_VALUE fADC2 = 1.0;
// SFO and MEP variables
// HRPWM testing
// pik-20190307
#define AUTOCONVERT 1 // 1 = Turn auto-conversion ON
// 0 = Turn auto-conversion OFF
void main(void)
{
//
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the F2837xD_SysCtrl.c file.
//
InitSysCtrl();
// ClkCfgRegs.PERCLKDIVSEL.bit.EPWMCLKDIV = 0; // 0: Set the ePWM clock to /1 of PLLSYSCLK 1: Set the ePWM clock to /2 of PLLSYSCLK
//
// Step 2. Initialize GPIO:
// This example function is found in the F2837xD_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
//
InitGpio(); // Skipped for this example
//
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
//
DINT;
//
// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the F2837xD_PieCtrl.c file.
//
InitPieCtrl();
//
// Disable CPU interrupts and clear all CPU interrupt flags:
//
IER = 0x0000;
IFR = 0x0000;
//
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in F2837xD_DefaultIsr.c.
// This function is found in F2837xD_PieVect.c.
//
InitPieVectTable();
//
// Map ISR functions
//
EALLOW;
PieVectTable.ADCA1_INT = &adca1_isr; //function for ADCA interrupt 1
EDIS;
//
// Configure the DAC
//
//
// Configure the ADC and power it up
//
ConfigureADC();
//
// Configure the ePWM
//
EALLOW;
CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 0;
EDIS;
ConfigureEPWM();
EALLOW;
CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 1;
EDIS;
//Setup the ADC for ePWM triggered conversions on channel 0
// SetupADCEpwm();
SetupADCEpwm();
//
// Enable global Interrupts and higher priority real-time debug events:
//
IER |= M_INT1; // Enable group 1 interrupts
//
// enable PIE interrupt
//
PieCtrlRegs.PIEIER1.bit.INTx1 = 1; // adc
//
// start ePWM
//
EPwm12Regs.ETSEL.bit.SOCAEN = 1; //enable SOCA, ADC conversion trigger
EPwm12Regs.TBCTL.bit.CTRMODE = 0; //unfreeze, and enter up count mode
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
//
// take conversions indefinitely in loop
//
while(1);
}
//
// ConfigureADC - Write ADC configurations and power up the ADC for both
// ADC A and ADC B
//
void ConfigureADC(void)
{
EALLOW;
//
//write configurations
//
AdcaRegs.ADCCTL2.bit.PRESCALE = 6; //set ADCCLK divider to /4
AdcbRegs.ADCCTL2.bit.PRESCALE = 6; //set ADCCLK divider to /4
AdcSetMode(ADC_ADCA, ADC_RESOLUTION_12BIT, ADC_SIGNALMODE_SINGLE);
//
//Set pulse positions to late
//
AdcaRegs.ADCCTL1.bit.INTPULSEPOS = 1; // tINT will coincide with the conversion results being latched into the result register.
//
//power up the ADC
//
AdcaRegs.ADCCTL1.bit.ADCPWDNZ = 1;
//
//delay for 1ms to allow ADC time to power up
//
DELAY_US(1000);
EDIS;
}
//
// SetupADCEpwm - Configure ADC EPWM acquisition window and trigger
//
void SetupADCEpwm()
{
// Burst mode
// pik-2017.08.29
Uint16 acqps;
//determine minimum acquisition window (in SYSCLKS) based on resolution
if (ADC_RESOLUTION_12BIT == AdcaRegs.ADCCTL2.bit.RESOLUTION)
{
acqps = 14; //100ns - factoring in PRESCALE = 6; //set ADCCLK divider to /4
}
else
{ //resolution is 16-bit
acqps = 63; //320ns
}
//Select the channels to convert and end of conversion flag
EALLOW;
// ADC A
AdcaRegs.ADCSOC0CTL.bit.CHSEL = 3; // SOC0 will convert pin A4
AdcaRegs.ADCSOC0CTL.bit.ACQPS = acqps; // sample window is 100 SYSCLK cycles
AdcaRegs.ADCSOC0CTL.bit.TRIGSEL = 27;
AdcaRegs.ADCSOC1CTL.bit.CHSEL = 5; // SOC0 will convert pin A5
AdcaRegs.ADCSOC1CTL.bit.ACQPS = acqps; // sample window is 100 SYSCLK cycles
AdcaRegs.ADCSOC1CTL.bit.TRIGSEL = 27; //trigger on ePWM12 SOCA/C
AdcaRegs.ADCINTSEL1N2.bit.INT1SEL = 0; // EOC15 will set INT1 flag
AdcaRegs.ADCINTSEL1N2.bit.INT1E = 1; //enable INT1 flag
AdcaRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; //make sure INT1 flag is cleared
EDIS;
}
//
// ConfigureEPWM - Configure EPWM SOC and compare values
//
void ConfigureEPWM(void)
{
//InitEPwm1Gpio();
EALLOW;
//
// Assumes ePWM clock is already enabled
//
//EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;
//EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1;
EPwm12Regs.ETSEL.bit.SOCAEN = 0; // Disable SOC on A group
EPwm12Regs.ETSEL.bit.SOCASEL = 4; // Select SOC on up-count
EPwm12Regs.ETPS.bit.SOCAPRD = 1; // Generate pulse on 1st event
EPwm12Regs.CMPA.bit.CMPA = 0x0800; // Set compare A value to 2048 counts
EPwm12Regs.TBPRD = 0x1000; // 1 pwm lock->currently 10ns
EPwm12Regs.TBCTL.bit.CTRMODE = 3; // freeze counter
EDIS;
}
//
// adca1_isr - Read Temperature ISR
//
interrupt void adca1_isr(void)
{
fADC1 = (DB_VALUE)AdcaResultRegs.ADCRESULT0;
fADC2 = (DB_VALUE)AdcaResultRegs.ADCRESULT1;
DELAY_US(2);
}
//
// End of file
//