<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 PMOD TIMER
In this notebook, PMOD Timer functionalities are illustrated. The Timer has two sub-modules
Step1:  Instantiate Pmod_Timer class. The method stop() will stop both timer sub-modules.
In this example, we will use pin 0 of the PMODA interface. PMODB and other pins can also be used.
Step2:  2. Generate pulses for a certain period of time
In this example, we choose the Digilent Analog Discovery 2 as the scope. 
The 1+ pin (of channel 1) has to be connected to pin 0 on PMODA interface. 
Use the following settings for waveform.
<img src="data/generate_1us_forever_settings.jpg" width="200px"/>
Generate a 10 ns clock pulse every 1 microseconds for 4 seconds and then stop the generation.
Note that pulses are generated every $count\times10$ ns. Here count is defined as period.
You should see output like this
Step3:  3. Generate a certain number of pulses
Note first parameter is the period interval. 
Denoting the desired period as $T$ (in ns), we need to set the first parameter period to
Step4:  Now generate the pulses at every 1 $\mu$s interval.
Step5:  Stop the generation.
Step6:  4. Determine if an event has occurred at the input
An event is either a rising edge or a high logic level. The parameter is duration, $period\times10$ ns, in which the event is to be detected. It returns 0 if no event occurred, otherwise it returns 1.
Use a waveform generator in this example. Connect W1 channel of the Analog Discovery to pin 0 of PMODA.
Do not run the waveform generation in the next cell.
Step7:  Now run the waveform generation and then run the next cell. Set the waveform generator settings as shown below
Step8:  5. Count number of events occurred during a desired period
An event is either a rising edge or a high logic level. The parameter is duration, $period\times10$ ns, in which the number of event are counted. In this example we are interested in number of events occurring in 10 $\mu$s.
Use a waveform generator in this example. Use the following settings of the waveform generator and run the generator. Then run the next example.
<img src="data/count_events_10us.jpg" width="200px"/>
Step9:  6. Measure period between two rising edges
An event is either a rising edge or a high logic level. It expects at least two rising edges. The return result is in units of nanoseconds.
Use a waveform generator in this example. Use the following settings of the waveform generator and run the generation. Then run the next example.
<img src="data/measure_period_200KHz.jpg" width="200px"/>
<ASSISTANT_TASK:>
Python Code:
from pynq.overlays.base import BaseOverlay
base = BaseOverlay("base.bit")
Explanation: PMOD TIMER
In this notebook, PMOD Timer functionalities are illustrated. The Timer has two sub-modules: Timer0 and Timer1. 
The Generate output and Capture Input of Timer 0 are assumed to be connected to PMODA pin 0. 
    1. The Generate function outputs one clock (10 ns) pulse after a desired period. 
    2. The Capture input is sensitive to a rising edge or high level logic.
To see the results of this notebook, you will need a Digilent Analog Discovery 2
<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>
and WaveForms 2015
<td> <img src="https://reference.digilentinc.com/_media/reference/software/waveforms/waveforms-3/waveforms3-0.png" alt="Drawing" style="width: 250px;"/> </td>
1. Instantiation
Import overlay to use the timers.
End of explanation
from time import sleep
from pynq.lib import Pmod_Timer
pt = Pmod_Timer(base.PMODA,0)
pt.stop()
Explanation: Instantiate Pmod_Timer class. The method stop() will stop both timer sub-modules.
In this example, we will use pin 0 of the PMODA interface. PMODB and other pins can also be used.
End of explanation
# Generate a 10 ns pulse every period*10 ns
period=100
pt.generate_pulse(period)
# Sleep for 4 seconds and stop the timer
sleep(4)
pt.stop()
Explanation: 2. Generate pulses for a certain period of time
In this example, we choose the Digilent Analog Discovery 2 as the scope. 
The 1+ pin (of channel 1) has to be connected to pin 0 on PMODA interface. 
Use the following settings for waveform.
<img src="data/generate_1us_forever_settings.jpg" width="200px"/>
Generate a 10 ns clock pulse every 1 microseconds for 4 seconds and then stop the generation.
Note that pulses are generated every $count\times10$ ns. Here count is defined as period.
You should see output like this:
<img src="data/generate_1us_forever.jpg" width="800px"/>
End of explanation
# Generate 3 pulses at every 1 us
count=3
period=100
pt.generate_pulse(period, count)
Explanation: 3. Generate a certain number of pulses
Note first parameter is the period interval. 
Denoting the desired period as $T$ (in ns), we need to set the first parameter period to:
$period = \frac{T}{10} $
The second parameter is the number of pulses to be generated.
Run the following cell and you should see output in the scope like this:
<img src="data/generate_1us_n_times.jpg" width="800px"/>
End of explanation
# Generate pulses per 1 us forever
count=0
period=100
pt.generate_pulse(period, count)
Explanation: Now generate the pulses at every 1 $\mu$s interval.
End of explanation
pt.stop()
Explanation: Stop the generation.
End of explanation
# Detect any event within 10 us
period=1000
pt.event_detected(period)
Explanation: 4. Determine if an event has occurred at the input
An event is either a rising edge or a high logic level. The parameter is duration, $period\times10$ ns, in which the event is to be detected. It returns 0 if no event occurred, otherwise it returns 1.
Use a waveform generator in this example. Connect W1 channel of the Analog Discovery to pin 0 of PMODA.
Do not run the waveform generation in the next cell.
End of explanation
# Detect any event within 20 ms
period=200000
pt.event_detected(period)
Explanation: Now run the waveform generation and then run the next cell. Set the waveform generator settings as shown below:
<img src="data/measure_period_200KHz.jpg" width="200px"/>
End of explanation
# Count number of events within 10 us
period=1000
pt.event_count(period)
Explanation: 5. Count number of events occurred during a desired period
An event is either a rising edge or a high logic level. The parameter is duration, $period\times10$ ns, in which the number of event are counted. In this example we are interested in number of events occurring in 10 $\mu$s.
Use a waveform generator in this example. Use the following settings of the waveform generator and run the generator. Then run the next example.
<img src="data/count_events_10us.jpg" width="200px"/>
End of explanation
period = pt.get_period_ns()
print("The measured waveform frequency: {} Hz".format(1e9/period))
Explanation: 6. Measure period between two rising edges
An event is either a rising edge or a high logic level. It expects at least two rising edges. The return result is in units of nanoseconds.
Use a waveform generator in this example. Use the following settings of the waveform generator and run the generation. Then run the next example.
<img src="data/measure_period_200KHz.jpg" width="200px"/>
End of explanation
<END_TASK>