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.