1

I want to generate square clock waveform to external device.

I use python 2.7 with Windows 7 32bit on an old PC with a LPT1 port. The code is simple:

import parallel
import time
p = parallel.Parallel()     # open LPT1
x=0
while (x==0):
    p.setData(0xFF)
    time.sleep(0.0005)
    p.setData(0x00)

I do see the square wave (using scope) but with not expected time period.

I will be gratefull for any help

smci
  • 32,567
  • 20
  • 113
  • 146
Sergey
  • 11
  • 2

2 Answers2

0

To generate signals like that is hard. To mention one reason why it is hard might be that the process gets interrupted returns when the sleep time is exceeded.

Found this post about sleep precision with an accepted answer that is great: How accurate is python's time.sleep()?

another source of information: http://www.pythoncentral.io/pythons-time-sleep-pause-wait-sleep-stop-your-code/

What the information tells you is that Windows will be able to do a sleep for a minimum ~10ms, in Linux the time is approximately 1ms, but may vary.

Update I made function that make possible to sleep less then 10ms. But the precision is very sketchy.

In the attached code I included a test that presents how the precision behaves. If you want higher precision, I strongly recommend you read the links I attached in my original answer.

from time import time, sleep
import timeit


def timer_sleep(duration):
    """ timer_sleep() sleeps for a given duration in seconds
    """
    stop_time = time() + duration

    while (time() - stop_time) < 0:
        # throw in something that will take a little time to process.
        # According to measurements from the comments, it will take aprox
        # 2useconds to handle this one.
        sleep(0)


if __name__ == "__main__":
    for u_time in range(1, 100):
        u_constant = 1000000.0
        duration = u_time / u_constant

        result = timeit.timeit(stmt='timer_sleep({time})'.format(time=duration),
                               setup="from __main__ import timer_sleep",
                               number=1)
        print('===== RUN # {nr} ====='.format(nr=u_time))
        print('Returns after \t{time:.10f} seconds'.format(time=result))
        print('It should take\t{time:.10f} seconds'.format(time=duration))

Happy hacking

Community
  • 1
  • 1
David Bern
  • 778
  • 6
  • 16
  • Thanks to your answer. But anyway i need to communicate with external devices and i guess that i am not the first one, it means it's possible. – Sergey Jan 10 '15 at 20:51
  • To help you solving the problem, you need to give me more information about the operating system your using. Did you read the link i gave you? – David Bern Jan 10 '15 at 20:56
  • Yes i read... I use Windows 7 32bit. The waveform behaves weird... when i use big delays it stops to look like squarewave. I cant understand correlation between delays and waveform look like... without delay i pget something like 2usec period waveform... – Sergey Jan 10 '15 at 21:03
  • Sergey, think about what is happening when you toggle the output of the par-port for a while. You instruct the system to set the pins high then low, how long will it take in between? – David Bern Jan 10 '15 at 22:28
  • David, it obviously shorter, i tried it, but i think it will differ from PC to PC... Anyway it works for me with the slow waveform. I suppose that for faster waveforms i'll need CPLD/FPGA connected to PC – Sergey Mar 03 '15 at 19:57
  • That is correct, it will differ from PC to PC and operating system to operating system. – David Bern Mar 04 '15 at 20:24
0

It gives an expected performance for a while... Continue to reduce times

import parallel
import time
x=0
while (x<2000):
    p = parallel.Parallel()
    time.sleep(0.01)     # open LPT1
    p.setData(0xFF)

    p = parallel.Parallel()     # open LPT1
    time.sleep(0.01)
    p.setData(0x00)
    x=x+1
Sergey
  • 11
  • 2