This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TMS320F28379D: DAC and ADC

Part Number: TMS320F28379D


Tool/software: Code Composer Studio

Hi

I am using the DAC and the ADC at the same time, initially I used the example buffdac_random as a base to generate a signal with a vector data, I use a delay of 0.5 us and at the output I verify that each data of the vector has a delay of 1 us which it works properly, now on the other hand with the adc_soc_epwm example I have modified the TBPRD to obtain a 30 kHz signal this has also worked well. My problem is that when one of the two examples into one, the DAC stops working as before, it no longer performs in specified "delay", I don't know why this happens.

I attach the code in which the two examples are found.
I hope someone can help me with my problem.

//###########################################################################
//
// FILE:   buffdac_random_cpu01.c
//
// TITLE:  Buffered DAC Random Output Example for F2837xD.
//
//! \addtogroup cpu01_example_list
//! <h1> Buffered DAC Random (buffdac_random) </h1>
//!
//! This example generates random voltages on the buffered DAC output,
//! DACOUTA/ADCINA0 (HSEC Pin 9) and uses the default DAC reference setting
//! of VDAC.
//!
//! When the DAC reference is set to VDAC, an external reference voltage
//! must be applied to the VDAC pin. This can accomplished by connecting a
//! jumper wire from 3.3V to ADCINB0 (HSEC pin 12).
//!
//
//###########################################################################
// $TI Release: F2837xD Support Library v3.11.00.00 $
// $Release Date: Sun Oct  4 15:55:24 IST 2020 $
// $Copyright:
// Copyright (C) 2013-2020 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions 
// are met:
// 
//   Redistributions of source code must retain the above copyright 
//   notice, this list of conditions and the following disclaimer.
// 
//   Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the 
//   documentation and/or other materials provided with the   
//   distribution.
// 
//   Neither the name of Texas Instruments Incorporated nor the names of
//   its contributors may be used to endorse or promote products derived
//   from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//###########################################################################

//
// Included Files
//
#include <time.h>
#include <stdlib.h>
#include "F28x_Project.h"

//
// Defines
//

#define REFERENCE_VDAC      0
#define REFERENCE_VREF      1
#define DACA                1
#define DACB                2
#define DACC                3
#define REFERENCE           REFERENCE_VREF
#define DAC_NUM             DACB

#define RESULTS_BUFFER_SIZE 256
//
// Globals
//
void ConfigureADC(void);
void ConfigureEPWM(void);
void SetupADCEpwm(Uint16 channel);
interrupt void adca1_isr(void);

volatile struct DAC_REGS* DAC_PTR[4] = {0x0,&DacaRegs,&DacbRegs,&DaccRegs};
Uint16 low_limit = 410;
Uint16 high_limit = 3686;

Uint16 signal[]={2048,2048,2048,2049,2050,2052,2054,2056,2059,2062,2064,2065,2065,2064,2061,2055,2047,2038,2026,2012,1998,1983,1968,1955,1944,1936,1932,1933,1939,1952,1970,1995,2025,2060,2098,2140,2182,2223,2262,2295,2322,2341,2349,2346,2330,2302,2261,2209,2146,2073,1994,1911,1827,1746,1670,1604,1550,1512,1491,1491,1512,1554,1618,1702,1803,1920,2048,2182,2319,2452,2578,2689,2782,2853,2896,2910,2893,2843,2762,2651,2513,2352,2173,1983,1788,1595,1412,1245,1101,988,909,869,871,917,1004,1133,1299,1497,1722,1964,2216,2470,2714,2941,3142,3307,3432,3509,3535,3508,3427,3294,3114,2891,2633,2349,2047,1741,1439,1153,895,674,498,375,310,307,366,487,665,896,1171,1482,1817,2164,2511,2846,3157,3430,3658,3830,3939,3983,3957,3862,3702,3482,3208,2891,2542,2173,1797,1427,1078,762,490,273,118,31,16,73,201,395,648,951,1294,1664,2048,2431,2801,3144,3447,3700,3894,4022,4079,4064,3977,3822,3605,3333,3017,2668,2298,1922,1553,1204,887,613,393,233,138,112,156,265,437,665,938,1249,1584,1931,2278,2613,2924,3199,3430,3608,3729,3788,3785,3720,3597,3421,3200,2942,2656,2354,2048,1746,1462,1204,981,801,668,587,560,586,663,788,953,1154,1381,1625,1879,2131,2373,2598,2796,2962,3091,3178,3224,3226,3186,3107,2994,2850,2683,2500,2307,2112,1922,1743,1582,1444,1333,1252,1202,1185,1199,1242,1313,1406,1517,1643,1776,1913,2047,2175,2292,2393,2477,2541,2583,2604,2604,2583,2545,2491,2425,2349,2268,2184,2101,2022,1949,1886,1834,1793,1765,1749,1746,1754,1773,1800,1833,1872,1913,1955,1997,2035,2070,2100,2125,2143,2156,2162,2163,2159,2151,2140,2127,2112,2097,2083,2069,2057,2048,2040,2034,2031,2030,2030,2031,2033,2036,2039,2041,2043,2045,2046,2047,2047,2048};
Uint16 x=0;

Uint16 AdcaResults[RESULTS_BUFFER_SIZE];
Uint16 resultsIndex;
volatile Uint16 bufferFull;

//
// Function Prototypes
//
void configureDAC(Uint16 dac_num);

void main(void)
{
//
// Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the F2837xD_SysCtrl.c file.
//
    InitSysCtrl();

//
// 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();

//
// Clear all interrupts and initialize PIE vector table:
//
    IER = 0x0000;
    IFR = 0x0000;
    InitPieVectTable();

//
    EALLOW;

       PieVectTable.ADCA1_INT = &adca1_isr; //function for ADCA interrupt 1
       EDIS;

   //
   // Configure the ADC and power it up
   //
       ConfigureADC();

   //
   // Configure the ePWM
   //
       ConfigureEPWM();

   //
   // Setup the ADC for ePWM triggered conversions on channel 0
   //
       SetupADCEpwm(0);

   //
   // Enable global Interrupts and higher priority real-time debug events:
   //
       IER |= M_INT1; //Enable group 1 interrupts
       EINT;  // Enable Global interrupt INTM
       ERTM;  // Enable Global realtime interrupt DBGM
// Initialize random seed
//

       for(resultsIndex = 0; resultsIndex < RESULTS_BUFFER_SIZE; resultsIndex++)
       {
           AdcaResults[resultsIndex] = 0;
       }
       resultsIndex = 0;
       bufferFull = 0;

   //
   // enable PIE interrupt
   //
       PieCtrlRegs.PIEIER1.bit.INTx1 = 1;

   //
   // sync ePWM
   //
       EALLOW;
       CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 1;

   //
   //take conversions indefinitely in loop
   //
    srand(time(NULL));

//
// Configure DAC
//
    configureDAC(DAC_NUM);

    while(1)
    {

        do
            {
            DAC_PTR[DAC_NUM]->DACVALS.all = signal[x++];
            DELAY_US(0.5);
            if(333<=x) x=0;


                //
                //start ePWM
                //
                EPwm1Regs.ETSEL.bit.SOCAEN = 1;  //enable SOCA
                EPwm1Regs.TBCTL.bit.CTRMODE = 0; //unfreeze, and enter up count mode

                //
                //wait while ePWM causes ADC conversions, which then cause interrupts,
                //which fill the results buffer, eventually setting the bufferFull
                //flag
                //
                while(!bufferFull);
                bufferFull = 0; //clear the buffer full flag

                //
                //stop ePWM
                //
                EPwm1Regs.ETSEL.bit.SOCAEN = 0;  //disable SOCA
                EPwm1Regs.TBCTL.bit.CTRMODE = 3; //freeze counter

                //
                //at this point, AdcaResults[] contains a sequence of conversions
                //from the selected channel
                //

                //
                //software breakpoint, hit run again to get updated conversions
                //
                //asm("   ESTOP0");


            }while(1);
    }
}

//
// configureDAC - Setup the DAC reference selection and enable specified DAC
//

void ConfigureADC(void)
{
    EALLOW;

    //
    //write configurations
    //
    AdcaRegs.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;

    //
    //power up the ADC
    //
    AdcaRegs.ADCCTL1.bit.ADCPWDNZ = 1;

    //
    //delay for 1ms to allow ADC time to power up
    //
    DELAY_US(10);

    EDIS;
}

void ConfigureEPWM(void)
{
    EALLOW;
    // Assumes ePWM clock is already enabled
    EPwm1Regs.ETSEL.bit.SOCAEN    = 0;    // Disable SOC on A group
    EPwm1Regs.ETSEL.bit.SOCASEL    = 4;   // Select SOC on up-count
    EPwm1Regs.ETPS.bit.SOCAPRD = 1;       // Generate pulse on 1st event
    EPwm2Regs.TBCTL.bit.CLKDIV = 1;          //
    EPwm2Regs.TBCTL.bit.HSPCLKDIV =1;       //
    EPwm1Regs.CMPA.bit.CMPA = 1660;     // Set compare A value to 2048 counts
    EPwm1Regs.TBPRD = 3332;             // Set period to 4096 counts
    EPwm1Regs.TBCTL.bit.CTRMODE = 3;      // freeze counter
    EDIS;
}

//
// SetupADCEpwm - Setup ADC EPWM acquisition window
//
void SetupADCEpwm(Uint16 channel)
{
    Uint16 acqps;

    //
    // Determine minimum acquisition window (in SYSCLKS) based on resolution
    //
    if(ADC_RESOLUTION_12BIT == AdcaRegs.ADCCTL2.bit.RESOLUTION)
    {
        acqps = 14; //75ns
    }
    else //resolution is 16-bit
    {
        acqps = 63; //320ns
    }

    //
    //Select the channels to convert and end of conversion flag
    //
    EALLOW;
    AdcaRegs.ADCSOC0CTL.bit.CHSEL = channel;  //SOC0 will convert pin A0
    AdcaRegs.ADCSOC0CTL.bit.ACQPS = acqps; //sample window is 100 SYSCLK cycles
    AdcaRegs.ADCSOC0CTL.bit.TRIGSEL = 5; //trigger on ePWM1 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;
}

//
// adca1_isr - Read ADC Buffer in ISR
//
interrupt void adca1_isr(void)
{
    AdcaResults[resultsIndex++] = AdcaResultRegs.ADCRESULT0;
    if(RESULTS_BUFFER_SIZE <= resultsIndex)
    {
        resultsIndex = 0;
        bufferFull = 1;
    }

    AdcaRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; //clear INT1 flag

    //
    // Check if overflow has occurred
    //
    if(1 == AdcaRegs.ADCINTOVF.bit.ADCINT1)
    {
        AdcaRegs.ADCINTOVFCLR.bit.ADCINT1 = 1; //clear INT1 overflow flag
        AdcaRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; //clear INT1 flag
    }

    PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}

//
// End of file
//
void configureDAC(Uint16 dac_num)
{
    EALLOW;
    DAC_PTR[dac_num]->DACCTL.bit.DACREFSEL = REFERENCE;
    DAC_PTR[dac_num]->DACOUTEN.bit.DACOUTEN = 1;
    DAC_PTR[dac_num]->DACVALS.all = 0;
    DELAY_US(10); // Delay for buffered DAC to power up
    EDIS;
}

//
// End of file
//