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.

Need help in setting the GPIO pins for generating a pulse

Other Parts Discussed in Thread: TMS320F28335, CONTROLSUITE

Hai,

   I am working on TMS320F28335. I need to generate a pulse when the sampled value through ADC is less than a specified value. Following is the piece of code I am trying to use. Kindly help me in getting the proper output.

#include "DSP2833x_Device.h"

extern void InitAdc(void);
extern void InitSysCtrl(void);
extern void InitPieCtrl(void);
extern void InitPieVectTable(void);

void Gpio_select(void);

#define AVG        100  // Average sample limit
#define BUF_SIZE   100  // Sample buffer size

Uint16 SampleTable1[BUF_SIZE];

float Output1[BUF_SIZE];

void main(void)
{
       Uint16 i;
         
    InitSysCtrl();    // Basic Core Init from DSP2833x_SysCtrl.c  
      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
        
    // Disable CPU interrupts and clear all CPU interrupt flags:
       IER = 0x0000;
       IFR = 0x0000;
    
    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 = 1;        // divide by 2
    AdcRegs.ADCTRL1.bit.CONT_RUN = 1;    // Continous run mode
    
    AdcRegs.ADCTRL2.all = 0;            
    AdcRegs.ADCTRL2.bit.SOC_SEQ1 = 1;    // 1=Software trigger
    AdcRegs.ADCTRL2.bit.EPWM_SOCA_SEQ1 =0;    // 0=SEQ1 cannot be started by ePWMx 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

    AdcRegs.ADCMAXCONV.all = 0x0000;   
    AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0;
   
       
       for (i=0; i<AVG; i++)
     {
        while (AdcRegs.ADCST.bit.INT_SEQ1== 0) {} // Wait for interrupt
        AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1;
        SampleTable1[i] =((AdcRegs.ADCRESULT0>>4) );
     
         if(SampleTable1[i]<685)
        { 
            GpioDataRegs.GPASET.bit.GPIO12 = 1;
        }
        else
        {           
            GpioDataRegs.GPASET.bit.GPIO12 = 0;
        }
        }  
       
     }
 
void Gpio_select(void)
{
    EALLOW;
    GpioCtrlRegs.GPAMUX1.all = 0;        // GPIO15 ... GPIO0 = General Puropse I/O
    GpioCtrlRegs.GPAMUX2.all = 0;        // GPIO31 ... GPIO16 = General Purpose I/O
    GpioCtrlRegs.GPBMUX1.all = 0;        // GPIO47 ... GPIO32 = General Purpose I/O
    GpioCtrlRegs.GPBMUX2.all = 0;        // GPIO63 ... GPIO48 = General Purpose I/O
    GpioCtrlRegs.GPCMUX1.all = 0;        // GPIO79 ... GPIO64 = General Purpose I/O
    GpioCtrlRegs.GPCMUX2.all = 0;        // GPIO87 ... GPIO80 = General Purpose I/O
    
    GpioCtrlRegs.GPADIR.all = 0;
        
    GpioCtrlRegs.GPBDIR.all = 0;        // GPIO63-32 as inputs

    GpioCtrlRegs.GPCDIR.all = 0;        
 
    GpioCtrlRegs.GPAMUX1.bit.GPIO12= 0; 
     GpioCtrlRegs.GPADIR.bit.GPIO12 = 1;  
 
    
       
    EDIS;
    
}


  • Jaya,

    Your code looks good for the most part.  What exactly isn't working?


    Trey

  • Trey,

       I want to see the output using a DSO or CRO. When I try that, I am not getting any wave instead I am getting some signal which looks like some noise. Is the code what I have written  is sufficient to generate a square wave?

    if(SampleTable1[i]<685)
            {
                GpioDataRegs.GPASET.bit.GPIO12 = 1;
            }
            else
            {
                GpioDataRegs.GPASET.bit.GPIO12 = 0;
            }
           
    Now if I take output from GPIO pin number 12 will I get a proper signal?

  • Jaya,

    Ooops, when I looked at your code earlier it didn't click that you are using the set register.  The set register can only be used to set the pin.  Instead I think you want to use the dat register which can be used to set or clear the pin.

    You shouldn't ever really see noise on a pin (other than thermal noise).  How big is the voltage swing on the noise?  If its a few millivolts, this is nothing to worry about, but if its bigger than a volt something very wrong is happening.

    Trey

  • Trey,

        Thanks for the quick reply. I think The noise signals what I am getting is similar to what you have mentioned. It is coming in millivolts only. So as per your view, I can ignore that. But my actual issue is how can I get a proper signal Will the code I mentioned is sufficient enough to get a proper signal which I can view through a DSO or CRO?

  • Trey,

        Thanks for the quick reply. I think The noise signals what I am getting is similar to what you have mentioned. It is coming in millivolts only. So as per your view, I can ignore that. But my actual issue is how can I get a proper signal Will the code I mentioned is sufficient enough to get a proper signal which I can view through a DSO or CRO?

    I replaced my code where I am using the SET bit with DAT bit. 

  • Jaya,

    Did it start working when you changed the code to use the "DAT" bit?  If not, is execution making its way into the "IF" statement where you change the pin state?  If not, then why is that happening?  You may have to do some debug work here.  Just walk through your code and see what is happening.  If something specific isn't happening as expected, try to figure out why.

    Trey

  • Hai Trey,

    I did as you have suggested. ie to use DAT in place of SET. Below is the code I am using, but still I am not getting proper pulses.

    if((SampleTable1[i-1]==0)&&(SampleTable1[i]<0))
            
            {
                GpioDataRegs.GPASET.bit.GPIO9 = 1;
               
            }
            else
            {
                GpioDataRegs.GPACLEAR.bit.GPIO9 = 1;
             
            }

    My idea is to do Zero Crossing Detector. When ever sampled signal does ZCD, I want to generate pulse. Please guide me in completing this. Another point I would like to clarify is whether I can control the sampling rate any way? If I need only 60 samples per cycle, what should be my code?

  • Jaya,

     That code should toggle the pin, so there must be something else going on.  Are you sure the GPIO registers are getting set as you have commanded in your code?  Take a look at the GPIO register memory to ensure this is happening.  In controlSUITE we provide several examples which show how to use the GPIOs.  Take a look at these and see if there are any major differences between the examples and your code.  If you run one of the examples does it successfully toggle the pin on your board?

    I can only provide so much support.  My job is not to write your code, that being said the best way to do zero crossing detection is with one of the on chip comparators.  This will give you an instantaneous indication that zero crossing has occurred.  You could also potentially use an external interrupt.  There are examples of both of these in controlsuite.  Read the user guides for these peripherals.

    Yes you can control the sample rate.  I would use a timer to generate an interrupt at the sample rate you desire and force a start of conversion in the timer interrupt service routine.  Once again, I cannot write this code for you, but there is more than enough information in the user guides and examples for you to learn and write this code yourself.

    Best of luck,

    Trey German