Looking
for a low-cost solution to generate an 8-bit audio source I decided to make
an R-2R D/A converter from Port B of a PIC16F84. The idea of constructing
a digital-to-analog converter from an 8-bit data port is not new, but I never
found it using a PIC controller:
The
idea is simple: fetch a sample from a table and output it to the R-2R network.
The sample, assuming all ports behave equally, should generate a proportional
analog output voltage. To test this assumption, I wrote a quick'n'dirty
program to output all values from zero to 0xFF to the R-2R network. But
first I needed the hardware. Having only 16 pin sockets I decided to forego
use of pins 1 and 18. Not that I needed those... The R-2R network was constructed
from a batch of 0805 resistors that I found in my junkbox. The "2R"
values were simply constructed of two 27k resistors in series. The SMD resistors
would fit nicely between two islands of a piece of 0.1" hole board. To
avoid loading the network, an output resistor of 220k was used, with a 10k
resistor to ground making a voltage divider. For full voltage swing I should
have used a proper buffer, but I only needed a small signal so this worked
perfectly. The finished result looked like this:
The
"ramp" results were perfect! There were no glitches nor discontinuities.
So I set out to generate the data for my two-tone generator. The intention
was to make an audio source to test the IM3 behavior of an SSB transmitter.
As my spectrum analyzer has a minimum resolution bandwidth of 1 kHz I
needed a larger frequency difference than usual: 2 kHz. This allows to view
the results with a fair accuracy. Of course, the PIC imposed some constraints
to the actual implementation, but it turned out that this was completely within
the limits of what would work. The sampling rate would be a whopping 91 kHz!
So I wrote a program in QuickBasic 4.5 (vintage
1988!!!) to generate the waveform of a sinewave of 0.50 kHz and 2.5 kHz
added together. The resulting table was plonked in the tutone
program and compiled. After tweaking the program
a bit it worked like a charm! Too bad I haven't made any screenshot from my
spectrum analyzer but I do have one from my scope:
I haven't tried it with a PIC16F628A, but I think that the internal RC oscillator might be a bit too noisy to generate a stable frequency. Then again, it might not even matter as the noise is divided by 1600.
Back to the homepage
Last Update: 3 May 2008
This software is licensed under the CC-GNU
GPL.