Measuring pulse shorter than 1 microsecond


Author Message
geodav
Newbie

Joined: 09/05/2014
Location: Australia
Posts: 12
Posted: 04:46am 07 Sep 2022      

My erudite colleagues,
I wish to measure a period of less than 1 uS  ,   every 100 mS.

Since mmbasic  has PULSIN with a resolution of 1 uS, it wont do.
The classic way is to measure  the number of clock cycles (ie from 40 MHz clock) that are counted between  when a gate is opened and closed, which would give 40 counts for a one uS pulse. These can be averaged

Are there any Csubs that can access the CPU clock, count the number of clock cycles that occur within  a digital pulse of duration say 0.1 uS   ( 100 nS)  on say PIN(4)?

Am using the 28 pin PIC32MX170F 256, but can upgrade, but prefer the single chip route for embedded control application


Regards
George

Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 05:26am 07 Sep 2022      

Might be talking out of my posterior but I suspect there might be PEEK/POKE tricks(?)

This is from ByPic. A prescale of 256 gives a resolution of 6.4uS. Looks like it can do way better.


// Timer projects
//
#option echo off
#option only on // only load functions that don't already exists
// include all of the registers although only some are needed
#include
"http://byvac.com/mBlib/flb/Library/PIC32MX1_Family/BRegisters/MX1_reg_
timer.bas"
// interrupts hold the flag addresses
#include
"http://byvac.com/mBlib/flb/Library/PIC32MX1_Family/BRegisters/MX1_reg_
interrupt.bas"
constant TIMERON 1 << 15 // on bit
constant T3IF 1 << 14
//
***********************************************************************
******
// clocked with PCLK that is running at 40MHz, prescale 256 = 156.25kHz
(6.4uS)
// setting PR2 1/6.4uS = 156250 should see the counter reset every
second so
// using 1562500 is 10 seconds.
//
***********************************************************************
******
function t23_set()
@TMR2=0 // clear counter
// timer_on | prescale_256 | 32_bit_on | Internal_clock
@T2CON = 0x8078
@PR2=1562500
endf
// just to look at timer values
function t23_test()
t23_set()
while comkey?(2) = 0
if (peek(IFS0) & T3IF) = T3IF
print "\n Flag triggered"
@IFS0CLR = T3IF // must clear flag
endif
wend
endf


Craig

phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 899
Posted: 05:26am 07 Sep 2022      

From the MicroMite V5.05.05 manual

"The firmware distribution for the Micromite includes a subdirectory titled "Embedded C Modules" which contains a selection of embedded C routines and fonts plus notes on how to use this feature and write your own embedded C modules and create embedded fonts."

I don't see a ready made CSUB for that but there is some info on DIY.

Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 06:03am 07 Sep 2022      

My go-to solution for such things....


Craig

Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 2027
Posted: 07:00am 07 Sep 2022      

Just for my understanding, can you elaborate a bit ?

1/ Are you using micromite or picomite (picomite has a PIO (hardware block) that could help you with this).
2/ You are measuring period (1us) once per 100ms. IS this a 1us pulse that repeats every 100ms, or is it a continuous clock that you want to measure and update your reading every 100ms?
3/ What is the desired resolution when measuring the 1us period. You talk about averaging a number that is 39,40 or 41 ? (40MHz clock).

I think what you want is possible using a picomite, that has a PIO state machine that can measure your signal with a clock of 62.5MHz, with 2 clocks per per sample, this is 31.25MHz (not 40), so a single period would give a reading of 31.

In case it is a continuous clock, you could also count 100 cycles (100ms). You would need 3 clocks per sample. In that case the virtual resolution would be 20.833*100=2GHz.

Regards,

Volhout
Edited 2022-09-07 17:01 by Volhout

Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 3327
Posted: 07:13am 07 Sep 2022      

  Tinine said  My go-to solution for such things....


Craig


Well, you would because, of course, you can buy those a lot cheaper than these...

  Quote  Am using the 28 pin PIC32MX170F 256


and you just happen to have 50 in your pockets.
;)

Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 08:21am 07 Sep 2022      

  Mixtel90 said  

Well, you would because, of course, you can buy those a lot cheaper than these...



Exactly, because we're talking minutes of development.

One Cog (CPU) dedicated to this purpose:

Exact 100mS sampling period (loop) and use WAITPEQ (wait-pin-equal) and WAITPNE (wait-pin-not-equal) to count the transitions.

Other cogs can be handling everything else such as comms, SD-card, VGA, etc.
All in BASIC  



Craig

Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 08:41am 07 Sep 2022      

P2 gets pretty crazy; any/all smartpins can count sysclock/2, all by themselves (no CPU involvement). So @250Mhz, each pin can handle 125MHz  


Craig

twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1071
Posted: 09:16am 07 Sep 2022      

  geodav said  Are there any Csubs that can access the CPU clock, count the number of clock cycles that occur within  a digital pulse of duration say 0.1 uS   ( 100 nS)  on say PIN(4)?

Am using the 28 pin PIC32MX170F 256,...

Hi,
are you looking for something like this or this (only 80000s/sec)?

At least it shows the limits and possibilities of the MX170.
The Picomite has the ADC command.

Regards
Michael

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
 PIO read 1,0,5,cnt%()       'read fifo pio 1 seq 0
 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

geodav
Newbie

Joined: 09/05/2014
Location: Australia
Posts: 12
Posted: 06:06am 08 Sep 2022      

Thanks guys,

Looks like the PicoM can do it with its PIO,
Thats awesome !
thanks Volhout

Pluto
Senior Member

Joined: 09/06/2017
Location: Finland
Posts: 181
Posted: 06:48pm 21 Nov 2022      

Thanks Volhout for the nice PIO-program!  
I have tested the program as a frequency counter for an inductance meter. The PicoMite with your program seems to work well about up to 5MHz input signal, which seems to be more than enough for this excercise.

I have no experience at all about PIO-programming, but I have a few questions.
1. cnt%(). Seems to hold 5 very similar (not always equal) values, of which only cnt%(4) is used. Are these the results of 5 different measurements?
2. I am running PicoMite at 378MHz and changed your program to also use 378MHz for the PIO (f=378e6). What would you expect the maximum measurable frequency to be? (I am using Geoff's signalgenerator and I think that the amplitude becomes too low at higher frequencies. I believe this is the reason for not acheiving higher measurements than 5MHz. Unfortunately I have no proper signal generator for higher frequency).

Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 2027
Posted: 10:04am 22 Nov 2022      

Hi Pluto,

The PIO will put a new value in the FIFO every full cycle of the input signal.
When the input signal is relatively constant, all values will be roughly the same, and you can use all 5 values can be used (increase resolution by averaging).

For SSTV decoding (the PIO program was designed for that) I needed to make sure I had the most recent value, not fifo history. So I read 5 values (1 beyond FIFO size) to make sure the I have a recent value.

I did not know you could run the PIO at such high clock frequencies. That opens new opportinuties.... I thought it was limitted to 63MHz (126MHz/2) as all other peripherals (PWM).

Volhout