12-1
EE 319KIntroduction to Microcontrollers
Lecture 12: A/D Conversion and Lab 8
Read Book Sections 10.2 and 11.4
12-2Ramesh Yerraballi
A/D and D/A Conversion BasicsDigitization:Amplitude and time quantizing
Time (s)048
121620242832
0 1 2 3 4 5 6 7 8 9 10
Continuous analog signalDiscrete digital signal
12-3Ramesh Yerraballi
Nyquist Theory If a signal is sampled at fs then the digital samples only
contain frequency components from 0 to (1/2)fs Conversely, if the analog signal does contain frequency
components larger than (1/2)fs, then there will be an aliasing error.
Aliasing is when the digital signal appears to have a different frequency than the original analog signal.
Say,V(t) = A sin(2πft + )
Nyquist theory says that if fs is strictly greater than twice f, then one can determine A f and from the digital samples.
But if fs is less than or equal to 2f, then the apparent frequency, as predicted by analyzing the digital samples, will be shifted to a frequency between 0 and (1/2)fs
12-4Ramesh Yerraballi
.. Nyquist Theory
Sampled Data
-3
-2
-1
0
1
2
3
0 2 4 6 8 10
Time (ms)
Voltage (V)
Sampled Data
True Data
A 1500 Hz sine wave is sampled at 1600 Hz.
12-5Ramesh Yerraballi
A/D and D/A Conversion Basics Range of the system is
the maximum minus the minimum values
Precision of the system defines the number of values from which the amplitude of the digital signal is selected. (Alternatives)
Resolution is the smallest change in value that is significant.
Range = Precision x Resolution
12-6Ramesh Yerraballi
A/D Converter ExampleAnalog to digital Converter
Analog input 0 ≤ Vin ≤ +5Digital output 0 ≤ ATD0DR0 ≤ 255
o Digital Output is about 256*Vin/5 o Or Digital Output is about 255*Vin/5
0 +5
0 255
Analog
Digital
Vin
0.00 1.50cmAnalogD
12-7Ramesh Yerraballi
9S12 has 16 Analog Inputs
AD1
AD0
One Control Register per ADC module: ATD0CTL2 and ATD1CTL2
12-8Ramesh Yerraballi
Port AD0Address Bit 7 6 5 4 3 2 1 Bit 0 Name$0082 ADPU AFFC AWAI ETRIGLE ETRIGP ETRIG ASCIE ASCIF ATD0CTL2$0083 0 S8C S4C S2C S1C FIFO FRZ1 FRZ0 ATD0CTL3$0084 SRES8 SMP1 SMP0 PRS4 PRS3 PRS2 PRS1 PRS0 ATD0CTL4$0085 DJM DSGN SCAN MULT 0 CC CB CA ATD0CTL5$0086 SCF 0 ETORF FIFOR 0 CC2 CC1 CC0 ATD0STAT0$008B CCF7 CCF6 CCF5 CCF4 CCF3 CCF2 CCF1 CCF0 ATD0STAT1$008D Bit 7 6 5 4 3 2 1 Bit 0 ATD0DIEN$008F PAD07 PAD06 PAD05 PAD04 PAD03 PAD02 PAD01 PAD00 PORTAD0address msb lsb Name$0090 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 ATD0DR0$0092 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 ATD0DR1$0094 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 ATD0DR2$0096 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 ATD0DR3$0098 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 ATD0DR4$009A 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 ATD0DR5$009C 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 ATD0DR6$009E 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 ATD0DR7
ATD0CTL2=$80 ; set bit 7 to 1 to enable ADC
ATD0CTL3=$08 ; sequence length = 1
SRES8 ; 1 for 8-bit values, 0 for 10-bit values
12-9Ramesh Yerraballi
ADC Clock The ADC has an internal clock that controls how
fast the ADC runs Not how many samples it takes but how fast the
conversion (A/D) works. Speed controlled by 5-bit value in the lower 5-
bits (PRS5-PRS1) of ATD0CTL4 Say this value is m (e.g., 00100 => m=4) The ATD internal clock speed is given by:
(1/2)E/(m+1) where E is the E clock speed Say we want this speed to be 1 MHz, So for:
o E-clock speed of 4 MHz m is set to 1o E-clock speed of 8 MHz m is set to 3o E-clock speed of 24 MHz m is set to 11
Internal clock speed can range from 500 kHz to 2 MHz
12-10Ramesh Yerraballi
Other settings ATD0CTL5 write channel number (analog input)
to start ADC channel number $80 to $87 (CC:CB:CA)
ATD0STAT bit 7 SCF cleared by write to ATD0CTL5 (i.e., starting a new
conversion) or just write 1 to it set when ADC finished
ATD0DR0 first 8/10-bit ADC result precision 8/10-bit, 256/1024 alternatives range 0 to +5V resolution
(5-0)/255 0.02 V ; (5-0)/1023 0.005 V 1.5/255 0.006 cm; 1.5/1023 0.0015 cm
12-11Ramesh Yerraballi
8-bit Conversion Example
Digital o/p = 255 * Vin/5Ex1:
Vin = 1.50 => Dig out = Integer approx (255 * 1.50/5)
= 76 = $4C = %01001100Ex2:
Vin = 3.75 => Dig out = Integer approx (255 * 3.75/5)
= 191 = $BF = %10111111
Analog Input (V) Digital Output0.00 %00000000 $00 00.02 %00000001 $01 11.50 %01001100 $4C 763.75 %10111111 $BF 1915.00 %11111111 $FF 255
12-12Ramesh Yerraballi
10-bit Conversion Example
Digital o/p = 1023 * Vin/5Ex1:
Vin = 0.005 => Dig out = Integer approx (1023 * 0.005/5)
= 1 = $001 = %0000000001Ex2:
Vin = 3.750 => Dig out = Integer approx (1023 * 3.750/5)
= 768 = $300 = %1100000000
Analog Input (V) Digital Output0.000 %0000000000 $000 00.005 %0000000001 $001 12.500 %1000000000 $200 5123.750 %1100000000 $300 7685.000 %1111111111 $3FF 1023
12-13Ramesh Yerraballi
Complete ADC Example Tut3 in TExaS is an
ADC example See ADC Driver
ADC_Inito Turns it ono Sets it to 10-bit mode
ADC_Ino write channel number
to ATD0CTL5 o wait for SCF flag in ATD0STAT
o read 10-bit result from ATD0DR0
SCI_OutDec
mainserial portdriver
SCI hardwareADC hardware
A/Ddriver
12-14Ramesh Yerraballi
Lab 8: Design a Position Meter Hardware
Transducer Electronics ADC
Software ADC device driver Timer routines
o Output compare interrupts LCD driver Measurement system
o How fast to updateo Fixed-point number systemo Algorithm to convert ADC into position
12-15Ramesh Yerraballi
Lab8: Data Flow Graph
P o s i t i o n
S e n s o r
V o l t a g e
0 t o + 5 V
A D C
h a r d w a r e
A D C
d r i v e r
S a m p l e
0 t o 1 0 2 3
O C
I S R
S a m p l e
0 t o 1 0 2 3
O C
h a r d w a r e
L C D
d i s p l a y
L C D
d r i v e r
F i x e d - p o i n t
0 t o 3 . 0 0 0
P o s i t i o n
0 t o 3 c m1.50 cm
1.50
255
255
12-16Ramesh Yerraballi
Lab8: Transducer A transducer converts some observable
phenomena (temperature, pressure, opening, position, speed etc. to a analog form that can then be digitized. A potentiometer converts position to a resistance
Solder wires to pins 1,2,3 Glue potentiometer to a solid base Position metric ruler (for calibration and testing) Create a hair-line cursor
12-17Ramesh Yerraballi
Lab8: Circuit
What is R12 + R23 at all times? What are R12 and R23 when cursor is at 1 cm? What is Vin when cursor is at 1 cm? What is ATD0DR0 when cursor is at 1 cm? What do you want to display on the LCD when
cursor is at 1 cm?
12-18Ramesh Yerraballi
Lab8: Call Graph
m a i n
O C
h a r d w a r e
O C
i n i t
L C D
h a r d w a r e
L C D
d r i v e r
O C
I S R
A D C
h a r d w a r e
A D C
d r i v e r
12-19Ramesh Yerraballi
Convert ADC data into integer part of fixed-point
Three possibilitiesThe relationship between the ADC data
and what it represents is expressible as an equation
The relationship is expressible as a partially complete table and we use interpolation to infer the missing
The relationship is expressible as a complete table and we perform explicit table lookup
12-20Ramesh Yerraballi
Equation for Temperature ExampleComplex Equationlet n be ADC (0 to 1023) V = 5.0*n/1023
(in volts)R = 18.31 + 5.312*V
(in k)T = 1/(H0+H1ln(R)) -273.15
(in oC) (H0 = 0.002486844 , H1 = 0.000243014)
I = 100*T (in 0.01 oC)
Simple Equation I = 3971 – 1.9768*n
12-21Ramesh Yerraballi
Muls and Divsmul unsigned A*B into D
emul unsigned D*Y into 32-bit Y:D
emuls signed D*Y into Y:D
idiv unsigned D/X into X, D remainder
idivs signed D/X into X, D remainder
fdiv unsigned (D:0)/X into X, D remainder
ediv unsigned (Y:D)/X into Y, D remainder
edivs signed (Y:D)/X into Y, D remainder
12-23Ramesh Yerraballi
Promotion/DemotionUnsigned 8 to 16-bit promotion;to promote RegB into RegD clra ; to promote RegA into RegX tfr A,B clra tfr D,XSigned 8 to 16-bit promotion sex A,D (same as tfr A,D) sex B,D (same as tfr B,D) sex A,X (same as tfr A,X) sex B,X (same as tfr B,X) sex A,Y (same as tfr A,Y) sex B,Y (same as tfr B,Y)
16 to 8-bit demotion (signed or unsigned)tfr D,A tfr D,B tfr X,A tfr X,B tfr Y,A tfr Y,B
12-24Ramesh Yerraballi
Equation for Temperature ExampleComplex Equationlet n be ADC (0 to 1023) V = 5.0*n/1023
(in volts)R = 18.31 + 5.312*V
(in k)T = 1/(H0+H1ln(R)) -273.15
(in oC) (H0 = 0.002486844 , H1 = 0.000243014)
I = 100*T (in 0.01 oC)
Simple Equation I = 3971 – 1.9768*nUsing fdiv, find m such that65536/m = 1.9768m = 65536/1.9768 =
33152.5698 ≈ 33153* Reg D has ADC result,
0 to 1023 ldx #33153 fdiv pshx ; 1.9768*n ldd #3971 subd 2,SP+ std I ; 0.01 C
12-25Ramesh Yerraballi
Simple Equation
Temperature versus ADC
T = -1.9768n + 3971.1
1000
1500
2000
2500
3000
3500
4000
4500
0 128 256 384 512 640 768 896 1024
ADC, n
T (0.01˚C)
12-26Ramesh Yerraballi
InterpolationWe will look at examples in TExaS help
system8-bit table access on the 681216-bit table access on the 6812
Basic ideaYL - Y1
Y2 - Y1
XL - X1
X2 - X1
YL = Y1 + B *(Y2 - Y1)
=
B
The TBLInstruction does this
12-27Ramesh Yerraballi
Back to Lab 8 Sample ADC every 0.2s Map (Data: 0 to 255) into (Position: 0000 to +1500) Option A: Use a linear function
Position = 1500*(Sample)/255 (NOT THIS ONE) Option B : Use a paired calibration table (S[i],P[i])
S[i] are ADC samples measured at corresponding positions P[i]
Given sample, find i such that S[i]<=sample<S[i+1] Use linear interpolation (look up etbl in TExaS help) position = P[i]+((sample-S[i])*(P[i+1]-P[i]))/(S[i+1]-S[i])
Option C : Create a 255-entry calibration table (P[ATD0DR0L])
Fixed-Point output 1234 is displayed as “1.234 cm”