# POLLMODE

POLLIN / POLLOUT / POLLMODE Example

Syntax: POLLMODE Mode

## Function

Specify a polled command mode.

- Mode is a variable/constant/expression (0 - 15) that indicates the mode in which to process the polled command configuration.

## Quick Facts

| BS2p, BS2pe, and BS2px

Special Notes

- Polled-output pins will either change states continuously, just once or not at all, depending on the POLLMODE command
- A poll mode of 2 or 4 is required for a POLLWAIT command to work
- If both polled-outputs and polled-run are active, the polled-output event will occur before the polled-run event.

Related Commands | POLLIN, POLLOUT, POLLRUN, POLLWAIT

## Explanation

The POLLMODE command is one of a family of unique "polling" commands on the BS2p, BS2pe, and BS2px modules. The other commands in this family include POLLIN, POLLOUT, POLLRUN and POLLWAIT. The POLLMODE command is used to specify the mode in which polling events and activities are processed. This activity will occur in between instructions during the rest of the PBASIC program.

The "polling" commands allow the BASIC Stamp to respond to certain I/O pin events at a faster rate than what is normally possible through manual PBASIC programming. The term "poll" comes from the fact that the BASIC Stamp's interpreter periodically checks the state of the designated polled-input pins. It "polls" these pins after the end of each PBASIC instruction and before it reads the next PBASIC instruction from the user program; giving the appearance that it is polling "in the background".

This feature should not be confused with the concept of interrupts, as the BASIC Stamp does not support true interrupts.

The POLLMODE command sets one of 15 possible modes for the polling commands. It is used mainly before and/or after any POLLIN, POLLOUT and POLLRUN commands to disable and enable the polling features as desired. The table below shows the mode values and their effect.

Mode | Effect
0 | Deactivate polling, clear polled-input and output configuration.
1 | Deactivate polling, save polled-input and output configuration.
2 | Activate polling with polled-output action (and polled-wait) only.
31 | Activate polling with polled-run action only.
42 | Activate polling with polled-output/polled-wait and polled-run actions.
53 | Clear polled-input configuration.
63 | Clear polled-output configuration.
73 | Clear polled-input and output configuration.
8 - 15 | Same at 0 - 7 except polled-output states are latched.

1 | After the polled-run action occurs, the mode switches to 1 (deactivated, saved)
2 | After the polled-run action occurs, the mode switches to 2 (activated, outputs)
3 | These modes do not override the previous mode. Also, the output state of polled-outputs does not change as a result of these modes.

The polled-run modes, 3 and 4, are unique. As soon as the polled-run action occurs, the mode switches to 1 (deactivated, saved) or 2 (activated, outputs), respectively. This is so that the BASIC Stamp doesn't continuously go to the start of the designated program slot while the polled-inputs are in the desired poll state. Without this "one shot" feature, your program would appear to lock-up as long as the polled-inputs are in the designated state.

The clear configuration modes, 5, 6 and 7, are also unique. These modes do not override the previous mode. For example, if polled-inputs, polled-outputs and a polled-run configuration was set and the mode was set to 4 (activated, outputs and run) and later the program issued a POLLMODE 6 command, the polled-output configuration would be cleared but the mode would switch back to 4... still allowing the run action. This also means if, later still, the program issues a POLLOUT command, this polled-output would take effect immediately (since the mode is still 4). Also note that these modes do not change the output state of previously defined polled-output pins.

The POLLMODE command determines what action, if any, will occur in response to a polled-input event. This command works in conjunction with the POLLIN, POLLOUT and POLLRUN commands. The following is an example of the POLLMODE command:

    Setup:
     POLLIN 0, 0
     POLLOUT 1, 1
     POLLMODE 2
    Main:
     DEBUG "Looping...", CR
     GOTO Main

In this example, the first two lines configure I/O pin 0 as a polled-input (looking for a low state) and I/O pin 1 as a polled-output (going high if I/O pin 0 goes low, and vice versa). The third line, POLLMODE, initiates the polling process and configures polled-outputs to be active. From then on, as the BASIC Stamp executes the rest of the program, it will check for a low level (logic 0) on I/O pin 0 in between instructions and will set I/O pin 1 accordingly.

If, in the above example, the poll mode was set to 1 (which means deactivate polling but save configuration) I/O pins 0 and 1 would still be defined the same way, and I/O pin 1 would still be set to output mode, but no polling would take place during the rest of the program.

Here's another example that demonstrates mode 1 (deactivate but save configuration).

    Setup:
     POLLIN 0, 0
     POLLOUT 1, 1
     POLLMODE 2
     DEBUG "Polling configured", CR
    Main:
     POLLMODE 1
     DEBUG "No polling allowed here...", CR
     PAUSE 1000
     POLLMODE 2
    Poll_Now:
     DEBUG "Polling now...", CR
     GOTO Poll_Now

In this case, polling is configured and activated before "Polling configured" is printed on the screen. Once we reach the Main routine, however, polling is disabled (via the POLLMODE 1 command) and no polling occurs during the printing of "No polling allowed here..." or during the 1 second pause afterward. Finally, polling is activated again, and since the configuration was saved (because of mode 1, before) the polling activity acts just like it did initially for the remainder of the program. The ability to temporarily disable polling, without changing the configuration, can be a powerful feature for certain "critical" parts of a program.

The following example contains two programs. The first should be downloaded into program slot 0 and the second into program slot 1. We'll assume they are called POLL0.BSP and POLL1.BSP, respectively (as defined in the $STAMP directive lines).

    ' ----- Program #1 (Slot 0) -----
    ' {$STAMP BS2p, POLL1.BSP}
    Setup:
     POLLIN 0, 0
     POLLOUT 1, 1
     POLLRUN 1
     POLLMODE 4
    Main:
     DEBUG "Program 1", CR
     GOTO Main

    ' ----- Program #2 (Slot 1) -----
    ' {$STAMP BS2p}
    DEBUG "Switching...", CR
    Again:
     DEBUG "Program 2", CR
     GOTO Again

In this example (containing two programs; one is slot 0 and the other in slot 1) program 1 (slot 0) will configure polled-input pin 0 to detect a low state and polled-output 1 to go high in response. Program 1 also configures a polled-run activity (see the POLLRUN description for more information) to run the program in slot 1. The POLLMODE setting activates the polled-output and the polled-run. Then, program 1 continuously prints "Program 1" on the PC screen.

Once I/O pin 0 goes low, however, the BASIC Stamp will set I/O pin 1 high, then execution will be switched to the program in slot 1 (program 2). Program 2 will first print "Switching..." on the PC screen and then will continuously print "Program 2". From this point forward, I/O pin 1 will continue to be set low and high in response to changes occurring on I/O pin 0, but the polled-run activity is disabled and the BASIC Stamp endlessly runs the code in program 2's Again routine.

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012