Name:
compsetup
Syntax:

COMPSETUP config, ivr

Config - is a constant/variable specifying the comparator configuration.

Ivr - is a constant/variable specifying the internal voltage reference 'resistor-ladder' configuration.

Description:

Configure the internal comparators on X2 parts.

PICAXE-X2 chips have 2 comparators, each with the capability of comparing two analogue voltages from two external ADC pins or from an external ADC pin and an internally generated voltage reference. External ADC must be configured using the adcsetup variable before using this command.

PICAXE-28X2 (PIC18F25K22) / 40X2 (PIC18F45K22) and

PICAXE-28X2-3V (PIC18F25K20) / 40X2-3V (PIC18F45K20)

Config:

bit9 = 0 Comparator 2 Vin+ is set from voltage divider
  = 1 Comparator 2 Vin+ is from fixed 1.2V reference
bit8 = 0 Comparator 1 Vin+ is set from voltage divider
  = 1 Comparator 1 Vin+ is from fixed 1.2V reference
bit7 = 0 Comparator 2 Vin+ is ADC2
  = 1 Comparator 2 Vin+ is from voltage divider/fixed ref
bit6 = 0 Comparator 1 Vin+ is ADC3
  = 1 Comparator 1 Vin+ is from voltage divider/fixed ref
bit5 = 0 Change in comparator 2 does not cause change in compflag
  = 1 Change in comparator 2 sets compflag
bit4 = 0 Change in comparator 1 does not cause change in compflag
  = 1 Change in comparator 1 sets compflag
bit3 = 0 Comparator 2 output is not inverted
  = 1 Comparator 2 output is inverted
bit2 = 0 Comparator 1 output is not inverted
  = 1 Comparator 1 output is inverted
bit1 = 0 Comparator 2 is disabled
  = 1 Comparator 2 is enabled
bit0 = 0 Comparator 1 is disabled
  = 1 Comparator 1 is enabled

PICAXE-28X2-5V (PIC18F2520) / 40X2-5V (PIC18F4520)

Config:

bit7 not used, use 0
bit6 = 0 Comparator 1 Vin+ is ADC3 and Comparator 2 Vin+ is ADC2
  = 1 Comparator of both Vin+ is from voltage divider
bit5 not used, use 0
bit4 = 0 Change in either comparator does not cause change in compflag
  = 1 Change in either comparator sets compflag
bit3 = 0 Comparator 2 output is not inverted
  = 1 Comparator 2 output is inverted
bit2 = 0 Comparator 1 output is not inverted
  = 1 Comparator 1 output is inverted
bit1 = 0 Comparator 2 is disabled
  = 1 Both Comparator 1 & 2 are enabled
bit0 = 0 Comparator 1 is disabled
  = 1 Comparator 1 is enabled

PICAXE-20X2

Config:

bit9 = 0 Comparator 2 Vin+ is set from voltage divider
  = 1 Comparator 2 Vin+ is from fixed 1.024V reference
bit8 = 0 Comparator 1 Vin+ is set from voltage divider
  = 1 Comparator 1 Vin+ is from fixed 1.024V reference
bit7 = 0 Comparator 2 Vin+ is ADC2
  = 1 Comparator 2 Vin+ is from voltage divider/fixed ref
bit6 not used, use 1
bit5 = 0 Change in comparator 2 does not cause change in compflag
  = 1 Change in comparator 2 sets compflag
bit4 = 0 Change in comparator 1 does not cause change in compflag
  = 1 Change in comparator 1 sets compflag
bit3 = 0 Comparator 2 output is not inverted
  = 1 Comparator 2 output is inverted
bit2 = 0 Comparator 1 output is not inverted
  = 1 Comparator 1 output is inverted
bit1 = 0 Comparator 2 is disabled
  = 1 Comparator 2 is enabled
bit0 = 0 Comparator 1 is disabled
  = 1 Comparator 1 is enabled

Comparator Result

The result of the two comparators can be read at any time by reading the 'compvalue' variable - bits 0 and 1 of compvalue contain the comparator output.

Bit 0 is the output of comparator 1. This output can be inverted, equivalent to reversing the comparator inputs, by setting bit 2 of config.

Bit 1 is the output of comparator 2. This output can be inverted, equivalent to reversing the comparator inputs, by setting bit 3 of config.

If required a change in value can be used to trigger a change in the 'compflag' bit. When flag change is enabled (via bits 4 and 5 of config) the 'compflag' will be set whenever there is a change in input condition. This can be used to trigger a 'setintflags' interrupt if required. A change will also trigger a wake from sleep.

Internal Voltage Reference

Each comparator can be compared to a configurable internal voltage reference, generated from an internal resistor ladder (select via bits 6 and 7 of config). On some parts it is also possible to compare to a fixed internal voltage instead of the resistor ladder (select via bits 6, 7, 8 and 9 of config).

The voltage reference is generated from an internal resistor ladder between the power rails as shown in the diagrams on the compsetup page, in section 2 of the manual.

The ivr byte used within the compsetup command is configured as follows:

20X2, 28X2, 40X2

bit7 = 0 Voltage Ladder is disabled
  = 1 Voltage Ladder is enabled
bit6 not used, use 0
bit5 not used, use 0
bit4-0 Select 1 of the 32 voltage tap-off positions

28X2-5V, 28X2-3V, 40X2-5V, 40X2-3V

bit7 = 0 Voltage Ladder is disabled
  = 1 Voltage Ladder is enabled
bit6 not used, use 0
bit5 = 0 Bottom '8R' resistor is used
  = 1 Bottom '8R' resistor is shorted out and hence not used
bit4 not used, use 0
bit3-0 Select 1 of the 16 voltage tap-off positions
Applies To:
20X2, 28X2, 40X2
See Also:
Related Create:
Share:
Print:

Comparing two voltages

This program configures the comparator to take to ADC inputs and compare the two, then reports on whether one was greater than the other.

Code Example:
init:	adcsetup = 4		; use adc 0-3 (28X2-5V)
	compsetup %00000011,0	; use comparators 1 and 2

main:	b1 = compvalue		; read value
	debug			; display value
	pause 500		; short delay
	goto main		; loop back
Copy Code Submit an Example

Submit Your Own Code!

You must be logged in to submit code examples. Login now.

Comments

  • Login to leave a comment.