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.

Picollo F2807pt

Other Parts Discussed in Thread: 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 GPIO34/comp2out, GPIO16 is connected to DAC's SDI(Serial Data in)-input, and

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

MY ACINA4/AIO4 is not active (grey)should be red in simulation, and the DAC Output is zero, looks like as if it does not read the ADC channel 4, obviously I

am doing smth wrong with either ADC or SPI  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



                    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
                    GpioCtrlRegs.AIOMUX1.bit.AIO4 = 2;    // Configure AIO4 for A4 (analog input) operation

                    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.GPIO16 = 0;   // Enable pull-up on GPIO16 (SPISIMOA)
            GpioCtrlRegs.GPAPUD.bit.GPIO17 = 0;   // Enable pull-up on GPIO17 (SPISOMIA)
            GpioCtrlRegs.GPAPUD.bit.GPIO18 = 0;   // Enable pull-up on GPIO18 (SPICLKA)
            GpioCtrlRegs.GPAPUD.bit.GPIO19 = 0;   // Enable pull-up on GPIO19 (SPISTEA)

            GpioCtrlRegs.GPAQSEL2.bit.GPIO16 = 3; // Asynch input GPIO16 (SPISIMOA)
            GpioCtrlRegs.GPAQSEL2.bit.GPIO17 = 3; // Asynch input GPIO17 (SPISOMIA)
            GpioCtrlRegs.GPAQSEL2.bit.GPIO18 = 3; // Asynch input GPIO18 (SPICLKA)
            GpioCtrlRegs.GPAQSEL2.bit.GPIO19 = 3; // Asynch input GPIO19 (SPISTEA)

            GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 1; // Configure GPIO16 as SPISIMOA
            GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 1; // Configure GPIO17 as SPISOMIA
            GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1; // Configure GPIO18 as SPICLKA
            GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1; // Configure GPIO19 as SPISTEA


    GpioCtrlRegs.GPADIR.bit. GPIO17 = 0;// GPIO17 is input
    GpioCtrlRegs.GPADIR.bit.GPIO16 = 1;

    GpioCtrlRegs.GPADIR.bit.GPIO18 = 1;
    GpioCtrlRegs.GPADIR.bit.GPIO19 = 1;

    GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0;    // 0=GPIO,  1=COMP2OUT,  2=EMU1,  3=Resv
        GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1;        // 1=OUTput,  0=INput
        GpioDataRegs.GPBSET.bit.GPIO34 = 1;

    EDIS;
}

void DAC(unsigned int value )

{
    EALLOW;
     char  tem;

    
     tem = (value>>8) & 0x0F ; //store bits 8:11 in "tem"
         tem |= 0x30; // DAc setting for gain

    GpioDataRegs.GPBDAT.bit.GPIO34 = 0; //ChipSelect = 0; for Enable DAC

    
    spi_xmit(tem);// write to DAC

    while(SpiaRegs.SPIFFRX.bit.RXFFST !=1) { }
    

     tem = value ; //send low byte

     spi_xmit(tem);// write to DAC

         while(SpiaRegs.SPIFFRX.bit.RXFFST !=1) { }

    GpioDataRegs.GPBDAT.bit.GPIO34 =1; //ChipSelect = 1; //Disable DAC


}



 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
           SysCtrlRegs.PCLKCR0.bit.SPIAENCLK = 1;       // SPI-A enable clock
EDIS;


  DINT;


  InitPieCtrl();


  IER = 0x0000;
  IFR = 0x0000;


  InitPieVectTable();

  InitGpio();

      
      spi_init();

      spi_fifo_init();

  int16 temp ;


  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;
                       

                        DAC(temp);


                 }
    
    return ;





}


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

  • Hi Ahmad,

    How do you want to trigger SOC0? I don't see ADCSOC0CTL.TRIGSEL getting configured anywhere.

    Also, InitAdc() probably should only be called once, so consider moving it out of that for-loop.

    I'm not familiar with Proteus, so I'm not sure what kinds of debugging capabilities it offers. Does it let you step through the code? Can you tell if it's getting stuck at any of those while loops?

    Whitney
  • First Thanks a lot Whitney. I will definitely do that trigsel, I thought when ADCINT1 triggers SOC0 I do not need Trigsel, and I will surely move out  InitAdc() of the for loop.


    And Proteus does not let me do that. after all this is done I will get back to you , but Probably not before Thursday or Friday.


    Thank You for your advise.

    Ahmad

  • Ahmad,

    Okay, it is true that you can use ADCINT1 to trigger the SOC without having to configure TRIGSEL. However, it looks like you have configured ADCINT1 to be triggered by EOC6, but what is supposed to trigger EOC6?

    Whitney
  • Hi Whitney;
     
    First Sorry for being late.If you OR anyone of you can give me your wise suggestion , I will Appreciate.   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 GPIO34/comp2out, GPIO16 is connected to DAC's SDI(Serial Data in)-input,

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

    MY ACINA4/AIO4 is not active (grey)should be red in simulation, and the DAC Output is zero, looks like as if it does not read the ADC channel 4, obviously

    I am doing smth wrong with either ADC or SPI  Which I cannot fix. IF you Can Please help.I am Using Proteus to simulate all this. the code compiles with no

     error.I had changed the code several times changes the SOC triggerings , still the same result, also do I have to do the following so that data to be

    transmitted through SPI is left justified:

                        temp = AdcResult.ADCRESULT0;// right justified
                            shift = temp << 4; // left justified and send shift to DAC();
                           


    Here I paste my complete Code:

    Thanks,

    Ahmad

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

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

    //#define ADC_usDELAY  1000L
    #define TMR0_FREQ    60L
    #define TMR0_PERIOD  100L

    Uint16 temp_measured;

    void DAC(unsigned int value )
    {
        EALLOW;
         char  tem;
         int i,j;
         GpioDataRegs.GPBDAT.bit.GPIO34 = 0; //ChipSelect = 0; for Enable DAC

        
         tem = (value>>8) & 0x0F ;
             tem |= 0x30;

             SpiaRegs.SPITXBUF =tem;

        


        
        while(SpiaRegs.SPISTS.bit.INT_FLAG !=1) {} // Waits until data rx’d
        //for (i=0;i<100;i++);

        i = SpiaRegs.SPIRXBUF; // Clears junk data from itself, dummy read

         tem = value ; //send low byte

         //
         SpiaRegs.SPITXBUF =tem;
         while(SpiaRegs.SPISTS.bit.INT_FLAG !=1) {} // Waits until data rx’d

             for (j=0;j<100;j++);


             

        GpioDataRegs.GPBDAT.bit.GPIO34 =1; //ChipSelect = 1; //Disable DAC
        j = SpiaRegs.SPIRXBUF; // Clears junk data from itself, dummy read




    }


    __interrupt void adc_isr(void)
    {
        temp_measured = AdcResult.ADCRESULT0;
           AdcRegs.ADCINTFLGCLR.bit.ADCINT1 = 1;        //Clear ADCINT1 flag reinitialize for next SOC
           PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;   // Acknowledge interrupt to PIE
           DAC(temp_measured);

           return;
    }


    void initialize_peripheral()
     {
        #ifdef _FLASH
        memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
        #endif
    // PLL, WatchDog, enable Peripheral Clocks
       InitSysCtrl();

    // Clear all interrupts and initialize PIE vector table:
       DINT;
    // Initialize PIE control registers to their default state.
    // The default state is all PIE interrupts disabled and flags are cleared.
       InitPieCtrl();

       IER = 0x0000;
       IFR = 0x0000;

       InitPieVectTable();

       EALLOW;
    // 1. PERIPHERAL CLOCK ENABLES
    //---------------------------------------------------
    // If you are not using a peripheral you may want to switch
    // the clock off to save power, i.e. set to =0
       SysCtrlRegs.PCLKCR0.bit.ADCENCLK = 1;     // ADC
       SysCtrlRegs.PCLKCR3.bit.GPIOINENCLK = 0;    // GPIO
       SysCtrlRegs.PCLKCR3.bit.COMP1ENCLK = 0;    // COMP1
       SysCtrlRegs.PCLKCR3.bit.COMP2ENCLK = 0;    // COMP2
       SysCtrlRegs.PCLKCR0.bit.I2CAENCLK = 0;    // I2C
       SysCtrlRegs.PCLKCR0.bit.SPIAENCLK = 1;       // SPI-A
       SysCtrlRegs.PCLKCR0.bit.SCIAENCLK = 0;      // SCI-A
       SysCtrlRegs.PCLKCR1.bit.ECAP1ENCLK = 0;    // eCAP1
       SysCtrlRegs.PCLKCR1.bit.EPWM1ENCLK = 0;   // ePWM1
       SysCtrlRegs.PCLKCR1.bit.EPWM2ENCLK = 0;   // ePWM2
       SysCtrlRegs.PCLKCR1.bit.EPWM3ENCLK = 0;   // ePWM3
       SysCtrlRegs.PCLKCR1.bit.EPWM4ENCLK = 0;   // ePWM4
       SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;    // Enable TBCLK


    // 2. Timer 0
       CpuTimer0Regs.PRD.all = (long) (TMR0_FREQ * TMR0_PERIOD);
        CpuTimer0Regs.TPR.all  = 0;         // Set pre-scale counter to divide by 1 (SYSCLKOUT):
        CpuTimer0Regs.TPRH.all  = 0;
        CpuTimer0Regs.TCR.bit.TSS = 1;      // 1 = Stop timer, 0 = Start/Restart Timer
        CpuTimer0Regs.TCR.bit.TRB = 1;      // 1 = reload timer
        CpuTimer0Regs.TCR.bit.SOFT = 0;
        CpuTimer0Regs.TCR.bit.FREE = 0;     // Timer Free Run Disabled
        CpuTimer0Regs.TCR.bit.TIE = 1;      // 0 = Disable/ 1 = Enable Timer Interrupt
       CpuTimer0Regs.TCR.all = 0x4001;     // Use write-only instruction to set TSS bit = 0

    // 2. ADC
       AdcRegs.ADCCTL1.bit.ADCREFSEL = 0;      // Select interal BG
       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
    asm(" RPT#100 || NOP");
        AdcRegs.ADCCTL1.bit.INTPULSEPOS=1;        //ADCINT1 trips after AdcResults latch
        AdcRegs.ADCSOC0CTL.bit.ACQPS     = 6;        //set SOC0 S/H Window to 7 ADC Clock Cycles, (6 ACQPS plus 1)
        AdcRegs.INTSEL1N2.bit.INT1SEL    = 0;        //setup EOC0 to trigger ADCINT1 to fire
        AdcRegs.INTSEL1N2.bit.INT1CONT= 0;        //Disable ADCINT1 Continuous mode
        AdcRegs.INTSEL1N2.bit.INT1E   = 1;        //Enabled ADCINT1
        AdcRegs.ADCSOC0CTL.bit.CHSEL     = 4;        //set SOC0 channel select to ADCINA4
        AdcRegs.ADCSOC0CTL.bit.TRIGSEL= 1;        //set SOC0 start trigger on Timer0
       GpioCtrlRegs.AIOMUX1.bit.AIO4 = 2;      // Configure AIO4 for A4 (analog input) operation

       PieVectTable.ADCINT1 = &adc_isr;
        EDIS;

        //3. SPI

         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 =0x008F;                         // Relinquish SPI from Reset and removed LOOBACK??
            SpiaRegs.SPIPRI.bit.FREE = 1;      // Set so breakpoints don't disturb xmission


    //4. GPIO

            EALLOW;
                        GpioCtrlRegs.GPAPUD.bit.GPIO16 = 0;   // Enable pull-up on GPIO16 (SPISIMOA)
                        GpioCtrlRegs.GPAPUD.bit.GPIO17 = 0;   // Enable pull-up on GPIO17 (SPISOMIA)
                        GpioCtrlRegs.GPAPUD.bit.GPIO18 = 0;   // Enable pull-up on GPIO18 (SPICLKA)
                        GpioCtrlRegs.GPAPUD.bit.GPIO19 = 0;   // Enable pull-up on GPIO19 (SPISTEA)

                        GpioCtrlRegs.GPAQSEL2.bit.GPIO16 = 3; // Asynch input GPIO16 (SPISIMOA)
                        GpioCtrlRegs.GPAQSEL2.bit.GPIO17 = 3; // Asynch input GPIO17 (SPISOMIA)
                        GpioCtrlRegs.GPAQSEL2.bit.GPIO18 = 3; // Asynch input GPIO18 (SPICLKA)
                        GpioCtrlRegs.GPAQSEL2.bit.GPIO19 = 3; // Asynch input GPIO19 (SPISTEA)

                        GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 1; // Configure GPIO16 as SPISIMOA
                        GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 1; // Configure GPIO17 as SPISOMIA
                        GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1; // Configure GPIO18 as SPICLKA
                        GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1; // Configure GPIO19 as SPISTEA


                GpioCtrlRegs.GPADIR.bit. GPIO17 = 0;// GPIO17 is input
                GpioCtrlRegs.GPADIR.bit.GPIO16 = 1;

                GpioCtrlRegs.GPADIR.bit.GPIO18 = 1;
                GpioCtrlRegs.GPADIR.bit.GPIO19 = 1;



                    GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0;    // 0=GPIO,  1=COMP2OUT,  2=EMU1,  3=Resv
                    GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1;        // 1=OUTput,  0=INput
                    GpioDataRegs.GPBSET.bit.GPIO34 = 1;
                    GpioDataRegs.GPBDAT.bit.GPIO34 =1; //ChipSelect = 1; //Disable DAC

                EDIS;

                PieCtrlRegs.PIEIER1.bit.INTx1 = 1;    // Enable INT 1.1 in the PIE
                   IER |= M_INT1;
                   EINT;   // Enable Global interrupt INTM

     }











     void main() {

         //int16 temp, shift;
         uint16_t LoopCount;
         LoopCount = 0;

         initialize_peripheral();


      for(;;)
                     {

                         LoopCount++;

                     }
        
        return ;



    }


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

  • Hi Ahmad,

    I ran your code on a F28027x Launchpad and I could see the ADC converting ch4 correctly. I could also see the SPI pins toggling. It appears to be sending two 16-bit words every time DAC() is called. Is that your intention? Or did you want it to send two 8-bit bytes (or just one 16-bit word) instead?

    Whitney

  • Hi Whitney,

    I really wonder what effort you had put for me to help. Thank you very much, that you have even ran the code for

    me.Yes, I tried to transmit by two 8 bits data, through SPI , every time DAC() is called. Now if you suggest that the code is ok, then my

    Project here is done.

    Now All that I had done for learning your MCUs actually, The Main project of mine is to do the almost same thing

    with TMS320f2812 DSP proccesor and with DAC of ti TLV's or MCP4912. As my future attempt I would write a code I

    would ask your kind attention to me that if you would run that code for me just to let me know if every thing is ok

     or not, and later on I would get that TMS320F2812 from your comapny. All this will not be befor the end of

    Jan 2017 at least. If you allow me to get back to you I would be very happy.Cuz the way you dealt with me it's

    really precious for me. I appreciate once again all your effort.

    Thanks , Ahmad.