Hi
I'm using TM4C123G board to generate three pair of PWM signal to control 3 phase of BLDC motor.
The code i wrote is enable 3 pwm at the same time, and how to make pwm0 then 120 degree delay to pwm1 then 120 degree to pwm2.
like this, with dead-bane config.
Here is the code :
#include "include.h"
#include "inc/hw_gpio.h"
#include "driverlib/qei.h"
#include "driverlib/pwm.h"
#include "driverlib/fpu.h"
#include "driverlib/rom.h"
inline void ConfigSystem(void)
{
// Config clock
ROM_SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
}
void ConfigPWM(void)
{
//Configures the rate of the clock provided to the PWM module
//= System Clock / 1
ROM_SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
//Enable PWM0
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
//Enable GPIO B
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//Enable GPIO D
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
//Configures PB6, PB7 for use by the PWM0 peripheral.
ROM_GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_7 | GPIO_PIN_6);
ROM_GPIOPinConfigure(GPIO_PB7_M0PWM1);
ROM_GPIOPinConfigure(GPIO_PB6_M0PWM0);
//Configures PB4, PB5 for use by the PWM1 peripheral.
ROM_GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5);
ROM_GPIOPinConfigure(GPIO_PB4_M0PWM2);
ROM_GPIOPinConfigure(GPIO_PB5_M0PWM3);
//Configures PD0, PD1 for use by the PWM2 peripheral.
ROM_GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
ROM_GPIOPinConfigure(GPIO_PD0_M0PWM6);
ROM_GPIOPinConfigure(GPIO_PD1_M0PWM7);
//Set the mode of operation for a PWM generator 0
ROM_PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN |
PWM_GEN_MODE_NO_SYNC);
//Set the mode of operation for a PWM generator 1
ROM_PWMGenConfigure(PWM0_BASE, PWM_GEN_1, PWM_GEN_MODE_DOWN |
PWM_GEN_MODE_NO_SYNC);
//Set the mode of operation for a PWM generator 2
ROM_PWMGenConfigure(PWM0_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN |
PWM_GEN_MODE_NO_SYNC);
//Sets the period of the specified PWM0 generator block - Chu ky = ROM_SysCtlClockGet() / DEFAULT
ROM_PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, ROM_SysCtlClockGet() / DEFAULT);
//Sets the period of the specified PWM0 generator block - Chu ky = ROM_SysCtlClockGet() / DEFAULT
ROM_PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, ROM_SysCtlClockGet() / DEFAULT);
//Sets the period of the specified PWM0 generator block - Chu ky = ROM_SysCtlClockGet() / DEFAULT
ROM_PWMGenPeriodSet(PWM0_BASE, PWM_GEN_3, ROM_SysCtlClockGet() / DEFAULT);
//Sets the pulse width for the specified PWM0 output
//25%-75%
ROM_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0,
(ROM_SysCtlClockGet() / DEFAULT) / 4);
ROM_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1,
(ROM_SysCtlClockGet() / DEFAULT) / 4);
//Enable specified PWM0 output
ROM_PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, true);
ROM_PWMOutputState(PWM0_BASE, PWM_OUT_1_BIT, true);
//Select the inversion mode for the selected PWM0 outputs
ROM_PWMOutputInvert(PWM0_BASE, PWM_OUT_0_BIT, false);
ROM_PWMOutputInvert(PWM0_BASE, PWM_OUT_1_BIT, true);
//Sets the pulse width for the specified PWM1 output
//25%-75%
ROM_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2,
(ROM_SysCtlClockGet() / DEFAULT) / 4);
ROM_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3,
(ROM_SysCtlClockGet() / DEFAULT) / 4);
//Enable specified PWM0 output
ROM_PWMOutputState(PWM0_BASE, PWM_OUT_2_BIT, true);
ROM_PWMOutputState(PWM0_BASE, PWM_OUT_3_BIT, true);
//Select the inversion mode for the selected PWM0 outputs
ROM_PWMOutputInvert(PWM0_BASE, PWM_OUT_2_BIT, false);
ROM_PWMOutputInvert(PWM0_BASE, PWM_OUT_3_BIT, true);
//Sets the pulse width for the specified PWM2 output
//25%-75%
ROM_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_6,
(ROM_SysCtlClockGet() / DEFAULT) / 4);
ROM_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_7,
(ROM_SysCtlClockGet() / DEFAULT) / 4);
//Enable specified PWM0 output
ROM_PWMOutputState(PWM0_BASE, PWM_OUT_6_BIT, true);
ROM_PWMOutputState(PWM0_BASE, PWM_OUT_7_BIT, true);
//Select the inversion mode for the selected PWM0 outputs
ROM_PWMOutputInvert(PWM0_BASE, PWM_OUT_6_BIT, false);
ROM_PWMOutputInvert(PWM0_BASE, PWM_OUT_7_BIT, true);
//Enable PWM generator 0
ROM_PWMGenEnable(PWM0_BASE, PWM_GEN_0);
//Enable PWM generator 1
ROM_PWMGenEnable(PWM0_BASE, PWM_GEN_1);
//Enable PWM generator 2
ROM_PWMGenEnable(PWM0_BASE, PWM_GEN_3);
}
Thank you.
