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.

PWM on PA7 (TM4C123BE6PM)

Hi,

I have adapted a example in the Tiva-ware to generate a PWM on the pin PA7.  I am unable to get the PWM output (although I see the output toggling every 5 seconds, which is the effect of the PWMOutputInvert function).

Please see the code given below.  There is no compilation error and I am able to see the messages on the serial port output.  However, I do not see any PWM output.  (The original example to generate PWM on GPIO_PB6_M0PWM0 works fine.)

My eventual goal is to generate the following four PWMs:

  GPIO_PA6_M1PWM2 using PWM1_BASE, PWM_GEN_1

  GPIO_PA7_M1PWM3 using PWM1_BASE, PWM_GEN_1

  GPIO_PB6_M0PWM0 using PWM0_BASE, PWM_GEN_0

  GPIO_PC4_M0PWM6 using PWM0_BASE, PWM_GEN_3

Once again the code for these four PWMs (not included below) compiles without error and other parts of the code (UART, ADCs etc.) seem to work fine except for the PWM output.

Any help would be appreciated.

Regards,

Anand

========================================================================================

#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_memmap.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/pwm.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "driverlib/pin_map.h"


int
main(void)
{
    //
    // Set the clocking to run directly from the external crystal/oscillator.
    // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
    // crystal on your board.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_16MHZ);

    //
    // Set the PWM clock to the system clock.
    //
    SysCtlPWMClockSet(SYSCTL_PWMDIV_1);

    //
    // Set up the serial console to use for displaying messages.  This is
    // just for this example program and is not needed for Timer operation.
    //
    InitConsole();

    //
    // Display the setup on the console.
    //
    UARTprintf("PWM ->\n");
    UARTprintf("  Module: PWM0\n");
    UARTprintf("  Pin: PD0\n");
    UARTprintf("  Configured Duty Cycle: 25%%\n");
    UARTprintf("  Inverted Duty Cycle: 75%%\n");
    UARTprintf("  Features: PWM output inversion every 5 seconds.\n\n");
    UARTprintf("Generating PWM on PWM0 (PD0) -> State = ");

    //
    // The PWM peripheral must be enabled for use.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);

    //
    // For this example PWM0 is used with PortB Pin6.  The actual port and
    // pins used may be different on your part, consult the data sheet for
    // more information.
    // GPIO port B needs to be enabled so these pins can be used.
    // TODO: change this to whichever GPIO port you are using.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    //
    // Configure the GPIO pin muxing to select PWM00 functions for these pins.
    // This step selects which alternate function is available for these pins.
    // This is necessary if your part supports GPIO pin function muxing.
    // Consult the data sheet to see which functions are allocated per pin.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinConfigure(GPIO_PA7_M1PWM3);

    //
    // Configure the PWM function for this pin.
    // Consult the data sheet to see which functions are allocated per pin.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinTypePWM(GPIO_PORTA_BASE, GPIO_PIN_7);

    //
    // Configure the PWM0 to count up/down without synchronization.
    //
    PWMGenConfigure(PWM1_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) * 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.
    // TODO: modify this calculation to use the clock frequency that you are
    // using.
    //
    PWMGenPeriodSet(PWM1_BASE, PWM_GEN_1, 64000);

    //
    // Set PWM0 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 16000 clock ticks (64000 / 4).
    //
    PWMPulseWidthSet(PWM1_BASE, PWM_OUT_3,
                     PWMGenPeriodGet(PWM1_BASE, PWM_OUT_3) / 4);

    //
    // Enable the PWM0 Bit0 (PD0) output signal.
    //
    PWMOutputState(PWM1_BASE, PWM_OUT_3_BIT, true);

    //
    // Enable the PWM generator block.
    //
    PWMGenEnable(PWM1_BASE, PWM_GEN_1);

    //
    // Loop forever while the PWM signals are generated.
    //
    while(1)
    {
        //
        // Print out that the level of PWM is normal.
        //
        UARTprintf("Normal  \b\b\b\b\b\b\b\b");

        //
        // This function provides a means of generating a constant length
        // delay.  The function delay (in cycles) = 3 * parameter.  Delay
        // 5 seconds arbitrarily.
        //
        SysCtlDelay((SysCtlClockGet() * 5) / 3);

        //
        // Invert PWM0 signal.
        //
        PWMOutputInvert(PWM1_BASE, PWM_OUT_3_BIT, true);

        //
        // Print out that the level of PWM is inverted.
        //
        UARTprintf("Inverted\b\b\b\b\b\b\b\b");

        //
        // This function provides a means of generating a constant length
        // delay.  The function delay (in cycles) = 3 * parameter.  Delay
        // 5 seconds arbitrarily.
        //
        SysCtlDelay((SysCtlClockGet() * 5) / 3);

        //
        // Switch PWM0 signal back to regular operation.
        //
        PWMOutputInvert(PWM1_BASE, PWM_OUT_3_BIT, false);
    }
}

  • Anand Paralkar said:
    Seek to generate a PWM on the pin PA7 - am unable to get that PWM output

    Hoping not to duplicate yesterday's PWM crash/burn...  (although one post appears to have succeeded)

    Have looked - everything seems to duplicate code we have - which generates 6 PWM signals across 3 PWM Generators - except for:

    PWMPulseWidthSet(PWM1_BASE, PWM_OUT_3,
                         PWMGenPeriodGet(PWM1_BASE, PWM_OUT_3) / 4);

    My suspicion - the parameter fed to PWMGenPeriodGet() may be incorrect.  Suggest you switch to:

    PWMPulseWidthSet(PWM1_BASE, PWM_OUT_3, 32000);  // should yield 50% duty cycle when PWMGenPeriodSet employs 64000 parameter...

    Do note that you "got" the differing parameter names employed w/in PWMPulseWidthSet() and PWMOutputState().  

    Note: assumes that PWM1_BASE is "alive/well" your MCU - and that PA7 has no "special restrictions."   MCU Manual & errata - surely reveals...
                         

     

  • Hello cb1_mobile,

     

    Sometimes I find it difficult to understand your posts.  This one was simple enough even for me to understand.

     

    And it was spot-on!  Thanks!!  You saved me a great deal of time and agony.

    Thanks for your dedication to this forum,

    Anand

  • Anand Paralkar said:
    Sometimes I find it difficult to understand your posts.

    And - one fears - you are not alone...  Attention deficit + 65WPM touch-typing (credit Jr. High) + demands of full-time tech job - "fast/furious" thought/composition  sometimes yield less than "Stellar" posts...  You should find my "conversations enabled" - if you'd be good enough to provide some specifics (via that means) - I'll do my best to try and incorporate/improve...

    Seek to "fill the gaps" (especially engineering) which often intrude when mating MCU to the "real-world."  Have reasonably broad-based engineering/bsns background - some record of success (right time/place, training/experience, hard work & good fortune) - attempt to, "pay it forward." 

    Thanks for kind remarks - do try to be of some value/service - this/other tech communities...