Analog comparator in action
+ Reply to Thread
Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2015
    Posts
    6

    Post Analog comparator in action

    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!

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,258

    Default Re: Analog comparator in action

    Hi,
    > 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.

    > 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.

    /Henrik.

  3. #3
    Join Date
    Sep 2015
    Posts
    6

    Default Re: Analog comparator in action

    Name:  Comparator in action.jpg
Views: 305
Size:  84.7 KBHi 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.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,258

    Default Re: Analog comparator in action

    Hi,
    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?

    /Henrik.

  5. #5
    Join Date
    Sep 2015
    Posts
    6

    Default Re: Analog comparator in action

    Hi Henrik
    thank you very much on your suggestion.
    Shall try the way you suggested.
    Mike

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,258

    Default Re: Analog comparator in action

    Since you neglected to answer my question I'll ask again: What IS the typical value for a "good" risetime?

    /Henrik.

  7. #7
    Join Date
    Sep 2015
    Posts
    6

    Default Re: Analog comparator in action

    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.
    Mike

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,258

    Default Re: Analog comparator in action

    Hi,
    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.

    /Henrik.

  9. #9
    Join Date
    Sep 2015
    Posts
    6

    Default Re: Analog comparator in action

    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:
    http://list.picbasic.com/forum/messages/3064/3607.html?
    It seems to be about how to use analog comparators on PICs.
    Regards , Mike

  10. #10
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,258

    Default Re: Analog comparator in action

    Hi,
    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 from Microchip has a bunch of tips and tricks for using the comparators, perhaps it'll help.

    /Henrik.

  11. #11
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,541

    Default Re: Analog comparator in action

    looking for that one ???

    Code:
    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 
    ' -------------------------- 
    @ DEVICE pic16F628, INTRC_OSC_NOCLKOUT 
    ' System Clock Options 
    @ DEVICE pic16F628, WDT_ON 
    ' 
    
    Watchdog Timer 
    @ DEVICE pic16F628, PWRT_ON 
    ' Power-On Timer 
    @ DEVICE pic16F628, MCLR_OFF 
    ' Master Clear Options (Internal) 
    @ DEVICE 
    
    pic16F628, BOD_ON 
    ' Brown-Out Detect 
    @ DEVICE pic16F628, LVP_OFF 
    ' Low-Voltage Programming 
    @ DEVICE pic16F628, CPD_OFF 
    ' Data Memory 
    
    Code Protect 
    @ DEVICE pic16F628, PROTECT_OFF 
    ' 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 
    ' ------------------ 
    CMCON=%00100101 
    ' CM7 C2OUT 
    
    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 
    
    inversion 
    ' 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 
    
    
    ' ------------------------- 
    VRCON=%11101100 
    ' 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 
    ' -------------- 
    BatLoLoop: 
    
    
    BatLoLED=BatVal 
    ' You better believe it... just ONE line of code! 
    Goto BatLoLoop 
    
    ' 
    End 
    
    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  12. #12
    Join Date
    Sep 2015
    Posts
    6

    Default Re: Analog comparator in action

    Dear Alain
    thank you very much on your support

Similar Threads

  1. Replies: 1
    Last Post: - 5th September 2015, 18:52
  2. Replies: 2
    Last Post: - 31st May 2013, 18:19
  3. pic18f analog comparator problem
    By david.silaghi in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 6th May 2009, 09:38
  4. Replies: 4
    Last Post: - 5th October 2008, 09:11
  5. Action on line sync
    By hansknec in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 23rd August 2006, 13:42

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts