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.

EK-TM4C1294XL: PWM Problem

Part Number: EK-TM4C1294XL

Hi, I wrote the following code to get a PWM signal on my PF0 pin. I created this function and call it in my main function, however its not doing anything, the LED is not turning on and the oscilloscope i have shows that there is no pulse there can someone please help me out:

void
configurePWM(void)
{
    //
    // Enable the GPIO Peripheral used by PWM (PF0, and eventually PF1)
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

    //
    // Enable PWM0
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);

    //
    //Dividing the clock for PWM use - Will use one for now
    //
    SysCtlPWMClockSet(SYSCTL_PWMDIV_1);

    //
    //Unlocking the pins
    //
    HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
    HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x01;

    //
    // Configure GPIO pin for PWM
    //
    GPIOPinConfigure(GPIO_PF0_M0PWM0);
    GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_0);

    //
    // Cofigure PWM
    //
    PWMGenConfigure(PWM_GEN_0_OFFSET, PWM_GEN_0, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);

    //
    //Setting PWM Period
    //
    PWMGenPeriodSet(PWM_GEN_0_OFFSET, PWM_GEN_0, 99); // 99+1 clock ticks per period just to test on

    //
    //Setting duty cycle
    //
    PWMPulseWidthSet(PWM_GEN_0_OFFSET, PWM_OUT_0 , 49); //49+1 clock ticks per period just to test on

    //
    // Enable PWM
    //
    PWMGenEnable(PWM_GEN_0_OFFSET, PWM_OUT_0);
    PWMOutputState(PWM_GEN_0_OFFSET, PWM_OUT_0_BIT, true);
}

  • I get no compiler errors when i build and debug
  • And I have a solution for your compiler error.
  • Hello Hisham,

    What is PWM_GEN_0_OFFSET being used for? That should be PWM0_BASE for all of your functions. I've never seen a configuration for the PWM using that.

    Please try changing that and see if you get a result.

    Also note the comments from our example code regarding the parameters for PWMGenPeriodSet and PWMPulseWidthSet

        //
        // Set the PWM period to 250Hz.  To calculate the appropriate parameter
        // use the following equation: N = (1 / f) * SysClk.  Where N is the
        // function parameter, f is the desired frequency, and SysClk is the
        // system clock frequency.
        // In this case you get: (1 / 250Hz) * 16MHz = 64000 cycles.  Note that
        // the maximum period you can set is 2^16.
        //
        PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 64000);
    
        //
        // For this example the PWM0 duty cycle will be variable.  The duty cycle
        // will start at 0.1% (0.01 * 64000 cycles = 640 cycles) and will increase
        // to 75% (0.5 * 64000 cycles = 32000 cycles).  After a duty cycle of 75%
        // is reached, it is reset to 0.1%.  This dynamic adjustment of the pulse
        // width is done in the PWM0 load interrupt, which increases the duty
        // cycle by 0.1% everytime the reload interrupt is received.
        //
        PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, 64);

  • Hisham Hafez said:
    PWMGenEnable(PWM_GEN_0_OFFSET, PWM_OUT_0);

    Note that beyond the "inventive" (yet erroneous) creation of,  "PWM_GEN_0_OFFSET"  your coding of this (very) critical function proves (also) in error:

    Hisham Hafez said:
    PWMGenEnable(PWM_GEN_0_OFFSET,  PWM_OUT_0);

    The correct coding would be:   PWMGenEnable(PWM0_BASE,   PWM_GEN_0);


    We should note that the "teased solution" for "compiler error"  - if any  (proposed by another) - surely  "missed"  the  REAL  MISTAKES  -  identified by vendor agent  AND  this reporter...   (both whom should properly  "Share" the Awarding of   Green.)    Somehow that   "green" has already infiltrated ...  THIS post!

  • Hi cb1,

    Good catch, I missed that mismatch as well.

    Though interestingly, I think that may still work unintentionally because of the defines for each match for that one specific case, but using GEN over OUT is key to know for all other channels as only Channel 0 happens to magically have the same define for those 2 variables... a brief success would have invited future Trouble!
  • Wow - that's an EVEN BETTER "catch" than my own!    And indeed - once such (error) - "Appears to work" - it will require the "Sun, Moon & Stars" to all align - before users will "Admit their mistake!"

    "But it worked ONCE" - echoes (far too often) across the fruited plain!      As consultants - my firm faces this,  "Attempt to generalize such "fortuitous solution"  (worked ONCE - and ONLY Once) far more often than desired!

    Unfortunately "Working ONCE" cannot consistently: "Keep the doors open AND bathe staff in the warm, cheering (incandescent) glow!"    (even though such "glow" has been (destructively) banned - especially as such "glow" has been banned...)

  • @CB1 @Ralph Jacobi
    Thank you so Much
  • Thank you - proper "Award of "Green to Each"  (Ralph et moi) would "Complete this thread!"    

    Oh ... don't forget your solicitation of the "helpful" Compiler Error - "teased but not submitted" ...  or (maybe) not!

    And your kind (yet proper) "Award of Green" (before it too is "PULLED" ...  just like "LIKE") is duly recognized, applauded & appreciated...    You are progressing in "leaps & bounds" - good job...