{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 12 : D/A and A/D Converters"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "examople 12.1, Page No. 461"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# DAC Resolution\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "n = 8              # Number of bits\n",
      "VoFS = 2.55        # Maximum output voltage to be represented\n",
      "\n",
      "#Calculations\n",
      "#(i)\n",
      "R1 = 2**(n)\n",
      "#(ii)\n",
      "R2 = VoFS/(2**n-1)\n",
      "\n",
      "#\n",
      "print(\"(i) Resolution  = %d, i.e. output voltage can have %d different values including 0.\"%(R1,R1))\n",
      "print(\"(ii) Resolution = %.0f mV/LSB, i.e. an input change of 1 LSB output to change by %.0f mV\"%(R2*1000,R2*1000))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "(i) Resolution  = 256, i.e. output voltage can have 256 different values including 0.\n",
        "(ii) Resolution = 10 mV/LSB, i.e. an input change of 1 LSB output to change by 10 mV\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.2, Page No.461"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Analog output from digital output\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "n = 4                # 4-bit DAC\n",
      "VoFS = 15.0          # maximum output voltage\n",
      "\n",
      "\n",
      "\n",
      "#Calculations\n",
      "R = VoFS/((2**n)-1)\n",
      "Vout = R*int('0110',2)\n",
      "\n",
      "#Result\n",
      "print(\"Vout = %.0f V\"%Vout)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Vout = 6 V\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.3, Page No.462"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Analog output from digital output\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "n = 8                # 4-bit DAC\n",
      "R = 20.0*10**-3      # Resolution in V/LSB\n",
      "\n",
      "\n",
      "#Calculations\n",
      "VoFS= R*((2**n)-1)\n",
      "Vout = R*int('10000000',2)\n",
      "\n",
      "#Result\n",
      "print(\"VoFS = %.1f V\"%VoFS)\n",
      "print(\"Vout = %.2f V\"%Vout)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "VoFS = 5.1 V\n",
        "Vout = 2.56 V\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.4, Page No.462"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Step size and analog outtput of DAC\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "n = 4                     # 4-bit DAC\n",
      "VoFS = 5.0                # maximum output voltage\n",
      "\n",
      "#Calculations\n",
      "R = VoFS/((2**n)-1)\n",
      "Vout1 = R*int('1000',2)\n",
      "Vout2 = R*int('1111',2)\n",
      "\n",
      "# Result\n",
      "print(\"Step Size i.e. Resolution = %f V/LSB\"%R)\n",
      "print(\"Output corresponding to input 1000 is %.4f V\"%Vout1)\n",
      "print(\"Output corresponding to input 1111 is %.0f V\"%Vout2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Step Size i.e. Resolution = 0.333333 V/LSB\n",
        "Output corresponding to input 1000 is 2.6667 V\n",
        "Output corresponding to input 1111 is 5 V\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.5, Page No.462"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# 12-bit DAC\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "n = 12                  # 12-bit DAC\n",
      "R = 8*10**-3            # Step Size\n",
      "\n",
      "#Calculations\n",
      "VoFS = R*((2**n)-1)\n",
      "Res = R*100/VoFS\n",
      "Vout = R*int('010101101101',2)\n",
      "\n",
      "#Result\n",
      "print(\"VoFS = %.2f V\\n%% Resolution = %f \\nOutput voltage = %.3f V\"%(VoFS,Res,Vout))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "VoFS = 32.76 V\n",
        "% Resolution = 0.024420 \n",
        "Output voltage = 11.112 V\n"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.6, Page No.467"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# R/2R ladder typr DAC\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "n = 4                 # 4-bit DAC\n",
      "Res = 0.5             # Resolution  \n",
      "Vr = 10.0             # Reference voltage (assumed)\n",
      "Rf = 10*10**3         # reference resistor\n",
      "\n",
      "#Calculations\n",
      "R = Vr*Rf/(2**n*Res)\n",
      "print(\"Reference Voltage, Vr = %.0f V\\nRf = %.0f k-ohm\\nR = %.1f k-ohm\"%(Vr,Rf/1000,R/1000))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Reference Voltage, Vr = 10 V\n",
        "Rf = 10 k-ohm\n",
        "R = 12.5 k-ohm\n"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.7, Page No.473"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# 8-bit ADC\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "n = 8                   # 8-bit ADC\n",
      "VoFS = 5.1              # full scale output voltage\n",
      "Vi = 1.28               # input voltage\n",
      "\n",
      "#Calculations\n",
      "R = VoFS/((2**n)-1)\n",
      "D = (Vi/R)\n",
      "D = round(D,0)\n",
      "D = bin(64)[2:]\n",
      "d = format(int(D,2),'08b')\n",
      "print(\"Resolution = %d or Resolution = %.1f mV/LSB\\nBinary output corresponding to 1.28V input is %s\"%(2**n,R*1000,d))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Resolution = 256 or Resolution = 20.0 mV/LSB\n",
        "Binary output corresponding to 1.28V input is 01000000\n"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.8, Page No.474"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# quantization error for 12-bit ADC\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "n = 12                       # 12-bit ADC \n",
      "ViFS = 4.095                 # full scale input voltage\n",
      "\n",
      "#Calculations\n",
      "Qe = ViFS/((2**n-1)*2)\n",
      "\n",
      "#Result\n",
      "print(\"Quantization Error, Qe = %.1f mV\"%(Qe*1000))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Quantization Error, Qe = 0.5 mV\n"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.9, Page No.477"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Dual slope ADC\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "t1 = 83.33*10**-3              # time t1\n",
      "Vr = 100*10**-3                # reference voltage\n",
      "Vi_1 = 100*10**-3              # input voltage value for case 1\n",
      "Vi_2 = 200*10**-3              # input voltage value for case 2\n",
      "\n",
      "#Calculations\n",
      "t2_1 = Vi_1*t1/Vr\n",
      "t2_2 = Vi_2*t1/Vr\n",
      "\n",
      "#Result\n",
      "print(\"(i)  t2 = %.2f ms\\n(ii) t2 = %.2f ms\"%(t2_1*1000,t2_2*1000))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "(i)  t2 = 83.33 ms\n",
        "(ii) t2 = 166.66 ms\n"
       ]
      }
     ],
     "prompt_number": 26
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.10, Page No. 477"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Digital output of an ADC\n",
      "\n",
      "import math\n",
      "# Variable declarartion\n",
      "t1 = 83.33*10**-3               # time t1\n",
      "Vr = 100*10**-3                 # Reference Voltage\n",
      "Vi = 100*10**-3                 # input voltage\n",
      "f = 12*10**3                    # clock frequency\n",
      "\n",
      "#Calculations\n",
      "Vout = f*t1*Vi/Vr\n",
      "\n",
      "#Result\n",
      "print(\"Digital output = %.0f counts\"%(math.ceil(Vout)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Digital output = 1000 counts\n"
       ]
      }
     ],
     "prompt_number": 31
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.11, Page No. 479"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Successive approximation ADC\n",
      "\n",
      "import math\n",
      "n = 8                   # 8-bit ADC \n",
      "f = 1.0*10**6           # clock frequency\n",
      "\n",
      "#Calculations\n",
      "T = 1/f\n",
      "Tc = T*(n+1)\n",
      "\n",
      "\n",
      "#Result\n",
      "print(\"Conversion time = %.0f micro-sec\"%(Tc*10**6))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Conversion time = 9 micro-sec\n"
       ]
      }
     ],
     "prompt_number": 33
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.12, Page No. 480"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Maximum input frequency\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "n = 8                         # 8-bit ADC\n",
      "Tc = 9*10**-6                 # Conversion time\n",
      "\n",
      "#Calculations\n",
      "fmax = 1/(2*math.pi*Tc*2**n)\n",
      "\n",
      "#Result\n",
      "print(\"Maximum frequency, Fmax = %.2f Hz\"%(math.floor(fmax*100)/100))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Maximum frequency, Fmax = 69.07 Hz\n"
       ]
      }
     ],
     "prompt_number": 35
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.13, Page No. 501"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Dual slope ADC\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "Vr = 2                  # Reference Voltage\n",
      "t2 = 20*10**-3          # time duration\n",
      "Vo = 5                  # Peak value of output voltage\n",
      "C = 0.1*10**-6          # integrator capacitance\n",
      "\n",
      "#Calculations\n",
      "R = Vr*t2/(Vo*C)\n",
      "\n",
      "#Result\n",
      "print(\"Integrating resistor, R1 = %.0f k-ohm\"%(R/1000))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Integrating resistor, R1 = 80 k-ohm\n"
       ]
      }
     ],
     "prompt_number": 37
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.4, Page No. 501"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Resolution and equivalent Iout for digital input\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "n = 4                     # 4-bit resistance ladder\n",
      "R1 = 10*10**3             # Resistance R\n",
      "R2 = 20*10**3             # Resistance 2*R\n",
      "Vref = 10.0               # reference Voltage\n",
      "b1 = 1.0                  # LSB valueof input\n",
      "b2 = 1.0                  # 2nd bit value of input\n",
      "b3 = 1.0                  # 3rd bit value of input\n",
      "b4 = 1.0                  # MSB value of input\n",
      "\n",
      "# Calculations\n",
      "Res = Vref/(2**n*R1)\n",
      "Iout = Vref*((b1/2**1)+(b2/2**2)+(b3/2**3)+(b4/2**4))/R1\n",
      "\n",
      "#Result\n",
      "print(\"Resolution = %.3f * 10^-5 \\nIout(1111) = %.3f * 10^-4 A\"%(Res*10**5,Iout*10**4))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Resolution = 6.250 * 10^-5 \n",
        "Iout(1111) = 9.375 * 10^-4 A\n"
       ]
      }
     ],
     "prompt_number": 83
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.15, Page No. 501"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# 4-bit weighted resistor type DAC\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "n = 4                   # 4-bit DAC\n",
      "R1 = 10*10**3           # Resistor corresponding to LSB(Value used in calculations)\n",
      "Vr = 2.5                # reference Voltage\n",
      "VoFS = 5                # Full scale output voltage\n",
      "\n",
      "#Calculations\n",
      "Rf = VoFS*R1/(Vr*((b1/2**1)+(b2/2**2)+(b3/2**3)+(b4/2**4)))\n",
      "\n",
      "#Result\n",
      "print(\"Rf = %d k-ohm\"%(Rf/1000))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Rf = 21 k-ohm\n"
       ]
      }
     ],
     "prompt_number": 61
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.16, Page N0. 502"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# LSB, MSB and full scale output for 8-bit DAC\n",
      "\n",
      "import math\n",
      "#Variable ddeclaration\n",
      "n = 8                   # 8-bit DAC\n",
      "Vmin = 0                # minimum input voltage\n",
      "Vmax = 10.0             # Maximum input voltage \n",
      "\n",
      "#Calculations\n",
      "LSB = (Vmax-Vmin)/(2**n - 1)\n",
      "MSB = LSB*2**(n-1)\n",
      "VoFS = LSB*(2**n -1)\n",
      "\n",
      "#Result\n",
      "print(\"LSB  = %.2f mV\\nMSB  = %d V\\nVoFS = %d V\"%(math.floor(LSB*100000)/100,MSB,VoFS))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "LSB  = 39.21 mV\n",
        "MSB  = 5 V\n",
        "VoFS = 10 V\n"
       ]
      }
     ],
     "prompt_number": 71
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.17, Page No. 502"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# 8-bit ADC\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "n = 8                   # 8-bit ADC\n",
      "Vmin = 0                # minimum input voltage\n",
      "Vmax = 3.0              # Maximum input voltage \n",
      "\n",
      "#Calculations\n",
      "MinVol = (Vmax-Vmin)/(2**n-1)\n",
      "\n",
      "#Result\n",
      "print(\"Minimum Voltage = %.2f mv\"%(MinVol*1000))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Minimum Voltage = 11.76 mv\n"
       ]
      }
     ],
     "prompt_number": 82
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.18, Page No. 502"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# 10-bit ADC Resolution\n",
      "\n",
      "import math\n",
      "# Variable declaration\n",
      "n = 10                  # 10-bit ADC\n",
      "Vmin = -10.0            # minimum input voltage\n",
      "Vmax = 10.0             # Maximum input voltage \n",
      "\n",
      "#Calculations\n",
      "r = (Vmax-Vmin)/(2**n-1)\n",
      "\n",
      "#Result\n",
      "print(\"Resolution = %f mv\"%(r))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Resolution = 0.019550 mv\n"
       ]
      }
     ],
     "prompt_number": 74
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 12.19, Page No. 502"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# 8-bit DAC Analog Output\n",
      "\n",
      "import math\n",
      "# Variable declaration\n",
      "n = 8                   # 8-bit DAC\n",
      "Res = 20*10**-3         # DAC resolution\n",
      "D = int('00010110',2)     # digital input\n",
      "\n",
      "#Calculations\n",
      "Vout = Res*D\n",
      "\n",
      "#Result\n",
      "print(\"Vout = %.0f mv\"%(Vout*1000))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Vout = 440 mv\n"
       ]
      }
     ],
     "prompt_number": 81
    }
   ],
   "metadata": {}
  }
 ]
}