{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pmod PWM\n",
    "\n",
    "In this notebook, The Pmod PWM driver is exercised.  Specifically, An AXI Timer is used to a generate pulse width modulated (PWM) signal.  \n",
    "\n",
    "To see the results of this notebook, you will need a [Digilent Analog Discovery 2](http://store.digilentinc.com/analog-discovery-2-100msps-usb-oscilloscope-logic-analyzer-and-variable-power-supply/) <tr>\n",
    "<td> <img src=http://cdn6.bigcommerce.com/s-7gavg/products/468/images/2617/Analog_Discovery_2_obl_Academic_600__01249.1447804398.1280.1280.png alt=\"Drawing\" style=\"width: 250px;\"/> </td>\n",
    "</tr>\n",
    "\n",
    "and [WaveForms 2015](https://reference.digilentinc.com/waveforms3#newest)\n",
    "\n",
    "<tr>\n",
    "<td> <img src=http://cdn6.bigcommerce.com/s-7gavg/products/481/images/2623/TILE_WaveForms2015_150__84968.1447878611.1280.1280.png alt=\"Drawing\" style=\"width: 250px;\"/> </td>\n",
    "<td> <img src=http://cdn6.bigcommerce.com/s-7gavg/products/481/images/3961/img.07.0__38949.1458772469.1280.1280.png alt=\"Drawing\" style=\"width: 250px;\"/> </td>\n",
    "</tr>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Instantiation\n",
    "Import overlay and instantiate Pmod_PWM class. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pynq import Overlay\n",
    "Overlay(\"base.bit\").download()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Connect Scope\n",
    "In this example, we choose the Digilent Analog Discovery 2 as the scope. \n",
    "\n",
    "* The `1+` pin (of channel 1) has to be connected to pin 0 on PMODA interface. \n",
    "* The `1-` pin (of channel 1) has to be connected to `GND` on PMODA interface. \n",
    "\n",
    "This example uses PMODA interface. In order to use PMODB interface, users can replace PMODA to PMODB in the examples below. Similarly, users can change the pin number."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from pynq.iop import Pmod_PWM\n",
    "from pynq.iop import PMODA\n",
    "\n",
    "pwm = Pmod_PWM(PMODA, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Generate a clock of $50\\%$ duty cycle and $10\\,\\mu$s period\n",
    "\n",
    "In this example, we generate a $10\\,\\mu$s clocks with $50\\%$ duty cycle for 4 seconds and the stop. Issuing stop command stops both timer sub-modules.\n",
    "\n",
    "Users have to choose channel 1 for waveform display in the scope.\n",
    "\n",
    "The output would look like this:\n",
    "\n",
    "<img src=\"data/pwm_50_duty_cycle.jpg\" width=\"791px\"/>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "# Generate a 10 us clocks with 50% duty cycle\n",
    "period=10\n",
    "duty=50\n",
    "pwm.generate(period,duty)\n",
    "\n",
    "# Sleep for 4 seconds and stop the timer\n",
    "time.sleep(4)\n",
    "pwm.stop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Generate a clock of $25\\%$ duty cycle and $20\\,\\mu$s period\n",
    "\n",
    "Repeating the above test for another set of parameters. The output would look like this:\n",
    "\n",
    "<img src=\"data/pwm_25_duty_cycle.jpg\" width=\"791px\"/>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "# Generate a 20 us clocks with 25% duty cycle\n",
    "period=20\n",
    "duty=25\n",
    "pwm.generate(period,duty)\n",
    "\n",
    "# Sleep for 5 seconds and stop the timer\n",
    "time.sleep(5)\n",
    "pwm.stop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
