There is also PFM (pulse frequency modulation" where you keep the size of pulses constant, and vary the rate that they are generated. Of course it is possible to combine PWM and PFM.
In the case of the Convoy S2+ flashlight running either the Convoy or Biscotti firmware, only PWM is used. The Convoy firmware uses a pulse rate of 4.52 kHz, where the Biscotti firmware uses 9 or 18 kHz.
"Why these rates?", you ask. This is all being handled by an 8 bit counter timer inside of the ATtiny13 chip. We have an 8 bit counter that is clocked by the basic CPU clock rate. The Convoy firmware runs the CPU clock at 1.2 Mhz. If we use that to clock an 8 bit counter, it will roll over (repeat) at 1200/256 Khz = 4.7 kHz. Apparently the CPU clock is actually 1.152 Mhz, so we get 4.5 Mhz on the one device I measured with an oscilloscope.
In the case of the Biscotti firmware, the CPU clock is run at 4.8 Mhz (4 times as fast). Dividing that by 256 we get 4800/256 = 18.75 kHz. Once again, the CPU clock is not exactly 4.8 Mhz (it is controlled by an RC network inside the 50 cent ATtiny13 chip). To get 18 kHz, the CPU clock must be 4.6 Mhz rather than 4.8. I have measured a PWM rate as low as 17.5 kHz, but we will just call it "18" and move on.
Why do we measure 18 kHz for some brightness levels and 9 kHz for others? The counter/timer chip supports a bunch of different "modes of operation". The Biscotti firmware selects two of these and switches between them.
One mode is called "Fast" and yields the 18 kHz rate. In the fast mode, the counter counts from 0 up to 255, then immediately goes back to 0 and starts over.
The other mode is called phase correct or just "Phase" in the software. In this mode, the counter counts from 0 up to 255, then from 255 down to 0 again and does this over and over. This yields the 9 kHz rate.
In fast mode, when the counter resets to 0, it also turns on the output. Then when the value in the counter matches the value in the compare register, it turns it off. This process repeats every time the counter resets to 0. If we put a small value in the compare register, we get a short (brief) pulse. If we put a value of 255 in the compare register, the output will be on all of the time. Given a CPU clock rate of 4.8 Mhz, the shortest pulse we can get would be 208 nanoseconds. This would be 1/255 of the overall cycle time (i.e. 0.39 percent). This is what the Biscotti firmware uses for its moonlight mode.
Phase correct mode works differently. Here the counter is going down then up over and over. The compare register is used both to start and to terminate the pulse. The game starts with the counter going down and the output off. When the counter value matches the compare register, the output gets turned on. Then when the counter is on the way it, it watches the compare register and when it matches, the output gets turned off. This yields a pulse that is symmetric about the time when the counter is zero. All pulses are symmetric about the time when the counter is zero. This doesn't matter whatsoever for flashlights, but it could be important for some applications.
We should talk about a term known as "duty cycle". This is simply the ratio of the time the ouput is on, divided by the cycle time of the PWM clock. If we set a value of 255/2 (128) in the compare register, we will get a 50 percent duty cycle. So we will get 50 percent of maximum brightness if we are talking about flashlights (as indeed we are). Interestingly this is true whether we select FAST or PHASE mode. PHASE mode yields both longer pulses and a longer cycle time.
When do we want to choose fast mode and when do we want to choose phase mode? Does it matter for a flashlight, since either gives the duty cycle we want. The fact that the Convoy firmware uses 4.5 kHz and the Biscotti uses 9 or 18 suggests that maybe it doesn't. The fact that the Biscotti firmware switches from one to the other suggests that it does.
The Biscotti firmware uses "fast" mode for the two low brightness modes (what they call 0.1 and 1.0 percent duty cycle). Why I don't know, but I presume that there is some good reason.
A possible reason is that the PWM signal is being used to control 7135 current control devices. These devices may be "slow", i.e. they have a rise time and a fall time that adds to the effective pulse width, making the pulses effectively longer than what comes out of the ATtiny.
Tom's Light Info / [email protected]