Post on 20-Dec-2015
Analog-to-Digital Converter
Chapter 9
A/D Converter
• Analog-to-Digital Conversion
• The 68HC11 A/D Converter
• The 68HC12 A/D Converter
• Design of a Digital Compass
Method of Successive Approximation
1111 1110 1101 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001 00000V
5V
2.5V
3.75V
3.125V3.4375V Vin = 3.5V
step 1 step 2 step 3 step 4
voltage
1111 1110 1101 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001 00000V
5V
2.5V
3.75V
3.125V3.4375V Vin = 3.5V
step 1 step 2 step 3 step 4
voltage
Control
D/A Converter
V
V
in
DA
Binary OutputC+
-
Implementing Successive Approximation
A/D Converter
• Analog-to-Digital Conversion
• The 68HC11 A/D Converter
• The 68HC12 A/D Converter
• Design of a Digital Compass
Table 9.1 A/D Converter Registers in the 68HC11Name Register Addr DescriptionADCTL 1030 A/D Control RegisterADR1 1031 A/D Result Register 1ADR2 1032 A/D Result Register 2ADR3 1033 A/D Result Register 3ADR4 1034 A/D Result Register 4OPTION 1039 System Configuration Options
7 6 5 4 3 2 1 0$1039 ADPU CSEL IRQE DLY CME 0 CR1 CR0 OPTION
ADPU: A/D Power Up0 – A/D charge pump off1 – A/D charge pump on -- delay required before using A/D system
CSEL: Clock Select0 – A/D system uses the system E clock (must be above 750 KHz)1 – A/D system uses internal R-C clock source (about 1.5 MHz)
Figure 9.3 Setting the ADPU bit of the Option register turns on the A/D converter
7 6 5 4 3 2 1 0$1030 CCF 0 SCAN MULT CD CC CB CA ADCTL
CCF: Conversions Complete Flag (read only)0 – cleared by a write to ADCTL which starts a new conversion1 – set when all four A/D result registers contain valid conversion results
SCAN: Continuous Scan Control0 – four requested conversions fill four result registers once1 – conversions continue in a round-robin fashion
MULT: Multiple-Channel/Single-Channel Control0 – performs 4 consecutive conversions on a single channel1 – performs a separate conversion for each of four channels
CD, CC, CB, CA : Channel Selects
Figure 9.4 The A/D control register
CD CC CB CA Channel Signal MULT = 10 0 0 0 PE0 ADR10 0 0 1 PE1 ADR20 0 1 0 PE2 ADR30 0 1 1 PE3 ADR40 1 0 0 P34 ADR10 1 0 1 PE5 ADR20 1 1 0 PE6 ADR30 1 1 1 PE7 ADR41 0 0 0 Reserved ADR11 0 0 1 Reserved ADR21 0 1 0 Reserved ADR31 0 1 1 Reserved ADR41 1 0 0 VH ADR11 1 0 1 VL ADR21 1 1 0 1/2 VH ADR31 1 1 1 Reserved ADR4
Figure 9.5 The A/D channel assignments
Listing 9.1 68HC11 A/D Converter\ 68HC11 Analog-to-Digital Converter -- File: ATD11.WHPHEX
1030 CONSTANT ADCTL \ A/D Control Register1031 CONSTANT ADR1 \ A/D Result Register 11032 CONSTANT ADR2 \ A/D Result Register 21033 CONSTANT ADR3 \ A/D Result Register 31034 CONSTANT ADR4 \ A/D Result Register 41039 CONSTANT OPTION \ System Configuration Options
: ADCONV.ON ( -- )80 OPTION C! ( Set ADPU )5 FOR NEXT ; ( Wait at least 100 sec. )
: ADCONV.OFF ( -- )0 OPTION C! ; ( Clear ADPU )
: WAIT.FOR.CONV ( -- )BEGIN 7 ADCTL ?HIUNTIL ;
: AVG4 ( -- n ) \ Average 4 values in ADR1 - ADR40 ADR1 1-4 FOR \ sum addr DUP R@ + C@ \ sum addr val ROT + SWAP \ sum addrNEXTDROP 4 / ;
: ADCONV ( ch# -- val ) \ Avg 4 readings from channel ch#ADCONV.ON8 MOD ADCTL C! \ ch# < 8; SCAN=0 MULT=0WAIT.FOR.CONVAVG4 ADCONV.OFF ;
: GET.4VALUES ( -- val4 val3 val2 val1 ) \ read 4 a/d regsADR1 1-4 FOR \ addr1 DUP R@ + C@ SWAP \ valn addr1NEXTDROP ;
: ADCONV03 ( -- val3 val2 val1 val0 ) \ Convert 1st 4 channelsADCONV.ON10 ADCTL C!WAIT.FOR.CONVGET.4VALUES ADCONV.OFF ;
: ADCONV47 ( -- val3 val2 val1 val0 ) \ Convert 2nd 4 channelsADCONV.ON14 ADCTL C!WAIT.FOR.CONVGET.4VALUES ADCONV.OFF ;
DECIMAL
A/D Converter
• Analog-to-Digital Conversion
• The 68HC11 A/D Converter
• The 68HC12 A/D Converter
• Design of a Digital Compass
Table 9.2 A/D Converter Registers in the 68HC12Name Register Addr Description
ATDCTL2 0062 ATD Control Register 2ATDCTL3 0063 ATD Control Register 3ATDCTL4 0064 ATD Control Register 4ATDCTL5 0065 ATD Control Register 5ATDSTAT 0066 ATD Status Register (H)ATDSTATL 0067 ATD Status Register (L)ADR0H 0070 A/D Result Register 0ADR1H 0072 A/D Result Register 1ADR2H 0074 A/D Result Register 2ADR3H 0076 A/D Result Register 3ADR4H 0078 A/D Result Register 4ADR5H 007A A/D Result Register 5ADR6H 007C A/D Result Register 6ADR7H 007E A/D Result Register 7
7 6 5 4 3 2 1 0$0065 0 S8CM SCAN MULT CD CC CB CA ATDCTL
S8CM: Select 8 Channel Mode0 – Conversion sequence consists of four conversions1 – Conversion sequence consists of eight conversions
SCAN: Continuous Scan Control0 – four requested conversions fill four result registers once1 – conversions continue in a round-robin fashion
MULT: Multiple-Channel/Single-Channel Control0 – performs 4 or 8 consecutive conversions on a single channel1 – performs a separate conversion for each of four or eight channels
CD, CC, CB, CA : Channel Selects
Figure 9.6 The 68HC12 ATD Control Register 5
7 6 5 4 3 2 1 0$0066 SCF 0 0 0 0 CC2 CC1 CC0 ATDSTA
7 6 5 4 3 2 1 0$0067 CCF7 CCF6 CCF5 CCF4 CCF3 CCF2 CCF1 CCF0 ATDSTATL
SCF: Sequence Complete Flag0 – if AFFC = 0, cleared by a write to ATDCTL5 which starts a new conversion if AFFC = 1, cleared after the first result register is read1 – set when a four or eight conversion sequence is complete
CC[2:0]: Conversion Counter for current (4 or 8 conversion) sequence3-bit number = channel number currently being converted
CCF[7:0]: Conversions Complete Flag (read only)0 – cleared by a write to ADCTL which starts a new conversion1 – set when all four A/D result registers contain valid conversion results
Figure 9.7 The 68HC12 ATD Status Register
7 6 5 4 3 2 1 0$0062 ADPU AFFC AWAI 0 0 0 ASCIE ASCIF ATDCTL
ADPU: A/D Power Up0 – A/D disabled1 – A/D enabled
AFFC: A/D Fast Flag Clear All0 – A/D flag clearing operates normally1 – Uses fast clear sequence for A/D conversion complete flags (Any access to result register will cause the associated CCF to clearautomatically.)
AWAI: A/D Wait Mode0 – A/D continues to run in wait mode1 – A/D stops to save power in wait mode
ASCIE: A/D Sequence Complete Interrupt Enable0 – A/D interrupt disabled1 – A/D interrupt (on sequence complete) enabled
ASCIF: A/D Sequence Complete Interrupt Flag0 – No A/D interrupt occurred1 – A/D sequence complete
Figure 9.8 The 68HC12 ATD Control Register 2
CD CC CB CA Channel Signal MULT = 10 0 0 0 AN0 ADR00 0 0 1 AN1 ADR10 0 1 0 AN2 ADR20 0 1 1 AN3 ADR30 1 0 0 AN4 ADR00 1 0 1 AN5 ADR10 1 1 0 AN6 ADR20 1 1 1 AN7 ADR31 0 0 0 Reserved ADR01 0 0 1 Reserved ADR11 0 1 0 Reserved ADR21 0 1 1 Reserved ADR31 1 0 0 VH ADR01 1 0 1 VL ADR11 1 1 0 (VH + VL)/2 ADR2
1 1 1 1 Reserved ADR3
Figure 9.9 The 68HC12 A/D channel assignments (S8CM = 0)
CD CC CB CA Channel Signal MULT = 10 0 0 0 AN0 ADR00 0 0 1 AN1 ADR10 0 1 0 AN2 ADR20 0 1 1 AN3 ADR30 1 0 0 AN4 ADR40 1 0 1 AN5 ADR50 1 1 0 AN6 ADR60 1 1 1 AN7 ADR71 0 0 0 Reserved ADR01 0 0 1 Reserved ADR11 0 1 0 Reserved ADR21 0 1 1 Reserved ADR31 1 0 0 VH ADR41 1 0 1 VL ADR51 1 1 0 (VH + VL)/2 ADR6
1 1 1 1 Reserved ADR7
Figure 9.10 The 68HC12 A/D channel assignments (S8CM = 1)
Listing 9.2 68HC12 A/D Converter\ 68HC12 Analog-to-Digital Converter -- File: ATD.WHPHEX
0062 CONSTANT ATDCTL2 \ ATD Control Register 20065 CONSTANT ATDCTL5 \ ATD Control Register 50066 CONSTANT ATDSTAT \ ATD Status Register0070 CONSTANT ADR0H \ A/D Result Register 0
: ADCONV.ON ( -- )C0 ATDCTL2 C! ; \ Set ADPU and AFFC
: ADCONV.OFF ( -- )0 ATDCTL2 C! ; \ Clear ADPU
: WAIT.FOR.CONV ( -- )BEGIN 7 ATDSTAT ?HIUNTIL ;
: AVG4 ( -- n ) \ Average 4 values in ADR0H - ADR3H0 ADR0H 2-4 FOR \ sum addr DUP R@ 2* + C@ \ sum addr val ROT + SWAP \ sum addrNEXTDROP 4 / ;
: ADCONV ( ch# -- val ) \ Avg 4 readings from channel ch#ADCONV.ON8 MOD ATDCTL5 C! \ ch# < 8; SCAN=0 MULT=0WAIT.FOR.CONVAVG4 ADCONV.OFF ;
: GET.4VALUES ( -- val4 val3 val2 val1 ) \ read 4 a/d regsADR0H 2-4 FOR \ addr1 DUP R@ 2* + C@ SWAP \ valn addr1NEXTDROP ;
: ADCONV03 ( -- val3 val2 val1 val0 ) \ Convert 1st 4 channelsADCONV.ON10 ATDCTL5 C!WAIT.FOR.CONVGET.4VALUES ADCONV.OFF ;
: ADCONV47 ( -- val3 val2 val1 val0 ) \ Convert 2nd 4 channelsADCONV.ON14 ATDCTL5 C!WAIT.FOR.CONVGET.4VALUES ADCONV.OFF ;
DECIMAL
A/D Converter
• Analog-to-Digital Conversion
• The 68HC11 A/D Converter
• The 68HC12 A/D Converter
• Design of a Digital Compass
12
3
1
2
3
GND
GND
VCC
VCC
V
V
RL
RH
PE2
PE3
A
B
Dinsmore Analog Hall-effect Transducer
Dinsmore Analog Hall-effect Transducer
A
B
pha se1 phase2 pha se3 ph ase4
dc
MaxAD
MinAD0 90 180 270 360
Listing 9.3 Digital Compass\ Digital compass File: COMPASS.WHPLOAD SPILED.WHP \ Fig. 7.11LOAD SINE.WHP \ Fig. 5.33LOAD ATD.WHP \ Listing 8.2
VARIABLE Amp \ max amplitude of sin waveVARIABLE MaxAD \ max A/D readingVARIABLE MinAD \ min A/D readingDECIMAL
\ Compass Using Dinsmore Analog Hall-Effect Sensor
: check.maxmin ( n -- ) \ update MaxAD & MinAD DUP MaxAD @ MAX \ n mx MaxAD ! \ n MinAD @ MIN \ mn MinAD ! ;
: get.dc ( -- dc ) \ 0 value of sine waves MaxAD @ MinAD @ \ mx mn OVER + 2/ \ mx dc TUCK - Amp ! ; \ Amp = MaxAD - dc
: read.A/D ( -- A B ) 3 ADCONV \ b DUP check.maxmin \ b 2 ADCONV \ b a DUP check.maxmin \ b a get.dc \ b a dc TUCK - \ b dc A -ROT - ; \ A B
: ASIN.scale ( n -- deg ) \ scaled arc sine 10000 Amp @ DUP 0= \ make sure Amp <> 0 IF \ if Amp = 0 DROP OVER \ take arcsine of 1 THEN */ ASIN ;
: ACOS.scale ( n -- deg ) \ scaled arc cosine ASIN.scale 90 SWAP - ;
: phase1 ( A B -- deg ) ABS 2DUP < IF DROP ASIN.scale ELSE NIP ACOS.scale THEN ;: phase2 ( A B -- deg ) 2DUP > IF NIP ASIN.scale 90 + ELSE DROP ACOS.scale 90 + THEN ;
: phase3 ( A B -- deg ) SWAP ABS 2DUP > IF NIP ASIN.scale 180 + ELSE DROP ACOS.scale 180 + THEN ;
: phase4 ( A B -- deg ) 2DUP < IF NIP ABS ASIN.scale 270 + ELSE DROP ABS ACOS.scale 270 + THEN ;
: read.compass ( -- deg ) read.A/D \ A B 2DUP XOR 0< IF \ phase 1 or 3 2DUP > IF phase1 ELSE phase3 THEN ELSE \ phase 2 or 4 2DUP + 0> IF phase2 ELSE phase4 THEN THEN ;
: compass ( -- ) SPI.INIT 0 MaxAD ! 255 MinAD ! BEGIN read.compass 359 SWAP - .4leds 5000 FOR NEXT AGAIN ;
Summary
Box 9.1 WHYP A/D Converter Words File: ATD.WHP
ADCONV ( ch# -- val )Averages four readings from channel ch# and leaves the result on the data stack.
ADCONV03 ( -- val3 val2 val1 val0 )Converts channels 0–3 once each and leaves all four values on the data stack.
ADCONV47 ( -- val7 val6 val5 val4 )Converts channels 4–7 once each and leaves all four values on the data stack.
ExercisesExercise 9.2
The LM35A from National Semiconductor is a 3-pin (power, ground, and output)Precision Centigrade Temperature Sensor that produces an analog output equal to 0 mV +10.0 mV/° C. A complete data sheet can be downloaded from http://www.nsc.com/.Assume that this output is connected to pin PAD3 of Port AD through an op-amp (e.g.the MC33272A from Motorola or the AD522 High Accuracy Data AcquisitionInstrumentation Amplifier from Analog Devices) in such a way that an op-amp output of 0to 5 volts corresponds to a temperature range of -30° to +40° C.
a. Write a WHYP word called degreeC ( -- n ) that will leave the temperature, indegrees Celsius (Centigrade) on the data stack.
b. Write a WHYP word called degreeF ( -- n ) that will leave the temperature, indegrees Fahrenheit on the data stack. To convert Celsius to Fahrenheit multiply by9/5 and add 32.
Exercise 9.3A thermistor is a solid state device whose resistance is a function of temperature. The
resistance of an NTC thermistor varies inversely proportional to temperature while theresistance of a PTC thermistor increases linearly with a temperature change. However, fora given temperature change the change in resistance is larger for an NTC thermistor than itis for a PTC thermistor. The resistance of a thermistor can produce a correspondingvoltage using a simple Wheatstone bridge circuit or voltage divider. For more detailedinformation on thermistors and how to use them to measure temperature you can see theweb site, http://www.thermistor.com/Catalog.
Assume that an NTC thermistor is used to measure temperature using a simple voltagedivider. The thermistor is calibrated by measuring ten different temperatures betweensome minimum and maximum temperature. These temperature values and thecorresponding A/D converter input (0-255) are stored in two tables called TEMP andADVAL.
Write a WHYP word called get.temp ( -- temp ) that will read an A/D value, look itup in the table ADVAL and find the corresponding temperature in the table TEMP.Perform linear interpolation between the two closest values in the table.
Exercise 9.4The TSL250 is a light-to-voltage optical sensor from Texas Instruments that can be
used to measure light intensity. Its spectral responsivity is above 0.5 for wavelengthsbetween about 450 and 950 nm with a maximum responsivity at about 880 nm. Thesensor includes a photodiode and transimpedance amplifier in a single package with threepins: power, ground, and output voltage (typically 2 volts). A detailed data sheet can bedownloaded from http://www.ti.com/sc/docs/schome.htm.
Assume that the TSL250 is connected to channel 3 of the 68HC12 A/D converter.Write a WHYP word called turn.on.lights ( -- ) that will turn on lights (by setting bit 1 inPort H high) when the output of the TSL250 falls below 1 volt.
Exercise 9.5The ADXL05 from Analog Devices is a ±1 g to ±5 g Single Chip Accelerometer. It
uses an internal sensor in which an applied acceleration in a particular direction causes thecenter plates of a string of capacitors to move relative to fixed outer plates. The devicecomes in a 10-pin circular package less than 10 mm in diameter. Using three externalcapacitors, three external resistors, and an external trim pot, you can make the outputvoltage vary from 0.5 volts (-1 g) to 4.5 volts (+1 g) with 0 g corresponding to 2.5 volts.You can go to http://www.analog.com/product/Product_Center.html to download acomplete data sheet.
Write a WHYP word called get.g ( -- n ) that will leave the acceleration, in milli-g, onthe data stack. That is, a value of 1000 corresponds to 1 g.
Exercise 9.6The ADXL05 described in Exercise 9.5 can be used to measure tilt by measuring the
component of the earth's gravity. If the sensitive axis of the ADXL05 is oriented parallelto the earth's surface (i.e. horizontal) then the output voltage will be 2.5 V (0 g). A tilt byan angle to the horizontal will cause the voltage to change to Vout given by
Vout = scale_factor (V/g) x 1g x sin + zero_g_output
For a zero_g_output voltage of 2.5 V and a scale_factor of 2 V/g, the angle is given by
= arcsin [(Vout - 2.5)/2]
Write a WHYP word called level ( -- angle ) that will leave the tilt angle, in tenths ofa degree, on the data stack. Hint: Use the arcsin table in Figure 5.33 and linearlyinterpolate to the nearest tenth of a degree.
Exercise 9.7The MPX2100 from Motorola is a 4-pin (power, ground, +Vout, and -Vout) 100 kPa
Pressure Sensor consisting of a strain gauge and thin-film resistor network on a singlechip. The output voltage varies linearly from 0 to 40 mV as the pressure differential variesfrom 0 to 100 kPa (0 to 14.5 PSI). A complete data sheet can be downloaded fromhttp://mot2.mot-sps.com/cgi-bin/dlsrch. Assume that this output is connected to pinPAD2 of Port AD through an op-amp (e.g. the MC33272A from Motorola or the AD522High Accuracy Data Acquisition Instrumentation Amplifier from Analog Devices) in sucha way that an op-amp output of 0 to 5 volts corresponds to a pressure range of 0 to 100kPa.
a. Write a WHYP word called pressure_kPa ( -- n ) that will leave the pressure, inkPa, on the data stack.
b. Write a WHYP word called pressure_PSI ( -- n ) that will leave the pressure, inPSI, on the data stack.
Exercise 9.8The MPX4115A from Motorola is a Manifold Absolute Pressure (MAP) sensor
consisting of a strain gauge, thin-film resistor network, and op-amp on a single chip. It isdesigned to measure absolute air pressure in applications such as engine control, aviationaltimeters, and weather barometers. The output voltage varies linearly from 0.2 to 4.8 Vas the absolute pressure varies from 15 to 115 kPa (2.2 to 16.7 PSI). A complete datasheet can be downloaded from http://mot2.mot-sps.com/cgi-bin/dlsrch. Assume that thisoutput is connected to pin PAD3 of Port AD.
a. Write a WHYP word called map_kPa ( -- n ) that will leave the pressure, in kPa,on the data stack.
b. Write a WHYP word called map_PSI ( -- n ) that will leave the pressure, in PSI,on the data stack.