Shop OBEX P1 Docs P2 Docs Learn Events
edge detection — Parallax Forums

edge detection

BebopALotBebopALot Posts: 79
edited 2005-12-17 14:07 in General Discussion
Hi folks, I have just started using the SX protoboard and it is awesome.·My question concerns·using the SX to detect when a pin experiences a leading edge.·Guenther's book·says at·50 MHz the SX can experience an interrupt every 5.12us using the interrupt prescaler. If I want to detect a leading edge even faster can I write my assembly·code·to check port PIN HI/LO status at 75MHz which is once every 13 ns? If I don't need the benefits of an interrupt wouldn't this be more accurate?

Bebopalot

Comments

  • PJMontyPJMonty Posts: 983
    edited 2005-12-17 03:09
    Bebopalot,

    Don't forget that it will be slower than that simply because you have to code the loop structure plus the comparison to see if the pin is at the state you want it to be. Also remember that when you branch it takes 4 clock cycles due to the penalty paid for having to flush the instruction pipeline.
      Thanks, PeterM
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2005-12-17 06:48
    I think you may be tangled up with the idea of a Prescaler being involved. Your instructions can run no faster that one per cycle.

    With a 50Mhz osillator, top speed is 50Mhz. For 75Mhz, you would need 75Mhz or faster. It is do-able and you might need 80Mhz for some headroom.

    It is also better to do so with the SX being powered by 3.3 volts as it runs cooler and less EMI. Of course, that would mean 'hacking' into the SX-proto board and replacing the regulator. I haven't found the ideal regulator to trade out yet.

    Just try a 75Mhz oscillator at 5.0 volts to start out with.
    If you want to consolidate an operable design, consider the lower supply voltage.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "When all think alike, no one is thinking very much.' - Walter Lippmann (1889-1974)

    ······································································ Warm regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2005-12-17 14:07
    BebopALot said...
    Hi folks, I have just started using the SX protoboard and it is awesome. My question concerns using the SX to detect when a pin experiences a leading edge. Guenther's book says at 50 MHz the SX can experience an interrupt every 5.12us using the interrupt prescaler. If I want to detect a leading edge even faster can I write my assembly code to check port PIN HI/LO status at 75MHz which is once every 13 ns? If I don't need the benefits of an interrupt wouldn't this be more accurate?

    Bebopalot

    I assume you are referring to the sentence

    When we assume a 50 MHz system clock, it is easy to determine the time interval between two ISR calls. The RTCC register overflows every 256 clock cycles of 20 ns length each. This means that an interrupt is triggered every 256 * 20 ns = 5.12 µs.

    in my book.

    Actually, this explains the longest possible interrupt period @ 50 MHz clock without using the prescaler, and it describes what happens in the sample code shown before. The following text in the book describes, how to achieve a certain shorter interrupt period by adjusting the value of the RTCC on exit from the interrupt service routine by using the RETIW instruction.

    To detect an edge on a port pin, you don't necessarily need an ISR. The following shows how you might poll an input pin:

    Loop
      sb RB.0
      jmp Loop
    ;
    ; process rising edge here, and wait for falling edge
    ;
      jmp Loop
    



    As long as the port pin is low, one cycle through the loop takes 4 clock cycles (1 for the SB, and 3 for the JMP), i.e. 80ns @ 50 MHz clock, so this is the shortest possible interval (@ 75 MHz it would be 53.4ns).

    Please note that this pollong loop keeps the main code "trapped" in this loop, i.e. no other task can be performet while in this loop. You may - of course - execute other instructions within the loop, or call subroutines but this would slow down the loop. You may also use an ISR triggered from RTCC run-overs to perform other tasks. Again, this will slow down the loop as it is halted as long as the ISR executes.

    When the pulses are relatively short, it may even happen that you miss a complete low-high-low transition on the port pin when this occurs while the ISR is active or some other code is executed.

    In their wisdom, the designers of the SX have added special functionality to port B: Edge Detection. You can configure each port B pin separately to monitor rising or falling signal edges (WKED_B), and if a detected edge shall trigger an interrupt (or a wakeup) (WKPEN_B). There is another register (WKPND_B) where each bit (when set) indicates that the specified transition was detected at the corresponding port pin. Using the edge-detect feature allows you to write an application where an interrupt routine is called when on one or more port pins the specified transitions have been detected. But even without an interrupt routine, you can make use of this feature:

      mode $09
    Loop
      mov w, #0
      mov !rb, w
      test w
      sz
      jmp Loop
    ;
    ; Handle transition here
    ;
      jmp Loop
    



    The mode $09 makes sure that the WKPND_B register is accessed by a mov !rb, w. I may be necessary to place it inside the loop in case the mode register contents are changed elsewhere. In the loop, w is cleared first, and then the mov !rb, w instruction is executed. This actually is not just a move, but an exchange operation. This means the contents of WKPND_B and w are swapped. After this instruction, WKPND_B is cleared, and w contains the former contents of WKPND_B. If an edge was detected since the last loop cycle, the corresponding bit was set in WKPND_B, and now is set in w. Note that it is not necessary here to wait for the falling edge on the port pin, as we don't poll the port pin but the edge register instead. Therefore, it is very important to clear WKPND_B on each loop cycle. This means that you can even detect very short pulses. Nevertheless, it is still possible that you miss pulses when they occur in shorter periods than the total loop cycle execution time.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Greetings from Germany,

    Günther
Sign In or Register to comment.