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.

TMS320F28027PT with 12 bit External DAC through SPI

Other Parts Discussed in Thread: CONTROLSUITE, TMS320F2812

Hi;
 
I am new with PICOLLO and Embedded coding.  I am using TMS320F28027PT, using CH4(AIO4) to read an analog input and I need to use a 12 bit DAC

MCP4921 that should output the analog signal read by ADC.

I am using SPI  3 wire connection.  Active low CS of the DAC is connected to the GPIO0/EPWM1A, GPIO16 is connected to DAC's SDI(Serial Data in)-input, and

DAC's SCk is connected to GPIO18 as an SPICLK.

Since the ADC gives right justified result 12 bit, so I left shifted 4 times the ADC result, to do left justified before transmitting. and the reced. data

is andded with 0fffh , now after this MY DAC's OUTPUT is Completely zero (and all Gpio's are never active in simulation )obviously I am doing smth wrong with

 SPI or With GPIO Which I cannot fix. IF you Can Please help.I am Using Proteus to simulate all this. the code compiles with no error.

Here I paste my Code and Schematic

Thanks,

Ahmad

*****************************************************************************************

#include "F2802x_Device.h"     // Headerfile Include File
#include "f2802x_examples.h"

#define ADC_usDELAY  1000L

void InitAdc(void)
{
    extern void DSP28x_usDelay(Uint32 Count);

   
        EALLOW;
        SysCtrlRegs.PCLKCR0.bit.ADCENCLK = 1;
        (*Device_cal)();
        EDIS;
        
            EALLOW;
            AdcRegs.ADCCTL1.bit.ADCBGPWD  = 1;      // Power ADC BG
            AdcRegs.ADCCTL1.bit.ADCREFPWD = 1;      // Power reference
            AdcRegs.ADCCTL1.bit.ADCPWDN   = 1;      // Power ADC
            AdcRegs.ADCCTL1.bit.ADCENABLE = 1;      // Enable ADC
            AdcRegs.ADCCTL1.bit.ADCREFSEL = 0;      // Select internal BG


            DELAY_US(ADC_usDELAY);         // Delay before converting ADC channels

            
                    GpioCtrlRegs.AIOMUX1.bit.AIO4 = 2;    // Configure AIO4 for A4 (analog input) operation
                    AdcRegs.ADCSOC0CTL.bit.CHSEL = 4;                        // Convert channel ADCINA4 (ch4); set SOC0 channel select to ADCINA4
                    AdcRegs.ADCSOC0CTL.bit.ACQPS = 6;                        // Acquisition window set to (6+1)=7 cycles

                    AdcRegs.INTSEL1N2.bit.INT1E = 1; // Enabled ADCINT1

                   
                       AdcRegs.INTSEL1N2.bit.INT1CONT = 0;

                       AdcRegs.ADCCTL1.bit.INTPULSEPOS = 1;    //ADCINTs trigger at end of conversion


                       AdcRegs.INTSEL1N2.bit.INT1SEL    = 6; // //EOC6 triggers ADCINT1

                       AdcRegs.ADCINTSOCSEL1.bit.SOC0  = 1;    //ADCINT1 starts SOC0

                  

               EDIS ;

               DELAY_US(ADC_usDELAY);   // Delay before converting ADC channels
  }

void spi_init()
{


        SpiaRegs.SPICCR.all =0x000F;                     // Reset on, rising edge, 16-bit char bits
        SpiaRegs.SPICTL.all =0x0006;                         // Enable master mode, normal phase,
                                                 // enable talk, and SPI int disabled.
        SpiaRegs.SPIBRR =0x007F;
    SpiaRegs.SPICCR.all =0x009F;                         // Relinquish SPI from Reset
    SpiaRegs.SPIPRI.bit.FREE = 1;      // Set so breakpoints don't disturb xmission

}

void spi_xmit(Uint16 a) //before it was Uint16 a
{
    SpiaRegs.SPITXBUF=a;
}



void spi_fifo_init()
{
// Initialize SPI FIFO registers
    SpiaRegs.SPIFFTX.all=0xE040;
    SpiaRegs.SPIFFRX.all=0x2044;
    SpiaRegs.SPIFFCT.all=0x0;
}

void InitGpio(void)
{
    EALLOW;
    GpioCtrlRegs.GPAPUD.bit.GPIO0 = 1;    // Disable pull-up on GPIO0 (EPWM1A)
    GpioCtrlRegs.GPAPUD.bit.GPIO16 = 1;    // Disable pull-up on GPIO16 (EPWM1A)
    //GpioCtrlRegs.GPAPUD.bit.GPIO17 = 1;    // Disable pull-up on GPIO0 (EPWM1A)
    GpioCtrlRegs.GPAPUD.bit.GPIO18 = 1;    // Disable pull-up on GPIO0 (EPWM1A)

    //GpioDataRegs.GPASET.bit.GPIO0 = 1; // condition for GPIO0 to be an output
    //GpioDataRegs.GPACLEAR.bit.GPIO0 = 1;

    //GpioDataRegs.GPASET.bit.GPIO16 = 1;
    //GpioDataRegs.GPACLEAR.bit.GPIO16 = 1;



    //GpioDataRegs.GPASET.bit.GPIO18 = 1;
    //GpioDataRegs.GPACLEAR.bit.GPIO18 = 1;

    GpioCtrlRegs.GPADIR.bit. GPIO0 = 1;// GPIO0 is output
    GpioCtrlRegs.GPADIR.bit.GPIO16 = 1;

    GpioCtrlRegs.GPADIR.bit.GPIO18 = 1;

    EDIS;
}

char DAC(unsigned int value )
{
    EALLOW;
     char rdata, mask;

    InitGpio();

    GpioDataRegs.GPADAT.bit.GPIO0 = 0; //ChipSelect = 0; for Enable DAC

    spi_init();

    spi_fifo_init();

    spi_xmit(value);// write to DAC

    while(SpiaRegs.SPIFFRX.bit.RXFFST !=1) { }
    rdata = SpiaRegs.SPIRXBUF;
     mask = rdata & 0x0FFF;
    GpioDataRegs.GPADAT.bit.GPIO0 =1; //ChipSelect = 1; //Disable DAC

    return(mask);



}


 void main() {

#ifdef _FLASH
    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
  #endif


  InitSysCtrl();


  EALLOW;
             GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 3; //enable XCLOCKOUT through GPIO mux
             SysCtrlRegs.XCLK.bit.XCLKOUTDIV = 2; //XCLOCKOUT = SYSCLK
EDIS;


  DINT;


  InitPieCtrl();


  IER = 0x0000;
  IFR = 0x0000;


  InitPieVectTable();

  int16 temp, shift;


  for(;;)
                 {
                              InitAdc();
                      
                        //Wait for end of conversion.
                        while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}  //Wait for ADCINT1
                        AdcRegs.ADCINTFLGCLR.bit.ADCINT1 = 1;        //Clear ADCINT1

                       
                        temp = AdcResult.ADCRESULT0;
                        shift = temp << 4;
                       


                        DAC(shift);


                 }
    
    return ;





}


*****************************************************************************************************************

  • Ahmad,

    First, welcome to embedded programming with the C2000!

    If you haven't already, be sure to download the TRM and Datasheet for your device.  Spend some time familiarizing yourself with the modules that you will be using. Run a few of the example programs for SPI and ADC. It looks like you might have a decent base of code, but there are definitely a few things missing.

    As far as I can tell, you are never configuring the GPIOs to actually have the SPI functionality. Please go through the ControlSUITE examples and TRM for the procedure for this.

    Another thing to do is to only do the SPI initialization once. You do not need to change GPIO setup, and reinitialize the SPI module every time you call the DAC() function.

    It might be a better use of your time to spend a day or two going through the existing documentation and examples to get a feel for programming with the C2000. Be sure to understand these examples to see the flow of the program. Here is a Getting Started Webpage that will walk you through the development process.

    If you have more specific questions, please do not hesitate to post.

    Regards,

    Mark

  • Hi Mark;


    Thank you Very much for your kind attention and advice. I will try my best to do what you have suggested, and will let you know  the result.

    I appreciate your helping me in this matter , my Final Target is to simulate that first with TMS20f28027 and then get your TMS320F2812 to design my controller .

    Thanks Again.

    Ahmad

  • Ahmad,

    Glad to help. I am going to close this post. If you have additional questions in the future, please create a new one.

    -Mark