Using on-board comparators

Discussion in 'Embedded Systems and Microcontrollers' started by mmendes, Nov 15, 2012.

  1. mmendes

    Thread Starter New Member

    Nov 15, 2012
    3
    0
    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.

    Code ( (Unknown Language)):
    1. define OSC 8
    2. OSCCON.4=1
    3. OSCCON.5=1
    4. OSCCON.6=1
    5.  
    6. ANSEL = %00000001 'setting 1 pin to analog
    7.  
    8. TRISA = %00000001  
    9. TRISB = %11111111 'defining inputs and outputs for the pic
    10.  
    11. CMCON = %00000110 'setting the comparator layout (2 multiplexed)
    12. CVRCON = %10000011 ' setting internal Vref to 1.72 volts
    13.  pause 1000
    14.  
    15. LEADLOOP
    16.             if portb.0 == 1 then  portb.0 = 1; //Showing by clearing LED at RB0
    17.             if portb.0 == 0 then portb.0 = 0; //Showing by lighting LED at RB0
    18.                
    19.  
    20. goto leadloop
    21. end
     
  2. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    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.
     
  3. mmendes

    Thread Starter New Member

    Nov 15, 2012
    3
    0
    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?
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605

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

    Thread Starter New Member

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