Part Number: TMS320F28335
Hello i am working on a frequency estimation of a sin signal which has 50Hz frequency in average. I will use the Adc to read the signal. I have only one signal from ADC. My project will make same calculations with 500Hz sampling frequency. For that i created a EPWM interrupt with 500Hz frequency. How can configure the ADC for sampling window and clock settings? I shared the code in below. In interrupt file i am substracting my analog signal with the estimation signal that i have. Thank you already.
//
// Lab8_1: TMS320F28335
// (c) Frank Bormann
//
//###########################################################################
//
// FILE: Lab8_1.c
//
// TITLE: DSP28335ControlCARD Analogue Input
// ADCIN_A0 , ADCIN_A1 connected to variable resistors VR1, VR2
// on Peripheral Explorer Board (3.3V...0V)
// ADC samples at 50KHz, hardware triggered by ePWM2
// Voltages are displayed alternately on 4 LEDs LD1...LD4
// (GPIO9, GPIO11, GPIO34 and GPIO49)
// CPU Timer0 ISR every 100 ms
// Watchdog active , cleared in ISR and main-loop
//
//###########################################################################
// Ver | dd mmm yyyy | Who | Description of changes
// =====|=============|======|===============================================
// 3.0 | 30 Jun 2009 | F.B. | Lab8_1 for F28335CC + peripheral explorer
// 3.1 | 09 Nov 2009 | F.B | Lab8_1 for F28335 and PE revision 5
//###########################################################################
#include "DSP2833x_Device.h"
#include "math.h"
#include "matrix_func.h"
// external function prototypes
extern void InitAdc(void);
extern void InitSysCtrl(void);
extern void InitPieCtrl(void);
extern void InitPieVectTable(void);
extern void InitCpuTimers(void);
extern void ConfigCpuTimer(struct CPUTIMER_VARS *, float, float);
extern void display_ADC(unsigned int);
// Prototype statements for functions found within this file.
void Gpio_select(void);
interrupt void cpu_timer0_isr(void);
interrupt void adc_isr(void); // ADC End of Sequence ISR
// Global Variables
unsigned int Voltage_VR1[2];
unsigned int Voltage_VR2[2];
unsigned int Sin_Ref[2]={0,0,0};
/* here we need a parameter matrix that includes dc ac and phase variables with 3*3 matrix
* this parameter matrix will be initialized with the value 0 in the main loop we will modify our values.
*
*/
//###########################################################################
// main code
//###########################################################################
void main(void)
{
InitSysCtrl(); // Basic Core Init from DSP2833x_SysCtrl.c
EALLOW;
SysCtrlRegs.WDCR= 0x00AF; // Re-enable the watchdog
EDIS; // 0x00AF to NOT disable the Watchdog, Prescaler = 64
DINT; // Disable all interrupts
Gpio_select(); // GPIO9, GPIO11, GPIO34 and GPIO49 as output
// to 4 LEDs at Peripheral Explorer)
InitPieCtrl(); // basic setup of PIE table; from DSP2833x_PieCtrl.c
InitPieVectTable(); // default ISR's in PIE
InitAdc(); // Basic ADC setup, incl. calibration
AdcRegs.ADCTRL1.all = 0;
AdcRegs.ADCTRL1.bit.ACQ_PS = 7; // 7 = 8 x ADCCLK
AdcRegs.ADCTRL1.bit.SEQ_CASC =1; // 1=cascaded sequencer
AdcRegs.ADCTRL1.bit.CPS = 0; // divide by 1
AdcRegs.ADCTRL1.bit.CONT_RUN = 0; // single run mode
AdcRegs.ADCTRL2.all = 0;
AdcRegs.ADCTRL2.bit.INT_ENA_SEQ1 = 1; // 1=enable SEQ1 interrupt
AdcRegs.ADCTRL2.bit.EPWM_SOCA_SEQ1 =1; // 1=SEQ1 start from ePWM_SOCA trigger
AdcRegs.ADCTRL2.bit.INT_MOD_SEQ1 = 0; // 0= interrupt after every end of sequence
AdcRegs.ADCTRL3.bit.ADCCLKPS = 3; // ADC clock: FCLK = HSPCLK / 2 * ADCCLKPS
// HSPCLK = 75MHz (see DSP2833x_SysCtrl.c)
// FCLK = 12.5 MHz
AdcRegs.ADCMAXCONV.all = 0x0000; // 2 conversions from Sequencer 1
AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0; // Setup ADCINA0 as 1st SEQ1 conv.
AdcRegs.ADCCHSELSEQ1.bit.CONV01 = 1; // Setup ADCINA1 as 2nd SEQ1 conv.
EPwm2Regs.TBCTL.all = 0xD030; // Configure timer control register
/*
bit 15-14 11: FREE/SOFT, 11 = ignore emulation suspend
bit 13 0: PHSDIR, 0 = count down after sync event
bit 12-10 100: CLKDIV, 100 => TBCLK = HSPCLK/1 ===16
bit 9-7 000: HSPCLKDIV, 101 ==10=> HSPCLK = SYSCLKOUT/1
bit 6 0: SWFSYNC, 0 = no software sync produced
bit 5-4 11: SYNCOSEL, 11 = sync-out disabled
bit 3 0: PRDLD, 0 = reload PRD on counter=0
bit 2 0: PHSEN, 0 = phase control disabled
bit 1-0 00: CTRMODE, 00 = count up mode
*/
EPwm2Regs.TBPRD = 1875; // TPPRD +1 = TPWM / (HSPCLKDIV(10) * CLKDIV(16) * TSYSCLK)
// = 2000 µs / 1,066.72 ns
EPwm2Regs.ETPS.all = 0x0100; // Configure ADC start by ePWM2
/*
bit 15-14 00: EPWMxSOCB, read-only
bit 13-12 00: SOCBPRD, don't care
bit 11-10 00: EPWMxSOCA, read-only
bit 9-8 01: SOCAPRD, 01 = generate SOCA on first event
bit 7-4 0000: reserved
bit 3-2 00: INTCNT, don't care
bit 1-0 00: INTPRD, don't care
*/
EPwm2Regs.ETSEL.all = 0x0A00; // Enable SOCA to ADC
/*
bit 15 0: SOCBEN, 0 = disable SOCB
bit 14-12 000: SOCBSEL, don't care
bit 11 1: SOCAEN, 1 = enable SOCA
bit 10-8 010: SOCASEL, 010 = SOCA on PRD event
bit 7-4 0000: reserved
bit 3 0: INTEN, 0 = disable interrupt
bit 2-0 000: INTSEL, don't care
*/
EALLOW;
PieVectTable.ADCINT = &adc_isr;
EDIS;
InitCpuTimers(); // basic setup CPU Timer0, 1 and 2
PieCtrlRegs.PIEIER1.bit.INTx6 = 1; // ADC
IER |=1;
EINT;
ERTM;
CpuTimer0Regs.TCR.bit.TSS = 0; // start timer0
unsigned int sample_counter=0;
while(1)
{
}
interrupt void adc_isr(void)
{
//
if(sample_counter==fs){//////this is for not getting to over
/////////////////////////limit of the 32 bit variable
sample_counter=0;
}
/////////////////analog signal ///////////////////////////////////////////
Voltage_VR1[2]= Voltage_VR1[1];
Voltage_VR1[1]= Voltage_VR1[0]; // shift the last analog voltage into index 1
Voltage_VR1[0]= AdcMirror.ADCRESULT0; // store results in 3x1 matrix
// 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
///////////////////////////////////// reference signal refreshing///////////////////////////
Sin_Ref[2]=Sin_Ref[1];
Sin_Ref[1]=Sin_Ref[0];
Sin_Ref[0]=parameter_vector[1][1]*sin(((2*3.14*50*sample_counter)/fs)+parameter_vector[1][2]);
///////////////////////////////////error matrix refreshing////////////////////////
error[0]=Sin_Ref[0]-Voltage_VR1[0];
error[1]=Sin_Ref[1]-Voltage_VR1[1];
error[2]=Sin_Ref[2]-Voltage_VR1[2];
/////////////////////////////////jacobian matrix///////////////////////////////////////
jacob[1][1]=sin(((2*3.14*50*sample_counter)/fs)+parameter_vector[1][2]);
jacob[1][2]=parameter_vector[1][1]*sin(((2*3.14*50*sample_counter)/fs)+parameter_vector[1][2]);
jacob[1][3]=1;
jacob[2][1]=sin(((2*3.14*50*sample_counter)/fs)+parameter_vector[2][2]);
jacob[2][2]=parameter_vector[2][1]*sin(((2*3.14*50*sample_counter)/fs)+parameter_vector[2][2]);
jacob[2][3]=1;
jacob[3][1]=sin(((2*3.14*50*sample_counter)/fs)+parameter_vector[3][2]);
jacob[3][2]=parameter_vector[3][1]*sin(((2*3.14*50*sample_counter)/fs)+parameter_vector[3][2]);
jacob[3][3]=1;
//////////////////////////////////////////////////////////////////////////////////////
transpose( t_jacob,jacob, 3); ////// (j^t)
multi( t_jacob,jacob,3,mul); ////// (j^t*j)
inverse(mul,inv,3); ////// (j^t*j)'
multi(inv,t_jacob,3,j_t_j_tjacob); ////// (j^t*j)'j^t
multi(j_t_j_tjacob,error,3,m_delta); ///// (j^t*j)'j^t*Ek
negative(delta,m_delta,3); ///this is mines parameter vector
add_matrix(parameter_vector, delta, parameter_vector,3);///parameter vector refreshed
//////////////////////////////////////////////////////////////////////////////////////
sample_counter++;
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
}
//===========================================================================
// End of SourceCode.
//===========================================================================