1 of 37

EE 319K�Introduction to Embedded Systems

Lecture 11: Sampling, �Analog-to-Digital Conversion

http://users.ece.utexas.edu/~valvano/Volume1/E-Book/C13_Interactives.htm

http://users.ece.utexas.edu/~valvano/Volume1/E-Book/C14_Interactives.htm

Today and cover lab 8, lec11

Exam2 Friday 4/8: hard cutoff at 8:30pm

Please attempt Quiz 9 and Quiz 10

10-1

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

2 of 37

Agenda

  • Recap
    • Local Variables
    • Stack frames
    • Recursion
    • Fixed-point numbers
    • LCD device driver (Lab 7)

  • Outline
    • Sampling, Nyquist theorem
    • Analog to Digital Conversion

Push {R11}

SUB SP,SP,#32

MOV R11, SP

;*********

;*********(

ADD SP,SP#32

POP {R11}

X86

ENTER #32

LEAVE (moves BP into, POP BP

10-2

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

3 of 37

Analog to Digital Converter (ADC)

Four limitations of digital sampling

  • Finite precision (4096 alternatives)
  • Finite voltage range (0 to 3.3V)
  • Discrete time sampling, fs
  • Finite number of samples, N

Voltage resolution = 3.3V/4095 =0.8 mV

Frequency range = 0 to ½ fs

Frequency resolution = fs / N

10-3

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

4 of 37

Nyquist Theorem

  • A bandlimited analog signal that has been sampled can be perfectly reconstructed from an infinite sequence of samples if the sampling rate fs exceeds 2fmax samples per second, where fmax is the highest frequency in the original signal.
    • 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.
  • Valvano Postulate: If fmax is the largest frequency component of the analog signal, then you must sample more than ten times fmax in order for the reconstructed digital samples to look like the original signal when plotted on a voltage versus time graph.

http://users.ece.utexas.edu/~valvano/Volume1/E-Book/C13_Interactives.htm

http://users.ece.utexas.edu/~valvano/Volume1/E-Book/C14_Interactives.htm

10-4

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

5 of 37

Sampling (option 1)

  • 200Hz signal sampled at 2000Hz

http://www.ece.utexas.edu/~valvano/Volume1/Nyquist.xls

Look at

200 Hz

2200 Hz

10-5

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

6 of 37

Sampling (option 1)

  • 1000Hz signal sampled at 2000Hz

http://www.ece.utexas.edu/~valvano/Volume1/Nyquist.xls

10-6

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

7 of 37

Sampling (option 1)

  • 2200Hz signal sampled at 2000Hz

This is aliasing

http://www.ece.utexas.edu/~valvano/Volume1/Nyquist.xls

10-7

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

8 of 37

Sampling (option 2)

  • 100Hz signal sampled at 1600Hz

http://www.ece.utexas.edu/~valvano/EE345L/Labs/Fall2011/FFT16.xls

10-8

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

9 of 37

Sampling (option 2)

  • A signal with DC, 100Hz and 400Hz sampled at 1600Hz

http://www.ece.utexas.edu/~valvano/EE345L/Labs/Fall2011/FFT16.xls

10-9

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

10 of 37

Sampling (option 2)

  • 1500Hz signal sampled at 1600Hz

This is aliasing

http://www.ece.utexas.edu/~valvano/EE345L/Labs/Fall2011/FFT16.xls

10-10

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

11 of 37

Analog to Digital Converter (ADC)

  • Successive approximation ADC
    • VIN is approximated as a �static value in a sample and hold (S/H) circuit
    • the successive approximation register (SAR) is a counter that increments each clock �as long as it is enabled by �the comparator
    • output of the SAR is fed �to a DAC that generates a voltage to compare with VIN
    • when the output of the DAC = VIN the value of SAR is the digital representation of VIN

end of conversion

http://users.ece.utexas.edu/~valvano/Volume1/E-Book/C14_Interactives.htm

10-11

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

12 of 37

Play the 6-bit SAR game

http://users.ece.utexas.edu/~valvano/Volume1/E-Book/C14_Interactives.htm

  • Interactive Tool 14.1
  • I choose a number from 0 to 63
  • You control the DAC

10-12

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

13 of 37

Sample-And-Hold Circuit

  • The software writes to PSSI to start conversion
  • Writing to PSSI latches the analog input and the actual conversion starts
  • The essence of sampling is to write to PSSI with a fixed and known period
  • The sampling rate, fs , is 1/period
  • Lab 8 will use interrupts to establish fs

S/H

10-13

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

14 of 37

ADC on TM4C123

  • Sampling Range/Resolution
    • 3.3V internal reference voltage
    • 0x000 at 0 V input
    • 0xFFF at 3.3 V
    • resolution = range/precision

= 3.3V/4095 alternatives < 1mV

or = 3.3V/4096 alternatives < 1mV

    • Actual resolution dominated by noise
  • Improve signal to noise ratio (SNR)
    • Slow down ADC (take longer to sample)
    • Analog filtering, ground shield
    • Digital filtering (average multiple samples)

Use either

10-14

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

15 of 37

ADC on TM4C123

  • Twelve analog input channels
  • Single-ended and differential-input configurations
  • On-chip internal temperature sensor
  • Sample rate up to one million samples/second 30 Hz
  • Flexible, configurable analog-to-digital conversion
  • Four programmable sample conversion sequences from one to eight entries long, with corresponding conversion result FIFOs
  • Flexible trigger control
    • Controller (software) We will use software-initiated trigger
    • Timers
    • Analog Comparators (trigger if analog input crosses a threshold)
    • Pulse Width Modulator (another timer)
    • GPIO (input pin)
    • Continuous
  • Hardware averaging of up to 64 samples for improved accuracy
  • Converter uses an internal 3.3V reference

PD2=Ain5 used for Lab 8, 9, 10

10-15

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

16 of 37

ADC on TM4C123

Ain5PD2

Software initiated

Bit 3 is done flag

Use sequencer 3

PD2=Ain5 used for Lab 8, 9, 10

10-16

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

17 of 37

ADC on TM4C123

Twelve different pins can be used to sample analog inputs.

PD3=Ain4 used for TExaS oscilloscope

PD2=Ain5 used for Lab 8, 9, 10

PE4=Ain9 used in book and ADCSWTrigger_4C123

10-17

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

18 of 37

ADC on TM4C123

  • TM4C ADC registers

10-18

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

19 of 37

ADC on TM4C123

  • TM4C123 ADC Operation
    • select rate
    • select sequencer
    • select trigger
    • select channel
    • select sample mode
      • 0 not temperature
      • 1 set completion flag
      • 1 end sequence
      • 0 not differential

Speed bits in ADC0_PC_R

EM3, EM2, EM1, and EM0 bits in ADC_EMUX_R

ADC0_SSCTL3_R = 0x06;

10-19

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

20 of 37

ADC on TM4C123

  • Initialization
    • Enable ADC clock: set bit 0 in SYSCTL_RCGCADC_R
    • Set 125kHz ADC conversion speed: write 0x01 to ADC0_PC_R
    • Set sequencer priority: 0,1,2,3 in ADC0_SSPRI_R
    • Disable selected sequence 3: zero bit 3 of ADC0_ACTSS_R
    • Set software start trigger event: zero bits 15-12 of ADC0_EMUX_R
    • Set input source (0-11): write channel number in bits 3-0 of ADC0_SSMUX3_R (channel 9 is PE4, channel 1 is PE2)
    • Set sample control bits: write 0110 in bits 3-0 ADC0_SSCTL3_R to disable temp measurement, notify on sample complete, indicate single sample in sequence, and denote single-ended signal mode
    • Disable interrupts: zero bit 3 of ADC0_IM_R
    • Enable selected sequencer 3: set bit 3 of ADC0_ACTSS_R

10-20

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

21 of 37

ADC on TM4C123

Channel 9 is PE4

void ADC0_InitSWTriggerSeq3_Ch9(void){

SYSCTL_RCGCGPIO_R |= 0x10; // 1) activate clock for Port E

while((SYSCTL_PRGPIO_R&0x10) == 0){};

GPIO_PORTE_DIR_R &= ~0x10; // 2) make PE4 input

GPIO_PORTE_AFSEL_R |= 0x10; // 3) enable alternate fun on PE4

GPIO_PORTE_DEN_R &= ~0x10; // 4) disable digital I/O on PE4

GPIO_PORTE_AMSEL_R |= 0x10; // 5) enable analog fun on PE4

SYSCTL_RCGCADC_R |= 0x01; // 6) activate ADC0

delay = SYSCTL_RCGCADC_R; // extra time to stabilize

delay = SYSCTL_RCGCADC_R; // extra time to stabilize

delay = SYSCTL_RCGCADC_R; // extra time to stabilize

delay = SYSCTL_RCGCADC_R;

ADC0_PC_R = 0x01; // 7) configure for 125K

ADC0_SSPRI_R = 0x0123; // 8) Seq 3 is highest priority

ADC0_ACTSS_R &= ~0x0008; // 9) disable sample sequencer 3

ADC0_EMUX_R &= ~0xF000; // 10) seq3 is software trigger

ADC0_SSMUX3_R = (ADC0_SSMUX3_R&0xFFFFFFF0)+9; // 11) Ain9 (PE4)

ADC0_SSCTL3_R = 0x0006; // 12) no TS0 D0, yes IE0 END0

ADC0_IM_R &= ~0x0008; // 13) disable SS3 interrupts

ADC0_ACTSS_R |= 0x0008; // 14) enable sample sequencer 3

}

Book shows Ain9=PE4 Lab 8, 9, 10 use Ain5=PD2

10-21

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

22 of 37

ADC on TM4C123

  • Analog to digital conversion
    • Set software trigger
      • Write to PSSI bit 3
    • Busy-Wait
      • Raw Interrupt Status = RIS bit 3
      • Poll until sample complete
    • Read sample
      • Read from SSFIFO3
    • Clear sample complete flag
      • Write to ISC bit 3

10-22

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

23 of 37

ADC on TM4C123

//------------ADC_InSeq3------------

// Busy-wait analog to digital conversion

// Input: none

// Output: 12-bit result of ADC conversion

uint32_t ADC0_InSeq3(void){

uint32_t data;

ADC0_PSSI_R = 0x0008;

while((ADC0_RIS_R&0x08)==0){};

data = ADC0_SSFIFO3_R&0xFFF;

ADC0_ISC_R = 0x0008;

return data;

}

10-23

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

24 of 37

Data Acquisition System (Lab 8)

  • Hardware
    • Transducer
    • Electronics
    • ADC
  • Software
  • ADC device driver
  • Timer routines
    • Output compare interrupts
  • LCD driver
  • Measurement system
    • How fast to update
    • Fixed-point number system
    • Algorithm to convert ADC into position

10-24

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

25 of 37

Analog Input Device

  • Transducer – A device actuated by power from one system that supplies power in the same or other form to another system.

3.3V

GND

PD2

10-25

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

26 of 37

Transducer Circuit

  • Position to voltage

There are 6 ways to connect 3 pins to 3 connections; 4 of which will explode; 2 of which will operate

10-26

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

27 of 37

Data Acquisition System

  • 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

+

3.3V

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

4

0

9

5

S

y

s

T

i

c

k

I

S

R

S

a

m

p

l

e

0

t

o

4

0

9

5

S

y

s

T

i

c

k

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

2

.

0

0

0

P

o

s

i

t

i

o

n

0

t

o

2

c

m

m

a

i

n

M

a

i

l

b

o

x

0 to 2.00cm

10-27

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

28 of 37

Data Acquisition System

  • Call graph

10-28

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

29 of 37

Thread Synchronization in Lab 8

  • SysTick ISR producer
    • Sample ADC
    • Store in ADCmail
    • Set ADCstatus
  • Main loop

consumer

    • Wait for ADCstatus
    • Read ADCmail
    • Clear ADCstatus
    • Convert to distance
    • Display on LCD

Background thread

Foreground thread

10-29

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

30 of 37

Sampling Jitter

  • Definition of time-jitter, δt:
    • Let nΔt be the time a task is scheduled to be run and tn the time the task is actually run
    • Then δtn= tn – nΔt
  • Real time systems with periodic tasks, must have an upper bound, k, on the time-jitter
    • -k ≤ δtn +k for all n

10-30

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

31 of 37

Measurement

  • Resolution: Limiting factors
    • Transducer noise
    • Electrical noise
    • ADC precision
    • Software errors
  • Accuracy: Limiting factors
    • Resolution
    • Calibration
    • Transducer stability

Average accuracy (with units of x) =

10-31

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

32 of 37

Fixed-Point Revisited

  • Why:

express non-integer values

no floating-point hardware support (want it to run fast)

  • When:

range of values is known

range of values is small

  • How:

1) variable integer, called I.

may be signed or unsigned

may be 8, 16 or 32 bits (precision)

2) fixed constant, called Δ (resolution)

value is fixed, and can not be changed

not stored in memory

specify this fixed constant using comments

value ≡ integer Δ

10-32

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

33 of 37

Fixed-Point Numbers

  • The value of the fixed-point number:

Fixed-point number ≡ IΔ

Smallest value = IminΔ, where Imin is the smallest integer

Largest value = ImaxΔ, where Imax is the largest integer

  • Decimal fixed-point, Δ=10m

Decimal fixed-point number = I • 10m

Nice for human input/output

  • Binary fixed-point, Δ=2m

Binary fixed-point number = I • 2m

Easier for computers to perform calculations

10-33

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

34 of 37

Fixed-Point Math Example

Consider the following calculation.

C = 2*π*R

The variables C, and R are integers

2π ≈ 6.283

C = (6283*R)/1000

2π ≈ 6434/1024 = 6.283203125

C = (6434*R)>>10

10-34

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

35 of 37

Fixed-Point Math Example

Calculate the volume of a cylinder

V = π*R2 * L

The variables are fixed-point

R = I*2-4 cm L = J*2-4 cm

V = K*2-8 cm3 π ≈ 3217*2-10 = 3.1416015625

K*2-8 cm3 = (3217*2-10)*(I*2-4 cm)2*(J*2-4 cm)

K = (3217*I*I*J)>>14

10-35

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

36 of 37

Vin (V)

Analog in

N

Digital out

I (1 mV)

Variable part

LCD

0

0

0

0.000

0.825

1024

825

0.825

1.650

2048

1650

1.650

2.475

3072

2475

2.475

3.3

4095

3300

3.300

Δ=0.001 V

Vin = 3.3•N/4095 how ADC works

or Vin = 3.3•N/4096 how ADC works

Vin = I • 0.001 definition of fixed point

I = (3300*N)/4096 substitution

I = (m•N+b)/ 4096 calibrate to get m and b

Make a Voltmeter with ADC

10-36

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi

37 of 37

Lab 8 Calibration (0.001)

Show Lab8_Accuracy

10-37

Bard, Tiwari, Telang, Holt, Cuevas, Gerstlauer, Valvano, Yerraballi