Use of Comparators in the PICAXE-18M2

kranenborg

Senior Member
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
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
 

PhilHornby

Senior Member
This response could be considered a little late, but #Picaxe18M2 should read #Picaxe 18M2 🙂

Also,
It is highly recommended to review the PIC16F1827 datasheet:
Also, the PICAXE website says that the Base PIC for the Picaxe18M2 is the PIC16(L)F1847, not the 1827...

I'm assuming it wasn't a typo and something changed with the 18M2 over the years?
 
Last edited:

kranenborg

Senior Member
It has been a while indeed ...
Could it be that the shortlived 18M2 (that I used for this story) was the 27, and the quick successor 18M2+ the 47 variant?
/Jurjen
 

hippy

Ex-Staff (retired)
Thanks to eclectic for finding the definitive document - The original 18M2 used 16F1827, 18M2+ uses the enhanced 16F1847 which wasn't available earlier., and that 18M2+ is what we generally mean when we now talk of "18M2", what one gets if one orders an 18M2.

As to "#picaxe18m2" versus "#picaxe 18m2", with a required space, I believe that was a change which came with later compilers or perhaps PE6. It probably worked with PE5 but not with PE6.
 

PhilHornby

Senior Member
As to "#picaxe18m2" versus "#picaxe 18m2"... It probably worked with PE5 but not with PE6.
Yes, having just tried it, you're right.

I assume I'm flogging a dead horse, in trying to use the C2 comparator output (C2OUT), directly on the 18M2 - since it shares a pin with SERIN ❓

I've tried setting RA4/C.4/SERIN to be an output (by writing to TRISA), but it seems to be overwritten by the Picaxe firmware. I know C2 is working, because I can get it set the SR Latch - but SRQ shares a pin with C1OUT, which I also want to use.
 

hippy

Ex-Staff (retired)
I assume I'm flogging a dead horse, in trying to use the C2 comparator output (C2OUT), directly on the 18M2 - since it shares a pin with SERIN
I would guess so and for the reasons suggested; that the firmware is undoing whatever you attempt to do.

CP2OUT is hard-wired to RA4 on the chip, which is SERIN for PICAXE, and there doesn't appear to be an alternative pin it can be moved to. Figure 19-2 in my copy of the datasheet, the comparator block diagram, shows CPxOUT enabled by the TRIS bit, which is likely reset to input at the end of every PICAXE instruction.

It might be possible to get a very brief output when poking the TRIS bit to be output but not easily usable.

The only suggestion I have, which perhaps isn't great, is to move to a different PICAXE which can better handle what you want, with an adapter board if you need the 18M2 footprint.

I recall it may have been the limited options imposed by silicon on the M2 chips which meant comparator commands weren't that useful so not included.
 

PhilHornby

Senior Member
The only suggestion I have, which perhaps isn't great, is to move to a different PICAXE which can better handle what you want
I've been slowly working my way through the "M2" range. The 08M2 only has one comparator, 14M2 & 20M2 overlay C2OUT with MDOUT which I also want to use. The 18M2 looked like a possibility until I ran into the SERIN issue :(

Looks like a 20X2 then, (or the mythical 16M2 - i.e. 2 x 08m2 in one socket :whistle: )
 
Top