Part Number: TM4C1294NCPDT
Tool/software:
Hi,
I have PWM module on my Tiva.
I use the code bellow (according to the PWM invert example) and its working.
But I would like to generate the PWM module so that I won't need the while(1) loop, I want that all the PWM system will work automatically with fixed duty cycle and fixed frequency, without me handling and performing the SysCtlDelay by myself..
How I do that?
I attached my code.
Thanks,
Tzipi
void PortFunctionInit(void)
{
...
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
//
// Enable pin PG1 for PWM0 M0PWM5
//
ROM_GPIOPinConfigure(GPIO_PG1_M0PWM5);
ROM_GPIOPinTypePWM(GPIO_PORTG_BASE, GPIO_PIN_1);
//
// Enable pin PG0 for PWM0 M0PWM4
//
ROM_GPIOPinConfigure(GPIO_PG0_M0PWM4);
ROM_GPIOPinTypePWM(GPIO_PORTG_BASE, GPIO_PIN_0);
...
}
int PwmInit(uint32_t ui32SysClock)
{
uint32_t ui32PWMClockRate;
//
// Set the PWM clock to be SysClk / 8.
//
ROM_PWMClockSet(PWM0_BASE, PWM_SYSCLK_DIV_8);
//
// Use a local variable to store the PWM clock rate which will be
// 120 MHz / 8 = 15 MHz. This variable will be used to set the
// PWM generator period.
//
ui32PWMClockRate = ui32SysClock / 8;
//
// Configure PWM2 to count up/down without synchronization.
//
ROM_PWMGenConfigure(PWM0_BASE, PWM_GEN_1,
PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
//
// Set the PWM period to 250Hz. To calculate the appropriate parameter
// use the following equation: N = (1 / f) * PWMClk. Where N is the
// function parameter, f is the desired frequency, and PWMClk is the
// PWM clock frequency based on the system clock.
//
ROM_PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, (ui32PWMClockRate / 250));
//
// Set PWM2 to a duty cycle of 25%. You set the duty cycle as a function
// of the period. Since the period was set above, you can use the
// PWMGenPeriodGet() function. For this example the PWM will be high for
// 25% of the time or (PWM Period / 4).
//
ROM_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_4,
ROM_PWMGenPeriodGet(PWM0_BASE, PWM_GEN_1) / 4);
ROM_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_5,
ROM_PWMGenPeriodGet(PWM0_BASE, PWM_GEN_1));
//
// Enable PWM Out Bit 2 (PF2) output signal. PWM_OUT_4_BIT
//
ROM_PWMOutputState(PWM0_BASE, (PWM_OUT_4_BIT | PWM_OUT_5_BIT), true);
//
// Enable the PWM generator block.
//
ROM_PWMGenEnable(PWM0_BASE, PWM_GEN_1);
return 0;
}
int PwmBlinkLedsLoop(uint32_t ui32SysClock, int times)
{
//
// Loop forever while the PWM signals are generated.
//
while(times>0)
{
//
// Print out that the level of PWM is normal.
//
printf("PWM Output: Normal\n");
//
// This function provides a means of generating a constant length
// delay. The function delay (in cycles) = 3 * parameter. Delay
// 2 seconds arbitrarily.
//
SysCtlDelay((ui32SysClock) / 3);
//
// Invert PWM2 signal. PWM4
//
ROM_PWMOutputInvert(PWM0_BASE, PWM_OUT_4_BIT, true);
ROM_PWMOutputInvert(PWM0_BASE, PWM_OUT_5_BIT, false);
//
// Print out that the level of PWM is inverted.
//
printf("PWM Output: Inverted\n");
//
// This function provides a means of generating a constant length
// delay. The function delay (in cycles) = 3 * parameter. Delay
// 2 seconds arbitrarily.
//
SysCtlDelay((ui32SysClock) / 3);
//
// Switch PWM2 signal back to regular operation. PWM4
//
ROM_PWMOutputInvert(PWM0_BASE, PWM_OUT_4_BIT, false);
ROM_PWMOutputInvert(PWM0_BASE, PWM_OUT_5_BIT, true);
times--;
}
ROM_PWMOutputInvert(PWM0_BASE, PWM_OUT_4_BIT | PWM_OUT_5_BIT, false);
return 0;
}