PWM Pin Breakdown
#1
Posted 11 March 2012 - 03:49 AM
- Arron Chapman likes this
#2
Posted 15 March 2012 - 02:40 PM
#5
Posted 15 March 2012 - 06:53 PM
Can anyone fully explain the PWM pin in retrospect to the Netduino board and Micro Framework.
1. How do they work? Whats going on behind the scenes.
2. How to access them to their full (hack) potential and manipulate them with the Micro Framework?
3. What are the definitions of the SetPulse parameters and how does it equate to Mhz conversion? Max/min values and how they equate?
4. What is SetDutyCycle and how does is relate and how can I use it?
Maybe give a thorough example of how it controls a servo and what is really happening behind the scenes.
Thank you.
Bendage!...How are you going?
Here are some details...
The PWM is NOT a port, but rather a particular drive applied on an OutputPort.
The ultimate goal is having an analog output, even not having the hardware dedicated. In other words, the goal is to symmetrize the AnalogPort, which converts a "real" value (in terms of voltage) applied in it, to a numeric value, ranging from 0 to 1023 (for the Netduino).
The hardware components acting the analog-to-digital conversion is called ADC, and it's embedded in the Netduino microcontroller. For making the opposite function, so generating a "real" voltage from a number, the component is called DAC.
Both ADCs and DACs are typically expensive, and the manufacturers tend to avoid to surcharge a MCU without any valid reason. Furthermore, while an ADC can't be replaced easily with equivalent solutions, the DAC can be "simulated" with a PWM output drive.
The ability of simulate a DAC rely on several limitations:
- the precision is much lower;
- the resulting voltage is *FAR* from being stable ("stable" is not the right term, but let's see later...)
- the settling time is much slower.
The principle is somewhat simple.
The PWM stage drives the output port as a square wave, which is a period waveform having only two states (high and low). The ratio of the high-level time to the full period time is called "Duty-Cycle", thus it spans from 0% to 100%.
From the secondary school, you can calculate the area of the rectangle having height=+V and as base the duration of the high level. This area could be thought as the energy given by the battery (via the port). By the way, this area should be seen over the whole period, so the *AVERAGE* is proportional to the duty-cycle.
We still haven't mentioned how should be the "whole" period. Mathematically the average is always the same, but...if the period were 10 seconds, your light dimmer would be a garbage.
Indeed, the frequency (which is the inverse of the period) should be chosen carefully, according to the kind of device that you want to drive.
In the Netduino (4.1) the frequency has been fixed at 10kHz, which is a pretty good choice. If you connect a led or a bulb, of sure you won't notice any blink...I guess that maybe many animals cannot perceive a so high frequency. In particular for a bulb, the warm inertia is huge respect to the oscillation of the PWM, thus the brightness is actually "stable". For a led is very different, and 10kHz is still a frequency able to be reproduced "by live".
Most of the users love to use the PWM with small motors. I don't want to talk about how to drive a motor, because is a very complex task. If you connect a scope on a motor leads, you'll be scared.
Instead, I'd like to point out what I meant for "stable". The correct term should be "continuous", but I was afraid to create confusion with the ability to span the voltage from 0 to +3.3V, as the brother ADC does.
Well, yeah...the problem is right the square wave, because it's so: it's a square wave, not a continuous level. Thus you must to "filter" it. To filter it you should add capacitors and inductors, then resistors...all that is cumbersome, and moreover increase the slowness of the response.
As "response" I mean how fast my average voltage takes to settle from a certain value to another one. For instance, my PWM is set to 50%, then I set to 100%. Of sure the square wave changes immediately, but -once filtered- how long will take the filtered voltage to get to the maximum?
That's another fault of the PWM respect to the DAC.
How is the PWM made internally?
Yet another "stupid" machine. It's just a free counter, let's say counting at 1MHz rate (I don't know by real). This counter is pretty limited, and it counts up to 1000 (just as an example), then rolls to zero again.
Then there's a "comparator", that matches the counter value with a prefixed set, basically the "duty-cycle". When you set the duty at 35%, then the low-level firmware sets 350 as value to be matched. When the comparator matches the preset, then lowers the output, till the counter rolls to zero. At that point the output is pulled high for a new comparison.
I don't know very much the features of the Netduino PWM, nor the changes made on the firmware 4.2.
In the 4.1 version the frequency is fixed at 10kHz, but should be variable in the new version.
The SetPulse function, however, should allow to set different frequencies, even on the 4.1.
Hope it helps.
Cheers
- Arron Chapman likes this
#6
Posted 15 March 2012 - 07:15 PM
There is also very nice article by Pete Brown.
Yes, Pete's article was very helpful to me, but I think there is a mistake in the example code.
I did ask a question on the page, but he never replied.
He says the values are in micro-seconds, but then in his code examples he multiplies by a million to get milli-seconds:
const uint period = 3 * 1000 * 1000; // 3 ms
I'll leave it to you to make your own mind up.
Someone tell me if I am being stupid.
Paul
#7
Posted 16 March 2012 - 03:47 AM
- Arron Chapman likes this
#8
Posted 16 March 2012 - 06:36 AM
#9
Posted 28 August 2012 - 06:46 AM
Yes, Pete's article was very helpful to me, but I think there is a mistake in the example code.
I did ask a question on the page, but he never replied.
He says the values are in micro-seconds, but then in his code examples he multiplies by a million to get milli-seconds:
const uint period = 3 * 1000 * 1000; // 3 ms
I'll leave it to you to make your own mind up.
Someone tell me if I am being stupid.
Paul
He still never did get back to you Paul did he. Well, I ordered a scope today and can't wait to get to the bottom of this.
Stand by
#10
Posted 12 September 2012 - 02:25 AM
#11
Posted 12 September 2012 - 05:53 AM
Paul.... Milliseconds it is
So I'm not going mad after all.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users