the university of texas at arlington lecture 10 adc and...

Post on 28-Feb-2020

0 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

The University of Texas at Arlington

Lecture 10 ADC and DAC

CSE 3442/5442

2

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).

3

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.

4

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 full-scale value to the minimally detectable signal variation.

•  In order to use the outputs of analog sensors in calculations, their outputs have to be digitized.

5

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).

6

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 flip-flop of the real value).

•  In most cases sensor’s 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.

7

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.

8

Example: Output of a Sensor

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

Time (s)

Ampl

itude

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

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

9

Example: Output of a Sensor (cont’d)

•  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 over- or under-sample?

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

Time (s)

Ampl

itude

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

10

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)

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

Time (s)

Ampl

itude

Sampled Version of x(t) with Ts = 0.05 s

11

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?

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

Time (s)

Ampl

itude

Sampling x(t) and x1(t) with Ts = 0.2 s

x(t)

x1(t)

12

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!

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

Time (s)

Ampl

itude

Sampled Version of x(t) with Ts = 0.1 s

R

C Ch

Ctrl

13

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.

14

Successive Approximation ADC

+!

-!

Comparator!

CONTROL!LOGIC!

SUCCESSIVE!APPROXIMATION!

DAC!

Vin!

10!

Successive Approximation ADC!11!10!01!00!

Vin!

V0!

V3!V2!V1!

l  Set DAC output to V2 = 01!

l  DAC generates analog voltage V2 (Vin>V2)!

l  Set MSB to 1!

l  DAC now generates V3!

l  Set LSB to 0 since V3>Vin!

l  Digitize in n Bits = n Cycles!

l  Due to errors does not scale well to above 12 -14 bits!

Voltage!

time!

15

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)

16

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.

17

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!

18

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.

19

Vref (cont’d)

•  In general Vref-s could be any voltage but many times it is required to be within [Vss, Vcc]. The PIC microcontroller’s built in ADCs do require that.

•  So, what’s 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.

20

2’s Complement vs. Offset Binary

21

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.

22

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.

23

PIC18 ADC Channel Selection

24

ADCON0 Control Register

Determines conversion time More about it shortly

25

ADCON1 Control Register

26

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.

27

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-15µs.

•  Newer PIC microcontrollers have specific control for TACQ (determined in TADs)

28

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.6µs! (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-6µs and thus total conversion time is around 72µs (i.e., sampling rate cannot be larger than ~1.4kHz).

29

Oscillator Prescalar vs. TAD

30

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.

31

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.

32

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) }

}

33

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.

34

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; } } Talk about problems with this code!

35

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. (LM34, LM35)

36

Sensor Interfacing and Signal Conditioning (Passive Thermistor)

•  As a passive sensor let’s look at a typical thermistor:

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

Temperature [C] Resistance Ω

0 2949025 1000050 389375 1000

100 817

Thermistor

0

5000

10000

15000

20000

25000

30000

35000

0 20 40 60 80 100 120

Celsius

Ohm

37

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:

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

R1

C Rt

Vcc Vout (to PIC)

Signal Conditioning and Interfacing

38

39

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)

•  Bandwidth should still be limited!

40

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.)

•  Bandwidth should still be limited!

41

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

42

Buffered Binary Resistor Network DAC

•  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?

•  Very difficult to manufacture very accurate resistors over this range

R

2R

8R

4R

LSB

MSB

-

+

Rf

43

DAC Block Diagram

44

R/2R Resistor Network DAC

•  The usual choice these days in DACs

2R

I/4

2R 2R

I

MS

B

LSB

4-bit register

I/8 I/16I/2

2RVs

-

+

Rf

Vo

2R

RRR

45

PIC18 Connection to DAC0808

46

DAC in the QwikFlash

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

47

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

48

MAX522 DAC (cont’d)

49

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.

50

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.

top related