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.

TIVA C PWM (lowlevel)

Other Parts Discussed in Thread: TM4C123GH6PM

Greetings from Germany,

I've got a little problem with the initialization of the PWM module of my tm4c123gh6pm. I've written the following code:

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_sysctl.h"
#include "driverlib/timer.h"
#include "inc/hw_pwm.h"
#include "driverlib/pwm.h"

int main(void) {
    volatile uint32_t foo;
    SYSCTL_RCC_R = SYSCTL_XTAL_16MHZ | SYSCTL_USE_PLL | SYSCTL_SYSDIV_5 | SYSCTL_OSC_MAIN;
    SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOF;
    foo = SYSCTL_RCGC2_R;
    SYSCTL_RCGC0_R |= SYSCTL_RCGC0_PWM0;
    GPIO_PORTF_AFSEL_R |= GPIO_PIN_3;
    GPIO_PORTF_DEN_R |= GPIO_PIN_3;
    GPIO_PORTF_PCTL_R |= GPIO_PCTL_PF3_M1PWM7;
    SYSCTL_RCC_R |= SYSCTL_RCC_PWMDIV_4;
    PWM1_CTL_R |= 0;
    PWM1_3_GENA_R |= 0x0000008C;
    PWM1_3_GENA_R |= 0x0000080C;
    PWM1_3_LOAD_R = 0x0000018F;
    PWM1_3_CMPA_R = 0x0000012B;
    PWM1_3_CMPB_R = 0x00000063;
    PWM1_CTL_R |= PWM_3_CTL_ENABLE;
    PWM1_ENABLE_R |= PWM_ENABLE_PWM7EN;
    while(1){}
    return 0;
}

And i think there's a little problem in this code i'm not able to find. Can anyone of you please help me?

Thanks a lot

  • Hello Lukas,

    The Setting of Clock for PLL Use is incorrect. I would ask you to refer to the example code for PWM in TIVAWare as the code is more readable and manageable.

    Regards

    Amit

  • Hello Amit,

    thanks for your answer. I read the example code, but i don't wan to use any highlevel APIs such as the TivaWare. Can you please tell me what's wrong with this code? Does the PWM get a clock? In the hw_sysctl.h isn't any bitmask for the PWM1 just for the PWM0. But the initialization of the PWM1 is correct?

  • Lukas Rumpel said:
    i don't wan to use any highlevel APIs such as the TivaWare.

    Then suffer!  

    Vendor rep has suggested the "normal/customary" means of quick/efficient MCU programming - which is tried/true/tested.  Your persistence dilutes his time/energy (thus value) to all others here.  Is that fair - is that proper?

    If your homework assignment dictates otherwise - is that not "your" duty/obligation - thus why seek help here?

    All said - by employing the TivaWare library you can more quickly/easily discern which (PWM - and other critical) registers are "in play" - and model your, "non-high-level" code after that...

  • @ Lukas,

    To receive always hallowed, "Verify" after my heartfelt comment shows great character - your part.  I salute that.

    Recall that myself/several others have been active this & predecessor and even founding firm LMI forum - thus have some interest in preserving its efficiency & success.

    It's possible that you recognized my "better" roadmap (if I may be so bold) suggestion of your review of the details & mechanisms - as found w/in TivaWare.  By examining the files w/in that driverlib - specifically pwm.c - you can quickly and powerfully "extract" the who's who and what's what of key register operation and set-up/config...

    Use of the "direct register" method requires your helpers here to really "dig" into arcane register use & dynamics - is neither time nor effort efficient - and I believe vendor rep rightly resisted such direction.

    You should note that my grandfather sprang (your soil) - and I lived there for 2+ years score or so past - had great time - and most always fly, Lufthansa...

  • @cb1_mobile

    First i want to say thank you for your honest answer.

    Finally i solved the problem with a timer module.

    P.S. : I'm very happy that you liked it here in Germany. Where did you live?

  • @ Lukas,

    Yes - you can achieve quite adequate PWM by configuring a Timer Module in its, "PWM Mode."  And - to my knowledge - that is the sole (only) means to cause that timer signal to directly, "escape" the MCU.  (i.e. that MCU pin, dedicated to that timer (after proper set-up/config) will output the PWM waveform upon that timer pin.)

    Beware that there may be "issues" for extreme duty cycles!  (i.e. 0% and 100% duties)  Should you note these - you may test for "extreme" and then "over-ride" the timer operation - during those extremes.

    While a young, U.S.  Army officer stationed in Germany I lived first in Frankfurt - had a radio show on "AFN" - then met/married a German girl - and after my service ended we lived in Munchen & Nurnberg.  (her family was related to Lufthansa founders)  I learned to fly Cessna twin while in Germany - fly that same craft today in US midwest...

    Good luck w/your studies - again your attitude & persistence impresses...

  • @Lucas:

    Out of curiosity - what exactly was the fix?

    Thanks - Martin

  • If I may hazard an "educated" guess - suspect this poster moved away from the far more complex PWM Generator and instead employed the PWM Mode w/in each/every ARM MCU Timer.  These are far faster & easier to set-up & configure - and for many applications prove most adequate.

    Should your application require a full-fledged PWM Generator - Stellaris/RebrandWare (both) provide multiple examples (in the provided library - of course) which detail the set-up/config of the PWM Generators.  Use of "direct register" for such peripheral set-up/config - imho - unduly complicates by forcing extensive/exhaustive read/review of each/every MCU Register - drawn (kicking/screaming) into this demanding procedure.  All of that complexity/expertise has already been done for you - if you simply employ the standard driverlib functions.  Culling oneself from the herd adds great risk - and surely eats time/effort...

    You'll find few (if any) examples of a complete peripheral "set-up/config" achieved via direct register.  (that for good reason)  And - adding complexity to a code block which (ideally) runs just once - and is not memory intensive - goes far to defeat any potential value provided by direct register.  

    KISS dictates: (always) simple, known, tried/true/tested - none of those qualities attach to direct register...

  • Yes that's true now i'm using the timer in the pwm mode.(like in the old avr days ;) )

    And it's working fine.

    I  think that using the registers directly isn't wrong. I think if you want to know how anything's working you have to look at the little things. Yes i takes a bit more time to write the code but if you have learned the system it's quite as easy as using the highlevel API and it's more flexible if you want to change the mcu family. And i personally prefer this method (just my opinion). 

    Thanks to the community

    Lukas

  • Lukas Rumpel said:
    I  think that using the registers directly isn't wrong.

    In defense of my more agile, alter ego, no one here ever said that "direct register" was wrong.  It surely is far more demanding, takes substantially longer than, "bit more time to debug & verify" - and will surely drive poor Amit to distraction!  (and/or mental health days)  Note that it was he who first sounded the, "direct register" (Oh no!) alert...

    Recall that the comprehensive driver libraries have been used by thousands - have withstood that onslaught - neither proves true when, "direct register" enters this fray.  Each use is likely to be unique - and I defy you/others to succeed without the MCU manual, "at the ready" - and pages & pages of Register prints loitering your desk/work area.

    And - months later - when your, "photographic grasp" of Register bits & calling sequences has likely dimmed - what then?

    As a small tech firm owner - I'd discourage direct register as the, "initial programming attack" - especially so when, "time is of the essence." (and it most always is...)  Vendor libraries are powerful & efficient - their use should be encouraged...

  • @

    I want to say thank you that you show me your point of view. And i can understand that in commercial use it's very important to do a task in a very short time.