- Discrete LEDs
- Dot/Bar LEDs
- Matrix LEDs
- 7-Seg LEDs
- Serial -- Software
- Serial -- Hardware
- RS-232 via the MAX3232
- I2C -- EEPROM
- I2C -- DS1307 Real Time Clock
- 1-Wire -- DS18B20, Powered, Single-Drop
- 1-Wire -- DS18B20, Powered, Multi-Drop
- 1-Wire -- DS18B20, Parasitic, Multi-Drop
- TI Link Protocol -- Calculator Demo
- Infrared Communication
- MIDI -- Output Demo
- MIDI -- Input Demo
- MIDI to Analog Synth
8-Bit DAC via PWM
Now here's a quick-and-dirty approach to digital-to-analog conversion: pulse width modulation followed by a filter. The basic idea is that the duty cycle of a pulse wave is linearly proportional to its DC equivalent voltage after filtering. PIC Micro Pascal contains an internal command to handle this automatically: PWM().
To demonstrate it, I've purposely kept the circuit as simple as possible (about more which, later, when we investigate its weaknesses):
The variable duty-cycle pulse is generated at pin 6 (Port B.0) and applied to a one-pole RC lowpass filter. The output sweeps from 0V to +5V, give or take a fraction.
Here's what the waveform thus created appears like:
With so many steps (256 of them), this looks for all practical purposes like a garden variety ramp wave.
Now obviously, there must be trade-offs with such a simple method as this. First is that PWM() consumes a fair amount of clock cycles. (The previous methods I've shown here are for all intents and purposes instantaneous). And with 8-bit resolution, the frequency of the carrier really gets bogged down; you'll note in the scope shot, above, that it weighs in at about 2 Hz. Thus, this method is really not usable for audio or other rapidly changing signals.
And according to the PMP manual, one can't expect the output to make it completely to 0V or 5V, but this really wasn't much of a problem when I ran the test.
Finally, anytime a filter is involved, there is a battle between smoothing the signal decently versus residual ripple. The demo circuit here, however, could be improved upon considerably with a better filter employing op-amps. The LM358 might be a good choice.
Now having said all that, if you only need to create a DC voltage with 256 levels which don't change wildly all the time, this is a perfectly fine method I think.
Click to get the source code.
Click to get the schematic PDF.
Next Project: Audio Output