Shop OBEX P1 Docs P2 Docs Learn Events
external interrupts + RTCC internal interrupts — Parallax Forums

external interrupts + RTCC internal interrupts

loojadeloojade Posts: 30
edited 2008-02-25 21:37 in General Discussion
Hallo everybody,

I have a dubt about interrupt configuration. I need to fix a SX28p to accept 3 external signal (from 3 proximity sensors). For every edge I have to check a counter value to define the time between pulses.
I got the following code from SX-KEY help file. I think this is enough to detect the external pulses from the sensors.


' -------------------------------------------------------------------------
' Program Description
' -------------------------------------------------------------------------
'
' 
' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------

DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ            4_000_000
ID              "ISRPORTB"

' -------------------------------------------------------------------------
' IO Pins
' -------------------------------------------------------------------------

Buttons         VAR     RB                      ' button inputs
TRIS_Btns       VAR     TRIS_B


' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------


' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------

sensor          VAR     Byte                    ' sensor conneted to RB.0, RB.1 and RB.2 
A                 VAR     Word
counter1       VAR     Word
counter2       VAR     Word
counter3       VAR     Word

' -------------------------------------------------------------------------
  INTERRUPT
' -------------------------------------------------------------------------

ISR_Start:
  WKPND_B = sensor                              ' get which sensor cause the pulse

Ch1:                                            ' check channel
  IF sensor <> %0001 THEN Ch2                   ' if not, try next
  A=counter1
  counter1=0
  GOTO ISR_Exit

Ch2:
  IF sensor <> %0010 THEN Ch3
  A=counter2
  counter2=0
  GOTO ISR_Exit

Ch3:
  IF sensor <> %0100 THEN Ch4
  A=counter3
  counter3=0
  GOTO ISR_Exit

ISR_Exit:
  WKEN_B = %11111111                            ' no ISR until reset
  RETURNINT

' =========================================================================
  PROGRAM Start
' =========================================================================

' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------

Start:
  
  WKPND_B = %00000000                           ' clear pending
  WKED_B = %00000111                            ' falling edge detect
  WKEN_B = %11111000                            ' use bits 0..3 for inputs
  END





Now I don't know How can I use the internal interrupts to incremement the counters (counter1, counter2 and counter3: 1 for each sensor).

In SX-KEY help file I found the following code to repeat a function a specific times for second:

' -------------------------------------------------------------------------
  INTERRUPT 77_000
' -------------------------------------------------------------------------

ISR_Start:
  INC counter1 
  INC counter2 
  INC counter3 

  RETURNINT






How can I have both kind of interrupts in one program??? I'm not sure I can do this.
Any help will be apreciate.
Thanks in advance


Loojade

Comments

  • ZootZoot Posts: 2,227
    edited 2008-02-17 18:41
    If you need both (a regularly running interrupt and edge detection) it is probably easier to just do the former, and within the regularly scheduled interrupt you can check for edges, i.e.:

    
    pulseDivider VAR Byte
    edges VAR Byte
    oldEdges VAR Byte
    
    INTERRUPT 100_000
    
    DEC pulseDivider
    IF pulseDivider = 0 THEN
       pulseDivider = 10    
       ' do your regular pulse work. for example this would run 100000/10 or 10000x second
       pin = ~pin ' or whatever
    ENDIF
    
    ' now check for edge detect changes 1000000x per second
    edges = RB  ' or whereever pins are
    IF edges.0 <> oldEdges.0  THEN ' it's different, must be an edge
       ' do some stuff
    ENDIF
    IF edges.1 <> oldEdges.1 THEN ' it's different, must be an edge
       IF edges.1 = 1 THEN ' high going edge, for example
       ' do some stuff
       ELSE                       ' low going edge
         ' do some stuff 
       ENDIF
    ENDIF
    oldEdges = edges
    
    RETURNINT
    
    



    There are much more efficient ways to do this, but hopefully the above is clear enough that you get the basic idea. Others may have much cleverer suggestions as well.

    Remember too that WKPEND is set whether or not you are using it to trigger an interrupt, so you could check (and deal with results of, and clear) WKPEND each time through the regular interrupt. The biggest issue is sample rate -- you want the regular interrupt to check the edges often enough that you won't miss an edge change while you are dealing with an edge change (i.e., the interrupt needs to run much faster than the possible fastest edge changes so that the interrupt runs and is done before the next possible edge change comes). There are ways around that too, and others have posted some pretty clever code in that regard, but hopefully this will get you started.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php


    Post Edited (Zoot) : 2/17/2008 6:48:02 PM GMT
  • loojadeloojade Posts: 30
    edited 2008-02-19 13:57
    thanks... very clear!!
  • loojadeloojade Posts: 30
    edited 2008-02-25 12:01
    Hallo,

    I'm sorry for all my questions...
    Actually for beginners with SX (like me) it's difficult to learn about SXB. The documentation about SX assembly is very complete but I's not the same regarding SXB. For this reason many time I need to ask many things to forum users.

    Anyway... this is my question: When I use PAUSE (or PAUSEUS) in my code (for example in main code), the SX is able to answer to interrupt requests or even interrupts routines are stoped???
    Basically I would like to use PAUSE in main program but I would like to keep interrupts routines working well too. It's possible??

    One more question: If the code included in interrupt routines take more time that interval between 2 interrupts call, what happen?
    I would have a interrupts routine 50000 time per second (by INTERRUPT 50_000) , but if routine code take more time than minimum available the next interrupt call will be delayed or ... what???

    Thanks in advance for your help
  • BeanBean Posts: 8,129
    edited 2008-02-25 14:13
    Interrupt do continue even during a PAUSE or PAUSEUS. In fact this make the pause take longer than the specified value, this can be corrected by using the second parameter on the FREQ command.

    If the interrupt code takes too long, you will miss an interrupt. The RTCC will have to wrap around again to trigger the next interrupt.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.iElectronicDesigns.com

    ·
  • loojadeloojade Posts: 30
    edited 2008-02-25 15:44
    Thanks a lot Bean,

    About my second question.. this mean that when the code is too long, simply the next interrupt call will not execute.
    My problem is the following: Normally the interrupts must check (50_000 time per second = 20 uSec) the state of 2 sensors (working freq max = 350 Hz) and 2 buttons for user interface. Some time a user can press a button to save some data in external eeprom and display some value to LCD. This procedure can take (I think) more than 20 uSec. So If every time this happen (user press a button) simply the next interrupt call will not execute, is very good!!! Basically I don't need a good accuracy when the user is managing the ECU by buttons, but I need it when the ECU is far away from user...

    Everytime you are very kind.
  • ZootZoot Posts: 2,227
    edited 2008-02-25 21:37
    At a certain point you may need to move things like "pause" and such into the ISR (Interrupt Service Routine). This makes them independent of any timing *but* the ISR. You can still do this in SXB, though it's not as efficient as some assembly methods.

    Check out the Nuts n Volts articles from the last 6 months or so -- there are lots of good SX/B examples of doing this.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
Sign In or Register to comment.