Using on-board comparators

Thread Starter

mmendes

Joined Nov 15, 2012
3
Hi, got a problem hopefully someone can answer for me. (im a noob)

Im trying to use the on-board comparator function of the PIC16f88 and a 3 axis accelerometer (https://www.sparkfun.com/products/9652?) on a project. I am trying to use the comparator function to effectively turn my analog accelerometer into a "switch." the end goal is to have the pic ouput a signal to another pic when the accelerometer is activated a certain amount. For instance you shake the accelerometer in the XY plane and it sets pin 9 high, for example.

Ive got some code that hopefully will give some insight on what im trying to do. the code is assuming I have to take the ouput of CM1 and attach it to another pic for the logic to work. CM1 ouputs on RA3, that is then wired into the portb.0 im using in the code.

Now my question is whether or not the PIC recognizes the value of the comparator output internally or do I have to connect the comparator output to another pin on the pic for the logic to work? Im trying to avoid this so I have more pins to use for other things. also, if anybody has a better idea on how to turn my analog accelerometer into a "switch" for the pic Im obviously open to suggestions.

Rich (BB code):
define OSC 8
OSCCON.4=1
OSCCON.5=1
OSCCON.6=1

ANSEL = %00000001 'setting 1 pin to analog

TRISA = %00000001 	
TRISB = %11111111 'defining inputs and outputs for the pic
 
CMCON = %00000110 'setting the comparator layout (2 multiplexed)
CVRCON = %10000011 ' setting internal Vref to 1.72 volts
 pause 1000
 
LEADLOOP 
            if portb.0 == 1 then  portb.0 = 1; //Showing by clearing LED at RB0
            if portb.0 == 0 then portb.0 = 0; //Showing by lighting LED at RB0
               

goto leadloop
end
 

John P

Joined Oct 14, 2008
2,026
It's all in the data manual:

13.5 Comparator Outputs
The comparator outputs are read through the CMCON
register. These bits are read-only. The comparator
outputs may also be directly output to the RA3 and RA4
I/O pins. When enabled, multiplexors in the output path
of the RA3 and RA4 pins will switch and the output of
each pin will be the unsynchronized output of the comparator.
 

Thread Starter

mmendes

Joined Nov 15, 2012
3
Yes, thank you, ive read all that about 5 times already. Are the CM1, CM2 ouputs readable without outputting to a different pin on the same pic to use in a logic loop. Ie: can I have a loop with

if CM1 = 1 then portb.0 = 1

Something like that in order to save pin ouputs. If thats not possible then I have to wire up my pics diffferently. IS there a way to setup th register to behave the way I wrote above?
 

ErnieM

Joined Apr 24, 2011
8,377
Yes, thank you, ive read all that about 5 times already. Are the CM1, CM2 ouputs readable without outputting to a different pin on the same pic to use in a logic loop. Ie: can I have a loop with

if CM1 = 1 then portb.0 = 1

Something like that in order to save pin ouputs. If thats not possible then I have to wire up my pics diffferently. IS there a way to setup th register to behave the way I wrote above?

Yes. Comparator output is internal only, and software directs that to any pin you choose.
 

Thread Starter

mmendes

Joined Nov 15, 2012
3
are there any other registry changes I need to make to get them to work properly, other than the ones I have already made?
 
Top