PDA

View Full Version : Pulse Width Measurement



atomski
- 31st August 2004, 07:55
Hello everyone,

I have an interesting task in front of me and I would
appreciate your advice. I need to measure pulse widths
sent by an encoder using a pretty neat way of modulating
the VCO in an amateur (HAM) radio e.g.

Hi 200uS, Low 200uS, total = 400uS digital bit "0"
Hi 200uS, Low 400uS, total = 600uS digital bit "1"
Hi 200uS, Low 600uS, total = 800uS gives "StartBit"

Now the tricky part is that a phase shift might occur, thus
the data might be inverted. The ingenious thing about
modulating the data in this manner is that anyway you
send it data will always be the same. I was wondering,
if it would be better to use PULSIN, RCTIME combination
in conjuction with couple of IF, THEN statements or maybe
use the 16F628's CCP module. Any advice would be greatly
appreciated.

--
Sincerest regards,

YZ7REA Vladimir M Skrbic
4N7ATV Repeater Administrator
YU7GHZ Radio Club President

carl_schell
- 1st September 2004, 03:09
Atomski -

Hi. I am not familiar with amerature radio and all of its ins and outs, and am not sure I am clearly understanding what you are doing, but...

I can tell you that I have used PULSIN commands on regular pins such as PortB pins on 16F877 for frequency measurements and have been able to achieve very nice accuracy down to 10ths of Hz without any problems, (using the CCP pins might be better practice, but for my project they were being used for something else) With the PULSIN command you can measure the High pulse and the Low pulse very easy...

just keep in mind that the results are in 10-microsecond resolution...so if it says it was high for 20, that means it was high for 20 x 10 micro seconds, therefore ==> High for 200 micro seconds

Format for PULSIN command is as follows:
PULSIN Pin, State Variable, Var

Where:

State Variable = 1 measures Time Pulse is High
State Variable = 0 measures Time Pulse is Low

so...


PULSIN PortB.1,1,Pulse_High 'State Value of 1 => Measures Time High
PULSIN PortB.1,0,Pulse_Low 'State Value of 0 => Measures Time Low


If a 4MHz resomator is used, resolution is about 10 micro-seconds

Part of my point is that, unless I am misunderstanding what you are doing which is definately possible, I do not see a need to use the RCTIME command, because the PULSIN does such a nice job of measuring the pulses.


Best of luck to you and let us know what you find out or fill us in on a few more details for those of us lacking knowledge of what you amerature radio guys do! It is very cool to me to see the different kinds of clever projects everybody here is working on. It always serves to inspire me to always work harder, to learn more, to try new projects of my own. I also like the international communications and friendships we are all building! We are doing every day what politicians are incapable of!

Your friend in the USA

Carl

--------------------------------------------------------
PIC Development Boards with BOOTLOADER
www.schellelectronics.com
Novi, Michigan USA
--------------------------------------------------------

atomski
- 1st September 2004, 10:40
Carl,

Tnx for the reply. Well the idea is quite simple. Since radio
communications don't have the benefit or using wire lines
and are subjectable to all kinds of RF interference, certain
so called, modulation techniques have to be employed in
order to insure "clean" data transfer between two radios.
With this particular modulation scheme you can actually
"twist & turn" the RF signal and distort it to the point of
only couple of dB S/N ratio and still get good data out of
the demodulator, cuz digital 0 and 1 are represented by
pulses of different lengths. But let me get to my question,
as I'm sure the class is anxious to hear it ;)

I need to do couple of other things while I decode the data
i.e. measure pulse widths and that's why I was looking into
CCP module as it can work in the background, undisturbed.
Another thing, if I use the statement:

PULSIN PortB.1,1,Pulse_High
PULSIN PortB.1,0,Pulse_Low

does it mean that PULSIN (high) will wait the timeout
period, before it does the second PULSIN (low) or it just
checks both and does it again (providing I put it in a loop)?

And the last but not the least important. What about the
timings? How fast PULSIN and CCP hardware really are?
Can I precisely measure 200uS with either of these?

Greetings from Novi Sad, Serbia!

--
Sincerest regards,

YZ7REA Vladimir M Skrbic
4N7ATV Repeater Administrator
YU7GHZ Radio Club President

Dave
- 1st September 2004, 12:06
Atomski, Yes you are right in assuming that the statements will work in sequence. Infact you may lose some of the pulse information as it takes some time for the command to execute. I have used your method of looking at the high and then the low pulse width's for measuring temperature with TMP03 temperature sensors. These output a ratio for temperature change and the signal is continuous. In your case the information to be measured is not continuously the same as each pulse high and low time can possibly be different. I would suggest using the CCP module to do the timing measurements. And to answer the last question, the Pulsin command resolution is a function of the OSC speed. This information should be in your manual.

Dave Purola,

atomski
- 7th September 2004, 14:09
Dave,

how long is the PULSIN command execution time for a 20MHz
clock? Also, is there an example for CCP under PBP? I'd like to
try it out. Is it a case of direct addressing with CCP as with
reading ADC (if ADCIN is not used)? Tnx!

73!

Vlad

Acetronics2
- 7th September 2004, 14:31
Hi, Atomski

I currently use the following lines for that

PULSIN , input,1, highpart
RCTIME , input,0, lowpart

PULSE = Highpart + lowpart + offset

( offset is time between falling edge of PULSIN and RCtime measurement beginning )

... and I measured offset to be 4 units ( !) at 20 Mhz and 0 at 4 Mhz

But note you lose the following pulse each time ( PULSIN has always to wait for the next rising edge ...)

Alain

atomski
- 8th September 2004, 07:45
Alain,

I was just wondering why is it that some use:

PULSIN, input, 1, Highpart
PULSIN, input, 0, Lowpart

and others:

PULSIN, input, 1, Highpart
RCTIME , input,0, lowpart

Is it just a matter of preference or is there some higher
concept advantage to one of these? ;)

BTW, when you said that the offset @ 20MHz is 4 units,
you ment 4 x 200ns, right? Or am I mistaking here?

I still haven't had time to try this out but I guess the
following code should work:

Loop:

PULSIN, input, 1, Highpart
PULSIN, input, 0, Lowpart

PULSE = (Highpart + Lowpart + Offset) * 10 'pulse width in us

GOTO Loop

Acetronics2
- 8th September 2004, 08:38
Hi, Atomski

1) little mistake from me : there's no comma close to PULSIN or RCTIME ...

so: PULSIN input, 1, value ... is correct writing.

2) PuLsIn or PULSIN or pulsin are strictly the same for PBP ( and only PBP !!! )

3) offset is in count units : so, 1 unit is 2µs at 20 Mhz Clock, and 1 unit is 10 µs at 4 Mhz ...

as far as i can see, you have a 4Mhz clock ... so, offset value is zero : no reason to keep one extra variable !!!

Your code example will work ( without the comma ...) well, but it reads the high part of the first pulse, and the low part of the SECOND pulse ... generally, it' no problem, but remember it when you have an error detection or use the DEFINE PULSIN_MAX statement - or with non repetitive signals !!!

Alain

atomski
- 8th September 2004, 10:51
Alain,

I will use 20 MHz clock for my project as I need to use
FREQOUT statement on same hardware and I need the
signal to be as smooth as possible.

Ok, 'bout the comma. I didn't notice the error either...

I actually don't need that much accuracy when I come to
think of it bacause digital zero is represented with 400us,
digital one with 600us and startbit with 800us so I could
use a couple IF, THEN statements to convert pulses to bits.

For example:

Loop:

PULSIN input, 1, Highpart
PULSIN input, 0, Lowpart

PULSE = (Highpart + Lowpart) * 10 'pulse width in us

IF PULSE > 700 THEN Loop ' If startbit capture again
IF PULSE < 500 THEN Rxd = 0
IF PULSE > 500 AND PULSE < 700 THEN Rxd = 1

GOSUB Send_Data

GOTO Loop

... or something like this ;)


--
Sincerest regards,

YZ7REA Vladimir M Skrbic
4N7ATV Repeater Administrator
YU7GHZ Radio Club President

Dave
- 8th September 2004, 12:12
Atomski,
Like Alain said "But note you lose the following pulse each time ( PULSIN has always to wait for the next rising edge ...)" this also takes place when you change the state and once again issue the PULSIN command. I don't beleive this scheme will work for what you are trying to accomplish. I beleive you will have to use the CCP module directly, preferably with interrupts to capture the waveform timings you require for decoding. To answer your question, there is NO support in PBP for the CCP module when using it for waveform timing. Only PWM output.

Dave Purola,

Acetronics2
- 8th September 2004, 12:57
Welcome, dave ...

There might be a joke: the Hi 200 µs part is always the same value ...so, may be it is not compulsory to MEASURE it, just verify its presence ... ( no change for 150µs, for instance ...)

a simple PULSIN input, 0, value could do the work ... and catch all the incoming pulses !!!

a small mistake, in the lines:

PULSE = (Highpart + Lowpart) * 10 'pulse width in us

as resolution is 2µs @ 20 Mhz ... you should write:

PULSE = (Highpart + Lowpart) * 2 'pulse width in us

Alain

atomski
- 8th September 2004, 13:04
Nice one Alain! I'll give it a try. I like the way you think :D

Vlad

Acetronics2
- 8th September 2004, 13:28
Hé, Hé,

Thinking simple is always the best way ...

Alain

atomski
- 5th November 2004, 09:25
Guys,

The project id developing just fine. I have written both
the encoder and decoder software for 16F628 and it
looks sweet. However, I still have some minor problems
to address and hopefully I'm going to solve this in the
next couple of weeks. I'd like to thank all of you who
helped me!

atomski
- 15th November 2004, 07:49
Ok, here I am again.

Alain, I have a little problem with your measurement method.
For example, if I just verify the presence of high pulses
and measure low pulses, everything works just fine until
the phase shift in the signal occurs. Then my high pulse
becomes low pulse and vice versa. I really need to
measure both high and low pulses. The problem is that if
I use two consecutive PULSIN statements it doesn't work.
For example:

PULSIN Rx_Pin, 1, Pulse_Hi
PULSIN Rx_Pin, 0, Pulse_Lo

First PULSIN waits the rising edge and measures the
period that pin stays in that state, right? The second
PULSIN waits for the falling edge and repeats the process
for low pulse. Well I'm running my pic 16F628 @ 20MHz
and it seems to be missing the falling edge after
measuring Pulse_hi. Any ideas? Maybe if I used a
combination of PULSIN and RCTIME this might not happen?
If I understand the manual correctly RCTIME measures
the state of the pin right after the statement has been
issued in the program. That way the pic doesn't need to
detect the falling edge, right?

PS Is there a way to toggle between rising/falling edge in
a single PULSIN statement? For example:

Edge Var Bit
...
Edge = 0
IF Rx_Pin = 1 Then : Edge = 1
PULSIN Rx_Pin, Edge, Pulse

Acetronics2
- 15th November 2004, 09:05
Hi, atomski

You're a bit hard with me ...

So, you have to create a "rectifier" stage with a bit of analog and logic circuits before the PIC; see:

Consider half of the pulse voltage, and make a, say precise 50µS, positive pulse at each crossing, pos or neg ...

then, you can use PULSIN as it will be "resettable" during the 50µS pulses ... then add 50µS to what you've measured.

Just program what to do if two identical values follow each other ( it's then a phase shift ...)

Cheeeeeese, you're on the pic' !!!

Alain

atomski
- 15th November 2004, 10:11
Good idea mate, I'll give it a thought. I have something else
in the works to solve this problem. I'll keep you all posted...
If anyone has anything interesting to propose, I'm all ears!

Thanks!

jetpr
- 29th January 2005, 00:53
what type of pulse you goint to read...

if Rc then
DEFINE OSC 4
DEFINE PULSIN_MAX 6000 '6000 Define the Pulsin time

PulsIn Throtpin, 0, Throttle