Page 1 of 1

timing an input pulse

Posted: Sun Oct 29, 2006 2:04 pm
by iaindunn
Hi
I am new to PICs and my main interest is in RC models.
I need to time an input pulse from the reciever. These are typically 1 to 2 milliseconds long and occur every 20 milliseconds.

I was trying to setup a basic timing loop with a 10us resolution but cannot set a delay in the loop below 1ms using flowcode.
The loop was something like this;

Is input high
if yes wait for 10us
is the input still high
if yes continue loop
if no store vale and move on

Anyone any ideas on how to do this?

The second part im trying to achieve is ;
Is this time <= 1.4 ms
if yes set an output high
if no is time>=1.6ms
if yes set output high
if no set output low

go back and wait for next pulse

Any help appreciated
Iain

Posted: Mon Oct 30, 2006 12:26 pm
by Steve
To get a shorter delay, you could use a 'C' icon with one of the following lines in it:

Code: Select all

delay_us(10);   //Use this for Flowcode v2

delay_10us(1);    //Use this for Flowcode v3
As for the second part, maintain a variable that is increased for each 10us delay. When this variable has reached 140, this should equate (roughly) to 1400us, i.e. 1.4ms.

This approach will not take into account the time of the other code to execute (e.g. the loops), so will not be 100% accurate. For better accuracy, I would suggest you use interrupts with an appropriate crystal clock (but this will be more difficult to program).

Posted: Mon Oct 30, 2006 9:58 pm
by iaindunn
Thanks for the reply Steve.

I'll give this a whirl. :)