Measuring pulse shorter than 1 microsecond

Author Message
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 2027
 Posted: 01:38pm 07 Sep 2022

This shows code that can measure what you want on a picomite.

'GP0measure frequency/period using PIO

'pio program measure pause and pulse time from GP0 in us and push both to FIFO
'0 E020   'set X=0
'1 A029   'X -> fffffff
'2 00C4   'jmp (pin=1) to loop2
'3 0042   'count loop1
'4 0045   'count loop2
'5 00C4   'jmp (pin=1) in loop2
'6 A0C9   'mov -X to ISR
'7 8000   'push noblock
'8 0000   'jmp 0 (rest is filled with 0 = jmp->0)
Dim a%(7)=(&h004200C4A029E020,&h8000A0C900C40045,0,0,0,0,0,0)
f=63e6     '2MHz gives 1us per count resolution

'configure pio1
e0=Pio(execctrl 0,0,&h1f)  'use gp0 for PIN
p=0                        'no GPxx pins for PIO

'program pio1 and start
PIO program 1,a%()
PIO init machine 1,0,f,p,e0,,0 'start pio 1,0 from adress 0
PIO start 1,0

'measure time and convert to frequency
Dim cnt%(4)
Do
period1% = cnt%(4)+3         'correction for push and loop

freq=f/(2*period1%)         'calc freq
err=Int((1-freq)*1e6)       'pico clock error (ppm)
Print period1%;" counts  "; freq;" Hz   "';err;" ppm pico clock err"

Pause 100

Loop While Inkey\$=""

PIO stop 1,0
End

The output looks like this (1us pulses)

29 counts   1.086206897e+06 Hz
29 counts   1.086206897e+06 Hz
29 counts   1.086206897e+06 Hz
29 counts   1.086206897e+06 Hz
29 counts   1.086206897e+06 Hz
29 counts   1.086206897e+06 Hz
29 counts   1.086206897e+06 Hz
29 counts   1.086206897e+06 Hz

Volhout
Edited 2022-09-07 23:47 by Volhout