# COMPARE

COMPARE Example

Syntax: COMPARE Mode, Result

## Function

Enable or disable comparator, compare voltages on P1 and P2 and retrieve comparison result to store in Result.

- Mode is a variable/constant/expression (0 – 2) that enables or disables the comparator (input pins P1 and P2) and determines if the optional comparator output pin (pin P0) is enabled or not. See the table below for an explanation of the Mode values.
- Result is a variable (usually a bit) in which the comparison result is stored.

## Quick Facts

| BS2px
Mode Values | 0: Disables comparator.
1: Enables comparator with P0 as Result output.
2: Enables comparator without P0 as Result output.
Result Values | 0: Voltage P1 > P2; P0 optionally outputs 0.
1: Voltage P1 < P2; P0 optionally outputs 1.

## Explanation

The COMPARE command enables or disables the built-in comparator hardware on the BS2px's I/O pins P0, P1, and P2. I/O pins P1 and P2 are the comparator inputs and P0 is optionally the comparator result output pin.

By default, the comparator feature is disabled. Using the COMPARE command with a Mode argument of 1 or 2 enables the comparator feature (using input pins P1 and P2) and returns the result of the comparison in Result. If Mode is 1, the result of the comparison is also output on I/O pin P0. The following is an example of the COMPARE command:

    result  VAR     Bit

    Main:
      COMPARE 1, result

This example enables the comparator (setting P0 to output the result, with P1 and P2 as the comparator inputs) and writes the result of the comparison into result. Both Result and the output pin P0 will be 0 if the input voltage on P1 was greater than that of P2. result and the output pin P0 will be 1 if the input voltage on P1 was less than that of P2.

Note that the comparator hardware operates independently of the execution speed of the BS2px and will continue to run and update P0 if Mode = 1, even during sleep mode (execution of END, NAP, POLLWAIT 8, or SLEEP commands). To avoid spurious current draw during sleep mode, disable the comparator first.

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012