# PMOD\_timer pulse width issue

**URL:** <https://discuss.pynq.io/t/pmod-timer-pulse-width-issue/928>\
**Category:** Support\
**Created:** [February 11, 2020, 2:24pm UTC](https://discuss.pynq.io/t/pmod-timer-pulse-width-issue/928 "2020-02-11T14:24:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhm1](https://avatars.discourse-cdn.com/v4/letter/j/6bbea6/32.png) [@jhm1](https://discuss.pynq.io/u/jhm1)\
**Post date:** [February 11, 2020, 2:24pm UTC](https://discuss.pynq.io/t/pmod-timer-pulse-width-issue/928/1 "2020-02-11T14:24:08Z")

</div>

Hello,

PYNQ version: 2.5  
Board: PYNQ-Z2

I am playing around with the PMOD\_timer in my PYNQ-Z2. pynq VERSION IS 2.5.

First of all, I import the overlay to use the timers:

> from pynq.overlays.base import BaseOverlay  
> base = BaseOverlay(“base.bit”)

Then I choose pin 0 of PMODA

> from time import sleep  
> from pynq.lib import Pmod\_Timer
> 
> pt = Pmod\_Timer(base.PMODA,0)  
> pt.stop()

Then I want to create a square wave where I have 3.3V half of the time 0V the rest of time. Thus a clock signal. The example in the board says I have to choose a “period” variable such that the “desired period” (T) is T = 10\*period

On a different note, the variable clk\_period\_ns is said to be the pulse width. So I choose period=20 which means T=10\*period=200, and clk\_period=100, which means I should have a clock signal with period 200 ns

> period=20  
> pt.clk\_period\_ns = 100  
> print (pt.clk\_period\_ns)

I generate the pulse:

> pt.generate\_pulse(period)

But what I obtain is

 ![IMG-20200211-WA0006 (1)](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/1X/52e02a4b51057a3ae0afccc51b2ca8c3f9885e43.jpeg)

Nothing like a clock signal. Also, there are no changes if I modify clk\_period\_ns

What could be the reason behind this?

---

<div class="post-metadata">

**Author:** ![rock](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/rock/32/32_2.png) [@rock](https://discuss.pynq.io/u/rock)\
**Post date:** [February 11, 2020, 6:16pm UTC](https://discuss.pynq.io/t/pmod-timer-pulse-width-issue/928/2 "2020-02-11T18:16:50Z")

</div>

Hi,

Based on:

> <https://github.com/Xilinx/PYNQ/blob/master/pynq/lib/pmod/pmod_timer.py#L92>

It is saying the pulse width is equal to the clock period (you can’t really change signals within a single IOP clock, so it is a reasonable assumption). And you can adjust how many clocks a pulse will appear, by changing the argument `period`. So if you want a clock signal output, you should change period=2, so

```auto
pt.generate_pulse(period=2)

```

will give you an output clock signal.

Btw,

> <https://github.com/Xilinx/PYNQ/blob/master/pynq/lib/pmod/pmod_timer.py#L74>

shows that you should not change `clk_period_ns` since it is determined by the microblaze clock, therefore determined by fclk0.

So if you want to change the output clock frequency, try to adjust fclk0 using our `Clocks` class.

> <https://github.com/Xilinx/PYNQ/blob/master/pynq/notebooks/common/zynq_clocks.ipynb>

But I do think if you really want to generate an adjustable clock signal, you should use Pmod\_PWM instead of Pmod\_Timer.

---

<div class="post-metadata">

**Author:** ![jhm1](https://avatars.discourse-cdn.com/v4/letter/j/6bbea6/32.png) [@jhm1](https://discuss.pynq.io/u/jhm1)\
**Post date:** [February 12, 2020, 3:29pm UTC](https://discuss.pynq.io/t/pmod-timer-pulse-width-issue/928/3 "2020-02-12T15:29:10Z")

</div>

Hi Rock,

Thank you very much for your answer.

Yes, you are 100% right on the fact I want to generate an adjustable clock signal. The problem I see with Pmod\_PWM is the period is restricted between 1 and 65536 as specified in the line 97 of the following link:

> <https://github.com/Xilinx/PYNQ/blob/master/pynq/lib/pmod/pmod_pwm.py>

That means the maximum I can generate is a 1 MHz clock signal, but I would like to get more than that if possible, especially seeing as the maximum frequency I can get playing with the Clocks class is 500 MHz.

As for PMOD\_timer, which on paper would allow me to get bigger frequencies, the minimum period is 3, and I am not able to generate anything like a clock signal with it (I tried many options).

I wonder if there is an alternative solution for this I have been bypassing so far.

Thank you again.

---

<div class="post-metadata">

**Author:** ![rock](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/rock/32/32_2.png) [@rock](https://discuss.pynq.io/u/rock)\
**Post date:** [February 14, 2020, 6:50pm UTC](https://discuss.pynq.io/t/pmod-timer-pulse-width-issue/928/4 "2020-02-14T18:50:02Z")

</div>

Our source code for `timer_pwm_generate` is here:

> <https://github.com/Xilinx/PYNQ/blob/master/boards/sw_repo/pynqmb/src/timer.c#L135>

So it can generate clock pulse in the units of number of microblaze clock cycles.  
We are using it this way:

> <https://github.com/Xilinx/PYNQ/blob/master/pynq/lib/pmod/pmod_pwm/src/pmod_pwm.c#L112>

You can modify the microblaze code, or even try ipython microblaze to directly code microblaze code in jupyter, something like:

> <https://github.com/Xilinx/PYNQ/blob/master/boards/Pynq-Z1/base/notebooks/microblaze/microblaze_c_libraries.ipynb>

Just keep in mind that the IO pins coming out of the Pmod has a maximum clock frequency, which is around 10 - 15 MHz. Even if you are able to generate above 15MHz clock, you won’t be able to see it on the IO pins - those IO pins are not designed for extremely high clocks.

---

<div class="post-metadata">

**Author:** ![jhm1](https://avatars.discourse-cdn.com/v4/letter/j/6bbea6/32.png) [@jhm1](https://discuss.pynq.io/u/jhm1)\
**Post date:** [February 17, 2020, 10:00am UTC](https://discuss.pynq.io/t/pmod-timer-pulse-width-issue/928/5 "2020-02-17T10:00:00Z")

</div>

Hello Rock,

Thank you for your insightful comments.

I am wondering, is there any pin in the PYNQ that is able to handle more than those 10-15 MHz you speak about?

Let’s say I build a clock divider with Vivado parting from the 125 MHz referred in the constraints file. Would any pin around be able to support it? Which is the maximum supported anywhere?

Thank you again.

---

<div class="post-metadata">

**Author:** ![rock](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/rock/32/32_2.png) [@rock](https://discuss.pynq.io/u/rock)\
**Post date:** [February 17, 2020, 5:59pm UTC](https://discuss.pynq.io/t/pmod-timer-pulse-width-issue/928/6 "2020-02-17T17:59:24Z")

</div>

I am not aware of any pins that can do that. There might be future versions of PYNQ boards that can handle higher frequency, but for z1 and z2, the pins are quite normal and not high speed.

---

<div class="post-metadata">

**Author:** ![jhm1](https://avatars.discourse-cdn.com/v4/letter/j/6bbea6/32.png) [@jhm1](https://discuss.pynq.io/u/jhm1)\
**Post date:** [February 18, 2020, 10:46am UTC](https://discuss.pynq.io/t/pmod-timer-pulse-width-issue/928/7 "2020-02-18T10:46:10Z")

</div>

Thank you Rock for all your help.

At the end I could find another solution modifying the Pmod\_Timer Python code, so the period stored could lead to a clock signal.

> <https://github.com/Xilinx/PYNQ/blob/master/pynq/lib/pmod/pmod_timer.py#L114>

For instance, one possible solution is to modify that line like this:

> self.microblaze.write\_mailbox(0, period-3)

If period = 3, I am able to see a clock signal in my oscilloscope.
