# Timing a 20KHz signal coming into a PIC 16F88

Started by May 29, 2009
I have the need to measure the duration of short bursts of a square or sine wave signal coming into a PIC 16F88 at a constant frequency of 20KHz. The bursts last between one and 150 milliseconds and I need the time measurement to be accurate to about a half millisecond. I was thinking of using an interrupt-on-change on the input pin and saving the time of the first interrupt and subtract it from the time of the last interrupt that occurs before there are no more interrupts for at least 1/5000 sec. Should this plan work? Do I need anything on the input line other than maybe a pull-down resistor?

Yikes. In my opinion you need to rectify or "detect" the incoming signal
like in an AM radio. I would use a series diode feeding an RC pair with a
suitable time constant relationship. Then you just simply measure the length
of the resulting rectified pulses making allowance for jitter as the RC
voltage transitions between low and high. You need to ensure that the steady
(off) state of the incomng signal is low otherwise you will need to invert
it.

You haven't mentioned the minimum interburst period which needs to be
considered in calculations. At 20kHz the shortest burst is 20 cycles and
each cycle is 50uS. With a 4.7k resistor you would probably need a 0.022uF
capacitor which has a TC (5CR) of 517uS. Other readers might suggest
different values.

That's for a square wave of normal logic levels. A sine wave is a whole
other story and may need to be amplified or attenuated to the correct height
and then isolated through a cap before rectifying using two diodes, one
series and the other shunt.
On 5/22/09, silverfoxgmail wrote:
>
> I have the need to measure the duration of short bursts of a square or sine
> wave signal coming into a PIC 16F88 at a constant frequency of 20KHz. The
> bursts last between one and 150 milliseconds and I need the time measurement
> to be accurate to about a half millisecond. I was thinking of using an
> interrupt-on-change on the input pin and saving the time of the first
> interrupt and subtract it from the time of the last interrupt that occurs
> before there are no more interrupts for at least 1/5000 sec. Should this
> plan work? Do I need anything on the input line other than maybe a pull-down
> resistor?
>
I agree. A "simple" diode rectifier and an RC filter will do the trick. You'll need to select the filter corner frequency low enough that the AC component(20Khz) is attenuated so that it does not cause the PIC input pin to toggle between 1 and 0.

However, the lower you set the corner frequency the slower the response becomes causing the measured duration to become smeared. You can set a higher corner frequency if you use a higher order filter. Beyond first order I would go with an active filter. Microchip's website has some Filter Lab software. Its nice!

Levels are an issue as well as the output of the filter will be an "average" of the signal. So a 5V p-p sine wave won't come out as 5V but somewhere around 2.5V (right?) So you might have to put in a gain stage or make the filters have some gain.

Also, I am assuming that the incoming 20Khz signal is relatively pure without much unwanted noise. If there are other signals on the line you'd need to build a bandpass filter that would only pass the 20Khz signal and then integrate it to DC....

That "simple" got complicated... =(

You could also do it your digital way. In fact that would be a nice "trick" software based way of doing it. As far as what the put in the line depends on what's driving it. I would always put an inline resistor so if the levels exceed specifications you don't end up burning the protection diodes out. I always set mine up so that the highest "disaster" levels won't push more than 10ma of current through.

If the driving circuit can source and sink current, then no pull-up/pull-down resistor is needed.

Sorry to ramble on...

--- In p..., Phil Seakins wrote:
>
> Yikes. In my opinion you need to rectify or "detect" the incoming signal
> like in an AM radio. I would use a series diode feeding an RC pair with a
> suitable time constant relationship. Then you just simply measure the length
> of the resulting rectified pulses making allowance for jitter as the RC
> voltage transitions between low and high. You need to ensure that the steady
> (off) state of the incomng signal is low otherwise you will need to invert
> it.
>
> You haven't mentioned the minimum interburst period which needs to be
> considered in calculations. At 20kHz the shortest burst is 20 cycles and
> each cycle is 50uS. With a 4.7k resistor you would probably need a 0.022uF
> capacitor which has a TC (5CR) of 517uS. Other readers might suggest
> different values.
>
> That's for a square wave of normal logic levels. A sine wave is a whole
> other story and may need to be amplified or attenuated to the correct height
> and then isolated through a cap before rectifying using two diodes, one
> series and the other shunt.
> On 5/22/09, silverfoxgmail wrote:
> >
> >
> >
> > I have the need to measure the duration of short bursts of a square or sine
> > wave signal coming into a PIC 16F88 at a constant frequency of 20KHz. The
> > bursts last between one and 150 milliseconds and I need the time measurement
> > to be accurate to about a half millisecond. I was thinking of using an
> > interrupt-on-change on the input pin and saving the time of the first
> > interrupt and subtract it from the time of the last interrupt that occurs
> > before there are no more interrupts for at least 1/5000 sec. Should this
> > plan work? Do I need anything on the input line other than maybe a pull-down
> > resistor?
> >
> >
>

Surely at 50uS period all the op would need to do is count the number of incoming pulses? 50uS is a fair length of time for a PIC and it woud be pretty trivial to just increment a register or pair of registers, possibly timing between pulses to eliminate noise and also to detect the end of the burst?

More pulses than a given maximum and something's wrong and less than a minimum and the same applies?

Squaring up the sine wave shouldn't be too difficult (amplify and then into a schmitt) as long as you don;t have to act on the eaxact start of the pulse?
To: p...
From: a...@resortatredhawk.com
Date: Sat, 30 May 2009 08:02:22 +0000
Subject: [piclist] Re: Timing a 20KHz signal coming into a PIC 16F88

I agree. A "simple" diode rectifier and an RC filter will do the trick. You'll need to select the filter corner frequency low enough that the AC component(20Khz) is attenuated so that it does not cause the PIC input pin to toggle between 1 and 0.

However, the lower you set the corner frequency the slower the response becomes causing the measured duration to become smeared. You can set a higher corner frequency if you use a higher order filter. Beyond first order I would go with an active filter. Microchip's website has some Filter Lab software. Its nice!

Levels are an issue as well as the output of the filter will be an "average" of the signal. So a 5V p-p sine wave won't come out as 5V but somewhere around 2.5V (right?) So you might have to put in a gain stage or make the filters have some gain.

Also, I am assuming that the incoming 20Khz signal is relatively pure without much unwanted noise. If there are other signals on the line you'd need to build a bandpass filter that would only pass the 20Khz signal and then integrate it to DC....

That "simple" got complicated... =(

You could also do it your digital way. In fact that would be a nice "trick" software based way of doing it. As far as what the put in the line depends on what's driving it. I would always put an inline resistor so if the levels exceed specifications you don't end up burning the protection diodes out. I always set mine up so that the highest "disaster" levels won't push more than 10ma of current through.

If the driving circuit can source and sink current, then no pull-up/pull-down resistor is needed.

Sorry to ramble on...

--- In p..., Phil Seakins wrote:

>

> Yikes. In my opinion you need to rectify or "detect" the incoming signal

> like in an AM radio. I would use a series diode feeding an RC pair with a

> suitable time constant relationship. Then you just simply measure the length

> of the resulting rectified pulses making allowance for jitter as the RC

> voltage transitions between low and high. You need to ensure that the steady

> (off) state of the incomng signal is low otherwise you will need to invert

> it.

>

> You haven't mentioned the minimum interburst period which needs to be

> considered in calculations. At 20kHz the shortest burst is 20 cycles and

> each cycle is 50uS. With a 4.7k resistor you would probably need a 0.022uF

> capacitor which has a TC (5CR) of 517uS. Other readers might suggest

> different values.

>

> That's for a square wave of normal logic levels. A sine wave is a whole

> other story and may need to be amplified or attenuated to the correct height

> and then isolated through a cap before rectifying using two diodes, one

> series and the other shunt.

>

>

> On 5/22/09, silverfoxgmail wrote:

> >

> >

> >

> > I have the need to measure the duration of short bursts of a square or sine

> > wave signal coming into a PIC 16F88 at a constant frequency of 20KHz. The

> > bursts last between one and 150 milliseconds and I need the time measurement

> > to be accurate to about a half millisecond. I was thinking of using an

> > interrupt-on-change on the input pin and saving the time of the first

> > interrupt and subtract it from the time of the last interrupt that occurs

> > before there are no more interrupts for at least 1/5000 sec. Should this

> > plan work? Do I need anything on the input line other than maybe a pull-down

> > resistor?

> >

> >

> >

>

_________________________________________________________________
Share your photos with Windows Live Photos Free.
http://clk.atdmt.com/UKM/go/134665338/direct/01/
I think your basic plan will work, as long at the CPU core frequecy (Fosc) is fast enough. Of course you don't want your input signal going higher or lower than what is allowed at the input pin. You might use a series resistor to limit current into the pin, maybe 1k. Use a timer to measure the times involved--You may need a variable to count timer overflows (increment it in the timer overflow interrupt routine).
BasicPoke
--- In p..., "silverfoxgmail" wrote:
>
> I have the need to measure the duration of short bursts of a square or sine wave signal coming into a PIC 16F88 at a constant frequency of 20KHz. The bursts last between one and 150 milliseconds and I need the time measurement to be accurate to about a half millisecond. I was thinking of using an interrupt-on-change on the input pin and saving the time of the first interrupt and subtract it from the time of the last interrupt that occurs before there are no more interrupts for at least 1/5000 sec. Should this plan work? Do I need anything on the input line other than maybe a pull-down resistor?
>

To add to BasicPoke's already sound advice, if you use a 20MHz Fosc, your basic instruction cycle would be 200ns or 5 million instructions per second. One cycle of 20KHz is, of course, 50us giving you about 250 1-cycle instructions per 20KHz cycle. So your interrupt load should be manageable. You can implement your 1/5000th second timeout with another timer or in software as you see fit.

You may want to mentally work through what would happen if the incoming signal were noisy and delivered bursts at higher than 20KHz. The inherent interrupt enabling and disabling may be all you need. But it's always a good exercise to imagine potential trouble cases as you're probably already doing.
Best Wishes,

Coop, AA1WW

To: p...
From: b...@gmail.com
Date: Tue, 16 Jun 2009 13:41:17 +0000
Subject: [piclist] Re: Timing a 20KHz signal coming into a PIC 16F88

I think your basic plan will work, as long at the CPU core frequecy (Fosc) is fast enough. Of course you don't want your input signal going higher or lower than what is allowed at the input pin. You might use a series resistor to limit current into the pin, maybe 1k. Use a timer to measure the times involved--You may need a variable to count timer overflows (increment it in the timer overflow interrupt routine).
BasicPoke

--- In p..., "silverfoxgmail" wrote:
>
> I have the need to measure the duration of short bursts of a square or sine wave signal coming into a PIC 16F88 at a constant frequency of 20KHz. The bursts last between one and 150 milliseconds and I need the time measurement to be accurate to about a half millisecond. I was thinking of using an interrupt-on-change on the input pin and saving the time of the first interrupt and subtract it from the time of the last interrupt that occurs before there are no more interrupts for at least 1/5000 sec. Should this plan work? Do I need anything on the input line other than maybe a pull-down resistor?
>

_________________________________________________________________
Bing brings you maps, menus, and reviews organized in one place. Try it now.
http://www.bing.com/search?q=restaurants&form=MLOGEN&publ=WLHMTAG&crea=TEXT_MLOGEN_Core_tagline_local_1x1