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.

How to make simple code with pwm output on a pin?

Other Parts Discussed in Thread: TM4C123GH6PM, EK-TM4C123GXL

Can someone write simple code, using tivaware api  to make simple pwm output and then comment everything why are there some functions used? I need really slow freq like few hertz.

  • There are examples distributed with TivaWare

    Robert

    No, I'm not going to do your homework
  • It is not homework. Im just new to this and I cant find any example using pwms, i tried configuring my slef but all i get is countinous signal. I believe someą with good skills should be able to do that in few minutes. Which tiwavare example would be the best in my case?
  • Heh, homework is not necessarily an assignment from school but includes work that you need to do yourself.

    Such as look at the examples and determine what they are doing, Then come back with clarification questions if something is not clear.

    Robert
  • Have a look at this link: processors.wiki.ti.com/.../Getting_Started_with_the_TIVA™_C_Series_TM4C123G_LaunchPad

    The link is a comprehensive training page that you can use to get familiar with how to do basic tasks on TM4C. This specific training uses the TM4C123G Launchpad and given you didn't mention which device/board you are using I assume this will work for you. None the less, even if you are using a TM4C129, this training should give you the basic essentials for accomplishing the task since the timer peripherals are pretty much the same. Keep in mind any board or pin number references might be different if you are using a different board.
  • user4096729 said:
    I need really slow freq like few hertz.

    That extremely low PWM frequency (may) draw folks in - yet it's unexplained!  

    Best results often occur when you "sell" your post.    (make it of interest - even possible benefit - to others)

    How well have you sold your post?

  • Dont judge me, for trying to learn something. I found some example using timers as pwm generators, but I think there is a way using pwm fucntions. heres my code (didnt post it at the beginning because it was giving me continous signal on pf0 so assumed my code was just bad):

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

    int main(void) {


    // 40 MHz system clock
    SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|
    SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);




    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);


    SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
    // setting 10 hz
    //
    PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
    PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 4000000000);
    //
    // Set the pulse width of PWM0 for a 25% duty cycle.
    //
    PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, 100);
    //

    //
    // Start the timers in generator 0.
    //
    PWMGenEnable(PWM0_BASE, PWM_GEN_0);
    //
    // Enable the outputs.
    //
    PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT , true);

    GPIOPinConfigure(GPIO_PF0_M1PWM4);
    GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PF0_M1PWM4);

    while(1) { // The following code varies the duty cycle over time

    }
    }
  • im using TM4C123GH6PM. Checked your link but i couldnt find this: "To complete lab15 you will need a hobby-type servo" where I can find lab 15?
  • user4096729,

    Sorry you are feeling judged. I think there are many posts on this forum where it is obvious that the poster is just looking for a quick answer as an easy way out rather than taking the time to research and learn. This is a bit frustrating to those that are here to provide assistance when it is really needed.

    In your case, I would urge you to have a look at the training material that I pointed you to. Chapter 15 of the workbook (downloadable) goes through PWM generation with TM4C and even includes a lab exercise to show how to create the code to generate a PWM. You are on the right track with the timers.

    Getting used to a device and its supporting tools and documentation takes time and some pointing in the right direction. Stay diligent and you will get what you are looking for.
  • Im rarely asking anything in forums, i usually find something fimilar which fits my case and then use it. But all i can find is too hard or dosnt not fit my needs. Sorry for bad post. will try to make it better next time.
  • Judgment is not conveyed via a question.

    Employing PWM at too low a frequency may cross into the realm of, "On-Off Keying" which justifies the (deflected) question.
  • Thank you. I did not see that book before and i was missing that kind of information soo bad. Thanks again!
  • Again, I have no issues with your posts. Everyone has to start somewhere.

    I wasn't aware of the requirements for the lab for chapter 15 pwm generation. The text of the chapter should be a good reference nonetheless on how to setup and program a PWM.

    If you are using the TM4C123G Launchpad, have a look at the out of box project that is programmed into the board on delivery. It uses PWMs to drive the LED color changes and should serve as a good example. The project is located in the TivaWare directory. if you installed to a default location it would be at C:\ti\TivaWare_C_Series-2.1.1.71\examples\boards\ek-tm4c123gxl\qs-rgb
  • Chuck,

    Still missing here is poster's (need - and the "specifics") of very low-frequency PWM. As I stated - that's a slippery path - and surely "untouched" by hallowed Lab 15. (or any such lab)

    I stand by the obvious advantage of, "Drawing responders to poster's aid" by post creations which serve to benefit and/or intrigue many!   That's (surely) not judgmental - that's reality - Selling works!   (how I took past tech firm public...)

  • yea at first i thought i will use on-off keying ant that would solve my case in a way, but then I realised I could not go any further with that kind of programming so that's why I decided to use real PWM. I wanted low freq (few hertz) because because i don't have scope or anything else to check if pwm working at high freq. My multimeter does not have freq check option. I started coding like 3 days ago and I didn't find the book which Chuck Davenport referred, but I think i will go fine now. Your questions "How well have you sold your post?" does not include anything usefull about my question, so this is why i felt judged. I dont know if understood your post correctly again and if I explained well what i meant, but thanks again for your remarks.
  • Kindly read my 3:00 post (one up). It is never my intent to be "mean" - instead I believe most here are smart enough to benefit from (sound) advice. By "selling" you make your post FAR MORE ATTRACTIVE to those who can best help you.

    Usually - but not always - this is achieved by describing the "need & advantages" which you believe - your issue (potentially) creates. Your post gave no such "pull" to attract responders - that was my point...
  • Hmm, I looked through it but I couldn't find any PWM functions. Anyway I'm going to use lab 15, even if I don't have requirements.
  • Ok i now have working pwm, also I want to get UART working in different file. It is in my project uartas.c, header file also there. AS soon as i cool my function ConfigureUART();

    it shows following error:

    Description    Resource    Path    Location    Type
    unresolved symbol UARTStdioConfig, first referenced in <whole-program>    pwm             C/C++ Problem

    I got my all libraries included  in main.c file and uart.c dont know whats wrong :/

  • From that description I'd say that your link didn't include the Uart object file.

    Check that you are actually compiling the uart code (erase the object file and then compile). If the object file isn't being produced then you do not have the appropriate dependencies set up. That will be build environment specific.

    Also check that UARTStdioConfig is actually in one of the files you are compiling and not in a support file that you've overlooked. Also verify it has public linkage (i.e. it's not declared as static)

    Also you should verify the object file with the config function is in your list of files to link.

    These are all generic places to look. It might be useful to tell us what your build environment is.

    Finally, when tracking down puzzlers like this it's often useful to turn on verbose output from your tools and get a map file. Looking at this output can be quite revealing even if it does take patience to wade through.

    Robert
  • user4096729 said:
    I need really slow freq like few hertz.

    Robert,

    Such appeared poster's opening salvo.   PWM at such low frequency descends awfully close to "On/Off" keying - if the 16 bit PWM counter can even be forced to that low a frequency.

    Opening post sought, "Simple code w/PWM output on a pin."   KISS "kicked to the curb" now the PWM becomes held hostage to UART (and others - (always) unspecified - added to the witches' brew...)

  • As Robert has mentioned, it appears as the code is not being included in the project. Make sure the new file is not only included in the main.c file but also in other files if there are elements to include from a header file/global variables, etc. Also check that you have added the file to the project include path in CCS.
  • I'm not sure what to make of this Chuck

    Chuck Davenport said:
    Make sure the new file is not only included in the main.c

    I'm hoping this is just badly worded. If what it says is what you meant then it is wrong. There are very few cases where it makes any sense to include a source file within another source file. I can come up with only a few but this is not one of them.

    Source files should be compiled separately and linked together. You need a very very good reason to give up the benefits of separate compilation before doing otherwise.

    Robert

    BTW the cases I can come up with are

    • Private automatic data generation
    • Extending broken or insufficiently flexible  automatically generated code
    • Providing access helpers for testing private code/data (purists will argue that the code needs to be re-designed in this case, they may be right)

  • Robert,

    Don't be so quick to judge. If you re-read my post you will notice I specifically mention header files as well as including files in the project include paths.

    And, by the way, I have a bit of experience to know that files are compiled separately and linked together. Thanks for the lesson though; perhaps someone else on the forum may find it useful.
  • I did read it Chuck and although you did mention header files it left the strong impression you were recommending against separate compilation. I had hoped and am now reassured that is not what you meant but I did think it needed to be pointed out.

    I had just spent some time in the last few days correcting some vendor code that did, in fact, commit that sin so I was sensitive to it. Hopefully not too strong a reaction.

    Robert

    BTW, not TI code. But practices like that by various IC vendors have been a good deal of what has made me very skeptical of the quality of vendor supplied code

  • Understand, Robert. Perhaps I should have been more concise in my answer.

    My background is SW Engineering so I certainly understand the sensitivity. Sometimes these HW guys throw sound SW engineering principles out the window and exclaim everything is fine since "it works!" I haven't run into this at TI in a long while as most of the people in our group are well aware of the need for quality work and all are very experienced.

    In the end, let's hope our advice to the poster is understood and they are able to resolve their problems. Cheers!
  • Chuck Davenport said:
    Sometimes these HW guys throw sound SW engineering principles out the window and exclaim everything is fine since "it works!"

    And - to be fair - is it not rue that the corollary, "Sometimes these SW guys throw sound HW engineering principles out the window..." deserves admission?

    Very low frequency PWM (poster's goal - expressed w/in very 1st post) remains untouched/unprobed - should anyone care...

  • PWM question was solved. I marked it as answered.

    So far I haven't tried solution with my uart problem because i was extremely busy. I will try tomorrow and I will give response if the problem will be solved.

     

  • user4096729 said:
    PWM question was solved.

    Does this mean you achieved your goal of "Few Hz" PWM operation - while avoiding, "On/Off Keying?"

    Suspect that is the "real meat" of your posting - and remains silent...

    And - if you continue in the pursuit of "Few Hz PWM" AND seek to introduce a monitoring UART - that twain (may not) meet...

  • UART problem solved. It looks like code composer studio cant find utils folder, so I created folder utils in the project with shortcuts to uartstdio.c files and it worked. I dont know why it didnt work before because it was included with arm linker. I guess it is not included in libc.a file, but I dont know that for sure.


    If I have more questions wich is not associated with this post topic should I ask there, create another topic or contact you directly Robert? i am asking because of complains in this post :/

  • Create a new post.

    • You may get a faster answer (I may be busy or not find the question interesting)
    • You may get someone more knowledgeable to provide an answer
    • You may get a wider variety of ideas

    Questions we don't like

    • Ones that appear to ask us to do your work (answer your homework questions, design your class project, design your commercial product)
    • Ones that show you have not done even a minimal amount of research, like reading the Tivaware manual on a function you do not understand.
    • I don't like ones that contain large amounts of source code, I probably won't even bother reading them
    • Even worse, file attachments

    If you are unsure what information to provide, post a brief description of the problem and ask what information is needed/useful.

    Just remember most of us are here because we like to help. It's a way to pay back those who helped us when we did not understand. And we often have the faint hope that we will increase the general quality of firmware (which is why you will see some of us push for quality practices, and even chide TI, hopefully respectfully, when we see them not following them).

    Forums sometimes feel harsher than they are. Text often carries emotions to the reader that the writer did not intend.

    Robert

  • Damn Straight! (Part Deux) (how likely are two forum bannings (vous et moi) w/in the same day?)