Shop OBEX P1 Docs P2 Docs Learn Events
how to simulate interupts — Parallax Forums

how to simulate interupts

ionion Posts: 101
edited 2004-09-20 22:39 in BASIC Stamp
Hello everybody,
I have a weird question. As all of us know about lack of interrupts on a stamp.
I have a program when if a certain input is on, the program jump to a subroutine to display error messages. Because I wanted to make it nice I put the message to flash, which means display· on, one sec pause , display off, one second pause, return, to check the status of the input.
This is ok , but during the 2 seconds on that subroutine, my stamp is blind for what does happen with the inputs.
What I would like to know if it is a way to put a counter in the program and turn the display on or off based in that counter without spending time in the subroutine.
Original code:
·
MAIN:
····· IF (RESET = 0)· THEN NORMAL_RUN· ······················ ·'RESET
RESET_ON:
AUXIO
LOW LOCK
MAINIO
GOSUB displ_error_start
GOTO MAIN
·
NORMAL_RUN:
AUXIO
LOW DRV
MAINIO
IF (PART=0) THEN·················· 'PART ON· INPUT 3
·
··········· EEADDR=0··········· 'SYSTEM READY"
··········· GOSUB SHOW
.
.·········· rest of the code here
.··········
.
·
end
·
displ_error_start:
··········· SEROUT lcdpin,lcdbaud,[noparse][[/noparse]12]
··········· PAUSE 1000
··········· EEADDR=320··········· 'RESET IS ON
··········· GOSUB SHOW
··········· EEADDR=340··········· 'SYSTEM· IN BYPASS
··········· GOSUB SHOW
··········· EEADDR=460··········· 'CHECK YOUR PARTS
··········· GOSUB SHOW
··········· PAUSE 1000
··········· RETURN
·
·
SHOW:
STORE 1
SEROUT lcdpin,lcdbaud,[noparse][[/noparse]14]
DO
· READ EEADDR, eeData
· SEROUT lcdpin,lcdbaud,[noparse][[/noparse]eedata]
· eeaddr=eeaddr+1
· IF (eedata = CR) THEN EXIT
LOOP
STORE 0
RETURN

So what I want is· that each time when the program pass through the main code, to add 1 to a certain variable. When the variables is a preset number I would like to jump to a subroutine, turn the display on, and start counting again from zero. The next time when the variable is at preset, jump to a different subroutine and turn it off.
In this case, the program executes normal and I have control over the inputs and outputs.
I am using a bs2pe24. The next question, is where to find out what is the length of time for an instruction to execute, so I can calculate my preset number rather then guessing?
And, because we are dealing with instructions, where I can find a table with the space taken for each instruction in the memory? ·In this case, if I run out of space in a slot I can look how to replace an instruction with a different one, which take less space.


Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-09-19 19:21
    The fact is that most embedded microcontrollers DON'T have or use interrupts and programmers have lived happily without them for years.· That said, it's easier to deal with no interrupts in assembly than in BASIC, but it can be done.· I know, because I built a commerical device (prior to my employement at Parallax) that I thought would need interrupts, but ended up working beautifully on a stock BS2.

    The trick is program organization.· Most of my programs have this general structure:

    Main: 
      DO 
        ' check "interrupt input -- branch if required 
        ON task GOSUB Task_0, Task_1, Task_2, ... 
      LOOP 
      END
    
    Interrupt: 
      ' do "interrupt" process 
      ' adjust program task pointer if required 
      GOTO Main 
    
    Task_0: 
      ' task 0 code 
      IF (some_condition) THEN 
        GOSUB Condition_Handler        ' you could also just change the task pointer
      ELSE 
        task = task + 1 // NumTasks    ' point to next task (with rollover)
      ENDIF 
      RETURN
    


    Every time the main loop runs the "interrupt" input is checked and handled if required; then the program runs the nomal task cycle.· So, instead of creating a long linear program, you can create a list of tasks to run (that can run in linear order), but also have the flexibility to be run out of sequence to deal with circumstances that may come up.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • ionion Posts: 101
    edited 2004-09-19 19:56
    Thank you Jon,
    I will try it
    Ion
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-09-19 20:02
    Ion,

    You could add a bit of flexibility to your Show subroutine by using Z-Strings -- this would allow you to embed carriage returns in your messages.· If you're using a multi-line display this can be useful.

    Show_Msg: 
      STORE MsgSlot 
      SEROUT LcdPin, LcdBaud, [noparse][[/noparse]14] 
      DO 
        READ eeAddr, eeData 
        eeAddr = eeAddr + 1
        IF (eeData = 0) THEN EXIT
        SEROUT LcdPin, LcdBaud, [noparse][[/noparse]eeData] 
      LOOP 
      STORE PgmSlot 
      RETURN
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • ionion Posts: 101
    edited 2004-09-19 23:21
    Thank you for all your assistance.
    Are you sure that you do not have any blood relation with Mother Theresa?
    Now serious:
    On the help file, under toggle command is an example
    OUT2 = IN2 ‘ make output driver match pin state
    TOGGLE 2
    I have a bs2p40 with in2 on the mainio and out2 on the auxio.
    This command seems like it will not work on this chip unless I put an intermediary bit variable. Is this correct ?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-09-20 01:46
    I am quite certain that I don't have any blood relationship with Mother Theresa (God bless her) -- but that's a nice thing to say. I just enjoy BASIC Stamps and working with our customers. I have a very fun job, so I do it as much as I can.

    You would not use the OUTx = INx thing on two physically different pins. You may want to re-read that section in the help file; its point is to make sure you get what you expect out of TOGGLE.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • ionion Posts: 101
    edited 2004-09-20 04:17
    My question it was not related to Togle. It did happen to find that expresion OUT2=IN2 and this it what i want to achive.
    The problem is the BS2p40. I can not address the auxiliary IO unless i use a command to transfer the control from MAINIO to AUXIO.
    My input is on my PCB on MAINIO and all the outputs are on the AUXIO.
    I have OPTO isolators from 24vdc on INPUTS and darlingtons 2003 on the OUTPUTS. How to activate the output from an input based in the same expresion. It is OK for a 16 IO chip where i do not use MAINIO or AUXIO or IOTERM.
    Can be done with IF..THEN but i like this aproach more as simpler then IF...THEN.
    Any sugestion ?
    Thanks
    Ion
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-09-20 05:37
    Gotcha -- and you're right. You can only use OUTx = INy on the same set of IO pins. If you want to take an input from say MAINIO and control an output on AUXIO, you will indeed require more than one line of code. To minimize code you could grab all you iputs with a byte or word, then switch to the outputs side and map the bits as required.

    MAINIO 
    inBits = INS 
    IF (Transfer = OK) THEN 
      AUXIO 
      OUTx = inBits.LOWBIT(x) 
      OUTy = inBits.LOWBIT(y) 
      OUTz = inBits.LOWBIT(z) 
    ENDIF
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • ionion Posts: 101
    edited 2004-09-20 06:47
    This looks smarter then mine. With your solution i can control more then one output at the time
    In the interim i used for only one IN and OUT this:
    AUXIO
    DRV VAR OUT3 'SIGNAL TO DC DRIVER TO ENABLE THE TOOL
    MAINIO
    VDRV VAR Bit ' Virtual input
    TRIG VAR IN7 'FROM DC CONTROLLER TRIGER IS IN ON

    MAIN:
    vdrv=trig 'make the virtual input to follow the state of the input 7 on mainio
    AUXIO
    drv=vdrv 'make the output 3 on auxio to follow the virtual bit == input 7 on mainio
    MAINIO

    It works but i will use yours because i have more then one bit to control
    Thanks for help
    Ion
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2004-09-20 22:15
    There is an approach you can take that uses the POLLing commands. Add this to your bag of BS2P/BS2PE programming tricks, as I am not sure it will help at all with your present effort. It does get at the topic of interrupts, as close as it gets on the Stamp.

    If you set the pins on mainio as polling inputs, you can then test the state of those pins without switching back and forth between auxio & mainio. Instead, you test them by GETing the contents of scratchpad locations 128 and 129 (read only--the state of auxio is held in locations 130 and 131).

    initialize_Polling:
    MAINIO
    FOR idx=0 to 15   ' look for noninverted target state on all mainio pins
      POLLIN idx,1
    NEXT
    POLLMODE 2    ' locations SP130 and SP131 follow the inputs.
    
    Main:
    AUXIO
    GET 128, Word inbits0   ' get all 16 bits, mainio states
    vdrv=inbits.bit3
    otherout=inbits.bi9  ' etc.
     '.....
    



    I don't think the above code is any better than the one Jon suggested, which involved switching to mainio, reading the pins into a word variable, and switching back to auxio to set the outputs.

    However, things get interesting if you use POLLMODE 10 instead of POLLMODE 2. The bits in the scratchpad will !!_latch_!! the in the target state. The state of the pins is checked and updated in the scratchpad buffer in between every PBASIC command, so these commands can be quite useful to catch a relatively short input pulse while the program is off doing other tasks. Polling is not a true interrupt, but maybe you can do something with it.

    Note that there is also a POLLOUT command, which would automatically (without further program code) transfer an input state to an output state, and the coupled inputs and outputs can be on either mainio or auxio. Don't get excited though! The command is of little use AFAIK when you have multiple outputs to be coupled separately to multiple inputs.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • ionion Posts: 101
    edited 2004-09-20 22:39
    Thanks Tracy. I will try it tonight and lett you know what are the results on my code
    Thanks again
    Ion
Sign In or Register to comment.