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 initialization

Other Parts Discussed in Thread: TM4C123GH6PM

Hi, I have the Stellaris launchpad , with the LM4F120 and I'm trying to use the PWM generator to vary the brightness of the LED included, controlled by an ADC input. The problem I have is that when I start the debugger and get to the PWM setup functions an error occurs, as if I had not used the functions to enable it. At first I had the same error with the configuration of the GPIO, until I realized that I lacked the habilitation. In this case I can not find what may be the failure of the code. This is the first part of the program. I really appreciate your time and help, and sorry if my English is not very good.


#define PART_LM4F120H5QR 

#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/cpu.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/pin_map.h"
#include "driverlib/uart.h"
#include <math.h>

#ifdef DEBUG
void__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif

int main(void) {

    SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ); 

    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); 
    SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    SysCtlADCSpeedSet(SYSCTL_ADCSPEED_125KSPS);
    SysCtlPWMClockSet(SYSCTL_PWMDIV_1);

    ADCSequenceDisable(ADC0_BASE, 0);
    ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0); 
    ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH0); 
    ADCSequenceEnable(ADC0_BASE, 0); 

    GPIOPinConfigure(GPIO_PF1_T0CCP1); 
    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3); 
    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);
    GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD_WPU); 
    GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1);    

    PWMGenDisable(PWM1_BASE, PWM_GEN_1);   //This line is where the error occurs
    PWMGenConfigure(PWM1_BASE, PWM_GEN_1, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC | PWM_GEN_MODE_GEN_NO_SYNC); 
    PWMGenPeriodSet(PWM1_BASE, PWM_GEN_1, 1000); 
    PWMPulseWidthSet(PWM1_BASE, PWM_OUT_4, 1); 
    PWMGenEnable(PWM1_BASE, PWM_GEN_1);
    PWMOutputState(PWM1_BASE, PWM_OUT_4, 0);


  • Ernesto Mauro L��pez said:
    Stellaris launchpad , with the LM4F120

    Might it be that your LX4F120 is devoid of any PWM Generator?  (thus dooming any call to, "PWMGen")

    Suspect that you've "adapted" code from more capable MCU - and discovered the horror of "down-sized" version...

    That stripped down MCU can realize PWM via a Timer - config'ed into PWM mode - which should prove suitable for your purpose...

  • Ernesto,

    cb1 is correct in that the root cause of your problem is trying to interface with a peripheral that is not present in the MCU, and that your best bet to get PWM functionality would be to use a timer configured to PWM mode.

    I just wanted to add that if you are doing this in preparation for a larger project that would benefit from having a real PWM module, you might want to investigate the Tiva C Series Launchpad, which uses a TM4C123GH6PM as opposed to the LM4F120H5QR for the microcontroller.  These two chips are almost identical in functionality (aside from the rebranding from Stellaris to Tiva), but one of the few differences is that the Tiva chip does contain two PWM modules.

    If all you're wanting to do is simple LED control, as you originally mentioned in your post, the Stellaris Launchpad should be more than sufficient.  If you're just starting with light control in anticipation of doing something bigger and more complex with PWMs, though, it might be worth grabbing a Tiva Launchpad.

  • Well, that was the problem, I should have known. What confuses me is that I use the StellarisWare libraries, not the TivaWare, and although I knew that it had no PWM module, the functions were there, so I thought they were an adaptation of the timer, to be used as PWM, as you suggested me. Thank you very much for the help

  • Ernesto,

    The StellarisWare (and TivaWare) libraries contain the software functions needed to talk to any hardware peripheral on any Stellaris or Tiva microcontroller that has ever been produced.  As a result, it is very common to find software functions in those libraries that will reference modules not included on the microcontroller you are working with.  As such, it is necessary for you to verify that your MCU contains a hardware peripheral before attempting to use the driverlib functions for that peripheral.  The alternative to this would be to have a separate StellarisWare/TivaWare release for each variant of each family of each chip we release, which would lead to a nightmarishly large collection of software for both us developers and you customers to have to slog through.

    Good luck with your project!

  • @ Ernesto-  Feel your pain - I have provided a "workaround path" - and thank & appreciate your neat, Verify.

    In fairness to this vendor - many, many of both past (now NRND) and current (& future) MCUs are "rich" in such peripheral modules.  But not that MCU - on that particular (very early) board.  (as I like to say, "Ratz")

    Of course what you report/say makes sense - best defense (imho) is to seriously review the key MCU specs - usually appearing early w/in the 1K+ page MCU manual. 

    Another method - "Product Selector" summary sheets exist - enabling you to discern "most" key features - both their presence & number. 

    Of course this usually is "bit much" for new user - but now you know - and even your "stripped" board provides fair value for its price.  And - as John states - new/similar version of your board now "bubbles up" - w/just that feature...

    Bon chance mon ami...