You are on page 1of 23

The University of Texas at Arlington Lecture 10 ADC and DAC

CSE 3442/5442
1

Measuring Physical Quantities


(Digital) computers use discrete values, and use these to emulate continuous values if needed. In the physical world most everything is continuous (analog), e.g., temperature, pressure, acceleration, location, etc. We can measure many physical quantities but to use them in circuits we need transducers (some physical quantity is converted into a electric quantity).
2

Sensors and Transducers


Transducer often used interchangeably with sensor Many sensors are actually a type of transducer. Transducers are devices that convert one form of energy into another. They include both actuators and a subset of the various types of sensors.
3

Sensors with Analog Outputs


Many sensors output analog (only) signals where output is proportional to some kind of measured physical quantity (temperature, acceleration, etc.) Accuracy or precision of a sensor determines how close the measured (and output) value is to the real value. (What is the smallest unit that makes a difference in the measurement). Dynamic range: what is the relation of the fullscale value to the minimally detectable signal variation. In order to use the outputs of analog sensors in calculations, their outputs have to be digitized.

A/D Conversion
The process of A/D conversion involves:
Band-limiting the analog signal Setting periodic sampling points (setting a sampling period) at which the continuous time analog signal is going to be digitized. Freezing the analog signal at the sampling points so that the signal does not change for the duration of conversion (sample and hold) Determining what digital value best represents the analog signal level (quantizing).
5

Limiting Bandwidth of Signal


Real life physical quantities can change extremely rapidly (i.e., continuously), i.e., being capable of an immediate change and thus having an infinite frequency band. Sensors measuring these quantities (as they have to deal with mechanical and electrical inertia of their measuring ways) usually reduce that bandwidth significantly (i.e., if they reduce it to a 100kHz, that means that they could not pick up a sudden 1ns flipflop of the real value). In most cases sensors bandwidths are orders of magnitudes above what we are interested in (e.g., temperature changes, we do not really care about sub second changes). In order to ignore sudden changes (and so that they do not negatively influence our ADC) we need to limit the bandwidth of the signal (smooth it out) based on our frequency resolution needs. Usually, analog low-pass filters (many cases with simple sensors simple R/C circuitry) is used.
6

Periodic Sampling
Now that we have limited the bandwidth of the signal, we need to sample it. The Nyquist/Shannon theorems say that you need to sample the signal at least at twice the maximum frequency contained in the signal. (E.g., if you limited your signal to a max frequency of 10Hz, You should sample it at least at 20Hz, i.e., 20 times a second). In many cases though we start from the sampling frequency and having a good understanding of that we limit the signal to at most half of that. 7

Example: Output of a Sensor


Continuous-Time Signal; x(t) = cos(10t) + cos(25t) 2

1.5

0.5 Amplitude

-0.5

-1

-1.5

-2

0.2

0.4

0.6

0.8

1 Time (s)

1.2

1.4

1.6

1.8

This is the superposition of two sine signals (two frequencies present)

Example: Output of a Sensor (contd)


Y(t) = cos(10t+0)+cos(25t+0) Thus, two frequencies one with 1=10 and the other with 2=25 This means f1=10/(2), f2=25/(2) f2 is higher (~4Hz) Thus, to be able to reconstruct the signal (without loosing any of its features) we need to sample at least 8 times a second (8Hz). So, what happens if we overor under-sample?

Continuous-Time Signal; x(t) = cos(10t) + cos(25t) 2

1.5

0.5 Amplitude

-0.5

-1

-1.5

-2

0.2

0.4

0.6

0.8

1 Time (s)

1.2

1.4

1.6

1.8

Example: Oversampling
If original signal was indeed nicely limited, oversampling will result in more samples than we need for processing and reconstruction (i.e., burden on our calculations/ processor time) Shown here is 20 samples per second (a sampling clock of 20Hz)
Sampled Version of x(t) with Ts = 0.05 s 2

1.5

0.5 Amplitude

-0.5

-1

-1.5

-2

0.2

0.4

0.6

0.8

1 Time (s)

1.2

1.4

1.6

1.8

10

Example: Undersampling
Undersampling will result loosing the features of a signal. If we wanted to reconstruct the signal the reconstructed signal (red) would not look the same as the original (black) signal What is the sampling clock used here?
Sampling x(t) and x 1(t) with Ts = 0.2 s 2

1.5

x1(t)

x(t) 1

0.5 Amplitude

-0.5

-1

-1.5

-2

0.2

0.4

0.6

0.8

1 Time (s)

1.2

1.4

1.6

1.8

11

Hold after Sample


We need circuitry that can remember (hold) the analog voltage at the sampling time The Ctrl signal in the circuit below should have the periodicity of the sampling rate and a duty cycle corresponding to the holding (charging time) Trade-offs of the holding time!
Ctrl
R
Sampled Version of x(t) with T = 0.1 s
s

1.5

0.5 Amplitude

-0.5

-1

-1.5

-2

0.2

0.4

0.6

0.8

1 Time (s)

1.2

1.4

1.6

1.8

Ch

12

Quantization
Now that the signal is not changing for some time (depending on the sampling period), we need to determine what digital value best represents the analog level. This will take some time, which should not be more than the sampling period! There are many methods of doing this, many microcontrollers use successive approximation to do this.
13

Successive Approximation ADC


Voltage

Successive Approximation ADC


Vin + Comparator
SUCCESSIVE APPROXIMATION

CONTROL LOGIC

11 V3 10 V2 01 V1 V0 00

Vin time

Set DAC output to V2 = 01 DAC generates analog voltage V2 (Vin>V2) Set MSB to 1
DAC

10

DAC now generates V3 Set LSB to 0 since V3>Vin Digitize in n Bits = n Cycles Due to errors does not scale well to above 12 -14 bits
14

A/D Converters
Analog-to-digital converters (A/D converters or ADCs) are used to convert continuous signals to discrete values.
Resolution: usually expressed in n-bits. Conversion time: how long it takes to convert from analog to digital. Reference: what is the reference (e.g., the highest and lowest value to which we are comparing). Linear vs. non-linear transfer (equal step size vs. changing step size) 15

ADC Resolution
n-bits, where n can be any number but rarely below 8 or rarely higher than 24. Resolution determines the quantization steps. An 8-bit ADC has 256 different values, thus if Vmin (V-ref) is 0V and Vmax (V+ref) is 5V and the quantization is linear, then the precision is 5/256 ~= 19.5mV (we could say 10mV). The resolution of an AD converter is unchangeable (we can lower it from software by ignoring LSBs), but the Vref can be changed to fit the dynamic range of the sensor to increase precision. 16

ADC Conversion Time


A/D conversion takes time. The analog part of the circuit needs to remember the analog voltage at the exact time of the sampling (recall, that this is usually done by charging a capacitor for a finite amount of time). Once that value is remembered, the ADC needs to determine (quantize) what the corresponding digital value to that voltage is. This is usually determined by a quantization clock. The conversion time has to be smaller than the sampling time! 17

Vref
We know that the resolution (n-bits) determines how many quantization steps there are, thus determining the relative scale. What determines the absolute scale though, i.e., what voltage value represents 0 and what voltage value represents 0b11111111 (for 8-bit resolution)? V-ref and V+ref are used for that. ADC circuitries usually have a default of V-ref=Vss and V+ref=Vcc . However most ADCs let you feed in analog voltages representing V-refand V+ref.
18

Vref (contd)
In general Vref-s could be any voltage but many times it is required to be within [Vss, Vcc]. The PIC microcontrollers built in ADCs do require that. So, whats the step size of an 8-bit ADC where V-ref =2V and V+ref=3V? (3.9mV) What would happen if V-ref =-2V and V+ref=2V, how would we want the values represented? The digital output can be expressed in a number of coding schemes, such as binary, or two's complement binary, or offset binary. If we know all this, we can reconstruct the voltage level in software.

19

2s Complement vs. Offset Binary

20

Stand-alone ADCs
Parallel versus serial output:
An n-bit parallel output ADC has n-pins to talk to a CPU unit in addition to data ready and channel selection bits. Less CPU time, more data pins. A serial output ADC has two pins to talk to the CPU, one for clock, the other for data. More CPU time, few pins. Can be a combination, where (similarly to how we talk to the LCD) x-bits at a time are sent out serially. Can balance CPU time and pins.

Number of analog input channels (i.e., how many sensor can be connected). They can be multiplexed (one ADC) or parallel (many ADCs inside). Start and end of conversion signals. We need to tell the ADC when to start conversion (indication of sampling) and it has to have feedback on when the conversion is finished. Thus there can be many pins in an ADC for communication and ADC channels. Most PIC microcontrollers have ADC peripherals built in. The above will have an effect on these peripherals.
21

PIC18 ADC
ADC can be found on most PIC18-s as an integrated peripheral The on chip ADC peripheral needs to be programmed before use. SFR registers are used for all communications between the ADC peripheral and the CPU. PIC18 ADC-s are 10-bit, successive approximation. (Other microcontrollers have different precision and architecture on chip ADCs). Thus two 8-bit registers are needed with 6 bits unused (ADRESL and ADRESH). Up to 16 analog channels (8 for PIC18F452; AN0-AN7) multiplexed to a single ADC. Reference voltage can be fed. Control registers used to set-up and start conversion.
22

PIC18 ADC Channel Selection

23

ADCON0 Control Register

Determines conversion time More about it shortly

24

ADCON1 Control Register

25

ADRESL and ADRESH


Only 10 bits out of 16 are used. Which 6 should be unused? There is a case for left justifying and one for right justifying as well. Let the user choose.

26

PIC18 A/D Acquisition Time


The A/D acquisition time is essentially the time required for the hold capacitor to charge. The time depends on the supply voltage and the out impedance of the sensor. However we can calculate with a typical value of 5-15s. Newer PIC microcontrollers have specific control for TACQ (determined in TADs)

27

PIC18 A/D Conversion Time


TAD is the conversion time per bit, i.e., the clock at which the successive approximation runs plus overhead. In addition to the 10 TAD cycles for conversion we need two more TAD cycles. Thus the PIC18 takes a total conversion time of 12 TAD-s. However, TAD has to be larger than 1.6s! (Thus, sampling rate cannot exceed 1/(1.6E-6*12+1E-5) ~= 30kHz, depending on TACQ). TAD is determined by the conversion clock (Fosc/2 Fosc/64, or internal RC). If internal RC is used then TAD is about 2-6s and thus total conversion time is around 72s (i.e., sampling rate cannot be larger than ~1.4kHz).
28

Oscillator Prescalar vs. TAD

29

Sampling Frequency
Note, that we have only described a single conversion. If periodical conversion needs to be set up, the GO bit has to be turned on periodically and data produced (ADRESH, ADRESL) has to be read periodically. This can be done by:
Calculating timing (i.e., NOP instructions after DONE) Using timer peripherals (not covered yet) Proper interrupt processing.
30

Steps When Using the PIC18 ADC


1. Configure the A/D module:
Configure analog pins, voltage reference and digital I/O (ADCON1) Select A/D input channel (ADCON0) Select A/D conversion clock (ADCON0) Turn on A/D module (ADCON0)

2. Configure A/D interrupt (if desired):


Clear ADIF bit Set ADIE bit Set GIE bit

3. Wait the required acquisition time. 4. Start conversion:


Set GO/DONE bit (ADCON0)

5. Wait for A/D conversion to complete, by either:


Polling for the GO/DONE bit to be cleared OR Waiting for the A/D interrupt

6. Read A/D Result registers (ADRESH/ADRESL);


clear bit ADIF if required.

7. For next conversion, go to step 1 or step 2 as required.


The A/D conversion time per bit is defined as TAD. A minimum wait of 2 TAD is required before next acquisition starts. 31

Programming the PIC18 ADC


void main(void) { TRISC = 0; TRISD = 0; TRISAbits.TRISA0 = 1; ADCON0 = 0x81; //FOSC/64, channel 0 A/D on ADCON1 = 0xCE; //right justified (FOSC/64) AN0=analog DELAY(1); //for TACQ while(1) { ADCONbits.GO = 1; while(ADCONbits.DONE == 1) ; PORTC = ADRESL; PORTD = ADRESH; DELAY(250); // set sampling rate (in ms) } }
32

Using Interrupts with the ADC We can ask the ADC peripheral to notify us when a conversion is done. This way we do not have to constantly poll it. The PIE1.ADIE mask is used to enable the interrupt (remember to enable GIE, and recall priorities!). The PIR1.ADIF flag is set when interrupt is triggered.
33

Programming the PIC18 ADC with the ADC Interrupt


#include <PIC18F458.h> void my_isr(void); #pragma code My_Int=0x0008 void My_Int (void) { my_isr(); } #pragma interrupt my_isr void my_isr(void) { if(PIR1bits.ADIF) { PORTC = ADRESL; PORTD = ADRESH; Delay(250); PIR1bits.ADIF=0; } } void main(void) { TRISC = 0; TRISD = 0; TRISAbits.TRISA0 = 1; ADCON0 = 0x81; //FOSC/64, channel 0 A/D on ADCON1 = 0xCE; //right justified (FOSC/64) AN0=analog PIR1bits.ADIF=0; PIR1bits.ADIE=1; INTCONbits.PEIE=1; INTCONbits.GIE=1; while(1) { DELAY(1); //for TACQ ADCONbits.GO = 1; } }

34 Talk about problems with this code!

Sensor Interfacing and Signal Conditioning Sensors can be of many kinds but mainly active or passive. If they are passive then they usually change their resistance (or charge) relative to the measured physical value, e.g., PTC and NTC temperature sensors, pressure sensors, etc. Active sensors these days are by far more common (as we prefer linear response), and thus need a voltage supply and provide a controlled voltage output.
35

Sensor Interfacing and Signal Conditioning (Passive Thermistor) As a passive sensor lets look at a typical thermistor: Thermistor
Temperature [C] Resistance 0 29490 25 10000 50 3893 75 1000 100 817
35000 30000 25000 Ohm 20000 15000 10000 5000 0 0 20 40 60 Celsius 80 100 120

It is non-linear but we can use software to compensate for that.

36

Sensor Interfacing and Signal Conditioning (Passive Thermistor) We need to convert this resistance into a voltage that we can sample. A voltage divider can come in handy:
Vcc
R1

Vout (to PIC)

Rt

What should the value of R1 be? How should Vref-s be set? Why is C necessary, what should be the value? 37

Sensor Interfacing and Signal Conditioning (Active Sensors) Active sensors output voltage that is proportional to the measured physical value. In many cases the output voltage is linear on some accepted physical scale (e.g., Celsius, Pascal, Hgmm, g, rpm, etc.) So what happens if the output voltage range is well within the supply range of the microcontroller? (Vref) What happens if the output voltage range is well outside the supply range of the microcontroller? (Resistor/diode conditioning) 38 Bandwidth should still be limited!

Digital to Analog Conversion


Unlike an ADC, a DAC takes a digital value and converts it into a quasi analog voltage. Usually the conversion is linear. PIC18-s do not have built-in DACs so external components can be used. (There is a possibility to generate PWM signals though! more later) A DAC can have a parallel input or a serial input

39

Buffered Binary Resistor Network DAC


R MSB 2R 4R 8R LSB Seldom used when more than 6 bits Consider the design of an 8-bit DAC if the smallest resistor has resistance R
what would be the value of the largest resistor? what would be the tolerance of the smallest resistor?

Rf

Very difficult to manufacture very accurate resistors over this range


40

R/2R Resistor Network DAC


I R R R

Vs

2R I/2

2R I/4

2R I/8

2R I/16

2R

Rf

+ Vo 4-bit register MSB LSB

The usual choice these days in DACs

41

DAC in the QwikFlash

MAX522
3-wire serial interface Two 8-bit, buffered DACs
42

MAX522 DAC
3-wire serial Interface
!CS: needs to be kept low when feeding in data. Rising edge will modify behavior of DAC. SCLK: every rising edge data is clocked in (<5MHz) DIN: Data line

Each command is 16 bits long For PIC interfacing, we can program it directly or use serial communication peripherals
43

MAX522 DAC (contd)

44

Generating a Sine Wave


Sin(x) values vary between -1 and 1 for angle values between 0 and 2. On our DAC we will have to offset the zero value to 2.5V We need a lookup table of values (quicker than using the sin() function) In a loop we have to send corresponding table values to the DAC.
45

Summary
AD conversion is the process of taking an analog level and converting it to a digital value. Many times we are interested in continuous time signals and thus have to sample that signal periodically. The Nyquist/Shannon theorems tell us to sample at least at twice the highest frequency for the signal to be reconstructable. First signal has to be band-limited, then sampled, then samples converted. After digital processing then the signal can be reconstructed using a DAC.
46

You might also like