Some help needed to measure pulse length

48 views
Skip to first unread message

Jesito's μFabLab

unread,
Sep 3, 2020, 4:49:48 AM9/3/20
to jallib
Hello,
I'm new to JAL and I would like to measure the length of some pulses that can be as long as 5 sec and as short as 1/8000 of second.
I have a handful of different pics, but ideally I would like to use a small one, I just need one pin for the pulse, two more to interface to an I2C small display and one more to reset and restart the measure.
Could anyone point me to an example that could guide me through this?.
I love the cleanless and simplicity of JAL.
Thanks in advance.
Jes.

Rob CJ

unread,
Sep 3, 2020, 6:01:51 AM9/3/20
to jal...@googlegroups.com
Hi Jes,
I thought there is already a Jal library to measure the width of a pulse created by Matt. Would that do the job?

Met vriendelijke groet,
Rob Jansen

From: jal...@googlegroups.com <jal...@googlegroups.com> on behalf of Jesito's μFabLab <jesus.c...@gmail.com>
Sent: Thursday, September 3, 2020 10:49:48 AM
To: jallib <jal...@googlegroups.com>
Subject: [jallib] Some help needed to measure pulse length
 
--
You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallib/29ca65ab-4f67-4e77-a321-9162fd396026n%40googlegroups.com.

Jes

unread,
Sep 3, 2020, 7:45:37 AM9/3/20
to jal...@googlegroups.com
Thanks Rob, I look for it, just what I needed to know :0)

Rob CJ

unread,
Sep 3, 2020, 12:51:51 PM9/3/20
to jal...@googlegroups.com
Hi Jes,

I just checked but I was mistaken, there is a procedure that calculates a value for the timer based on a frequency.

If you want to measure the length of a pulse you can use the capture mode of a timer which is available in many PICs, for example the 16F1823.From the datasheet:
Capture mode makes use of the 16-bit Timer1
resource. When an event occurs on the CCP1 pin, the
16-bit CCPR1H:CCPR1L register pair captures and
stores the 16-bit value of the TMR1H:TMR1L register
pair, respectively. An event is defined as one of the
following and is configured by the CCP1M<3:0> bits of
the CCP1CON register:
• Every falling edge
• Every rising edge
• Every 4th rising edge
• Every 16th rising edge
When a capture is made, the Interrupt Request Flag bit
CCP1IF of the PIR1 register is set. The interrupt flag
must be cleared in software. If another capture occurs
before the value in the CCPR1H, CCPR1L register pair
is read, the old captured value is overwritten by the new
captured value.

Kind regards,

Rob


Van: jal...@googlegroups.com <jal...@googlegroups.com> namens Jes <jesus.c...@gmail.com>
Verzonden: donderdag 3 september 2020 13:45
Aan: jal...@googlegroups.com <jal...@googlegroups.com>
Onderwerp: Re: [jallib] Some help needed to measure pulse length
 

Jesito's μFabLab

unread,
Sep 5, 2020, 3:55:01 AM9/5/20
to jallib
Many thanks Rob. I have to figure out how to do it. Is the timer working at the MCU clock speed?. If so the 16 bit value might be too small to hold the pulse length for i.e. a 5 sec pulse, isn't it?. I suppose some prescaling should be done there... Have to investigate and read more on that.
Kind regards.
Jes.


Oliver Seitz

unread,
Sep 5, 2020, 4:17:07 AM9/5/20
to jal...@googlegroups.com
No real help, just a bit of smartassing... ;-)


On certain chips like PIC18fxxk22 or PIC18Fxxk42 (and probably some more) the timer can run on the full Fosc speed. That is up to 64MHz. So a resolution of around 16 nanoseconds is possible.  Of course the 16 bit timer then overflows after about one millisecond. You can reduce the resolution by prescaling, or catch the overflows using the timer interrupt to increase an additional counter. As JAL allows variables of up to 256 bytes, you can capture times of about 2.5*10^607 years, even at the full resolution. The universe exists since about 5*10^9 years, so that should suffice all needs.

Greets,
Kiste

Am Samstag, 5. September 2020, 09:55:03 MESZ hat Jesito's μFabLab <jesus.c...@gmail.com> Folgendes geschrieben:


Rob CJ

unread,
Sep 5, 2020, 4:17:21 AM9/5/20
to jallib
Hi Jes,

Since my previous project was finished I was thinking of prototyping this frequency counter in JAL. My idea is to use it in combination with an SSD1306 display and test it using the library I created for the Si5351 although the range will be limited of this frequency counter.

I think of making it 'auto ranging' as to increase the frequency range indeed by changing prescaler values as you mentioned to measure lower frequencies. I do not yet know how low it can measure.

I might even post it on Instructables as prototype. I saw some examples there based on Arduino but most of them are very simple, that is no auto-ranging and so limited in the range it can handle.

I started today, not sure how long it wil take but when it is done I will post the source file on Jallib or on Instructables.

BTW I will base it on a PIC16F1823 since that is the one I often use in my projects but you can use other PICs as long as it has the capture function. As said I do not know how long it will take so don't wait for it 🙂.

Kind regards,

Rob
 

Van: jal...@googlegroups.com <jal...@googlegroups.com> namens Jesito's μFabLab <jesus.c...@gmail.com>
Verzonden: zaterdag 5 september 2020 09:55
Aan: jallib <jal...@googlegroups.com>

Jes

unread,
Sep 5, 2020, 5:55:33 AM9/5/20
to jal...@googlegroups.com
Thanks Oliver, nice to know that. :0)
(


Jes

unread,
Sep 5, 2020, 6:08:39 AM9/5/20
to jal...@googlegroups.com
Thanks Rob. I'm not in a hurry, just learning and trying to get profit from what I learn.
My project is a simple shutter speed meter for old film cameras (which I collect and repair).
There is a little led laser pointing to a phototransistor sitting on the film plane and going through the lens socket. Max shutter "speeds" (the term is used in photography) are of 1/8000 of sec. On the other end, time can be a long exposure of minutes or even hours, but normally measuring up to 5 sec should be enough to certify the accuracy of the shutter. Most pictures are taken in a range between 1/30 sec and 1/500 sec. Many old cameras have a restricted range, not beyond 1/250 sec.
I would like to have an autonomous unit (not tied to a, computer) so I would need only a few pins, just one for the signal and two for an small I2C display, like the ones powered by the SSD1306 :O)
I was following the thread because that.
Now I have to find out if one of the small 12F units is able to do that. Would be a perfect fit for my needs.
Kind regards.
Jes.

Rob CJ

unread,
Sep 5, 2020, 6:32:20 AM9/5/20
to jal...@googlegroups.com
Hi Jes,

Interesting project.

Attached you find a first prototype which does do something. I tested it with a frequency range of 2500 - 25000 Hz but it is not - yet - accurate. 

A frequency of 8000 Hz is displayed as 8237 Hz on the dispaly but at least it is something to get you going.

This version uses indeed the OLED display so if you want to compile this program make sure you use the latest bee package since there you find the driver for the SSD1306.

I wil continue with making it more accurate and 'auto-ranging' to measure lower frequencies.

Kind regards,

Rob


Verzonden: zaterdag 5 september 2020 12:08
16F1823_frequency_counter_draft.jal

Oliver Seitz

unread,
Sep 5, 2020, 6:47:08 AM9/5/20
to jal...@googlegroups.com
Hi Jes!

PIC12F1822 would be a nice candidate. 8 pins, one I2C, up to 32MHz on Timer1. For your application I wouldn't use "Capture", but "Timer1 Gate". 

As resolution, you could  think about 64µs (or 15625 Hz), it is 6.4% at shutter 1/1000. Without crystal you won't get more garanteed than 2-3% accuracy anyway. 

Without using interrupts, you could measure as long as (65536*64µs=) 4,19 seconds. 

Ok, well if you have speeds down to 1/8000 (which I have never seen ;-) can central shutters do that? ) you would need to use the interrupt method to extend the timer.

And... phototransistors are quite slow, especially then overdriven by something like a laser. Might be worth thinking about using a photodiode. 

Greets,
Kiste

Am Samstag, 5. September 2020, 12:08:40 MESZ hat Jes <jesus.c...@gmail.com> Folgendes geschrieben:


Jes

unread,
Sep 5, 2020, 7:21:42 AM9/5/20
to jal...@googlegroups.com
Hi Oliver, you are right on everything.
I got some 12F675 that if not using capture mode, I think they can be used in Timer1 gate mode (have to check it yet).
1/8000 means 125 useconds, I put that limit because some of the modern digital cameras have such high speeds, but in the practice for most of my cameras 1/1000 would be more than enough, so your recommendations are fully valid.
Regarding phototransistors, you are also right. Photodiodes are faster, but normally they need an opamp behind to drive the digital ports. I got some BPW40 phototransistors, with a supply of 5volt a load of 100 ohm and a current of 5mA use to have raise and fall times of less than 50 uSec, that I think is enough for the 1/1000 case  (1 mSec, 0.05% of the signal), so at first I'll try that. If I find any problems I'd switch over to the photodiode. And for the lighting I can use a standard led bulb DC driven, instead of the laser, you are right, the laser can overdrive the sensor, and may not be necessary.
Kind regards and many thanks for the help.
Jes.

Jes

unread,
Sep 5, 2020, 7:30:32 AM9/5/20
to jal...@googlegroups.com
Hi Rob,
Thanks for the feedback, I'll give a try. I'm a beginner in JAL, so your code will be very interesting for me to learn.
I built a freq. meter based on the PIC16F84A long ago, display was 7seg. Not sure if I keep any of the project files in some place.
Just up to 1 Mhz, 6 digits. In PIC assembly, (almost forgot now).
Kind regards,
Jes.




Oliver Seitz

unread,
Sep 5, 2020, 8:59:34 AM9/5/20
to jal...@googlegroups.com
Hi again ;-)

The 12F625 is a bit old, it has not as many features as the more recent controllers. One of the drawbacks here is, that it has only a fixed internal speed of 4MHz. Therefore you have only options to have Timer1 to run at 1000, 500, 250 or 125 kHz. So, if you need to measure times longer than 1/4s, you will have to extend the timer in software.

But hey, give it a try for "usual" speeds between 1/30 and 1/500 and extend it once you're there :-)

Greets,
Kiste

P.S.: If a digital camera exposes 1/8000s, that is the electronic shutter speed, surely not mechanical.

Am Samstag, 5. September 2020, 13:21:43 MESZ hat Jes <jesus.c...@gmail.com> Folgendes geschrieben:


Jes

unread,
Sep 5, 2020, 9:48:35 AM9/5/20
to jal...@googlegroups.com
Thanks again, I didn't recall that detail of the 4Mhz clock. Then I have to decide if  moving to the 16f628/648 that I also have in the drawer... Not so small, but not so big as well. 
Pls check the specs of the Canon R6, it has a 1/8000 mechanical shutter... (for a price!!) 
Definitely I will stay with 1/1000 as top. 
Kind regards, 
Jes. 


Rob CJ

unread,
Sep 5, 2020, 10:06:52 AM9/5/20
to jal...@googlegroups.com
Hi Jes, Kiste,

Good suggestion Kiste about the gate mode. I changed the program to gate mode and now it is very accurate, it now shows the exact input frequency.

As said I use the 16F1823 and in order to use the gate function I need the reset pin as input. This means that you cannot use LVP for programming but if you have a Pickit3 that is no issue.

Attached the draft version tested with frequencies between 2500 Hz and 50000 Hz and measurements show exact that frequency on the display. Nice thing is that I now also had an extra test for the Si5351 library that seems to produce the right clock frequency when using the sample program provided with it.

I also attached a photo of the result.

Kind regards,

Rob


Verzonden: zaterdag 5 september 2020 15:48
16F1823_frequency_counter_gate_draft.jal
Frequency_Counter.jpg
Reply all
Reply to author
Forward
0 new messages