Best way to find a rising edge?


Closed Thread
Results 1 to 5 of 5
  1. #1
    jcsquire's Avatar
    jcsquire Guest

    Default Best way to find a rising edge?

    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
    Last edited by jcsquire; - 31st May 2006 at 06:53.

  2. #2
    jcsquire's Avatar
    jcsquire Guest


    Did you find this post helpful? Yes | No

    Default

    ----------
    Last edited by jcsquire; - 31st May 2006 at 06:54.

  3. #3
    Join Date
    Oct 2003
    Location
    holland
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    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

  4. #4
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Default

    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

  5. #5
    jcsquire's Avatar
    jcsquire Guest


    Did you find this post helpful? Yes | No

    Default

    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

Similar Threads

  1. mS Timer
    By whmeade10 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 8th September 2020, 13:12
  2. RC Servo decoding/encoding using 12F683
    By ScaleRobotics in forum Code Examples
    Replies: 13
    Last Post: - 14th September 2010, 01:49
  3. Interrupt On both (Rising and Falling)
    By yasser hassani in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 12th March 2008, 15:34
  4. 2 interupts - portb.0 rising and falling edges
    By EDWARD in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 15th July 2005, 02:10
  5. Replies: 2
    Last Post: - 5th June 2005, 20:55

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts