Tutorial for learning the AVR assembly language of
AVR single chip controllers AT90S, ATtiny, ATmega and ATxmega
of ATMEL usind practical examples.
A simple 8-bit-Digital-to-Analog-Converter with a R/2R network RC filter for the R/2R network
RC filter properties
R/2R networks generate steep rectangle flanks when used in dynamic applications,
e.g. to generate a sine wave. Those are even more relevant if a 4- or 5-bit
converter is used, so a clean sine wave looks different. That is how a 4-bit sine
wave looks like:
Only from the very far that looks like a sine. It consists of a splendid mixture of
1.000 Hz, 3.000 Hz, 5.000 Hz, 7.000 Hz and many, many more.
Only those who love large varieties find this useful, all others do something
against that mess and filter this a little bit.
Filter for DACs
Single stage RC filter
The high frequencies, well above the sine wave to be generated, need a cleanup.
The simplest way to achieve that is an RC filter. There are other methods as well,
but not for the price of 20 cents and with two small components. That is
how the RC filter looks like (note that I prefer using the latin letter U for
voltages to not confuse it with the dimension of voltages):
It works as follows. At the frequency f the capacitor C has an impedance XC of
XC = 1 / (2 * Π * f * C)
As an example, at f = 1000 Hz a capacitor of 10 nF has a X of
Increasing frequency and higher capacity decrease the impedance. At 3 kHz
the impedance is down to 5305 Ω, at 5 kHz 3183 Ω, etc.
The resistor R and the impedance of C are a voltage divider. On the output of the
divider the voltage is
UOut = UIn * XC / (R + XC).
At f = 1000 Hz, C = 10 nF und R = 10 kΩ the voltage divider produces
P1kHz = 15916 / (10000 + 15916) = 0.614
This results in a decrease of the input voltage down to the 0.614 fold. At 3 kHz
P3kHz = 5305 / (10000 + 5305) = 0.347
approximately half of the voltage at 1 kHz. At 5 kHz it is the 0.241 fold,
a decrease by a factor of roughly three.
The amplitude at higher frequencies is not going down to zero, but can be very, very
small if R, C and f increase to the 10, 100 or 1,000 fold.
Two stage RC filter
If filtered twice, the resulting response curve is even steeper, approximately
the 0.641 * 0.641 = 0.411 fold at 1 kHz. But at 3 kHz it is the 0.120 fold
and at 5 kHz the 0.058 fold. Additional RC stages increase the effect, that we
will see later on.
This is the attenuation effect that one, two and three stage RC filters have.
Between 10 and 100 Hz nearly no attenuation takes place, the frequencies
pass the filter nearly unaltered. At 1,000 Hz the one stage filter has an
attenuation of roughly half, the three stage filter is at 0.2. A further
decade higher, at 10,000 Hz, roughly 0.1 for the one stage filter, 0.001 for
the two stage filter and the three stage filter attenuates by 40,000 fold. A
promising method to get rid of the higher frequency mess of a DAC.
Filter effect for DAC signals
To demonstrate the effect, here is a 100 Hz signal generated with a 5 bit DAC,
filtered with R=10k and C=10n. The stairs of the DAC are nearly removed already in
the first RC stage, the second (fuchsia) and the third stage (red) are clean sine
that show no sign of the way they have been generated from.
The green curve is nearly a sine, but still follows the rectangle a bit in the
upper and lower sections. The fuchsia and red curves, after two resp. three RC,
only show some unevenness if a very close look is taken. Nearly an ideal sine
wave has been reached.
With the same DAC resolution, R and C, but at the 10 fold frequency:
This is a bit too much RC, and the base frequency is already attenuated to
roughly half. In exchange for that, already after the first stage a well-situated
sine wave has been reached. The further filter stages are more than fluous,
superfluous. Note that their attenuation is not that dramatic any more.
The difference between two and three stage filter is demonstrated here, with
only two stages active:
Here, the amplitude in both stages is less attenuated. In the three stage version
the RC stages not only work as voltage dividers: they cause back currents from
C3 to C2, and therefore also from C2 to C1,
which attenuates further. So better do not more filtering by adding RC stages
At eight bit resolution of the DAC a single RC stage is sufficient to cleanup the
For those who believe they would be better of with a 10 bit DAC: be aware that
the maximum frequency that can be generated is smaller by a factor of four then.
One has to overclock an AVR with 256 MHz to generate a usual 20 kHz
audio signal. But it is rather simpler to invest into two resistors and capacitors
and to program a 5 bit DAC than to change to another controller family from AMD
or Intel. More cleverness instead of higher clock frequencies!
Software for the filter simulation
No, you don't have to buy new software packages to play around with Rs, Cs,
frequencies and DAC resolutions. It is for free, either as Lazarus Pascal source
code to compile it for your operating system or already compiled for Win64.
The source code is here, a compiled version
for 64-Bit-Win is here.
The program is so simple to use that you do not need an instruction manual.
Simply change the properties in the left part. Editor fields check correct ranges
and get red if somethings is wrong. Please do not change entries manually in the
dropdown fields, just use the given selections. The button "Store image"
saves the current image as PNG or BMP.
Other software and applications
Here you can find a different
software tool that allows to play with frequencies, resistors and capacitors as
A versatile hardware to adjust RC networks to very different frequencies is applied