Solution: Analog To Digital Converter
Solution: Analog To Digital Converter
The ADC is connected to an 8-channel Analog Multiplexer which allows each pin of
PortA to be used as input for the ADC. The analog input channel is selected by
writing to the MUX bits in ADMUX. This allows the selection of any of the 8
channels, AGND, or a fixed voltage reference.
The ADC has two different operating modes. In single conversion mode, each
conversion will be initiated by the user. In free running mode, the ADC is
constantly sampling and updating the ADC Data Registers. This mode is selected
by the ADFR bit in the ADC Control and Status Register.
The ADC is enabled by setting the ADC Enable bit, ADEN in ADCSR. Voltage
reference and input channel selections will not go into effect until ADEN is set.
A conversion is started by writing a logical one to the ADC Start Conversion bit,
ADSC. This bit stays high until the conversion is complete (Hardware will clear the
bit). If a different data channel is selected while a conversion is in process, the ADC
will finish the current conversion before performing the channel change.
The 10-bit result is stored in ADC data registers ADCH and ADCL. Default is right
justified, by setting the ADLAR bit in ADMUX, the result can be left adjusted. If
using left adjusted, reading ADCH is sufficient if 8-bit precision is only required.
Otherwise, ADCL must be read first, then ADCH.
When a conversion is complete, the result is written to the ADC data registers, and
ADIF is set. In single conversion mode, ADSC is cleared simultaneously. The
software may then set ADSC again to start a new conversion. In Free Running
Mode, a new conversion will be started immediately after the conversion completes
while ADSC remains high.
The ADC requires an input clock frequency between 50kHz and 200kHz. ADPS
bits in ADCSR are used to generate a proper ADC clock input frequency.
Following the steps below will set up the Analog to Digital Converter for use.
ADC Registers
We will discuss the registers one by one.
The bits that are highlighted are of interest to us. In any case, we will discuss all the bits one by
one.
● Bits 7:6 – REFS1:0 – Reference Selection Bits – These bits are used to choose the
reference voltage. The following combinations are used.
The ADC needs a reference voltage to work upon. For this we have three pins AREF, AVCC and
GND. We can supply our own reference voltage across AREF and GND. For this, choose the
first option. Apart from this case, you can either connect a capacitor across the AREF pin and
ground it to prevent noise, or you may choose to leave it unconnected. If you want to use the
VCC (+5V), choose the second option. Or else, choose the last option for internal Vref.
Let’s choose the second option for Vcc = 5V.
● Bit 5 – ADLAR – ADC Left Adjust Result – Make it ‘1’ to Left Adjust the ADC
Result. We will discuss this a bit later.
● Bits 4:0 – MUX4:0 – Analog Channel and Gain Selection Bits – There are 8 ADC
channels (PA0…PA7). Which one do we choose? Choose any one! It doesn’t matter.
How to choose? You can choose it by setting these bits. Since there are 5 bits, it consists
of 2^5 = 32 different conditions as follows. However, we are concerned only with the
first 8 conditions. Initially, all the bits are set to zero.
Input Channel and Gain Selections
Thus, to initialize ADMUX, we write
ADMUX = (1<<REFS0);
ADCSRA Register
The bits that are highlighted are of interest to us. In any case, we will discuss all the bits one by
one.
● Bit 7 – ADEN – ADC Enable – As the name says, it enables the ADC feature. Unless
this is enabled, ADC operations cannot take place across PORTA i.e. PORTA will
behave as GPIO pins.
● Bit 6 – ADSC – ADC Start Conversion – Write this to ‘1’ before starting any
conversion. This 1 is written as long as the conversion is in progress, after which it
returns to zero. Normally it takes 13 ADC clock pulses for this operation. But when you
call it for the first time, it takes 25 as it performs the initialization together with it.
● Bit 5 – ADATE – ADC Auto Trigger Enable – Setting it to ‘1’ enables auto-triggering
of ADC. ADC is triggered automatically at every rising edge of the clock pulse. View the
SFIOR register for more details.
● Bit 4 – ADIF – ADC Interrupt Flag – Whenever a conversion is finished and the
registers are updated, this bit is set to ‘1’ automatically. Thus, this is used to check
whether the conversion is complete or not.
● Bit 3 – ADIE – ADC Interrupt Enable – When this bit is set to ‘1’, the ADC interrupt
is enabled. This is used in the case of interrupt-driven ADC.
● Bits 2:0 – ADPS2:0 – ADC Prescaler Select Bits – The prescaler (division factor
between XTAL frequency and the ADC clock frequency) is determined by selecting the
proper combination from the following.
Assuming XTAL frequency of 16MHz and the frequency range of 50kHz-200kHz, we choose a
prescaler of 128.
Thus, F_ADC = 16M/128 = 125kHz.
ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
// prescaler = 128
You can very well see the effect of the ADLAR bit (in ADMUX register). Upon setting ADLAR
= 1, the conversion result is left adjusted.
Usage:
The ADC module contains a prescaler, which divides the system clock to an acceptable ADC
clock.
The ADC of the AVR converts analog signal into digital signal at some regular interval. This
interval is determined by the clock frequency. In general, the ADC operates within a frequency
range of 50kHz to 200kHz. But the CPU clock frequency is much higher (in the order of MHz).
So to achieve it, frequency division must take place. The prescaler acts as this division factor. It
produces desired frequency from the external higher frequency. There are some predefined
division factors – 2, 4, 8, 16, 32, 64, and 128. For example, a prescaler of 64 implies F_ADC =
F_CPU/64. For F_CPU = 16MHz, F_ADC = 16M/64 = 250kHz.
c) Design a 5V (DC) voltage source using 7805.
Solution:
The L293D is a 16 pin IC, with eight pins, on each side, dedicated to the controlling of a motor.
There are 2 INPUT pins, 2 OUTPUT pins and 1 ENABLE pin for each motor. L293D consists of
two H-bridges. H-bridge is the simplest circuit for controlling a low current rated motor.
Motor Driver ICs are primarily used in autonomous robotics only. Also most microprocessors
operate at low voltages and require a small amount of current to operate while the motors
require relatively higher voltages and current . Thus current cannot be supplied to the motors
from the microprocessor. This is the primary need for the motor driver IC.