I/O pins 0 through 2 in port B can be set for comparator operation. This can be configured by writing to the EN and OE bits of the Comparator register (CMP_B) and monitored by reading the RES bit. The comparator mode is disabled by default. Comparator mode can be activated for all three pins, regardless of pin direction, but really matters only when pin 1 and 2 are set to input mode (pin 0 can optionally be set to output the comparative result). By configuring Comparator mode, the SX chip can quickly determine logical differences between two signals and even indicate those differences for external circuitry.

When comparator mode is activated, the RES bit in the Comparator register indicates the result of the compare. A high bit (1) indicates the voltage on pin 2 is higher than that of pin 1, a low bit (0) indicates the voltage on pin 2 is lower than that of pin 1. If the OE bit (Output Enable) of the Comparator register is cleared, output pin 0 of port B reflects the state of the RES bit.

To configure port B I/O pins 0 though 2 for Comparator mode:

  1. Set CMP_B to enable the Comparator and, optionally, the result output on pin 0.
  2. Set I/O pin directions appropriately.

' -------------------------------------------------------------------------
' Program Description
' -------------------------------------------------------------------------
'
' Demonstrates the use of the SX comparator and controlling RB.0 for 
' external circuitry.

' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------

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

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

LED             VAR     RB.0

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

result          VAR     Byte
cmpValue        VAR     result.0                       

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

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

Start:
  CMP_B = 0                                     ' enable comparator
  TRIS_B = %11111110                            ' RB.0 is output

Main:
  CMP_B = result                                ' exchange result with CMP
  GOTO Main

See also: COMPARE instruction and ADC8 Example.