DAC using ATmega 16
DAC stands for digital to analog converter . In ATmega 16 there is no module which converts
digital value to ana analog voltage it only contains ADC (analog to digital converter ) .
Analog voltage from microcontroller can be used in many applications such as speed control of a
dc motor and dimmer of a light or LED .
In this tutorial we are going to use the timer module of the ATmega 16 to make DAC (i.e. to
obtain analog voltage from microcontroller) .
Timers in ATmega 16 works in 4 mode and there are two modes can be used for DAC :
1. Fast PWM (Pulse Width Modulation) .
2. Phase correct PWM .
In our example we will use Fast PWM but Phase correct PWM can be used in similar manner .
In Fast PWM the OCR register is always compared with the value of the timer and when they are
equal a signal is cleared (Non inv. mode) at the OC pin of the microcontroller and this signal is
set whe the timer reaches to the max (255 for timer0 or timer2) and is cleared and starts counting
again and the sequence is repeated again .
The average of the signal generated at the OC pin (represent the analog value) will correspond to
the digital value stored in the OCR register .
In the above figure the OCR value is 64 ( small value ) and the output pulse width is small so it's
average value is small and will be equal two ( (64 / 256)*5 =1.25 v approximately )
In the above figure the OCR value is 200 ( large value ) and the output pulse width is large so it's
average value is large and will be equal two ( (200 / 256)*5 =3.9 v approximately )
So the analoge output will range from 0 v to 5v .
Timer Adjustment :
In the code we used timer0 (counts from 0 to 255 ) adjusted in Fast PWM.
The OC pin is connected to Non inv. signal .
OCR value will take it's value from PINA (switches connected to PORTA ) .
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 8000.000 kHz
// Mode: Fast PWM top=FFh
// OC0 output: Non-Inverted PWM
Now there is a problem we want the average value of the output signal from OC pin . How we
can do this ?
It's so simple we want average i.e. DC value i.e the zero frequency component of the output
signal so we connect RC low pass filter at the output of the OC pin with a small cut off
frequency for example (50 HZ).
Fc (cut off frequency ) = 1/ (2*pi*R*C ) .
Fc =50 HZ .
we choose R=1k .
we calculate C =3.2uf .
In the above example the input digital value = " 1000 0000 " which corresponds to 128 which is
half 256 so the analog output as we see is half 5 v which is 2.5 v and so on we can change the
digital input .
Download the project and it's simulation
if you liked this tutorial visit us at :