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.

[SOLVED] PWM output using Timer0 A on LM4F232

Hallo,

Hi have EKS-LM4F232 eval board and I want to output a PWM signal from pin PF0 using Timer0 (T0CCP0).

Following some examples I wrote this:

 

#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/rom.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "inc/lm4f232h5qd.h"
 
int main(void)
{
    // Set the clocking to run directly from the crystal.
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
 
     // Enable the peripherals used by this example.
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
 
    // Configure GPIO pin as PWM output
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    ROM_SysCtlPeripheralReset(SYSCTL_PERIPH_GPIOF);
 
    ROM_GPIOPinConfigure(GPIO_PF0_T0CCP0);
//    ROM_GPIODirModeSet(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_DIR_MODE_HW);
    ROM_GPIOPinTypeTimer(GPIO_PORTF_BASE, GPIO_PIN_0);
 
    // Configure the timer.
    unsigned long uPeriod;
    uPeriod = ROM_SysCtlClockGet( ) / 50000;
    ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_A_PWM);
    ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, uPeriod);
    ROM_TimerMatchSet(TIMER0_BASE, TIMER_A, uPeriod>>1);
 
    // Enable the timers.
    ROM_TimerEnable(TIMER0_BASE, TIMER_A);
 
    // Loop forever and blink the led while the timers run.
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_2);
    while(1)
    {
            ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_2, GPIO_PIN_2);
            ROM_SysCtlDelay(300000);
            ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_2, 0);
            ROM_SysCtlDelay(300000);
    }
}

Unfortunately pin PF0 doesn’t move. The led on pin PG2 blink, so this firmware is running.

Where am I wrong? How to output the PWM signal on PF0?

 

Best regards

Max

  • PF0 receives special treatment in the M4F series - this is detailed w/in MCU datasheet (GPIO section as I recall) and multiple times w/in this forum.

    You must first unlock this pin - then you may re-purpose it away from "special default" to your mode of choice...  Datasheet and forum articles describe this procedure...

     

  • OK,

    I tried to change the port, since T0CCP0 signal can be routed also on PortL bit 0.


    It still doesn't work. Now I check PL0 pin that doesn't move.

    I dont know if I'm wrong on seting the GPIO config or Timer config (or both).

    best regards

    new source code:

    #include "inc/hw_memmap.h"

    #include "inc/hw_types.h"

    #include "driverlib/sysctl.h"

    #include "driverlib/timer.h"

    #include "driverlib/rom.h"

    #include "driverlib/pin_map.h"

    #include "driverlib/gpio.h"

    #include "inc/lm4f232h5qd.h"

     

    int main(void)

    {

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

        ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

     

         // Enable the peripherals used by this example.

        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);

     

        // Configure GPIO pin as PWM output

        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOL);

     

        ROM_GPIOPinConfigure(GPIO_PL0_T0CCP0);

        ROM_GPIOPinTypeTimer(GPIO_PORTL_BASE, GPIO_PIN_0);

     

        // Configure the timer.

        ROM_TimerConfigure(TIMER0_BASE,  TIMER_CFG_A_PWM);

        ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, 20000);

        ROM_TimerMatchSet(TIMER0_BASE, TIMER_A, 10000);

     

        // Enable the timer.

        ROM_TimerEnable(TIMER0_BASE, TIMER_A);

     

        // Loop forever and blink the led while the timers run.

        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);

        ROM_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_2);

        while(1)

        {

                ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_2, GPIO_PIN_2);

                ROM_SysCtlDelay(300000);

                ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_2, 0);

                ROM_SysCtlDelay(300000);

        }

    }

  • Sorry - short  of time - here is quick suggestion - will respond later in day (more detail) if necessary.  (thank you for kind "verify" earlier)

    Believe that you got earlier Port/Pin to work w/Timer.  Kindly realize - most ARM MCUs do not route the timer signal to a GPIO for output.  (usually this is accomplished via a timer triggered interrupt - operating upon a non-Timer GPIO pin)  Possibly this is your issue.

    If not - and you have one timer working successfully - use your IDE to log and print the key register settings for both related GPIO and Timer Registers for the working timer.  Then your job reduces to a simple - register by register comparison - working timer against failing one.  GPIOPCTL (or similar - am mobile now - no ref material) - is likely suspect.  Give a try please and report - good luck...

    *** Update - reread your 1st post - see that you want a Timer-created PWM signal.  Be sure to check SW-DRL-UG which should have such an example.  Indeed - the Timer GPIO can properly output a high quality PWM signal - with some cautions @ duty = 0 and 100%.  (my earlier comment - not your issue - spoke to most ARMs not providing a direct timer output to a GPIO pin - not your issue...)

  • You came SO close - appears that some haste did you in.  Look @ your updated code:

    ROM_TimerConfigure(TIMER0_BASE,  TIMER_CFG_A_PWM);

    Your initial listing included: "TIMER_CFG_16_BIT_PAIR" - while deprecated this should have worked - but Port pin "PF0" was too complex/defaulted.  So - here's the suggested fix to drive a PWM signal to your CCP0 pin:

    ROM_TimerConfigure(TIMER0_BASE , TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM);

    A review of the defines & asserts w/in timer.c and timer.h will highlight the impact which "TIMER_CFG_SPLIT_PAIR" brings to your result...  Appreciate your update after loading/exercising this code...

     

  • thank you very much.

    now it works

    best regards

  • Dear Sir,

    we want to use the Timer as PWM output and we follow the following congiuration but we dont get any output the pin stays at 3.3V only is the below configuration need anything else?

    void PWM_Configuration(void)
    {
    // Configure PA3 as T1CCP1
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    ROM_GPIOPinConfigure(GPIO_PA3_T1CCP1);
    ROM_GPIOPinTypeTimer(GPIO_PORTA_BASE, GPIO_PIN_3);
    }
    void PWM_Start(void)
    {
    ROM_TimerMatchSet(TIMER1_BASE, TIMER_A, 250);
    }

    void Timer1_A_PWM_Configuration(unsigned int Period_ms)
    {
    unsigned long ulPeriod,dutyCycle;

    ulPeriod = 1000;
    dutyCycle = 500;

    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
    ROM_TimerConfigure(TIMER1_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM);
    ROM_TimerLoadSet(TIMER1_BASE, TIMER_A, 20000);
    ROM_TimerMatchSet(TIMER1_BASE, TIMER_A, 10000); // PWM
    ROM_TimerEnable(TIMER1_BASE, TIMER_A);
    }

    in main loop
    void
    main(void)
    {
    //
    // Run from the PLL at 60 MHz.
    //
    ui32SysClock = ROM_SysCtlClockFreqSet((SYSCTL_XTAL_16MHZ |
    SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
    SYSCTL_CFG_VCO_480), 60000000);

    PWM_Configuration();
    Timer1_A_PWM_Configuration(1000);

    while(1)
    {
    }

    }

    Please suggest any configuration if missing!!
  • it seems that all is right. I don't know why it should not work.
    At the moment I cannot test your code, but maybe in few days...

    best regards
    Max