M182 Comparator

DNN

New Member
Hi

I'm a beginner to using PICAXE and have been trying to set up a comparator that looks at a 0v to 1v input signal, compairs this with an adjustable reference (0.45v to 0.8v) and then drives an external relay depending on the result.

With the help of code found in the archives I have managed to get it working with an internally set reference.

Code:
REM *********************************************
REM Test of the internal comparator functionality 
REM of the PICAXE M2 variants        
REM (tested on PICAXE-18M2, but possibly also
REM  available on 14M2 and 20M2 variants)
REM 
REM Jurjen Kranenborg, February 2011
REM *********************************************
REM 
REM The M2 variants of the PICAXE have two independent comparators which, 
REM although not directly supported by the COMPSETUP command
REM like with the X2 variants, nevertheless can be configured with the same
REM flexibility through direct POKESFR and PEEKSFR register operations.
REM 
REM Together with the DAC functionality of the M2 series
REM a huge range of analog signal processing capabilities
REM becomes available, since both comparators can have the 
REM programmable DAC or the FVR (Fixed Voltage Reference) 
REM as (+) input. Since the DAC range (32 steps) can be programmed in various 
REM ways (for example using the FVR which itself has three programmabl output
REM levels too), an extraordinary number of very useful configurations
REM for analog signal processing is possible.
REM Note also that the comparator inputs can be programmed (via multiplexing)
REM to be coupled with several input pins or Vcc/GND as well!
REM 
REM It is highly recommended to review the PIC16F1827 datasheet:
REM   - Chapter 18 on Comparator functionality
REM   - Chapter 14 on programmable FVR functionality
REM   - The pin diagram of the PIC16F1827 for the various possibilities
REM     for connecting the comparator inputs and outputs to the Picaxe legs 
REM 
REM All relevant control registers for the comparators
REM have been defined below using SYMBOL declarations


REM ==========================================
REM Test program: 
REM - Both comparators enabled and with same (-) and (+) inputs:
REM     - (+) input connected to the DAC output, which in this application
REM       is programmed at 0.5*Vcc
REM     - (-) input connected to C12IN0- input (C.0 / leg 17, see datasheet)
REM       The (-) input voltage in this example can vary between 0 and Vcc
REM       via a potentiometer coupled via a (protective) resistor to the input.
REM - The sole difference between the comparator application in this example
REM   is the inverted output of comparator 2
REM - The output of the comparators is stored in the CMOUT register bits 0 and 1
REM   and checked in a loop
REM - As a result the LEDs are toggled at approx half position of the potentiometer 
REM - Note that it is also possible to have the comparator output 
REM   available on two picaxe pins, allowing the programmable comparators
REM   to be used by the application hardware without any PICAXE intervention 


REM Local test hardware configuration settings
REM ==========================================
REM --> Please adapt to your local application!
REM 
REM LEDs connection:
SYMBOL LED_green = B.6
SYMBOL LED_red = B.5
'fvrsetup FVR1024 ; set to 1.024V
REM Comparator (-) input at C12IN0- input (C.0 / leg 17 of 18M2, see datasheet)
REM is connected to a potentiometer with a protective series resistor.
REM Comparator [+) input has been programmed to be connected to
REM DAC output, so no (+) input leg needed in this test application


#Picaxe18M2
#No_Data

REM Define DAC range
REM ================ 
REM DAC config data for DACsetup command:
REM   - DAC enabled (bit 7 = 1)
REM   - DAC level not made externally available (bit 
REM   - DAC upper level = VCC
REM   - DAC lower level = GND
SYMBOL dacdef = %10000000
DACsetup dacdef 

REM Set DAC level at approx 0.5*Vcc (15/32*Vcc)
REM ===========================================
REM (Note: max DAC output corresponds to level 31 -> 31/32*Vcc)
DAClevel 100

REM Set comparatór 1 and 2 characteristics and enable them
REM ======================================================
REM 
REM Configuration register adresses 
REM (derived from memory map Chapter 3 of PIC16F1827 datasheet
REM  and explanation of POKESFR register addressing in Rev-Eds manual) 
SYMBOL CM1CON0_address = %1010001 
SYMBOL CM1CON1_address = %1010010
SYMBOL CM2CON0_address = %1010011 
SYMBOL CM2CON1_address = %1010100 
SYMBOL CMOUT_address = 	 %1010101
REM Configure the comparators for this specific app, see Chapter 18):
SYMBOL CM1CON0_data = %10000000  'Enable comp1, non-inverted output
SYMBOL CM1CON1_data = %00010000  'Select comp1 inputs: DAC output (+) and C12IN0- (-) 
SYMBOL CM2CON0_data = %10010000	'Enable comp2, inverted output 
SYMBOL CM2CON1_data = %00010000  'Select comp2 inputs: same as comp1
SYMBOL CMOUT = b0
SYMBOL CMOUT_comparator1 = bit0  'Comparator output 1 
SYMBOL CMOUT_comparator2 = bit1  'Comparator output 1 


REM =========================================
REM Main Program
REM =========================================

REM Store config data and enable the comparators:
POKESFR CM1CON0_address, CM1CON0_data
POKESFR CM1CON1_address, CM1CON1_data
POKESFR CM2CON0_address, CM2CON0_data
POKESFR CM2CON1_address, CM2CON1_data

REM Endless loop:
REM Read comparator inputs and show results
REM in LED_green and (inverted) in LED_red:

DO
	PEEKSFR CMOUT_address, CMOUT 
	IF CMOUT_comparator1 = 1 THEN
		HIGH LED_green
	ELSE 
		LOW LED_green
	ENDIF
	IF CMOUT_comparator2 = 1 THEN
		HIGH LED_red
	ELSE 
		LOW LED_red
	ENDIF
	
LOOP

END
 

hippy

Ex-Staff (retired)
The code currently appears to use the DAC output level to compare against. With reference to figures 18-2 and 18-3 in the 16F1827 datasheet, the analogue multiplexor which feeds the DAC signal to the comparator needs to be changed so the signal from C12IN+ is used instead. This happens to be leg 1 of the PICAXE-18M2.

That would seem to involve changing the CxPCH<1:0> bit settings in the CM1CON1 or CM2COM1 register. So, without having tried it, changing -

SYMBOL CM1CON1_data = %00010000
SYMBOL CM2CON1_data = %00010000

to

SYMBOL CM1CON1_data = %00110000
SYMBOL CM2CON1_data = %00000000

Would seem to be what's required.

Added : Oops. Note CxPCH<1:0> bit settings are different depending on which comparator is used. Corrected the above.
 
Last edited:

DNN

New Member
The code currently appears to use the DAC output level to compare against. With reference to figures 18-2 and 18-3 in the 16F1827 datasheet, the analogue multiplexor which feeds the DAC signal to the comparator needs to be changed so the signal from C12IN+ is used instead. This happens to be leg 1 of the PICAXE-18M2.

That would seem to involve changing the CxPCH<1:0> bit settings in the CM1CON1 or CM2COM1 register. So, without having tried it, changing -

SYMBOL CM1CON1_data = %00010000
SYMBOL CM2CON1_data = %00010000

to

SYMBOL CM1CON1_data = %00110000
SYMBOL CM2CON1_data = %00000000

Would seem to be what's required.

Added : Oops. Note CxPCH<1:0> bit settings are different depending on which comparator is used. Corrected the above.
Thanks It's working now, also I understand it a bit more.

I'll post project when fully completed.
What i.m making is an Electronic Fuel Injection Enhancer, this is to adjust my O2 sensor on my car which have fitted a Hydrogen cell to.

DNN
 
Top