Shop OBEX P1 Docs P2 Docs Learn Events
Using interupts with the BS2p24/40 — Parallax Forums

Using interupts with the BS2p24/40

DonohoDonoho Posts: 3
edited 2004-12-15 18:03 in BASIC Stamp
How can i use interupts with the BS2p24/40?· The frequently asked questions on the website state that the microcontroller itself cannot support interrupts.· Is there an easy way to incorporate interrupts with the use of the BS2?· If not easy, then a good way?· Please reply.· Any helpful advice is well appreciated

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-15 18:03
    My February article in Nuts & Volts magazine explains why true interrupts in BASIC are impractical; the short version is this: What happens if you're doing a time sensitive function like SERIN, SEROUT, PULSIN, PULSOUT, PAUSE, etc. and you get an interrupt? If the interrupt is handled the serial data will be garbled, PULSOUT and PAUSE will be extended, and PULSIN will return the wrong value.

    With the BS2p you can setup a polling mode that checks the state of certain pins (you set them up) for certain states (you decide) and takes some action. This is called pin polling. It allows the high level to finish before the pins are scanned and other possible action taken. This is the only way to keep the high-level code working properly. It's not a true interrupt in that the inputs are not scanned until the currently-exectuing instruction is complete. The "POLL" instructions in the Help file can be used to setup pin polling and actions.

    That said, often program architecture can help. Here is a structure that I use in many of my programs.

    Main:
      DO
        GOSUB Background
        ON task GOSUB Task0, Task1, Task2
      LOOP
     
    Background:
      ' do something important here
      RETURN
     
     
    Task0:
      ' task 0 code
      task = task + 1 // NumTasks
      RETURN
     
     
    Task1:
      ' task 1 code
      task = task + 1 // NumTasks
      RETURN
    
     
     
    Task2:
      ' task 2 code
      task = task + 1 // NumTasks
      RETURN
    
    
    

    As you can see, the code in the subroutine Background runs every time at the top of the loop.· The variable called task is used to determine what the program does next.· I've made this very simple, but you can add more intelligence to the task updating to handle special conditions.

    Task0:
      ' task 0 code
      IF (condition) THEN
        task = NewTask                 ' redirect task pointer
      ELSE
        task = task + 1 // NumTasks    ' point to next task in order      
      ENDIF
      RETURN
     
     
    Task1:
      ' task 1 code
      IF (condition) THEN
        GOSUB TaskX                    ' handle condition
      ENDIF
      task = task + 1 // NumTasks      ' point to next task in order  
      RETUN   
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office


    Post Edited (Jon Williams) : 12/15/2004 6:10:02 PM GMT
Sign In or Register to comment.