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.

EKS-LM4F232 PWM example

I am trying to create an example of PWM working on an EKS-LM4F232.  I have an LED that is driven off of PG3.  When used as a GPIO, I can turn the LED on and off, so I conclude that my circuit is working.  I then stop using PG3 as an output pin and set it up to use as a PWM pin.  With a scope on PG3, I see nothing happening.  There are no examples of PWM for the M4 chip, but I have used examples for M3 chips and from Stellaris DRL pdf as a guide. 

I should expect to see PG3 work at a 50% duty cycle for the following code, correct?

I put in a forever loop in order to change the duty cycle via breakpoint and variable modification.

void PWM_Init(void) {

SysCtlPWMClockSet(SYSCTL_PWMDIV_1);

SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);

GPIOPinConfigure(GPIO_PG3_M1PWM1);

GPIOPinTypePWM(GPIO_PORTG_BASE, GPIO_PIN_3);

ulPeriod = 4000;

PWMGenConfigure(PWM1_BASE, PWM_GEN_0, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);

PWMGenPeriodSet(PWM1_BASE, PWM_GEN_0, ulPeriod);

PWMPulseWidthSet(PWM1_BASE, PWM_OUT_1, ulPeriod/2);

PWMOutputState(PWM1_BASE, PWM_OUT_1_BIT, true);

PWMGenEnable(PWM1_BASE, PWM_GEN_0);

while (1) {

int i = ulPeriod;

PWMPulseWidthSet(PWM1_BASE, PWM_OUT_1, i);

}

}

void main(void)

{

// Enable lazy stacking for interrupt handlers.

FPULazyStackingEnable();

// Set the clocking to run directly from the crystal.

SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |

SYSCTL_XTAL_16MHZ);

PWM_Init();

  • John,

    I'm going to take a deeper look into your code shortly. After reviewing it briefly -

    • Why do you reset the PWM pulse width continously at the end of the PWM_Init() function?
    • What is the initial value of ulPeriod?

    So you're wanting ulPeriod to be variable so that you may change the duty cycle of PWM1. Can you get a simple 50% working with an empty while(1)? You may want to add logic to check if ulPeriod has changed. It is has, only then will it call PWMPulseWidthSet.

    All the other setup looks great.

  • The while loop is just for trying to figure PWM out.  I assume that if I put a break point within the loop, the CPu stops, but not the subsystems.  I intended to change the value of i and just step through the while loop to give me 0, 25, 50 , 75 and 100% and confirm on a scope.

    Am I correct that the PWM subsystem will keep generating a signal witht he CPU stopped at a breakpoint?  (seems to be the truth with the CAN subsystem)

    With the while loop being empty, I still get nothing on PG3.

    Thanks for taking the time to inspect my example code.

  • Not quite sure how this worked out, but I needed to reboot my computer, which caused me to quit CSS and provided a power cycle to the EKS.  With this cycle, the PWM is working out of PG3, even though no new code was loaded.  Restarting CSS and downloading the program I had downloaded prior to the computer reboot is working as well. 

    I have seen something similar before.  Power cycling the EKS seems to get the virtual terminal working again.  (While debugging in CSS, reset the EKS using the reset button, the virtual terminal will lock, and whatever comm port it was on, say COM13, is no longer avaiable.  Cycle power to the EKS and COM13 is again available and  my com program was getting expected output from the EKS.)

  • John,

    I was just able to get your code running as well. Its odd that you needed to power cycle. Good to see you're running again.