pic18 adc 3

Upload: adamwaiz

Post on 04-Apr-2018

235 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 pic18 adc 3

    1/23

    The University of Texas at Arlington

    1

    Lecture 10

    ADC and DAC

    CSE 3442/5442

    2

    Measuring Physical Quantities

    (Digital) computers use discrete values, and usethese to emulate continuous values if needed.

    In the physical world most everything iscontinuous (analog), e.g., temperature,

    pressure, acceleration, location, etc.

    We can measure many physical quantities but to

    use them in circuits we need transducers (somephysical quantity is converted into a electricquantity).

  • 7/30/2019 pic18 adc 3

    2/23

    3

    Sensors and Transducers

    Transducer often used interchangeablywith sensor

    Many sensors are actually a type oftransducer.

    Transducers are devices that convert oneform of energy into another. They includeboth actuators and a subset of the varioustypes of sensors.

    4

    Sensors with Analog Outputs

    Many sensors output analog (only) signalswhere output is proportional to some kind ofmeasured physical quantity (temperature,acceleration, etc.)

    Accuracy or precision of a sensor determineshow close the measured (and output) value is tothe real value. (What is the smallest unit thatmakes a difference in the measurement).

    Dynamic range: what is the relation of the full-scale value to the minimally detectable signalvariation.

    In order to use the outputs of analog sensors incalculations, their outputs have to be digitized.

  • 7/30/2019 pic18 adc 3

    3/23

    5

    A/D Conversion

    The process of A/D conversion involves: Band-limiting the analog signal

    Setting periodic sampling points (setting asampling period) at which the continuous timeanalog signal is going to be digitized.

    Freezing the analog signal at the samplingpoints so that the signal does not change forthe duration of conversion (sample and hold)

    Determining what digital value bestrepresents 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 thushaving an infinite frequency band.

    Sensors measuring these quantities (as they have to deal withmechanical and electrical inertia of their measuring ways) usuallyreduce that bandwidth significantly (i.e., if they reduce it to a100kHz, that means that they could not pick up a sudden 1ns flip-flop of the real value).

    In most cases sensors bandwidths are orders of magnitudes abovewhat we are interested in (e.g., temperature changes, we do notreally care about sub second changes).

    In order to ignore sudden changes (and so that they do notnegatively influence our ADC) we need to limit the bandwidth of thesignal (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/30/2019 pic18 adc 3

    4/23

    7

    Periodic Sampling

    Now that we have limited the bandwidth of thesignal, we need to sample it.

    The Nyquist/Shannon theorems say that youneed to sample the signal at least at twice themaximum frequency contained in the signal.(E.g., if you limited your signal to a maxfrequency of 10Hz, You should sample it at leastat 20Hz, i.e., 20 times a second).

    In many cases though we start from thesampling frequency and having a goodunderstanding of that we limit the signal to atmost 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)

    Amplitude

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

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

  • 7/30/2019 pic18 adc 3

    5/23

    9

    Example: Output of a Sensor (contd)

    Y(t) = cos(10t+0)+cos(25t+0)

    Thus, two frequencies one with1=10 and the other with2=25

    This means f1=10/(2),f2=25/(2)

    f2 is higher (~4Hz)

    Thus, to be able to reconstructthe signal (without loosing anyof its features) we need tosample at least 8 times asecond (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)

    Amplitude

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

    10

    Example: Oversampling

    If original signal wasindeed nicely limited,oversampling will resultin more samples thanwe need for processingand reconstruction (i.e.,burden on ourcalculations/ processortime)

    Shown here is 20

    samples per second (asampling 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)

    Amplitude

    Sampled Version of x(t) with Ts

    = 0.05 s

  • 7/30/2019 pic18 adc 3

    6/23

  • 7/30/2019 pic18 adc 3

    7/23

    13

    Quantization

    Now that the signal is not changing forsome time (depending on the samplingperiod), we need to determine what digitalvalue best represents the analog level.

    This will take some time, which should notbe more than the sampling period!

    There are many methods of doing this,many microcontrollers use successiveapproximation to do this.

    14

    Successive Approximation ADC

    +

    -

    Comparator

    CONTROLLOGIC

    SUCCESSIVEAPPROXIMATION

    DAC

    Vin

    10

    Successive Approximation ADC

    111001

    00

    Vin

    V0

    V3V2V1

    z Set DAC output to V2 = 01

    z DAC generates analog voltage

    V2 (Vin>V2)

    z Set MSB to 1

    z DAC now generates V3

    z Set LSB to 0 since V3>Vin

    z Digitize in n Bits = n Cycles

    z Due to errors does not scalewell to above 12 -14 bits

    Voltage

    time

  • 7/30/2019 pic18 adc 3

    8/23

    15

    A/D Converters

    Analog-to-digital converters (A/Dconverters or ADCs) are used to convertcontinuous signals to discrete values. Resolution: usually expressed in n-bits.

    Conversion time: how long it takes to convertfrom analog to digital.

    Reference: what is the reference (e.g., thehighest 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 rarelybelow 8 or rarely higher than 24.

    Resolution determines the quantization steps.An 8-bit ADC has 256 different values, thus ifVmin (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 isunchangeable (we can lower it from software byignoring LSBs), but the Vref can be changed to fitthe dynamic range of the sensor to increaseprecision.

  • 7/30/2019 pic18 adc 3

    9/23

    17

    ADC Conversion Time

    A/D conversion takes time.

    The analog part of the circuit needs toremember the analog voltage at the exact timeof the sampling (recall, that this is usually doneby charging a capacitor for a finite amount oftime).

    Once that value is remembered, the ADC needsto determine (quantize) what the correspondingdigital value to that voltage is. This is usuallydetermined by a quantization clock.

    The conversion time has to be smaller than thesampling time!

    18

    Vref

    We know that the resolution (n-bits) determineshow many quantization steps there are, thusdetermining the relative scale.

    What determines the absolute scale though, i.e.,what voltage value represents 0 and whatvoltage value represents 0b11111111 (for 8-bitresolution)?

    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 feedin analog voltages representing V-refand V

    +ref.

  • 7/30/2019 pic18 adc 3

    10/23

    19

    Vref (contd)

    In general Vref-s could be any voltage but manytimes it is required to be within [Vss, Vcc]. ThePIC microcontrollers built in ADCs do requirethat.

    So, whats the step size of an 8-bit ADC whereV-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 numberof coding schemes, such as binary, or two'scomplement binary, or offset binary.

    If we know all this, we can reconstruct thevoltage level in software.

    20

    2s Complement vs. Offset Binary

  • 7/30/2019 pic18 adc 3

    11/23

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

    A serial output ADC has two pins to talk to the CPU, one for clock, theother for data. More CPU time, few pins.

    Can be a combination, where (similarly to how we talk to the LCD) x-bitsat a time are sent out serially. Can balance CPU time and pins.

    Number of analog input channels (i.e., how many sensor can beconnected). They can be multiplexed (one ADC) or parallel (manyADCs inside).

    Start and end of conversion signals. We need to tell the ADC whento start conversion (indication of sampling) and it has to havefeedback on when the conversion is finished.

    Thus there can be many pins in an ADC for communication andADC channels.

    Most PIC microcontrollers have ADC peripherals built in. The abovewill have an effect on these peripherals.

    22

    PIC18 ADC

    ADC can be found on most PIC18-s as an integratedperipheral

    The on chip ADC peripheral needs to be programmedbefore use. SFR registers are used for allcommunications between the ADC peripheral and theCPU.

    PIC18 ADC-s are 10-bit, successive approximation.(Other microcontrollers have different precision andarchitecture 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.

  • 7/30/2019 pic18 adc 3

    12/23

    23

    PIC18 ADC Channel Selection

    24

    ADCON0 Control Register

    Determines conversion time

    More about it shortly

  • 7/30/2019 pic18 adc 3

    13/23

    25

    ADCON1 Control Register

    26

    ADRESL and ADRESH

    Only 10 bits out of 16 are used. Which 6should be unused? There is a case for left

    justifying and one for right justifying aswell. Let the user choose.

  • 7/30/2019 pic18 adc 3

    14/23

    27

    PIC18 A/D Acquisition Time

    The A/D acquisition time is essentially the time required for the holdcapacitor 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)

    28

    PIC18 A/D Conversion Time

    TAD is the conversion time per bit, i.e., the clock at whichthe successive approximation runs plus overhead. Inaddition to the 10 TAD cycles for conversion we need twomore TAD cycles. Thus the PIC18 takes a totalconversion 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 TADis about 2-6s and thus total conversion time is around72s (i.e., sampling rate cannot be larger than ~1.4kHz).

  • 7/30/2019 pic18 adc 3

    15/23

    29

    Oscillator Prescalar vs. TAD

    30

    Sampling Frequency

    Note, that we have only described a singleconversion.

    If periodical conversion needs to be set up, theGO bit has to be turned on periodically and dataproduced (ADRESH, ADRESL) has to be readperiodically.

    This can be done by:

    Calculating timing (i.e., NOP instructions after DONE) Using timer peripherals (not covered yet)

    Proper interrupt processing.

  • 7/30/2019 pic18 adc 3

    16/23

    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 onADCON1 = 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)

    }}

  • 7/30/2019 pic18 adc 3

    17/23

    33

    Using Interrupts with the ADC

    We can ask the ADC peripheral to notifyus when a conversion is done. This waywe do not have to constantly poll it.

    The PIE1.ADIE mask is used to enable theinterrupt (remember to enable GIE, andrecall priorities!).

    The PIR1.ADIF flag is set when interrupt istriggered.

    34

    Programming the PIC18 ADCwith the ADC Interrupt

    #include void my_isr(void);#pragma code My_Int=0x0008void My_Int (void) { my_isr(); }#pragma interrupt my_isrvoid 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 onADCON1 = 0xCE; //right justified (FOSC/64) AN0=analogPIR1bits.ADIF=0;PIR1bits.ADIE=1;INTCONbits.PEIE=1;INTCONbits.GIE=1;while(1){

    DELAY(1); //for TACQADCONbits.GO = 1;} } Talk about problems with this code!

  • 7/30/2019 pic18 adc 3

    18/23

    35

    Sensor Interfacing and Signal Conditioning

    Sensors can be of many kinds but mainly activeor passive.

    If they are passive then they usually changetheir resistance (or charge) relative to the

    measured physical value, e.g., PTC and NTCtemperature sensors, pressure sensors, etc.

    Active sensors these days are by far morecommon (as we prefer linear response), andthus need a voltage supply and provide acontrolled voltage output.

    36

    Sensor Interfacing and Signal Conditioning(Passive Thermistor)

    As a passive sensor lets look at a typicalthermistor:

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

    Temperature [C] Resistance

    0 29490

    25 10000

    50 3893

    75 1000

    100 817

    Thermistor

    0

    5000

    10000

    15000

    20000

    25000

    30000

    35000

    0 20 40 60 80 100 120

    Celsius

    Ohm

  • 7/30/2019 pic18 adc 3

    19/23

    37

    Sensor Interfacing and Signal Conditioning(Passive Thermistor)

    We need to convert this resistance into a voltagethat we can sample. A voltage divider can comein handy:

    What should the value of R1 be? How should Vref-s be set?

    Why is C necessary, what should be the value?

    R1

    CRt

    Vcc Vout (to PIC)

    38

    Sensor Interfacing and Signal Conditioning(Active Sensors)

    Active sensors output voltage that is proportionalto the measured physical value.

    In many cases the output voltage is linear onsome accepted physical scale (e.g., Celsius,Pascal, Hgmm, g, rpm, etc.)

    So what happens if the output voltage range iswell 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!

  • 7/30/2019 pic18 adc 3

    20/23

    39

    Digital to Analog Conversion

    Unlike an ADC, a DAC takes a digital value andconverts it into a quasi analog voltage.

    Usually the conversion is linear.

    PIC18-s do not have built-in DACs so externalcomponents can be used. (There is a possibilityto generate PWM signals though! more later)

    A DAC can have a parallel input or a serial input

    40

    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

  • 7/30/2019 pic18 adc 3

    21/23

    41

    R/2R Resistor Network DAC

    The usual choice these days in DACs

    2R

    I/4

    2R 2R

    I

    MSB

    LSB

    4-bit register

    I/8 I/16I/2

    2RVs

    -

    +

    Rf

    Vo

    2R

    RRR

    42

    DAC in the QwikFlash

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

  • 7/30/2019 pic18 adc 3

    22/23

    43

    MAX522 DAC

    3-wire serial Interface !CS: needs to be kept low

    when feeding in data.Rising edge will modifybehavior of DAC.

    SCLK: every rising edgedata is clocked in(

  • 7/30/2019 pic18 adc 3

    23/23

    45

    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 zerovalue to 2.5V

    We need a lookup table of values (quickerthan using the sin() function)

    In a loop we have to send correspondingtable values to the DAC.

    46

    Summary

    AD conversion is the process of taking ananalog level and converting it to a digital value.

    Many times we are interested in continuous timesignals and thus have to sample that signalperiodically.

    The Nyquist/Shannon theorems tell us to sampleat least at twice the highest frequency for thesignal to be reconstructable.

    First signal has to be band-limited, thensampled, then samples converted. After digitalprocessing then the signal can be reconstructedusing a DAC.