Go Down

Topic: Old Topic, PWM ATMega328 changing the PWM Frequency for UNO pin 3 and pin 9 (Read 224 times) previous topic - next topic

Dana1954

Keep in mind I'm a hardware designer, retired, and working on my first Arduino project. Dumbing it down woud be nice. :)

Original Post https://forum.arduino.cc/index.php?topic=129159.0

http://playground.arduino.cc/Code/PwmFrequency   

setPwmFrequency(9, 1); // sets the clock frequency to 31.250 KHz for pin 9
setPwmFrequency(3, 1); // sets the clock frequency to 31.250 KHz for pin 3

Does an analogWrite(pin3, mydutycycle) resets the clock back to 490 hz. Or will the clock remain at 31.250 KHz

I've spent all day searching for one place that has definitive information to clarify this one question.

What is desired is obvious... a turn key solution :)

1. set the clock frequency at 31.250 KHz for timers 2 & 3
2. use a PWM analogWrite() like command to set the duty cycle just like in analogWrite()
3. my code updates the PWM duty cycle often
4. I am looking for a steady state frequency and variable duty cycle

It is hard to believe this does not already exist. Many people have wanted to do this. It would seem enhancements to analogWrite() would be a good place to start. Allow source clock and divider selection

I prefer not to learn the lower level code to make this happen.

Things I've reviewed are along with countless more

https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM
http://arduino.cc/en/Reference/AnalogWrite
http://arduino.cc/en/Tutorial/PWM
http://gammon.com.au/interrupts


cattledog

Quote
Does an analogWrite(pin3, mydutycycle) resets the clock back to 490 hz. Or will the clock remain at 31.250 KHz
analogWrite() sets the duty cycle, and uses whatever frequency the timer is set for.

Quote
I've spent all day searching for one place that has definitive information to clarify this one question.
The definitive information is in the source code for analogWrite().  If you are new to Arduino, looking at the source code for libraries and Arduino functions like analogWrite() is a good habit to develop.

You will find the source code for analogWrite() here
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\wiring_analog.c

 

DrAzzy

BUT if the frequency was obtained by changing TOP, which it probably did, the duty cycle won't be what you expect....? And if frequency isn't set by changing TOP, it won't be very accurate.

Edit - just looked at the code. It doesn't change TOP, so the frequency setting is not very accurate. 
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Dana1954

Thanks, 

it became evident last night it will require fully understanding timer 1 & 2 circuits. Once it becomes clear, assuming it does, I'll update this thread.


DrAzzy

It's explained in the datasheet chapters on timer1 and timer2 - you'll probably have to read them several times to understand it, but once you do, it's applicable to almost the whole AVR product line - most atmega chips have an identical timer2, and the larger chips with more than one 16-bit timer - those later timers are the same as timer1. Same goes for timer0 in fact - except for a very small number of weird ones, they all have the same 8-bit under-featured timer for timer0 - it's the least capable of the timers, which is why it gets used for millis(), since that leaves the more capable timers open for the user.
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Dana1954

Thanks to all, I vaulted this hurdle and moved on to get the basic robot operational. I now have a good grasp of the timers and how to reprogram them at will for this project. My little robot project is moving right along. Next phase is a MPU6050... it will arrive later today.

The timers is one of those things where complete confusion in the beginning ends up becoming crystal clear once all the work is done. Like why was that so hard to begin with kind of clarity.


cattledog

Quote
I now have a good grasp of the timers and how to reprogram them at will for this project.
Quote
The timers is one of those things where complete confusion in the beginning ends up becoming crystal clear once all the work is done.
+1 for working through this

Go Up