Jump to content
43oh

AnalogWrite Period


Recommended Posts

Hi!

i am making an small robot, i'm using 2 DC motors and 2 servo motors. Now, i want that the DC motors get a 20Khz PWM signal, but, in the default operation mode it is about 490Hz. I was looking into the wiring_analog.h to see if there was any chance to modify some code to get this PWM frequency. And my other question is: if i want to drive two servos (with servo.h) is there any "interference" or warning to use it at the same time that "analogWrite"?

 

Thanks a lot for your help. :)

Link to post
Share on other sites

You can use the function analogFrequency(). This code was contributed by a member but I have not used it myself and kind of forgot about that it was there until this post ;-). I should work perfectly though. I will document it and do some testing on it. Let me know if you run into any issues.

 

Make sure you call analogFrequency() before analogWrite().

The lowest frequency is defined by the clock frequency F_CPU / max counter value. Fmin = F_CPU / ((2^16) -1). If you have a launchpad that uses MSP430G2553 then F_CPU = 16MHz which makes Fmin = 244Hz.

 

For pin numbering and pin usage see diagrams on https://github.com/energia/Energia/wiki/Hardware

 

All of the P2.x pins that can be used with analogWrite() are on Timer 1 (not all P2.x pins can be used. See the diagrams in the link above). The servo happens to use Timer 0. So as long as you use P2.x pins for analogWrite then you can use both analogWrite() and Servo without interference. Note it is OK to use P2.x for both. Just don't use P1.x with analogWrite().

 

example:

/* Set PWM frequency to 20 KHz */
analogFrequency(20000);
/* PWM on P2.1 (aka pin 9) with 50% duty cycle */
analogWrite(P2_1, 127);

/* Use P1_0 (aka pin 2) as servo pin */
myservo.attach(P1_0);
/* Set servo pos. to 90 degrees */
myservo.write(90);

Link to post
Share on other sites

Oh!! i didn't know about that... i was planning to use all p2.x ports to get analogwrite...

After i took a look into the link and the pin capabilities... i have a new question. For my project, i use a l293D, with 4 lines controlling two dc motors. When i want to run straight, i put two pins in analogWrite(pinX,128) and other two in 0, when i want to turn, i change one motor to 255 and other to 128, but in a brief experiment, i see that all p2.x have the same pwm, the same duty cycle... is there any way to get a different PWM in some pins of the same port?

Link to post
Share on other sites

Join the conversation

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

Guest
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...