ADC, DAC and Sensor Interfacingsaqazi.com/EEE342/FA19_MP_Lecture18_20191126_ADC_DAC... · 2019. 12....
Transcript of ADC, DAC and Sensor Interfacingsaqazi.com/EEE342/FA19_MP_Lecture18_20191126_ADC_DAC... · 2019. 12....
ADC, DAC and
Sensor InterfacingLecture# 18
Microprocessor System and Interfacing
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
1
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
2
Submit
Assignment No 2
Next Class i.e. Thursday
ADC Characteristics
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
3
ADC Devices
Analog to Digital Converters, widely used in data
acquisition
To convert physical world input (analog) to computer
compatible (discrete, binary)
Physical world include Temperature, Pressure, Humidity,
Velocity etc
Physical quantity is converted to electrical voltage using
Sensors.
An ADC is required to convert this voltage/current to a
digital number to be processed by AVR/Computer
ADC Characteristics
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
4
Major Characteristics of ADC
Resolution
An ADC has n-bit resolution
n can be 8, 10, 12, 16 or even 24
Higher Resolution = Smaller step size
Step size the is smallest change possible
No. of steps = 2n
Max. Voltage = 5 V
n-bit No. of Steps Step Size (mV)
8 256 5/256 = 19.53
10 1024 5/1024 = 4.88
12 4096 5/4096 = 1.2
16 65536 5/65536 = 0.076
ADC Characteristics
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
5
Major Characteristics of ADC
Conversion Time
A major factor to check the performance of ADC
Defined as the time taken by ADC to convert the analog
input to a digital number.
It is dependent on the clock source used for AVR
ADC Characteristics
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
6
Major Characteristics of ADC
Reference Voltage (Vref)
Vref is input voltage pin, used for reference voltage
Voltage connected to Vref pin and resolution of ADC define
the step size
Step Size = vref / 2n
Example:
If Analog voltage is between range 0 – 4V.
Vref will be 4 Volts
Step size for 8-bit ADC will be = 4 / 256 = 15.62mV
ADC Characteristics
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
7
Major Characteristics of ADC
Digital Data Output
To calculate the output voltage use this formula
Dout = Vin / (step size)
Dout is analog data output
Vin = analog input voltage
Step size is the minimum change in voltage
ADC Characteristics
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
8
Major Characteristics of ADC
Parallel vs Serial ADC
Parallel ADC: 8 or more bits are used to bring out data
Serial ADC: Only one pin for data out.
Inside serial ADC a parallel-in-serial-out shift register is used
ADCs with more than 8 bits conversion size use same D0-D7
bits to send lower and higher bytes
Serial ADCs are preferred where space is a critical issue
Fewer Pins, Smaller Packages, Less Space
ADC Characteristics
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
9
Major Characteristics of ADC
Analog Input Channels
Many applications need more than one ADC
Chips with 2, 4, 8 or 16 ADC channels are available
Multiplexing is used for the multiple ADC channels
ADC Characteristics
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
10
Major Characteristics of ADC
Start and End of Conversion Signals
Multiple input channels and a single output channel needs
some synchronization.
Start of Conversion (SC) and End of Conversion (EOC) is
defined.
SC activated, conversion starts
After some time when conversion is complete, EOC signal tells
data is ready to be copied
ADC Characteristics
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
11
Major Characteristics of ADC
Successive Approximation ADC
Widely used method because
Conversion time is fixed, hardware compatible
Three main components
Successive approximation register (SAR)
Comparator
Control Unit
Example 1 – Successive Approximation ADC
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
12
Assume a step size of 10 mV and 8-bit SA-ADC and analog input is 1
Volts. The following steps will be followed to convert to digital output
1. Start with binary 1000 0000 = 128 in dec
128 x 10 mV = 1.28 V
1.28V > 1 V
Clear D7 bit (0000 0000)
2. Take next binary 0100 0000 = 64
64 x 10 mV = 640 mV
640mV < 1 V
Retain D6 bit (0100 0000)
3. Take next binary 0110 0000 = 96
96 x 10 mV = 960 mV
960mV < 1 V
Retain D5 bit (0110 0000)
4. Take next binary 0111 0000 = 112
112 x 10 mV = 1120 mV
1120 mV > 1 V
Clear D4 bit (0110 0000)
5. Take next binary 0110 1000 = 108
108 x 10 mV = 1010 mV
1080mV > 1 V
Clear D3 bit (0110 0000)
6. Take next binary 0110 0100 = 100
100 x 10 mV = 1000 mV
1000 mV = 1 V
Retain D2 bit (0110 0100)
We have reached the result, still it will iterate the
next two bits
7. Take next binary 0110 0110 = 102
102 x 10 mV = 1020 mV
1020 mV > 1 V
Clear D1 bit (0110 0100)
8. Take next binary 0110 0101 = 101
101 x 10 mV = 1010 mV
1010mV > 1 V
Clear D0 bit (0110 0100)
ADC Programming in AVR
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
13
ADC Hardware Considerations
For digital signals small change in voltage level is not a big
deal (<0.5 is considered low)
For ADCs, small change means a lot.
A voltage change equal to step size changes the output
Need a stable ADC supply voltage and Vref variations
Which can be achieved using
Inductor and Capacitor
between AVCC and GND
Capacitor between AVREF
and GND
ADC Programming in AVR
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
14
Registers for ADC Operation
ADCH (High Data Byte)
ADCL (Low Data Byte)
ADCSRA (ADC Control and Status Register)
ADMUX (ADC Multiplexer Selection Register)
SPIOR (Special Function IO Register)
ADC Programming in AVR
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
15
Registers for ADC Operation
ADMUX (ADC Multiplexer Selection Register)
Vref Source (Three Options)
AREF pin (0 0)
AVCC pin (0 1)
Internal 2.56 V (1 1)
REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0
ADC Programming in AVR
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
16
Registers for ADC Operation
ADMUX (ADC Multiplexer Selection Register)
ADC Input Channel Source
Single Ended input
ADC0 – ADC7 Channel
Differential Input
Can select Op-amp gain
1x, 10x or 200x
+ve pin of Op-amp (ADC0 -7)
-ve pin of Op-amp (ADC0-2)
REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0
ADC Programming in AVR
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
17
Registers for ADC Operation
ADMUX (ADC Multiplexer Selection Register)
ADLAR bit Operation
AVRs normally have 10-bit ADC
Result is stored in 16-bits (ADCH, ADCL)
6-bits are unused
ADLAR = 1 result is left-justified
ADLAR = 0 result is right-justified
REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0
ADC Programming in AVR
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
18
Registers for ADC Operation
ADCSRA (Status and Control Register)
ADC Start Conversion bit
AVR has a special circuit to trigger start conversion
ADSC is one to be used for start conversion
SFIOR register control other modes
For this course we are only focused on normal operation of ADC
ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0
ADC Programming in AVR
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
19
Registers for ADC Operation
ADCSRA (Status and Control Register)
A/D Conversion Time
Using ADPS 2:0 A/D conversion time can be controlled
ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0
ADSP2 ADSP1 ADSP0 Clock
0 0 0 Reserved
0 0 1 clk/2
0 1 0 clk/4
0 1 1 clk/8
1 0 0 clk/16
1 0 1 clk/32
1 1 0 clk/64
1 1 1 clk/128
ADC Programming in AVR
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
20
Steps in Programming the A/D Converter
1. Make the pin for selected ADC channel as input pin
2. Turn on ADC module (ADEN in ADCSRA) , it is disabled uponreset
3. Select Conversion Speed (ADPS2:0 in ADCSRA)
4. Select voltage reference (REFS1:0 in ADMUX) and ADC inputchannels (MUX4:0 in ADMUX)
5. Activate start conversion (ADSC in ADCSRA)
6. Wait for conversion complete (ADIF in ADCSRA)
7. When ADIF is HIGH, Read Data (ADCL, ADCH)
1. Read ADCL before ADCH
8. To read same Channel again, Go to Step 5
9. Read using diff. Vref or input channel, Go to Step 4
Example 1
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
21
Get data from Channel 0 (ADC0) of ADC and display the result
on PortB and PortD.
# include <avr/io.h>
int main (void)
{
DDRB = 0xFF;
DDRD = 0xFF;
DDRA = 0x00;
ADCSRA = 0x87; ADC enable & clk/128
ADMUX = 0xC0; 2.56 Vref, ADC0 channel
Right Justified
while (1)
{
ADCSRA |= (1 << ADSC);
while ((ADCSRA & (1 << ADIF)) == 0);
PORTD = ADCL;
PORTB = ADCH;
}
return 0;
}
Exercise 1
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
22
Perform same operation of last example using interrupts.
# include <avr/io.h>
# include <avr/interrupts.h>
ISR (ADC_vect)
{
PORTD = ADCL;
PORTB = ADCL;
ADCSRA |= (1 << ADSC);
}
int main (void)
{
DDRB = 0xFF;
DDRD = 0xFF;
DDRA = 0x00;
ADCSRA = 0x8F; ADC enable & clk/128 and
ADIE enable
ADMUX = 0xC0; 2.56 Vref, ADC0 channel
Right Justified
sei (); Enable Global Interrupt
ADCSRA |= (1 << ADSC);
while (1);
return 0;
}
Exercise 2
Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad
23
Get data from Channel 0 (ADC0) of ADC, divide 10-bit result by 4to fit in 8-bits and send result to PortB. Then get data from Channel1 (ADC1), divide 10-bit result by 4 to fit in 8-bits and send to PortD.Repaet this process again and again. Use ADC clock pre-scalingwith a factor of 32.# include <avr/io.h>
int main (void)
{
DDRB = 0xFF;
DDRD = 0xFF;
DDRA = 0x00;
ADCSRA = 0x85; ADC enable & clk/32
while (1)
{
ADMUX = 0xE0; 2.56 Vref, ADC0 channel
Left Justified
ADCSRA |= (1 << ADSC);
while ((ADCSRA & (1 << ADIF)) == 0);
PORTB = ADCH;
}
return 0;
}
ADMUX = 0xE1; 2.56 Vref, ADC1 channel
Left Justified
ADCSRA |= (1 << ADSC);
while ((ADCSRA & (1 << ADIF)) == 0);
PORTD = ADCH;
}
return 0;
}