LogoChip in an Analog World
LogoChip in an Analog World .................................................................... 1
Digital to Analog Conversion ..................................................................... 1
A Simple DAC ......................................................................................... 2
Challenge: Make Your Own Digital to Analog Converter ..................... 3
Analog to Digital Conversion ..................................................................... 4
Challenge: Make Your Own Analog to Digital Converter ..................... 4
LogoChip in an Analog World
While the LogoChip is essentially a digital device, the world is ultimately an
analog place. Think for example of the problem of stabilizing the length of a
laser cavity. Could a LogoChip be used to accomplish this? Perhaps, if you
have a way of feeding analog information (e.g. light intensity, although I
suppose one could argue that if one counts photons light intensity is already
a digital quantity!) and supply an analog voltage to control a piezoelectric
transducer.
Below we give examples of how to make the connection between these two
worlds. We seek to give a sense of how the processes of digital to analog
conversion and analog to digital conversion work, by focussing on simple
“transparent” versions of how that connect to basic principles that you’ve
already encountered.
Digital to Analog Conversion
There are a number of different workable schemes for accomplishing an
“digital to analog conversion”, that is for generating an analog output
voltage that is proportional to some binary number. For example in a few
weeks you will use an “op amp” to make the digital to analog converter
shown below. This "weighted summing amplifier" produces an analog
output voltage that is proportional to a 4-bit input number:
Page 1 Last Modified on 12/3/2011 11:52:00 PM
LogoChip in an Analog World
A Simple DAC
You can build a simple digital to analog converter (DAC) using the pulse
width modulation signal from the LogoChip and a simple RC low pass filter:
It is easy to understand how this approach works. Think of the PWM output
in terms of its Fourier components consisting of a fundamental frequency
and higher multiples) along with a dc component We attach a low pass filter
with the 3 dB point set well below the fundamental frequency, so that only
the dc component makes it through the filter.
Fourier theory tells us that the magnitude of the dc component of the signal
is equal to the time average value of the signal. Thus the value of the dc
component here is simply equal to the duty cycle times 5 volts. For example
entering
pwm 30
in the command center should produce a voltage equal to0.3 x 5 volts = 1.67
volts at the output. Look at the output signal on an oscilloscope and see how
Page 2 Last Modified on 12/3/2011 11:52:00 PM
LogoChip in an Analog World
well the circuit works. To emphasize the fact that we’ve entered the analog
world, try attaching an analog voltmeter (the kind with a needle).
The resulting DAC is simple but slow. Its rate of change is limited by the
filter response time. (You have to limit the rate at which you can vary the
analog output to frequencies below the 3 dB point of the low pass filter.)
Challenge: Make Your Own Digital to Analog Converter
Write a program to make a triangle wave that varies from 0 to 5 volts. It’s
best to view your creation on a digital oscilloscope. What’s the highest
frequency triangle wave you can make?
Note: In practice you’d probably want to buffer the output of your DAC.
The low pass filter has a relatively high (1k) output impedance and would
not be much good at directly driving very many things.
PWM code:
constants
[[t2con $12][ccp1con $17][ccpr1l $15][pr2 $92]]
to pwm :val
clearbit 2 portc-ddr ;set RC2 to output
write t2con 4 ; turns on timer2
write pr2 100 ;sets period in units of
4*clock period
write ccp1con 12 ;select PWM mode
write ccpr1l :val ;set PWM duty cycle (time
on) in units of 4*clock
period
end
Notes:
fclk
pr2 1
4 f pwm timer2prescale
t2con = 6 sets prescale to 16 t2con = 4 sets prsescale to 1
Page 3 Last Modified on 12/3/2011 11:52:00 PM
LogoChip in an Analog World
Analog to Digital Conversion
The LogoChip comes with built in analog to digital converters which you
have already made use of to read analog voltages. You may wonder how
they work. Here’s a scheme that shows how you can do analog to digital
conversion using your DAC and a “comparator”. The comparator is a device
that we will meet up close shortly, but it is easy to explain what it does. A
comparator “compares” two different analog voltages at its inputs and
produces either a digital HIGH or a digital LOW at its output, depending on
which of the two inputs is bigger.
Challenge: Make Your Own Analog to Digital Converter
Challenge: Build the circuit shown above and try it out using the stupid-
Page 4 Last Modified on 12/3/2011 11:52:00 PM
LogoChip in an Analog World
search and smart-search algorithms outlined below.
Stupid Search
constants
[[t2con $12][ccp1con $17][ccpr1l $15][pr2 $92]]
to pwm :val
clearbit 2 portc-ddr ;set RC2 to output
write t2con 4 ; turns on timer2
write pr2 255 ;sets period in units of
4*clock period
write ccp1con 12 ;select PWM mode
write ccpr1l :val ;set PWM duty cycle (time
on) in units of 4*clock
period
end
to stupid-ad
setn 0
repeat 255
[pwm n
if (testbit comparator comparator-port)
[output n]
setn n + 1 ; increment the counter
mwait 5 ;intentionally slow it down so
that you don’t surpass
the DACs speed limit
]
output 255
end
to mwait :num
resett
waituntil [timer > :num]
end
Page 5 Last Modified on 12/3/2011 11:52:00 PM
LogoChip in an Analog World
Binary Search
global [lower upper]
to binary-ad ;fewer steps
setlower 0
setupper 256
repeat 7 [setn (upper + lower) / 2
pwm n
ifelse testbit comparator comparator-port
[setlower n] [setupper n]
[mwait 10] intentionally slow it
down so that you don’t
surpass the DACs speed
limit
]
output n
end
Try watching the DAC output on the scope during the conversion process
for both the stupid and the binary search algorithms. Can you see the DAC
converging on the result in the case of the binary search?
It is necessary to set the cutoff frequency of the low pass filter so that the ac
component of the pwm signal will be strongly attenuated. In the above
1
example f3dB 72 Hz whereas the fundamental frequency of the
2RC
pwm signal is 50 kHz (assuming a 20 MHz clock). Note however that the
lower one sets the cutoff frequency of the low pass filter the longer the time
to complete an analog to digital conversion. Do you see why?
Page 6 Last Modified on 12/3/2011 11:52:00 PM