PDA

View Full Version : Comparator not tripping above 0.6V (Vref) on PIC 16F690



flotulopex
- 22nd May 2020, 22:41
Hi All,

I'm struggeling to get my 16F690's comparator 2 output to trip when the measured voltage gets above 0.6V (for battery level monitoring).


' ====== FUSES ================================================== ===================================
@ __config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_INTRC_OSC_NOCLKOUT &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF

' ====== REGISTERS ================================================== ===============================
' Registers 76543210
OPTION_REG = %10000000 ' PORT A&B Pull-Ups and Prescaler (def.: %11111111)
OSCCON = %01100000 ' Internal Oscillator 4MHz (def.: %01101000)
TRISC = %00001000 ' Set Input/Output (0 to 7) (def.: %11111111)

CM2CON0 = %10100011 ' Comparator2 Module (def.: %00000000)
' Bit1/C2ON = Comp C2 is ON
' Bit5/C2OE = Output is PORTC.4
' Bits1-0 = Input is PORTC.3

VRCON = %00110000
' Bit2/C2VREN = Comparator C2 voltage ref is ON
' Bit4/VP6EN = 0.6V ref is ON

' ====== DEFINES ================================================== =================================
DEFINE OSC 4

' ====== PROGRAM START ================================================== ===========================
MAIN:
GOTO MAIN:
END



These are the registers:

8874
8875


And this is my PIC:
8873

My voltage divider (that comes from Vbatt) is connected to PORTC.3 and I'm expecting the PORTC.4 (C2 output pin) to trip when Vbatt exceeds 0.6Volts, but this nevers happens.

What am I missing please?

richard
- 23rd May 2020, 03:09
this works if you mean "trip pin" means pin is set when below 0.6v and cleared when over



#CONFIG
__config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOD_ON & _IESO_ON & _FCMEN_ON
#ENDCONFIG

' ====== REGISTERS ================================================== ===============================
' Registers 76543210
OPTION_REG = %10000000 ' PORT A&B Pull-Ups and Prescaler (def.: %11111111)
OSCCON = %01100000 ' Internal Oscillator 4MHz (def.: %01101000)
TRISC = %00001000 ' Set Input/Output (0 to 7) (def.: %11111111)
led var portc.0 ; proof of life
CM2CON0 = %10100111 ' Comparator2 Module (def.: %00000000)
' Bit1/C2ON = Comp C2 is ON
' Bit5/C2OE = Output is PORTC.4
' Bits1-0 = Input is PORTC.3
;bit2 connect c2+ to fvr output
ansel.4=0 "just to prove c2+ is not connected to this
VRCON = %00110000
' Bit2/C2VREN = Comparator C2 voltage ref is ON
' Bit4/VP6EN = 0.6V ref is ON

' ====== DEFINES ================================================== =================================
DEFINE OSC 4

' ====== PROGRAM START ================================================== ===========================
MAIN:
toggle led ; proof of life
pause 1000
GOTO MAIN:
END

flotulopex
- 23rd May 2020, 07:24
Thanks Richards,

Sorry, maybe the word "trip" is not adequate (I'm speaking french) :o

BTW, what would be the correct word: "latching"?

I found this interresting video which made me start to try battery monitoring:

Microchip Minutes - Episode 6 - Basic Comparator Setup (https://www.youtube.com/watch?v=9uM0xPcnHCI).

Unlike the video, I started with a 2 resistors divider. But a potentiometer (maybe easier for a start) and a corrected register CM2CON0 as you suggested made it work.

So, I missed this one you kindly pointed me to:
8878

Finally, the "shortest possible" working code for a 0.6Volt threshold detection could be:

TRISC = %00001000 ' Comparator's input port

CM2CON0 = %10100111
' Bit7 = comparator 2 is ON
' Bit5 = comparator's output pin (i.e. LED) is PORTC.4 (for "proof of life")
' Bit2 = connect Vref to C2Vin+
' Bits1:0 = comparator's input pin (i.e. Vbatt) is PORTC.3

richard
- 23rd May 2020, 08:14
Sorry, maybe the word "trip" is not adequate (I'm speaking french)


BTW, what would be the correct word: "latching"?


the only problem with it is that the comparator state can be programmed to be high or low on either side of the transition point
the word trip conveys no indication of preference