Jump to content

analogWrite() precision question

Recommended Posts



Is there anyway to output pwm from a msp430g2553 other than analogWrite() ? 


I am expecially interested in increasing the resolution of pwm output. I have been implementing a pid motor controller, and i am almost done, but I am checking my options for precision and integration time.


Best Regards,


Link to post
Share on other sites

Oh yes, you can get far more precise and a higher or lower overall frequency too.

That's datasheet / UserGuide material though. I did some playing with copy/pasted code a while ago but don't remember enough to help you much.



The stellaris / Tiva-C boards have PWMWrite in Energia, which allows you to set the resolution, frequency and duty cycle in one go (takes a while and causes glitches, though). Sadly the 2553 doesn't have that function in energia.

Link to post
Share on other sites

@@energia Yes, I am interested in having higher resolution in changing duty cycle of the pwm signal.


@@bobnova are there any documentaion on the PWMWrite function? I could not see it in energia reference.


It would also be interesting to change pwm frequency itself (not duty cycle) - this is said to eliminate noise when driving a motor, but has some disadvantages as well.



Link to post
Share on other sites

@@altineller not really, no. It's something of a hidden function.


You have to include wiring_analog.c (#include <wiring_analog.c>) to kick things off. That includes the PWMWrite function.


Then you can call it thusly:   PWMWrite(PIN,numberOfSteps,dutyCycle,frequency);

For example:   PWMWrite(PD_1,500,250,6000);

Would start PWM on pin PD_1, 500 steps from 0 to 100%, 50% duty cycle (500 / 2 = 250), 6000Hz frequency.

It's worth noting that the function call does glitch when you call it on a pin already putting PWM out. It's not enough to hear driving a speaker, but it could cause you issues if you're calling it a lot. There's a thread around here somewhere on that subject.


The frequency cap is very high, if I recall correctly I'd tested it to work >1MHz. I could be thinking about a different MCU, I've tested sort of a lot of them.

Link to post
Share on other sites

Hmm... trying @@bobnova suggestion using:

#include <wiring_analog.c>

void setup()
  // put your setup code here, to run once:
  pinMode(P1_6, OUTPUT);

void loop()
  // put your main code here, to run repeatedly:

Results in the following error (on a G2553 LP): 

In file included from sketch_aug25a.ino:1:0:
E:\energia-0101E0012-windows\energia-0101E0012\hardware\msp430\cores\msp430/wiring_analog.c: In function 'void analogWrite(uint8_t, int)':
E:\energia-0101E0012-windows\energia-0101E0012\hardware\msp430\cores\msp430/wiring_analog.c:107:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
sketch_aug25a.ino: In function 'void setup()':
sketch_aug25a.ino:7:29: error: 'PWMWrite' was not declared in this scope
Link to post
Share on other sites

I think I might have edited my wiring_analog.

Try  #include <wiring_private.h>


It's in energia somewhere.





I have no idea if it works on G2553s, as I mentioned up above I've only used it as a Tiva-C / Stellarpad function, it may well not exist for the G2553.


The G2553 timers are certainly capable of different frequencies and resolutions though.

Link to post
Share on other sites
  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...