Use of Comparators in the PICAXE-18M2

kranenborg

Senior Member
#1
PICAXE-18M2 and Comparators

Hello,

The PICAXE-18M2 (and possibly also the other M2 variants of the PICAXE) has two independent comparators which, although not directly supported by the COMPSETUP command like with the X2 variants, nevertheless can be configured with the same flexibility through direct POKESFR and PEEKSFR register operations.

Together with the DAC and FVR functionality of the M2 series a huge range of analog signal processing capabilities becomes available, since both comparators can have the programmable DAC or the FVR (Fixed Voltage Reference) as (+) input. Since the DAC range (32 steps) can be programmed in various ways itself (for example using the FVR which itself has three programmable output levels too), a large number of very useful configurations for analog signal processing is possible, like with the X2 variants.

Note that the comparator inputs can be programmed (via multiplexing) to be coupled with several Picaxe input pins or with Vcc/GND. Furthermore, it is also possible to have the comparator outputs directly available on two picaxe pins, allowing the programmable comparators to be used by the application hardware without any PICAXE intervention.

It is highly recommended to review the PIC16F1827 datasheet:
- Chapter 18 on Comparator functionality
- Chapter 16 on DAC functionality
- Chapter 14 on programmable FVR functionality
- The pin diagram of the PIC16F1827 for the various possibilities
for connecting the comparator inputs and outputs to the Picaxe legs
The DAC and FVR are directly supported by the 18M2 through dedicated BASIC commands.

A simple tested test code application is included below and as an attachment in this post. The test code contains all SYMBOL definitions for programming the comparators in various applications. An example app that could be built with the Comparator/DAC/FVR combination is a programmable noise canceler for pulse measurements/counting.

The 18M2 appears to be a worthy successor to the venerable 18X in terms of extra features available by looking "under the hood" via direct SFR programming!


Test code:

Code:
*********************************************
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
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 15

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
 

Attachments

Last edited:

DNN

New Member
#2
Hi

I'm new to picaxe, but found your article very interesting. Thanks.
Iv'e got your code up and running but I would like to compair one input with a reference input on another pin.
I can't get this to work any chance you could help.
Thanks
 
Top