Topic 13 - ATMega32 ADC in C (ISMAIL - SKE - UTM 2021)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

Topic 13: ADC

ISMAIL ARIFFIN
SKE FK UTM SKUDAI JOHOR
(2017,2021)
Content
• Analogue to digital converter
• ADC in AVR, ADC Registers
• AVR’s ADC programming
• ADC Application using LM35
• ADC Problem Exercise
ADC
o Analog-to-digital converters (ADC) are among the most widely used
devices for data acquisition.
o Digital computer use binary (discrete), but physical world everything is
analog (continuous).
o Thus a device called transducer (sensor) is needed to convert physical
quantities (i.e. temperature, pressure, etc) to electrical (voltage/current)
signal.

o ADC is a circuit used to translate analog signal to digital signal.


ADC Characteristics
o In ATmega32, a 10-bit ADC is used to translate the analog signals to
digital signal so that micro-p can read and process them.

o Some of the major characteristics of ADC that we need to know.


1. Resolution
• ADC has n-bit resolution (8, 10, 12, 16..). 10-bit resolution ADC in
ATmega32
• Higher-resolution ADC provides smaller step size, the smallest change
can be discerned (notice) by an ADC.
Step size = Vref / no. of digital steps = Vref / 2n
• Although ADC resolution is fixed, but the step size can be controlled by
Vref.
ADC Characteristics
2. Conversion time
• Conversion time is the time taken by ADC to convert an analog input to
digital number.

3. Vref
• Vref is an input voltage used for reference voltage.
• The voltage connected to this pin, along with ADC resolution, will dictate
the step size.

4. Digital data output


• In an 8-bit ADC, we have an 8-bit digital data output of D0-D7.
• Thus, Dout:
ADC Characteristics

5. Parallel vs Serial ADC


• Parallel ADC – 8 or more pins dedicated to bring out the binary data.
• Serial ADC – Only one pin for data out.
ADC Characteristics
6. Analog input channel
• Many data acquisition applications need more than one ADC.
• Thus, an ADC chip comes with version of 2, 4, 8 or 16 channels.

7. Successive approximate ADC


• A widely used conversion circuit to convert analog input to digital input.
• Comprise four components: successive approximation register (SAR),
comparator, control unit and DAC
ADC Programming
ATmega32 ADC Features
o ADC peripheral of ATmega32 has the following characteristics:
1. 10-bit ADC.
2. Has 8 analog input channels, 7 differential input channels and 2 differential
input channel with optional gain of 10x and 200x.
3. The converted output binary data is held by two special function registers:
ADCL (A/D Result Low) & ADCH (A/D Result High).
4. Since ADCH:ADCL gives us 16-bit and ADC data out only 10-bit, thus
remaining 6-bit is unused.
5. Three option for Vref. Vref connected to AVCC (analog Vcc); internal 2.6V
reference;, external AREF pin.
6. Conversion time dictated by crystal frequency connected to XTAL and
ADPS0:2 bits.
ADC Connection & ADC Programming Register
o Use inductor and capacitor in order to get a stable voltage
source to the AVCC pin.

o In ATmega32, five major registers are associated with ADC:


1. ADCH (ADC high data)
2. ADCL (ADC low data)
3. ADCSRA (ADC Control and Status Register)
4. ADMUX (ADC multiplexer selection register)
5. SPIOR (Special Function I/O Register)
ADC Registers
ADMUX Register
o There are three
option for Vref
selection:
1. AREF pin
2. AVCC pin
3. Internal 2.56V
o The REFS1:0 bit 7:6
in ADMUX will
determine the Vref.
ADMUX Register
o The MUX4:0 bit 4:0 in ADMUX will
determine the analog channel and gain
selection to be used.
o Either single-ended or differential
input can be selected to be converted
to digital data.
o If single-ended is selected, you can
chooses input channel among ADC0 to
ADC7 – single pin is used as analog
line, and GND as common ground.
ADMUX Register
o If differential
input is
selected, you
can choose input
channel as well
as the op-amp
gain: 1x, 10x,
200x.
ADMUX Register
o The ADLAR bit 5 in ADMUX will determine either the left bits or the right
bits of ADCH:ADCL registers used to store the result.
o AVRs have a 10-bit ADC, but since two 8-bit registers dedicated to ADC result
(ADCH:ADCL), thus 6 bits are unused and need to select position which bits
are used and not.
o Set One ‘1’: left justified.
o Set Zero ‘0’: right justified.
ADCH:ADCL Registers
o ADCH:ADCL registers are used to store the results after the A/D conversion
is complete.
o Since the ADC is 10-bit while ADCH:ADCL are 16-bit, we need to set the
ADLAR bit of the ADMUX for making it right-justified or left-justified.
ADCSRA Register
o The ADCSRA register is the status and control register of ADC.
ADCSRA Register
o The ADPS2:0 Bit 2:0 : ADC Prescaler/Conversion time
o These bits will determine the division factor between XTAL frequency and the
input clock of ADC.

o To select conversion time, we can select any of Fosc/2, Fosc/4, Fosc/8, Fosc/16,
Fosc/32, Fosc/64 or Fosc/128 for ADC clock.
o Fosc is the speed of the crystal frequency connected to AVR.
o For the AVR, ADC requires an input clock frequency less than 200 KHz for the
maximum accuracy.
ADCSRA Register
ADC Programming procedure using Polling
1. Make the selected ADC channel as an input pin.
2. Enable ADC since its disable upon power-on-reset to save power.
3. Select conversion speed (ADPS2:0)
4. Select voltage reference (REFS1:0) and ADC input channel (MUX4:0) in
ADMUX register.
5. Activate start conversion bit – write ‘1’ to ADSC of ADCSRA register.
6. Wait for the conversion to be completed by polling the ADIF bit of
ADCSRA register.
7. After ADIF = HIGH ‘1’, read ADCH:ADCL to get digital data output. * read
ADCL before ADCH.
8. If want to read the selected channel again, go back to step 5.
9. If want to select another Vref source or input channel, go back to step 4.
Example: Programming the ADC using Polling
Initialise ADC
The following adc_init() function initializes the ADC. Assume
CPU frequency = 16Mhz.
void adc_init()
{
ADMUX = (1<<REFS0); // Select Vref, AREF = AVcc
// ADC Enable and prescaler of 128
// Conversion speed, 16000000/128 = 125000
ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}
Read ADC Value
The following adc_read() function reads the value of the ADC.

//Input: ADC Channel No in ch


//Return: ADC value
uint16_t adc_read(uint8_t ch)
{
/* select the corresponding channel 0~7 ANDing with ’7′ will always keep the
value of ‘ch’ between 0 and 7*/
ch &= 0b00000111; // AND operation with 7
ADMUX = (ADMUX & 0xF8)|ch; // clears the bottom 3 bits before ORing
ADCSRA |= (1<<ADSC); // start single conversion write ’1′ to ADSC
// until ADIF becomes ’1′
while ((ADCSRA & (1<<ADIF))==0); // wait for conversion to complete (ADIF=1)
return (ADC);
}
Convert a ADC Value to a Voltage
Example: Programming the ADC using Polling

#include <avr/io.h> int main () {


DDRC = 0xFF ; // PortC as output
void adc_init () { DDRD = 0xFF; // PortD as output
// Enable ADC, prescale 128 DDRA = 0x00; // PortA as input
ADCSRA = 0x87; adc_init () ;
//Internal Vref 2.56,ADC0,Right while (1) {
//Justified while ( (ADCSRA & 1<<ADIF )== 0 );
ADMUX = 0xC0; PORTD = ADCL;
//start conversion PORTC = ADCH;
ADCSRA = 1<<ADSC; ADCSRA=1<<ADSC; // restart ADC
} }
return 1;
}
Example: Programming the ADC using Interrupt
#include <avr/io.h> int main () {
#include <avr/interrupt.h> //port b , portd
void ISR(ADC_vect)
//output
ddrb=0xff;
ISR(ADC_vect) { ddrd=0xff;
portd = adcl ; //port A input
portb = adch; ddra=0x00;
//start conversion
adcsra = 1 << Adsc ;
adc_init();
} sei();
while (1) ;
void adc_init() { return 0;
adcsra=0x8F; //On ADC,on ADC Interrupt, CLK/128 }
admux=0xC0; //2.56Vref, Right,ADC0
adcsra= 1<<ADSC; // start convert
}
DAC Programming
Digital-to-Analog Converter (DAC)
o DAC widely used to convert digital pulses to analog signals.
o There are two methods of creating DAC:
1. Binary weighted
2. R/2R ladder – to achieve higher degree of precision. (DAC0808).
DAC0808
o In DAC0808, the digital inputs are converted to current (Iout), and by
connecting resistor to the Iout, we convert the result to voltage.

o Iref is the input current that must be applied to pin 14.


Sensor Interfacing
Temperature Sensors
Temperature Sensors
Temperature Sensor
Temperature Sensor
Example: Temperature Sensor Programming
Problem Exercise
QUESTION

An ATmega32-based system is going to be used in detecting


temperature using a sensor, LM35. The input from the sensor is
converted into digital data and the digital data is displayed on
ten common cathode LEDs connected to PORTB and PORTD
where PORTB displays the lower data and PORTD displays the
next higher data. The temperature sensor, LM35 with a change
rate of 10mV/C, is used to sense the heat condition. The circuit
in Figure Q.7 shows only ATmega32 and LM35 devices. For
system operation, at RESET, the LEDs will blink once and then
the LEDS will continuously display the digital. Use internal clock
of 1 MHz for ATmega32 frequency.
Problem exercise
(i) Sketch circuit to show ten LEDs interfaced to AVR PORTS as prescribed in the
question.

(ii) Write all the required definition and declaration header files for the
development of the C program.

(iii) Write a C code in function named as port_init() to configure the PORTs

(iv) Write a C code in function named as blinking() , to blink all LEDs once at the reset
time.

(v) Write a C code in function named as adc_init() to configure and enable ADC with
a prescale of clk/128 and internal 2.56 Vref.

(vi) Write the main function to continuously sample the temperature through the
LM35 sensor and display the sensor’s digital value on the LEDs.
SUMMARY
• Know general features of Analogue-to-
Digital Converter and features of ATmega32
ADC

• Know ADC programming in AVR

• Application of ADC with temperature


sensor
THE END
(Syllabus end)

You might also like