# 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))
# 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)
# 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)
# 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)
# 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))
# 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))
# 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))
# 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))
# 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))
# 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)))
# 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))
# 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))
# 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))
# 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))
# 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))
# 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))
# 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))
# 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))
# 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))