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.

TM4C123GE6PM: Using GPIOAFSEL and GPIOPCTL to enable PWM on PortC in a Tivaware way...

Part Number: TM4C123GE6PM

I am trying to enable PWM using PC4/5.  I understand from the documentation that I must set PMC field and possibly GPIOAFSEL to enable these pins.  I cannot find a syntax example to make this possible.  Can somebody please provide some assistance on how to write to these registers?

I have left out the portion of setting up the PWM clock.  The code worked until I tried to move it to different pins.

Thanks in advance for any help.

SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);  //PWM
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);  //PWM
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);  //GPIO

/*
* Left motor
*/
GPIOPinTypePWM(GPIO_PORTC_BASE, GPIO_PIN_4);
GPIOPinConfigure(GPIO_PC4_M0PWM6);

/*
* right motor
*/
GPIOPinTypePWM(GPIO_PORTC_BASE, GPIO_PIN_5); //pin1 = left motor, pin2 = right motor
GPIOPinConfigure(GPIO_PC5_M0PWM7);

PWMGenConfigure(PWM0_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN);


/*
* left motor
*/
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_6, 0);  // no motor speed
PWMOutputState(PWM0_BASE, PWM_OUT_6_BIT, true);

/*
* right motor
*/
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_7, 0);  // no motor speed
PWMOutputState(PWM0_BASE, PWM_OUT_7_BIT, true);

PWMGenEnable(PWM0_BASE, PWM_GEN_3);


PWMPulseWidthSet(PWM0_BASE, motor, 50 * ui32Load / 1000 );

  • Hi Alen,

     Do you mean the project will not compile if you choose PC4/5?  How did you create the project? Did you select Tiva TM4C123GE6PM like below?

  • Find your code to be generally well presented - you evidence good knowledge of the PWM API - yet several items "unsettle:"

    • PWMPeriodSet() ...  vital - yet  unshown!
    • Use of  "motor" (in place of PWM_OUT_n)  w/in final PWMPulseWidthSet   yet  "motor"  is  NOWHERE Defined!
    • Too early use of   a  "complex expression" w/in that last PWMPulseWidthSet().     Always best to FIRST - make the function WORK -  only then add complexity!
    • You load "0" into both (earlier) PWMPulseWidthSet() ...  it is established that  PWM Values  "At their limits"  frequently misbehave.    A "safe" value  (NOT 0 or 100%) - verifies code's operation!

    It must be assured that no Hardware - nor any (unsuspected) connections - intrude upon PC4/PC5.

    You appear well on your way - you've mastered the "usual suspects" - guidelines above aim to speed, ease, enhance your success...

  • It was a quite a silly issue with syntax, thank for your assistance.  It is working now.

  • Good that - might you "share the syntax fix" - for the benefit of "follow-on" readers?       And it is hoped that you "restricted/guard-banded" PWM from the "extremes."    ("0 & 100%" duty cycle - where "misbehavior" has been noted...)

  • Not that much to tell really.  I just have the PWM module for my program cut up into several function, and when moving the pins I missed an argument, despite having a really good look (writing code at night).  The pins required PWM 0 and I neglected to notice where it was still set to 1.  It worked out for the best as I have much better understanding of it, going through the trouble shoot.  

    In the end, I set it back similar to example 15 of the Tivaware Workbook after modifying the LaunchPad XL GPIO breakout board I am using.  I was running out of pins having serveral pins restriected to input only by the breakout.  I removed the buffers permitting bi directional use again.

  • Thank you - appreciated.
    I'm still at a loss - you (now) note (past) use of "PWM_1" - yet the code you presented here - (properly) targeted PWM_0.    

    What "did" appear questionable was: Lack of the call to: "PWMGenPeriodSet()" and "Setting PWM Duty" to its "0" extreme.

    Your "removal/bypass of the buffers" was resourceful.    It's said that MCU users can "never" be: too rich, too thin, or have too many Bi-Di GPIO.     As you likely know - "Port Expander chips" - can well satisfy - item 3.

    As you're "running a motor" via the MCU's PWM Generator - an excellent,  "PWM Fault pin" exists - which may immediately & efficiently,  "HALT ALL PWM Outputs" - in response to most any "Error Condition Detect" - you can devise.     (over-current, over-temp, deviation from the control loop(s) ... for example)      Surely warrants your consideration...