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.

TMS320F28377D: ePwm timer direction at start in up/down counting mode

Part Number: TMS320F28377D


Hi!

Is there a simple way to start ePwm timer counting down when in up/down counting mode? I know that it can be done with phase control once the timers are running, but I'd prefer to have proper phase relation from the start.

  • Hi Mitja,

    I believe the only way to do this is with the phase control you are referring to. You could use the phase control and use a forced software sync at the end of configuration to load the phase values.

    Regards,
    Kris
  • Yup you can only set the dirrection via phase control mechanism. But SW sync force work only when counter is running, you if you do this at initialization you have to start the timer, SW force sync event and then stop the timer.

    For anybody else stumbling on this, this is my solution:

    /**************************************************************
    * Set pwm timer directions - only works properly when timer is stopped
    * arg1: address of a timer structure (e.g. &EPwm1Regs)
    * arg2: the diretion of the time (either TB_UP==0x1 or TB_DOWN=0x0)
    **************************************************************/

    void PWM_timer_set_dir(volatile struct EPWM_REGS *pwm_module, unsigned int dir)
    {
        // store timer context
        Uint16 tmr_ctr = pwm_module->TBCTR;
        Uint16 tmr_ctl = pwm_module->TBCTL.all;
        Uint32 tmr_phs = pwm_module->TBPHS.all;

        // set counter in the middle in order to prevent trigering zero or period events
        pwm_module->TBCTR = pwm_module->TBPRD * 0.5;
        pwm_module->TBPHS.bit.TBPHS = pwm_module->TBPRD * 0.5;

        // set the direction in up counting mode
        if (dir == TB_UP)
        {
            pwm_module->TBCTL.bit.PHSEN = TB_ENABLE;
            pwm_module->TBCTL.bit.PHSDIR = TB_UP;
            pwm_module->TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
            pwm_module->TBCTL.bit.SWFSYNC = 1;
            pwm_module->TBCTL.bit.CTRMODE = TB_FREEZE;
            pwm_module->TBCTL.bit.PHSEN = TB_DISABLE;
        }
        // set the direction in down counting mode
        if (dir == TB_DOWN)
        {
            pwm_module->TBCTL.bit.PHSEN = TB_ENABLE;
            pwm_module->TBCTL.bit.PHSDIR = TB_DOWN;
            pwm_module->TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
            pwm_module->TBCTL.bit.SWFSYNC = 1;
            pwm_module->TBCTL.bit.CTRMODE = TB_FREEZE;
            pwm_module->TBCTL.bit.PHSEN = TB_DISABLE;
        }
        // restore timer context
        pwm_module->TBCTR = tmr_ctr;
        pwm_module->TBCTL.all = tmr_ctl;
        pwm_module->TBPHS.all = tmr_phs;
    }