{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 5: ADC and DAC"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example5_1,pg 491"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# output voltage\n",
      "\n",
      "import math\n",
      "# Variable declaration\n",
      "Vref=12.0              #ref. voltage\n",
      "n =4.0                 #no. of binary weighted resistors\n",
      "n1=3.0                 #input-1\n",
      "n2=1.0                 #input-2\n",
      "\n",
      "#Calculations\n",
      "Vo=-(Vref/2**n)*(2**n1+2**n2)\n",
      "\n",
      "#Result \n",
      "print(\"output voltage:\")\n",
      "print(\"Vo = %.1f V\"%Vo) "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "output voltage:\n",
        "Vo = -7.5 V\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example5_2,pg 491"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# voltage division ratio and feedback resistor\n",
      "\n",
      "import math\n",
      "# Variabe declaration \n",
      "# serie arm resistance = 10k, since the divider arm resistance Rsh=2Rse \n",
      "# therefore for straight binary code, one should have section voltage ratio as Vos/Vis=0.5\n",
      "\n",
      "#Vo/Vref=0.5\n",
      "Rse=10*10**3        #series resistance(Rsh/2)\n",
      "\n",
      "#Calculation\n",
      "Rf=0.5*(16*Rse)/15 #feedback resistor\n",
      "\n",
      "#Result\n",
      "print(\"voltage section ratio = 0.5\")\n",
      "print(\"\\nfeedback resistor:\")\n",
      "print(\"Rf = %.2f k-ohm\"%(Rf/1000)) "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "voltage section ratio = 0.5\n",
        "\n",
        "feedback resistor:\n",
        "Rf = 5.33 k-ohm\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example5_3,pg 492"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# output voltage\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "Rse = 1*10**3               #series resistance\n",
      "Rsh = 2*10**3               #shunt resistance\n",
      "Vref= 5.0                   #ref. voltage\n",
      "n1  = 0                     #input-1\n",
      "n2  = 3                     #input-2\n",
      "Ro=0.22*10**3               #load resistance\n",
      "\n",
      "#Calculations\n",
      "Vo=(Vref*(2**n1+2**n2)/16)*(Ro/(Ro+Rsh))\n",
      "\n",
      "#Result\n",
      "print(\"output voltage:\")\n",
      "print(\"Vo = %.3f V\"%(math.floor(Vo*1000)/1000)) "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "output voltage:\n",
        "Vo = 0.278 V\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example5_4,pg 492"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# find count\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "Vref=5.0               #ref. voltage\n",
      "t=1*10**-3             #sawtooth wave time\n",
      "f=100*10**3            #clock frequency\n",
      "Vi=1                   #input voltage\n",
      "\n",
      "#Calculations\n",
      "N=((t*f*Vi)/Vref)      #count\n",
      "\n",
      "#Result\n",
      "print(\"count = %d\"%N)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "count = 20\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example5_5,pg 492"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# find integrator output voltage\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "Tu=1*10**-3                   #wave time\n",
      "Vi=0.2                        #input voltage\n",
      "t=4*10**-3                    #integration time constant(1/RC)\n",
      "\n",
      "\n",
      "#Calculation\n",
      "V1=((Vi*Tu)/t)                #integrator output voltage\n",
      "\n",
      "#Result\n",
      "print(\"integrator output voltage:\")\n",
      "print(\"V1 = %.2f V\"%V1) "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "integrator output voltage:\n",
        "V1 = 0.05 V\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example5_6,pg 493"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# find rise in output voltage and charging time\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "Tz=0.6*10**-3                #discharge time\n",
      "Vref=1                       #ref. voltage\n",
      "t=4*10**-3                   #integrator time const.\n",
      "Vi=0.2                       #input voltage\n",
      "#Calculations\n",
      "Vk=((Vref*Tz)/t)             #rise in output integrator\n",
      "Tu=Vref*(Tz/Vi)              #charging time\n",
      "\n",
      "#Result\n",
      "print(\"Rise in integrator output:\")\n",
      "print(\"Vk = %.2f V\\n\"%Vk)\n",
      "print(\"charging time:\")\n",
      "print(\"Tu = %.0f msec\"%(Tu*1000))  "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Rise in integrator output:\n",
        "Vk = 0.15 V\n",
        "\n",
        "charging time:\n",
        "Tu = 3 msec\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example5_7,pg 493"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# find count of counter\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "Vref=1                 #ref. voltage\n",
      "Vi=0.2                 #input voltage\n",
      "n=15                   #no. of counts before reset(n+1)\n",
      "\n",
      "#Calculations\n",
      "N=((n+1)*Vi)/Vref     #no.of counts over charging time\n",
      "\n",
      "print(\"No of counts over charging time:\")\n",
      "print(\"N = %.1f = %d(approx.) \"%(N,N)) "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "No of counts over charging time:\n",
        "N = 3.2 = 3(approx.) \n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example5_8,pg 493"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# find input voltage\n",
      "\n",
      "import math\n",
      "Nx=2**6             #6 bit counteer register\n",
      "Vref=2.2            #ref. voltage\n",
      "N=32.0              #SAR output\n",
      "\n",
      "#Calculations\n",
      "Vi=(N/(Nx+1)*Vref) #input voltage\n",
      "\n",
      "#Result\n",
      "print(\"Input Voltage:\")\n",
      "print(\"Vi = %.2f V\"%Vi) "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input Voltage:\n",
        "Vi = 1.08 V\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example5_9,pg 493"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# conversion number\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "n=3                     #3-bit ADC\n",
      "Vref=2.2                #ref.voltage\n",
      "Vi=1                    #input voltage\n",
      "\n",
      "#Calculations\n",
      "N=(((2**n)-1)*Vi)/Vref  #SAR output\n",
      "\n",
      "#Result\n",
      "print(\"SAR conversion no.:\")\n",
      "print(\"N = %.2f \"%N) "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "SAR conversion no.:\n",
        "N = 3.18 \n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example5_10,pg 493"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# signal to noise ratio\n",
      "\n",
      "import math\n",
      "#Variable declaaration\n",
      "n = 3.0                                #3-bit ADC\n",
      "\n",
      "#Calculations\n",
      "SbyN=(((2**(n-1)*12**0.5)/2**0.5))     #S/N ratio\n",
      "\n",
      "#Result\n",
      "print(\"S/N ratio:\")\n",
      "print(\"SbyN = %.3f\\n\"%SbyN) \n",
      "print(\"This produces an error due to noise nearly 10%\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "S/N ratio:\n",
        "SbyN = 9.798\n",
        "\n",
        "This produces an error due to noise nearly 10%\n"
       ]
      }
     ],
     "prompt_number": 14
    }
   ],
   "metadata": {}
  }
 ]
}