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.

TMS570LS3137: QUADRATURE PULSE GENERATOR

Part Number: TMS570LS3137

Using  example  code  of encoder quadrature , for  that input  i  have  generate square  wave  pulse  with 90 degree phase shift    between them  ,  now  i  need  to  find  direction  as well as   RPM  ,  i will send  my source  if  any  modification  require please correct me 

#include "HL_sys_common.h"

/* USER CODE BEGIN (1) */
#include "HL_sys_core.h"
#include "math.h"
#include "HL_sci.h"
#include "HL_eqep.h"
#include "HL_etpwm.h"






#define UART sciREG1

void sciDisplayText(sciBASE_t *sci, uint8 *text, uint32 length);

  uint8 ppr = 30U;
uint16 frequency = 2000U;
  uint16 RPM= 0;
uint8 deltaT = 0U;
 uint8 velocity = 0;
  uint8 speed = 0;
 double pi = 3.14;
   uint32 channelA = 0U;
    uint32 channelB = 0U;
    uint8 direction;
  uint32   prevchannelA = 0U;
  uint32   prevchannelB = 0U;


/* USER CODE END */

/** @fn void main(void)
*   @brief Application main function
*   @note This function is empty by default.
*
*   This function is called after startup.
*   The user can use this function to implement the application.
*/

/* USER CODE BEGIN (2) */
 void pulse_width(void)
 {
     etpwmInit();
        etpwmSetClkDiv(etpwmREG1,   ClkDiv_by_1,  HspClkDiv_by_1);
          etpwmSetTimebasePeriod(etpwmREG1, 37499U);
          etpwmSetCount(etpwmREG1, 19750U);
          etpwmSetCounterMode(etpwmREG1, CounterMode_Up);
          etpwmStartTBCLK();

         etpwmInit();
              etpwmSetClkDiv(etpwmREG2,  ClkDiv_by_1,  HspClkDiv_by_1);
              etpwmSetTimebasePeriod(etpwmREG2, 37499U);
              etpwmSetCount(etpwmREG2, 20008U);
              etpwmSetCounterMode(etpwmREG2, CounterMode_Up);
              etpwmStartTBCLK();

 }
/* USER CODE END */

int main(void)
{
/* USER CODE BEGIN (3) */
    QEPInit();
    sciInit();
    _enable_interrupt_();

    pulse_width();

        /* EQEP initialization based on GUI Configuration. */


        /* Enable Position Counter */
        eqepEnableCounter(eqepREG2);

        /* Enable Unit Timer. */
        eqepEnableUnitTimer(eqepREG2);

        /* Enable capture timer and capture period latch. */
        eqepEnableCapture(eqepREG2);


        while(1)
        {
          /* Status flag is set to indicate that a new value is latched in the QCPRD register. */
          if((eqepREG2->QEPSTS & 0x80U) != 0U)
          {

            deltaT = eqepREG2->QCPRD;
            velocity = (ppr / deltaT);
  RPM =  ((frequency * 60)/ ppr);
   speed =   (( RPM * ( 0.92 * pi) *60 )/1000) ;

            /* Clear the Status flag. */
               eqepREG2->QEPSTS |= 0x80U;
          }

           channelA =  etpwmREG1->TBCTR ;
           channelB = etpwmREG2->TBCTR ;
            if (channelA > channelB) {
              if (prevchannelA <= prevchannelB)
                {
                direction = 1;                         /* if   its  channelA is greater than channel B its moving Forward direction  */
              }
            } else if (channelA < channelB) {
              if (prevchannelA >= prevchannelB)
                {
                direction = 2;                         /* if   its  channelA is less than channel B its moving Reverse direction  */
              }
            }

            prevchannelA = channelA;
            prevchannelB = channelB;

  • Hi Jeev,

    I don't see any issues in your code, but you can just simplify pulse_width function code as below:

    void pulse_width(void)
    {
    etpwmInit();
    etpwmSetClkDiv(etpwmREG1, ClkDiv_by_1, HspClkDiv_by_1);
    etpwmSetTimebasePeriod(etpwmREG1, 37499U);
    etpwmSetCount(etpwmREG1, 19750U);
    etpwmSetCounterMode(etpwmREG1, CounterMode_Up);

    etpwmSetClkDiv(etpwmREG2, ClkDiv_by_1, HspClkDiv_by_1);
    etpwmSetTimebasePeriod(etpwmREG2, 37499U);
    etpwmSetCount(etpwmREG2, 20008U);
    etpwmSetCounterMode(etpwmREG2, CounterMode_Up);

    etpwmStartTBCLK();

    }

    Apart from this i don't have any other findings.

    --
    Thanks & regards,
    Jagadish.

  • what modification  i   need to do  in  pulse_width function  ,  as wella as  in need  to find direction ,  if  channel A     lead    Channel B   its  forward direction otherwise it  should  be  reverse direction , for  this  logic  how  should write  a  code 

  • Hi Jeev,

    what modification  i   need to do  in  pulse_width function  ,

    It's just a small modification, please compare my shared previous code with yours, I mean no need to call "etpwmStartTBCLK" and "etpwmInit" two times.

    if  channel A     lead    Channel B   its  forward direction otherwise it  should  be  reverse direction , for  this  logic  how  should write  a  code 

    QDF bit in the QEPSTS register will provide the status of the driection.

    If this bit is 1 means then channel A leads the channel B or else if this bit is 1 means then channel B leads the channel A.

    --
    Thanks & regards,
    Jagadish.