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.

TM4C123GH6PM: Phase shifted PWM Generator

Part Number: TM4C123GH6PM

Hello,

I have 2ch PWM generator. What I need to try is to put 180 degree phase shift  on the outputs, so both outputs are not 1 at the same time. Naturally we should limit the PWM duty cycle to 50 percent maximum to accommodate that both outputs are not on at the same time.

I am able to make left aligned or center aligned pwm signals, Making searches on this forum for previous post on the subject I came up with:

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "inc/hw_gpio.h"
#include "inc/hw_i2c.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_nvic.h"
#include "driverlib/sw_crc.h"
#include "driverlib/fpu.h"
#include "driverlib/timer.h"
#include "driverlib/gpio.h"
#include "driverlib/i2c.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/eeprom.h"
#include "driverlib/flash.h"
#include "driverlib/systick.h"
#include "driverlib/qei.h"
#include "driverlib/adc.h"
#include "driverlib/uart.h"
#include "driverlib/hibernate.h"
#include "driverlib/ssi.h"




int main(void) {

    MAP_FPUEnable();
    MAP_FPULazyStackingEnable();
    MAP_SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);

    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);


    uint8_t PWM_DIV = 16;
    uint16_t PWM_SCALE = 256;
    MAP_SysCtlPWMClockSet(SYSCTL_PWMDIV_16);

    uint16_t PWM_FRQ = 100;
    uint32_t ui32SysClkFreq = MAP_SysCtlClockGet();

    uint32_t ui32PWMClock = ui32SysClkFreq / PWM_DIV;
    uint32_t ui32Load = (ui32PWMClock / PWM_FRQ) - 1;

    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);

    MAP_GPIOPinConfigure(GPIO_PB6_M0PWM0);
    MAP_GPIOPinConfigure(GPIO_PB7_M0PWM1);

    MAP_GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6);
    MAP_GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_7);

    MAP_PWMGenConfigure(PWM0_BASE, PWM_GEN_0,
           (PWM_GEN_MODE_UP_DOWN |
            PWM_GEN_MODE_GEN_SYNC_LOCAL |
            PWM_GEN_MODE_DB_SYNC_LOCAL)
    );

    // MAP_PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);

    MAP_PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, ui32Load);

    MAP_PWMGenEnable(PWM0_BASE, PWM_GEN_0);
    MAP_PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT | PWM_OUT_1_BIT, true);

    // calculate pulse width
    uint32_t ui32Width = (64 * ui32Load) / PWM_SCALE;

    MAP_PWMSyncTimeBase(PWM0_BASE, PWM_GEN_0_BIT | PWM_GEN_2_BIT | PWM_GEN_3_BIT);
    MAP_PWMSyncUpdate(PWM0_BASE, PWM_GEN_0_BIT | PWM_GEN_2_BIT | PWM_GEN_3_BIT);

    MAP_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, ui32Width);
    MAP_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, ui32Width * 2);


    while(true) {
        MAP_SysCtlDelay(1000);
    }
}

How can I use the synced pwm feature of the microcontroller to generate phase shifted pwm signals, with each varying duty cycles up to 50% ?

Best Regards,

C.A.

  • Hello C.A.

    Your code setup is a little confusing for me.

    The topic at hand is syncing PWM signals and the ability to do this is provided with the PWM Sync APIs you are using, but the synchronization is across multiple generators. Your API does call for multiple generators to be synchronized but your actual PWM output is only for GEN_0 so GEN_2 and GEN_3 aren't even used here.

    Since you are trying to get a 180 degree phase shift so only one signal is at a '1' at a time, have you considered using MAP_PWMOutputInvert?

    Best Regards,

    Ralph Jacobi

  • Hello Ralph Jacobi,

    I am using only 1 pwm generator, but using 2 outputs, to obtain 2 independent pwm signals.

    I understand in order to sync pwms, I need to use 2 different generators, is that right?

    Would it also be possible to use the same pins, PB6 and PB7 respectively, connected to two different pwm generators? or would i need to make a hardware change.

    Since you are trying to get a 180 degree phase shift so only one signal is at a '1' at a time, have you considered using MAP_PWMOutputInvert?

    I am not trying to get a 180 degree phase shift, I need two independent pwm signals, each variable independently, but still have them not on at the same time.

    Is this accomplishable?

    Best Regards,

    C.A.

  • Hello C.A.,

    Would it also be possible to use the same pins, PB6 and PB7 respectively, connected to two different pwm generators?

    No, those pins are tied to PWM0 Gen 0.

    I am not trying to get a 180 degree phase shift, I need two independent pwm signals, each variable independently, but still have them not on at the same time.

    Is this accomplishable?

    If you want them fully independent, you'd need to use separate generators. While you can control the individual outputs of a single generator, the same period is used for each signal of a given generator so you are bound by that which I think will make your specific need to avoid them being on at the same time to be more difficult. I think individual PWM generators would let you get what you want easier.

    Best Regards,

    Ralph Jacobi