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.

MSP432 PWM in Energia

Other Parts Discussed in Thread: ENERGIA

I am trying to generate PWM using Energia with MSP432.

I used analogWrite but it only has fixed frequency around 500 Hz. Is there a way to change the frequency in energia?

Plus, I also tried to use tone, but I could not get 40kHz. (I was able to get 10kHz)

I know CCS can do this, but I want to stay in Energia.

Thank you!

  • Energia API's are limited in their functionality to allow for reduced complexity, however it appears from documentation that the tone function should easily support 40 kHz. Have you considered using basic C code inside of Energia to accomplish this task? You can use the TI-provided examples like msp432p401r_ta1_16.c as a template.

    Regards,
    Ryan
  • Hi Ryan,

    Same code with tone function works in MSP430-6989, but not in MSP432 now.
    I found msp432p401x_tal_16.c example file, but I was not able to open it in Energia.
    I also copied the code and pasted it in Energia, then saved it with .c extension. But it shows following errors:

    Energia: 1.6.10E18 (Windows 10), Board: "LaunchPad w/ msp432 EMT (48MHz)"

    sketch\main.cpp.o: In function `main':

    sketch/main.cpp:57: multiple definition of `main'

    sketch\code.c.ino.cpp.o:C:\Users\bonhy\Desktop\code.c/code.c.ino:76: first defined here

    collect2.exe: error: ld returned 1 exit status

    exit status 1
    Error compiling for board LaunchPad w/ msp432 EMT (48MHz).

    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.


    I appreciate your help.

    regards,
    Bonhyun
  • Well one of the issues with the Energia API's is their compatibility across all MSP devices, I would hope first of all that you are starting with a tone example designed for the MSP432. If using the C code example then you should be trying to use the contents of the main function without copying the main function itself into the Energia sketch, and by no means should you be renaming the .ino to a .c file. Take the lines that set the timer registers and nothing more.

    Regards,
    Ryan
  • As stated earlier you mentioned tone() should function at 40khz, Currently i get 1-10khz perfectly, from 10k+ hz and on no signal is sent to the pin. Is this a limitation with 432 or Energia or perhaps faulty hardware.
    You stated inserting c code into energia but I don't have a good back of C would you be able to provide steps for either fixing the issue with energia or inserting your recommend C files into energia.

    For example tone(9, 40000) I'm reading the output from pin 9 and get no signal. same code with tone(9, 10000) I get 10k signal all reading are from Oscilloscope.
  • Another view is when taken manually. i've attempted to recreated the 40k hz pulse wave by manually setting the pin high, delaying then setting low and repeating. see below

    digitalWrite(triggerPin, HIGH);
    delay(.0125);
    digitalWrite(triggerPin, LOW);
    delay(.0125);

    this should write a pin high and stay high for .0125 msec then write the pin low for .0125 msec recreating the 40k signal. currently i get a unstable signal that varies from 16k-36k hz signal. Any reason or suggestion you could offer would be grealty appreciated thanks for all your hard work.
  • I haven't tested with the MSP432, but I have done something very similar with both the MSP430 and TM4C123 with Energia. I think one of these will work.

    With MSP430, in setup:
    pinMode(pwmPinNum, OUTPUT);
    analogFrequency(40000);
    in Loop:
    analogWrite(pwmPinNum, duty);

    With TM4C123, in setup:
    pinMode(pwmPinNum, OUTPUT);
    in Loop:
    PWMWrite(pwmPinNum, 128, duty, 40000);

    Darren
  • The tone function issue is not related to the MSP432's hardware or capabilities but rather a limitation of Energia. Take the main function register setting contents of the .c file and transfer them to the setup function of your Energia sketch. Or try following Darren's suggestion. Manual control is a bad idea due to extra cycles involved with commands/APIs, plus I believe that the delay function only accepts unsigned long values as compared to decimal/float/double.

    Regards,
    Ryan

**Attention** This is a public forum