How to measure pulse period?

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
victor336v
Posts: 9
Joined: Mon Dec 23, 2013 4:23 am
Been thanked: 1 time
Contact:

How to measure pulse period?

Post by victor336v »

Hello all,

Using PIC12F615 with older Flowcode 3 and trying to make seemingly simple gadget, but I'm struggling to combine
pieces of different flowcharts from tutorials to do what I need. Experts, help me out to start please.

In short - I need a controller that outputs a pulse proportional to the difference between two inputs. That's it.

More specifically:

First input - GP2, set up as digital, PIC should measure negative pulse length (varies 2...5ms) fed into it
every 20ms or so and I guess store measured lime period value as a variable, say, VAR1.

Second input - GP4, set up as analog - it measures voltage on it and converts its value (scaled if needed)
into a number stored as VAR2.

If VAR1<VAR2, do nothing and wait for the next pulse to GP2.
If VAR1>VAR2, multiply the difference by a fixed constant "K" and output a pulse with time length proportional
to this product (VAR1-VAR2)*K through the GP5 set up as output. Max. output pulse should be up to about 5 sec.

While this pulse is active, PIC ignores GP2 input. Once pulse ends, it starts monitoring GP2 input again -
either polls it, or on the interrupt.

So in this is in essence I'm trying to make a one-shot monostable multivibrator with variable output pulse length.
For now, I can't even measure a pulse length, getting confused with interrupts and variables...

Anybody?

Thanks in advance,

Victor

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: How to measure pulse period?

Post by QMESAR »

Hello

Your PIC has a CCP/PWM which is designed to do things as you want to do measure a pulse/duty cycle/low or high Pulse time etc it is just a matter of configuring the CCP module
Check the data sheet for the device section Capture Compare it will help you to achieve your goal.
Attachments
1.jpg
1.jpg (109.49 KiB) Viewed 15194 times

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: How to measure pulse period?

Post by QMESAR »

Hi
I do not have a PIC12F615 however I have a PIC12F675 and played a bit with you idea.
what i did as you will see I used the INT0 pin on the PIC to measure the input Pulse,I set the Interrupt to occur on the Falling edge and then I start Timer1 and set the Interrupt to occur on the rising edge
in a state machine I check if it is the rising edge then i stop the Timer and I read the time value(this corresponds to the low time of the pulse,to read the high time of the pulse you just need to switch the interrupts to occur
Rising edge -> Falling edge .

I used a 10Mhz crystal that gives me an 1 timer cycle as 400nSec and a pre-scaler 0f 8 which then equals 8*400nSec = 3.2uSec
to get the pulse time it is calculated from the timer cycle value which you read after the rising edge multiplied by the 1 Timer Cycle time
Pulse Low time = 3.2uSec * Timer Value read after rising edge example for 5msec low time I measured 1563 in the Timer1 then 1563 *3.2uSec = 5.0016 mSec

To test if the Interrupt follows the input signal I switch GP1 in the Interrupt falling and rising edge as you will see in the scope print GP1 follows the input signal 100% therefore I am sure the Interrupt is functioning correctly
and I measure the signal low time correctly.The red trace is a 100Hz 50% duty cycle input signal and the yellow trace is the GP1 which get set as it follow the Interrupt and the Input signal (the low time is inverted)

you just need to read your analog signal and do the maths and run the output pin as you would like

I hope this helps you
Attachments
CCP_P12.zip
(159.01 KiB) Downloaded 476 times
2.jpg
2.jpg (36.12 KiB) Viewed 15155 times
30.11.jpg
30.11.jpg (148.61 KiB) Viewed 15155 times

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: How to measure pulse period?

Post by QMESAR »

What I suggest you do is try to build upon the project I posted with your own code and post it here when you have problems and all of us can try to help you ,
this way you will learn FC and me too :D

Here is the C file I used for the pulse measurement anybody else with a better solution please let us know :D
Attachments
3.jpg
3.jpg (121.27 KiB) Viewed 15155 times

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: How to measure pulse period?

Post by QMESAR »

Hi

I have modified the project I added some code that outputs a pulse corresponding to the Input pulse low time
all you need to do now is do the maths and load the timer value and you have your pulse
this code should give you a kick start ,
There might be small problems as you go a long as I did not invest a huge amount of time to test all possible issues,however you will see the idea how to do this

The screen print shows in Red the input pulse which the low time is measured and the yellow is the output pulse corresponding to the low time,it a matter of maths now
you can add-scale divide do what you want to get the pulse length you want

Let us know how you get along :D
Attachments
CCP_P12__REV1.zip
(164.21 KiB) Downloaded 457 times
4.jpg
4.jpg (177.16 KiB) Viewed 15147 times

victor336v
Posts: 9
Joined: Mon Dec 23, 2013 4:23 am
Been thanked: 1 time
Contact:

Re: How to measure pulse period?

Post by victor336v »

Hi QMESAR,

Thanks for help, but I could not open any of the projects you've created - free demo version of Flowcode 6
starts loading all the program modules and at the end gives me error:
"Failed to open document.
Attribute <array> is not found"
Then all loaded windows go away, Flowcode itself is running but the work area is blank.

Of course, I can't open your examples in my older Flowcode 3 (.fcf project file) either.

So unless you have good idea what this error above is and how to get rid of it, at least could you please
generate project report (File->Project report) so if I can see it I might be able to reconstruct it
in Flowcode 3 block by block. Also, you said you've used a piece of your own c-code inserted into the flowchart.
Doesn't Flowcode have enough facilities to measure pulse without custom code, by using existing
icons and created macros?

As I understand it, all you need is to start a timer when a pulse arrives (can be on interrupt, or if the input
continuously polled in the loop), and count amount of overflows by the time pulse ends. This can be
done with standard blocks, I see some examples and exercises using this (just don't seem
to be able to combine them to do what I need). This does not involve CCP or PWM functions
of the chip, although it probably can be done this way too.

If I get beyond the point in my flowchart of measuring pulse and loading counter content representing its
length, I think I can keep modifying it to achieve desired functionality (and ask people here if I get lost), but I
need at least this starting point. I have no clue how to read a counter content. What block to use
to start counter. Do I refer to it as "a counter" or do I have to have a variable that gets a value of this counter,
so I read this variable, not a counter itself. Then, how to pass a counter value to a variable?
See, if people just suggest me "run the counter and read the variable", I don't know which blocks to grab
and what options to choose in each. You really HAVE to be a more-less programmer to understand how things are
done in controllers, flowcode just reduces tedious routines work for you. But if you're non-programmer as me,
I have no clue how exactly to implement all these suggestions in the flowcode, let alone in stand alone C.

Appreciate the help anyway, hope you can figure out what this "array" error means and make the project
loadable into flowcode 6. I then can look in each block you have in the flowchart, replicate then one
by one in my flowcode 3 by hand go from there...

Thanks in advance,

Victor

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: How to measure pulse period?

Post by QMESAR »

Victor

I have no idea why you cannot open the project files in FC6 Demo.
maybe one of the experience FC guys can help us with this problem :D
victor336v wrote:Doesn't Flowcode have enough facilities to measure pulse without custom code, by using existing
icons and created macros?
I did not find any blocks(component in FC that does Input Capture) the reason I used a C component that allows you to include C code in a project
victor336v wrote:all you need is to start a timer when a pulse arrives (can be on interrupt, or if the input continuously polled in the loop), and count amount of overflows by the time pulse ends. This can be
done with standard blocks, I see some examples and exercises using this (just don't seem to be able to combine them to do what I need). This does not involve CCP or PWM functions of the chip, although it probably can be done this way too.
This does not use the CCP/PWM module at all, it uses the Interrupt feature on GP2 which interrupts when an signal edge is detected and you can specify which edge it should interrupt on then in the Interrupt service handler/Routine you start and stop the timer depending on the edge that triggered the Interrupt.

The timer is set up by setting the pre-scaler value that it would not overflow for a pulse between 1-5mSec this way when you stop the timer and read out the TMRxH and TRMRxL registers you gave the exact time in Timer cycles
then multiplying the cycles with the Timer Constant of 3.2usec you have the pulse time in mSec. The Timer Constant is given by the FOSC and pre-scaler value you use.

I send you the Project document to your email address given above

hope this helps

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: How to measure pulse period?

Post by mikn »

Hi guys,

Working now with reading PWM, read this thread and want to clear one thing. Can you tell me how much time it takes to read the state of the pin?
I have 20ms period, in which first 2ms contains pulse from 1ms to 2ms length (1000us to 2000us). (it's servomotor signal).
chip is pic24fj64 running at 32mhz
I have tried this construction (it's macro which i call every second to read pwm input state):
readpwm1.PNG
(8.78 KiB) Downloaded 9824 times
result in terminal (sent after macro is finished, each 1 sec):
504;503;504;503;503;503;503;
then I removed delay and read as is:
readpwm2.PNG
(4.62 KiB) Downloaded 9824 times
the result is:
1035;1035;1035;1036;1035;1036;1036;
this seems to be related to what my transmitter sends, however actual period is around 1010us/
so seems like i can't read with frequency more than 1.000.000hz correctly?
or can you advice me another way to read servo signal from the rcmodel receiver?
I have tried to play with INT but still can't achieve the correct count.
FC 6.1.3.2 (18.02.2016)

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: How to measure pulse period?

Post by mikn »

pwm output and servomotor component works great. any advices how to read pwm input from the receiver? should I use C or ASM instead of flowcode to read input?
looking for some way like pulseIn function in arduino
FC 6.1.3.2 (18.02.2016)

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: How to measure pulse period?

Post by medelec35 »

victor336v wrote:but I could not open any of the projects you've created - free demo version of Flowcode 6
starts loading all the program modules and at the end gives me error:
"Failed to open document.
Attribute <array> is not found"
Then all loaded windows go away, Flowcode itself is running but the work area is blank.
That sounds like you are trying to open a fcfx file with a very out of date version of flowcode 6.

I know the first V6 versions did this when trying to open a flowchart that was created by a later version.

Martin
Martin

Post Reply