# Chapter 12 : D/A and A/D Converters¶

## examople 12.1, Page No. 461¶

In :
# DAC Resolution

import math
#Variable declaration
n = 8              # Number of bits
VoFS = 2.55        # Maximum output voltage to be represented

#Calculations
#(i)
R1 = 2**(n)
#(ii)
R2 = VoFS/(2**n-1)

#
print("(i) Resolution  = %d, i.e. output voltage can have %d different values including 0."%(R1,R1))
print("(ii) Resolution = %.0f mV/LSB, i.e. an input change of 1 LSB output to change by %.0f mV"%(R2*1000,R2*1000))

(i) Resolution  = 256, i.e. output voltage can have 256 different values including 0.
(ii) Resolution = 10 mV/LSB, i.e. an input change of 1 LSB output to change by 10 mV


## example 12.2, Page No.461¶

In :
# Analog output from digital output

import math
#Variable declaration
n = 4                # 4-bit DAC
VoFS = 15.0          # maximum output voltage

#Calculations
R = VoFS/((2**n)-1)
Vout = R*int('0110',2)

#Result
print("Vout = %.0f V"%Vout)

Vout = 6 V


## example 12.3, Page No.462¶

In :
# Analog output from digital output

import math
#Variable declaration
n = 8                # 4-bit DAC
R = 20.0*10**-3      # Resolution in V/LSB

#Calculations
VoFS= R*((2**n)-1)
Vout = R*int('10000000',2)

#Result
print("VoFS = %.1f V"%VoFS)
print("Vout = %.2f V"%Vout)

VoFS = 5.1 V
Vout = 2.56 V


## example 12.4, Page No.462¶

In :
# Step size and analog outtput of DAC

import math
#Variable declaration
n = 4                     # 4-bit DAC
VoFS = 5.0                # maximum output voltage

#Calculations
R = VoFS/((2**n)-1)
Vout1 = R*int('1000',2)
Vout2 = R*int('1111',2)

# Result
print("Step Size i.e. Resolution = %f V/LSB"%R)
print("Output corresponding to input 1000 is %.4f V"%Vout1)
print("Output corresponding to input 1111 is %.0f V"%Vout2)

Step Size i.e. Resolution = 0.333333 V/LSB
Output corresponding to input 1000 is 2.6667 V
Output corresponding to input 1111 is 5 V


## example 12.5, Page No.462¶

In :
# 12-bit DAC

import math
#Variable declaration
n = 12                  # 12-bit DAC
R = 8*10**-3            # Step Size

#Calculations
VoFS = R*((2**n)-1)
Res = R*100/VoFS
Vout = R*int('010101101101',2)

#Result
print("VoFS = %.2f V\n%% Resolution = %f \nOutput voltage = %.3f V"%(VoFS,Res,Vout))

VoFS = 32.76 V
% Resolution = 0.024420
Output voltage = 11.112 V


## example 12.6, Page No.467¶

In :
# R/2R ladder typr DAC

import math
#Variable declaration
n = 4                 # 4-bit DAC
Res = 0.5             # Resolution
Vr = 10.0             # Reference voltage (assumed)
Rf = 10*10**3         # reference resistor

#Calculations
R = Vr*Rf/(2**n*Res)
print("Reference Voltage, Vr = %.0f V\nRf = %.0f k-ohm\nR = %.1f k-ohm"%(Vr,Rf/1000,R/1000))

Reference Voltage, Vr = 10 V
Rf = 10 k-ohm
R = 12.5 k-ohm


## example 12.7, Page No.473¶

In :
# 8-bit ADC

import math
#Variable declaration
n = 8                   # 8-bit ADC
VoFS = 5.1              # full scale output voltage
Vi = 1.28               # input voltage

#Calculations
R = VoFS/((2**n)-1)
D = (Vi/R)
D = round(D,0)
D = bin(64)[2:]
d = format(int(D,2),'08b')
print("Resolution = %d or Resolution = %.1f mV/LSB\nBinary output corresponding to 1.28V input is %s"%(2**n,R*1000,d))

Resolution = 256 or Resolution = 20.0 mV/LSB
Binary output corresponding to 1.28V input is 01000000


## example 12.8, Page No.474¶

In :
# quantization error for 12-bit ADC

import math
#Variable declaration
n = 12                       # 12-bit ADC
ViFS = 4.095                 # full scale input voltage

#Calculations
Qe = ViFS/((2**n-1)*2)

#Result
print("Quantization Error, Qe = %.1f mV"%(Qe*1000))

Quantization Error, Qe = 0.5 mV


## example 12.9, Page No.477¶

In :
# Dual slope ADC

import math
#Variable declaration
t1 = 83.33*10**-3              # time t1
Vr = 100*10**-3                # reference voltage
Vi_1 = 100*10**-3              # input voltage value for case 1
Vi_2 = 200*10**-3              # input voltage value for case 2

#Calculations
t2_1 = Vi_1*t1/Vr
t2_2 = Vi_2*t1/Vr

#Result
print("(i)  t2 = %.2f ms\n(ii) t2 = %.2f ms"%(t2_1*1000,t2_2*1000))

(i)  t2 = 83.33 ms
(ii) t2 = 166.66 ms


## example 12.10, Page No. 477¶

In :
# Digital output of an ADC

import math
# Variable declarartion
t1 = 83.33*10**-3               # time t1
Vr = 100*10**-3                 # Reference Voltage
Vi = 100*10**-3                 # input voltage
f = 12*10**3                    # clock frequency

#Calculations
Vout = f*t1*Vi/Vr

#Result
print("Digital output = %.0f counts"%(math.ceil(Vout)))

Digital output = 1000 counts


## example 12.11, Page No. 479¶

In :
# Successive approximation ADC

import math
n = 8                   # 8-bit ADC
f = 1.0*10**6           # clock frequency

#Calculations
T = 1/f
Tc = T*(n+1)

#Result
print("Conversion time = %.0f micro-sec"%(Tc*10**6))

Conversion time = 9 micro-sec


## example 12.12, Page No. 480¶

In :
# Maximum input frequency

import math
#Variable declaration
n = 8                         # 8-bit ADC
Tc = 9*10**-6                 # Conversion time

#Calculations
fmax = 1/(2*math.pi*Tc*2**n)

#Result
print("Maximum frequency, Fmax = %.2f Hz"%(math.floor(fmax*100)/100))

Maximum frequency, Fmax = 69.07 Hz


## example 12.13, Page No. 501¶

In :
# Dual slope ADC

import math
#Variable declaration
Vr = 2                  # Reference Voltage
t2 = 20*10**-3          # time duration
Vo = 5                  # Peak value of output voltage
C = 0.1*10**-6          # integrator capacitance

#Calculations
R = Vr*t2/(Vo*C)

#Result
print("Integrating resistor, R1 = %.0f k-ohm"%(R/1000))

Integrating resistor, R1 = 80 k-ohm


## example 12.4, Page No. 501¶

In :
# Resolution and equivalent Iout for digital input

import math
#Variable declaration
n = 4                     # 4-bit resistance ladder
R1 = 10*10**3             # Resistance R
R2 = 20*10**3             # Resistance 2*R
Vref = 10.0               # reference Voltage
b1 = 1.0                  # LSB valueof input
b2 = 1.0                  # 2nd bit value of input
b3 = 1.0                  # 3rd bit value of input
b4 = 1.0                  # MSB value of input

# Calculations
Res = Vref/(2**n*R1)
Iout = Vref*((b1/2**1)+(b2/2**2)+(b3/2**3)+(b4/2**4))/R1

#Result
print("Resolution = %.3f * 10^-5 \nIout(1111) = %.3f * 10^-4 A"%(Res*10**5,Iout*10**4))

Resolution = 6.250 * 10^-5
Iout(1111) = 9.375 * 10^-4 A


## example 12.15, Page No. 501¶

In :
# 4-bit weighted resistor type DAC

import math
#Variable declaration
n = 4                   # 4-bit DAC
R1 = 10*10**3           # Resistor corresponding to LSB(Value used in calculations)
Vr = 2.5                # reference Voltage
VoFS = 5                # Full scale output voltage

#Calculations
Rf = VoFS*R1/(Vr*((b1/2**1)+(b2/2**2)+(b3/2**3)+(b4/2**4)))

#Result
print("Rf = %d k-ohm"%(Rf/1000))

Rf = 21 k-ohm


## example 12.16, Page N0. 502¶

In :
# LSB, MSB and full scale output for 8-bit DAC

import math
#Variable ddeclaration
n = 8                   # 8-bit DAC
Vmin = 0                # minimum input voltage
Vmax = 10.0             # Maximum input voltage

#Calculations
LSB = (Vmax-Vmin)/(2**n - 1)
MSB = LSB*2**(n-1)
VoFS = LSB*(2**n -1)

#Result
print("LSB  = %.2f mV\nMSB  = %d V\nVoFS = %d V"%(math.floor(LSB*100000)/100,MSB,VoFS))

LSB  = 39.21 mV
MSB  = 5 V
VoFS = 10 V


## example 12.17, Page No. 502¶

In :
# 8-bit ADC

import math
#Variable declaration
n = 8                   # 8-bit ADC
Vmin = 0                # minimum input voltage
Vmax = 3.0              # Maximum input voltage

#Calculations
MinVol = (Vmax-Vmin)/(2**n-1)

#Result
print("Minimum Voltage = %.2f mv"%(MinVol*1000))

Minimum Voltage = 11.76 mv


## example 12.18, Page No. 502¶

In :
# 10-bit ADC Resolution

import math
# Variable declaration
n = 10                  # 10-bit ADC
Vmin = -10.0            # minimum input voltage
Vmax = 10.0             # Maximum input voltage

#Calculations
r = (Vmax-Vmin)/(2**n-1)

#Result
print("Resolution = %f mv"%(r))

Resolution = 0.019550 mv


## example 12.19, Page No. 502¶

In :
# 8-bit DAC Analog Output

import math
# Variable declaration
n = 8                   # 8-bit DAC
Res = 20*10**-3         # DAC resolution
D = int('00010110',2)     # digital input

#Calculations
Vout = Res*D

#Result
print("Vout = %.0f mv"%(Vout*1000))

Vout = 440 mv