PDA

View Full Version : A quick way...



aherrera
- 27th November 2009, 18:01
I am using the following code to detect when a pulse arrives at a pin:

Wt:
IF fin = 0 then Wt

It works well, since the pulse is only a couple of micro seconds long and I am running at 4 MHz, but I also need the routine to timeout after about half a second. I tried using PulsIn, but it does not detect the pulse. I though may be using TMR0 and an interrupt coded in assembler could make this loop exit, but my expertise in assembler are very (extremely) limited.
Do any of you have an idea?

aratti
- 27th November 2009, 18:34
Tray and see if this works:



Wt:
W0=0
While fin = 0
W0=W0+1
pause 1
If W0> 500 then goto TimeOut
Wend


Al.

Melanie
- 27th November 2009, 19:56
If it is a very short Pulse (a few uS), you might miss it's arrival during the 1mS Pause statement.

If you can route your pulse into say RB0, then rather than polling the pin, you can poll the INTF bit (not knowing your PIC I'm using the registers from a 16F628 as an example). This bit will SET and stay SET when your pulse arrives. So if you blink and miss the pulse because you were doing something else, the Flag tells you it's happened.

Don't worry, the software doesn't actually need to vector to any Interrupt routine, once set up (eg INTEDG=1, INTE=1, GIE=0) you can just poll the INTF flag (rather than the pin) as per Al's example.

Don't forget, it's your job to reset the INTF flag ready for the next event.

aherrera
- 27th November 2009, 23:48
That sounds perfect, since the pulse could be a short as 2 uS. I am using a PIC12F509 and if you could please give me an example for this one I will really apreciate it...
By the way, what I need is to toggle pin GPIO.2 from 1 to 0 when this pulse arives in GPIO.1 and this should be as fast as possible, since the actual 1 to 0 edge is used to measure distance.

Melanie
- 28th November 2009, 07:17
Then really neither of the above replies are suitable.

This is because whilst you are away doing something, (for example the PAUSE instruction), you are not looking for the event in order to trigger your Output IMMEDIATELY. Even with my suggested modification, you won't miss the pulse, BUT you'll only do something about it some time later - and that time could be delayed by anything up to 1mS.

You would need something like Darrel's INSTANT INTERRUPTS - but NOT to Poll the input pin, but to give you your Timeout. If setting your Output pin relative to the Input trigger is so critical, then I would even consider if a PIC is the suitable device - you could do it quicker with discrete logic.

Acetronics2
- 28th November 2009, 09:52
Hi, Aherra

Which distance measurement do you want to do ???

sound in air, in water, light ???

2µs looks a VERY very very short - or long ( ! ) period for those ...

Alain

PS : http://pagesperso-orange.fr/atexa_elec/ultrason/radar.htm