PDA

View Full Version : Best way to find a rising edge?



jcsquire
- 31st May 2006, 04:50
Hi,

My first post here! Seems like a great forum; glad to be a member. I need to know when an input pin sees a 0-5V transition (must do with several pins; don't want to use interrupts).

In C I'd do it this way:
byte old, nowHigh;
old = pin_in; // set old as the old value of the pin
pause 10
nowHigh = pin_in & !old; // set nowHigh if it is now high but used to be low

My newbie questions:
1) In PBP is it smarter to use BIT variables to do this?
2) How do I get the NOT operator (! in C) in PBP? Or do I have to do something like
nowHigh = (pin_in==1) AND (old==0) ?

And what kind of variable is nowHigh = (pin_in==1) AND (old==0) anyway? Is it a BIT since pin_in is a bit, or if nowHigh is a BYTE is it automatically typecast to a BYTE?

Thanks for shedding any light on this!

- Jim

jcsquire
- 31st May 2006, 05:51
----------

mat janssen
- 31st May 2006, 07:14
I did it like this

RESETALL VAR PORTA.0
RSTPLS VAR BIT
HULP0 VAR BIT


RSTPLS = RESETALL & ~ HULP0
HULP0 = RESETALL

(& = logical and ~ = invert)

' At RSTPLS you will get a pulse what is one cycle of your program if RESETALL goes high.

Ofcource
while
RESETALL = 1
wend

will also work

paul borgmeier
- 31st May 2006, 07:26
I am not sure exactly what you are trying to do ???

; If you are only looking for 0V-5V (and not 5V-0V) then try this
; example of monitoring RB0, RB2, and RB4 for change
old var byte
main:
pause 10
old = PORTB & %00010101
if old = 0 then main
; do something here because a pin went high
; to find which pin went high
if old.0 = 1 then here4RB0
if old.2 = 1 then here4RB2
if old.4 = 1 then here4RB4

here4RB0:
do something
goto main
here4RB2
do something
goto main
here4RB4
do something
goto main

Another approach - You also could just poll the port directly,
main:
If PORTB.0 = 1 then here4RB0
If PORTB.2 = 1 then here4RB2
If PORTB.4 = 1 then here4RB4
pause 10
goto main
etc

************************************************** ************
; If you are looking for 0V-5V or 5V-0V then try this
; example of monitoring RB0, RB2, and RB4 for change
old var byte
nowHigh var byte
main:
old = PORTB & %00010101
pause 10
newHigh = PORTB & %00010101
if old = newHigh then main ; no change on RB0, RB2, RB4
; do something here because a pin changed
old = old ^ newHigh ; find changed pin(s) (bit or bits will be set for changed pin(s))
if old.0 = 1 then here4RB0
if old.2 = 1 then here4RB2
if old.4 = 1 then here4RB4

here4RB0:
if newHigh.0 = 1 then
do something ; pin when from low to high
else
do something else ; pin went from high to low
endif
goto main
here4RB2
do something
goto main
here4RB4
do something
goto main

Good Luck,

Paul Borgmeier
Salt Lake City, Utah
USA

jcsquire
- 31st May 2006, 15:11
Wow, thanks Paul and Mat; that was fast! Exactly what I needed...I couldn't find the ~ negator in the manual, and I wasn't sure if logical true and false could be automatically typecast to a byte variable. From your code, now I know, and I have a bunch of things to try to see which minimizes the generated code.

Thanks,

- Jim