View Full Version : Analog comparator in action

- 26th September 2015, 15:36
I should like to use both analog comparators on the 16F628A
I would like to compare a voltage (that ranges from 3 to 0 VDC relative to ground) to the internal voltage reference set to 0.5V. If the input voltage is greater than 0.5V, I want a LED go ON, or subrutine to be called.
I read the datasheet, but that just made me more confused. Couldn't find any good examples on how to use the comparator with Pic Basic.
Kindly ask recommendation about how to:
1. How do I set the internal voltage reference to 0.5V with PBP?
2. How do I setup the PIC to get the voltage in with PBP?
3. How do I use the VRCON register to call a subroutine with PBP?
The PIC doesn't need to do anything if the input voltage is below 0.5V.

Have searched forum and found recommended link:


(http://list.picbasic.com/forum/messages/3064/3607.html?)But got a message - This account has been suspended.
Anyone knows how to get data from above link?
Kindly ask for a help!

- 26th September 2015, 18:21
> 1. How do I set the internal voltage reference to 0.5V with PBP?

Depends on what Vdd is.
Look at equations in the datasheet, section 11. If you can't adjust Vdd is may not be possible to set the Reference voltage to exactly 0.5V.
Example, Vdd is 3.3V, using the low range (VRR=1) and setting VR to 4 gives a reference voltage of 4/24*3.3=0.55V, that is as Close as you're going to get with Vdd at 3.3V.

>2. How do I setup the PIC to get the voltage in with PBP?
Don't understand. If you're talking about the comparators inputs (and outputs) you configure them using the CMCON registers. See section 10.1 in the datasheet (http://ww1.microchip.com/downloads/en/DeviceDoc/40044F.pdf).

> 3. How do I use the VRCON register to call a subroutine with PBP?
You don't. VRCON Controls the voltage reference module.
If you want to execture a certain piece of code whenever the comparator output changes state you need to either poll the comparators every so often and "manually" call the subroutine or configure the comparator(s) to trip an interrupt when it changes state.


- 27th September 2015, 11:23
8057Hi Henrik
Thank you very much on your kind reply.
First I am new in PBP programing and PIC use.
Here is what I am trying to work out.
I shold like to measure or detect the rising edge deterioration on square vawe signal.
First compartor is put On by low threshold voltage of 0,5V.The output of first comparator starts counter.
Second comparator is put On by high threshold level of 4,75V where edge deterioration is expected..Ther output of second comparator stop counter.
The counter value detect the level of rising edge deterioration.

First is it idea about edge detecting OK?
Or anyone has better solution?

If idea is OK the Questions are:
How to set threshold level for COM1 and COM2 externaly.Is it enough only to make resistor network (as at OPM amplifiers) or Comparators have to be additionaly adjusted by code?
How to enable bouth comparators in PIC ?
Information about how to set appropriate registers should be of the great help.

- 27th September 2015, 18:05

First – is it idea about edge detecting OK?
I guess the general idea is OK but whether or not a PIC can be used depends on the nominal risetime of the squareware and the resoulution you expect to get out of the measurement. The software overhead involved with using interrupts and starting/stopping the timer will most likely kill it but without any numbers on what a good and bad risetime actually is it's hard to say.

Another option could be to design a window comparator so that its output is HIGH only when the voltage is between 0.5 and 4.75V. The output then drives the TMR1Gate input (on a device that has one of course). This will most likely work "better" than the comparator/software/timer aproach but again it depends on how fast/slow and godd/bad rise time actually is. Are we talking ns or ms?


- 28th September 2015, 09:16
Hi Henrik
thank you very much on your suggestion.
Shall try the way you suggested.

- 28th September 2015, 13:49
Since you neglected to answer my question I'll ask again: What IS the typical value for a "good" risetime?


- 28th September 2015, 16:34
Hi Henrik
have just measured rise time - it is 2,5uSec (0 to 5 V positive edge) - with no distorsion on positive egde.
signal under consideration - Quadrature signal, frequency = 30 KHz.
Thank you.

- 28th September 2015, 17:20
OK, so assuming a completely linear risetime (which most likely isn't the case but anyway) it'll be 2.125us from 0.5V to 4.75V.
If you run the PIC at 20MHz the timer will have a resolution of 200ns so you'll get a reading of around around 10 for a "good" pulse (provided the window comparator is fast enough). If you get a PIC capable of 32MHz, 40MHz or even 64MHz the resolution will of course increase.


- 29th September 2015, 13:49
Hi Henrik
Thank you very much on concise elaboration of the all aspects of this little project.
Shall try to work it out.

By the way - may be you have knowledge where to find Melanie's document she posted on link:
It seems to be about how to use analog comparators on PICs.
Regards , Mike

- 29th September 2015, 16:47
Sorry, I don't know anything about that document and the link just takes me to dead end.

Using the comparators in the PIC is like using any comparator except you "connect" the various inputs and outputs to different things (I/O-pin, internal peripherals etc) using bits in a register. What the bits in those registers do is spelled out in the datasheet, what to set the register TO totally depends on what it is you want to do.

This application note (http://ww1.microchip.com/downloads/en/DeviceDoc/41215C.pdf) from Microchip has a bunch of tips and tricks for using the comparators, perhaps it'll help.


- 10th October 2015, 21:25
looking for that one ???

Making all these decisions and calculations was the hard bit (honest)... the easy bit is the programming...

' Program BatLo
' ============
' Embedded Software Version 1.00
' Target Processor PIC16F628-04/P
' Target PCB BB-001

Created 09/04/02
' Last Updated 09/04/02
' Written by Melanie Newman
' Melanies very complicated Battery Low Indicator

' Device

Programming Options
' --------------------------
' System Clock Options
@ DEVICE pic16F628, WDT_ON

Watchdog Timer
@ DEVICE pic16F628, PWRT_ON
' Power-On Timer
@ DEVICE pic16F628, MCLR_OFF
' Master Clear Options (Internal)

pic16F628, BOD_ON
' Brown-Out Detect
@ DEVICE pic16F628, LVP_OFF
' Low-Voltage Programming
@ DEVICE pic16F628, CPD_OFF
' Data Memory

Code Protect
' Program Code Protection

' Hardware Assignments
' --------------------
BatVal var CMCON.7

' Read Comparator 2 for Battery Low
' Port RA1 has +VBatt Input
BatLoLED var PORTB.7
' Port for Battery Low LED

' Start Program


' Setup Hardware
' --------------
' Comparator Control
' ------------------

Comparator Output 2 (Read Only)
' CM6 C1OUT Comparator Output 1 (Read Only)
' CM5 C2INV Comparator 2 Output Inversion
' the way I'm driving my LED I need

' so C2OUT goes Low when Battery is Low
' CM4 C1INV Comparator 1 Output Inversion
' CM3 Comparator Input Switch
' not relevant for Comparator

Mode 101
' CM2 ) refer to Datasheet figure 9-1
' CM1 ) Mode 101 for Single Comparator C2
' CM0 ) Comparator C1 Disabled
' Voltage Reference Control

' -------------------------
' 7 VREN Voltage Reference Enable (1=on)
' 6 VRON VREF Output Enable
' 1=Connects output of VREF to RA2

' This is real convenient so we'll use it
' 5 VRR VREF Range Selection (1=Low Range)
' 4 Unused (0)
' VR3 )
' VR2 ) Set for Reference Voltage
' VR1

) 1100 sets 2.5v in Low Range
' VR0 )
Pauseus 10
' Delay to allow for VREF to settle
' see datasheet section 11-1
' And the Usual Stuff

TRISA=%11111111 ' Set All Inputs
TRISB=%00000000 ' Set All Outputs

' Actual Program
' --------------

' You better believe it... just ONE line of code!
Goto BatLoLoop



- 12th October 2015, 14:28
Dear Alain
thank you very much on your support