1st time COMPARATOR use...
+ Reply to Thread
Results 1 to 19 of 19
  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french region)
    Posts
    755

    Default 1st time COMPARATOR use...

    Hello,

    I'm trying to use a comparator with my 12F675 for the first time.

    This is a simple battery low-level indicator project and I have reduced the schema to its minimum. The LED should light-up when the battery voltage drops lower than 6,5V.
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1698&stc=1&d=118080488 0">

    I've found lots of information around the forum and the web but, I'm still stuck afters hours of testing...

    Here is my code:
    Code:
    ' Fuses
    @ DEVICE PIC12F675,INTRC_OSC_NOCLKOUT ;Internal oscillator 4MHz
    @ DEVICE PIC12F675,WDT_OFF
    @ DEVICE PIC12F675,PWRT_ON
    @ DEVICE PIC12F675,MCLR_OFF
    @ DEVICE PIC12F675,BOD_OFF
    @ DEVICE PIC12F675,PROTECT_OFF
    @ DEVICE PIC12F675,CPD_OFF
    
    '-------------------------------------------------------------------------------
    ' Registers    76543210
    OSCCAL      = %10000000 'Center frequency - used with Internal 4MHz OSC
    TRISIO      = %00000001 'Set Input/Output
    GPIO        = %00000000 'Ports High/Low
    WPU         = %00000000 'Weak pull-ups (check OPTION_REG)
    OPTION_REG  = %10000000 'Pull-ups / Interrupt / Prescaler
    ANSEL       = %00000001 'Analog I/O
    CMCON       = %01000010 'Comparator Module settings
    VRCON       = %10101100 'Voltage reference (= 2.5V)
    
    '-------------------------------------------------------------------------------
    ' Variables
    BatVal   var CMCON.6
    BatLoLED var GPIO.2
    
    '-----------------------------------------------------------------------------
    ' Program
    Pause 1 'Delay to allow for VREF to settle
    MAIN: 
        BatLoLED = BatVal
        Goto Main
    End
    I have to admit, I don't clearly understand all the comparator modes.

    But I hope to get it working so I can start doing experiences with all different modes... and learn.
    Attached Images Attached Images  
    Roger

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    Hi Roger,

    With a voltage divider of 22k and 10k, when the batt voltage is 6.5, the divider output will be 2.031 volts.

    But the VRCON setting is for 2.5V. So it would switch at a batt voltage of 8 volts instead of 6.5

    If VRCON were %10101010 it would give a reference of 2.08 V, and the comparator would switch at 6.66V.
    <hr>
    For the comparator you can use this configuration...

    <img src=http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1700&stc=1&d=118082173 6>

    With CIS set to 1 it puts the GP0 input on the "-" input of the comparator.
    Then as long as the batt is greater than 6.66V, the comparator output will be 0

    In that case, CMCON should be %00001110.

    HTH,

    PS: Do you play Half-Life? Roger(SWE)?
    <br>
    Attached Images Attached Images  
    DT

  3. #3
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french region)
    Posts
    755

    Default

    Hi Darrel,

    My battery delivers 9V so according to your post, the VRCON setting should be correct.

    I'm going to try the "110" Comparator mode; this, the functionality you explain, is exactly what I would like it to do.

    Darrel, would you know a tutorial about the use of Comparator? I'm getting lost more and more...

    Thank you.

    NB: every day, I play a full life; (SWItzerland - green circle - see attached map)
    Attached Images Attached Images  
    Roger

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    Yup, big difference between Sweden and switzerland. Doh!

    would you know a tutorial about the use of Comparator?
    Not really, but maybe this will help. From Microchip...

    PICmicro Comparator Tips n Tricks
    http://ww1.microchip.com/downloads/e...Doc/41215a.pdf

    Or, it might just confuse things even more
    <br>
    DT

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    Quote Originally Posted by flotulopex View Post
    My battery delivers 9V so according to your post, the VRCON setting should be correct.
    I've looked that statement over several times, and I'm still not sure what you meant.

    Your original VRCON should be correct?
    -- or--
    The VRCON according to my post should be correct?

    So, in case there's still some confusion, here's something that might help to understand the voltage divider part.
    It's already set up with your divider and 6.5v input...

    PIC Voltage Divider Calculator
    http://www.pbpgroup.com/modules/wfse...5&15,22000,6.5

    regards,
    DT

  6. #6
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517

    Smile

    Quote Originally Posted by flotulopex View Post
    would you know a tutorial about the use of Comparator? I'm getting lost more and more...
    See Melanie's post here
    http://list.picbasic.com/forum/messages/3064/3607.html?
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  7. #7
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french region)
    Posts
    755

    Default Even more basics...

    Yes Paul, I read Melanie's tutorial. At my level of comprehension, it is more like an example using a Comparator module in a 16F628.

    Yes Darrel, I read the MICROCHIP's Comparator Tips & Tricks document.

    The problem is, I don't clearly understand all the different pin/port/register names described in the datasheet. If some of them are obvious to me (VRef, A, D, etc..), others are not (CINV, VIN, CIN, etc..).

    Sorry if this sounds stupid and looked easy as a conventional ampli-op before I started, but all the C-specific terminology messes my mind up.

    Just an example; when I read the 12F765's datasheet Comparator Operating Modes, I get lost at the very first figure....
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1703&stc=1&d=118095092 1">
    Here, I assume I have to set ports GPIO.0 & GPIO.1 to Analog (ANSEL register); is this right? But what is with the Output? Is it going to turn to an "1" when CIN+ > CIN-? How will I use it?

    And now, is it just the same as before but when the condition is CIN+ > CIN-, does COUT (bit) go to a "1"?
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1704&stc=1&d=118095155 5">

    What are CINV, VIN, CIN and what are they doing exactly?

    It's confusing me.
    Attached Images Attached Images   
    Roger

  8. #8
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french region)
    Posts
    755

    Default Got it working!

    Okay, after some more trials and a correct connection to GP1 (... not GP0!!!), I got it working.

    BUT how do I improve the accuracy?

    When the measured voltage ist between 2,3V and 2,6V, my led flickers.

    How do I fix this?

    Another strange thing: if I "forget" to set the GP1 input as Analog (ANSEL), it still works(!?).

    BTW, here are my corrected schema and code:
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1708&stc=1&d=118097654 5">

    Code:
    ' Fuses
    @ DEVICE PIC12F675,INTRC_OSC_NOCLKOUT ;Internal oscillator 4MHz
    @ DEVICE PIC12F675,WDT_OFF
    @ DEVICE PIC12F675,PWRT_ON
    @ DEVICE PIC12F675,MCLR_OFF
    @ DEVICE PIC12F675,BOD_OFF
    @ DEVICE PIC12F675,PROTECT_OFF
    @ DEVICE PIC12F675,CPD_OFF
    
    '-------------------------------------------------------------------------------
    ' Registers    76543210
    OSCCAL      = %10000000 'Center frequency - used with Internal 4MHz OSC
    TRISIO      = %00000010 'Set Input/Output
    GPIO        = %00000000 'Ports High/Low
    WPU         = %00000000 'Weak pull-ups (check OPTION_REG)
    OPTION_REG  = %10000000 'Pull-ups / Interrupt / Prescaler
    ANSEL       = %00000010 'Analog I/O
    CMCON       = %01000100 'Comparator Module settings
    VRCON       = %10101100 'Voltage reference (= 2.5V)
    
    '-------------------------------------------------------------------------------
    ' Variables
    BatVal   var CMCON.6
    BatLoLED var GPIO.2
    
    '-----------------------------------------------------------------------------
    ' Program
    Pause 1 'Delay to allow for VREF to settle
    MAIN: 
        BatLoLED = BatVal
        pause 10
        Goto Main
    End
    Attached Images Attached Images  
    Roger

  9. #9
    Join Date
    Feb 2013
    Posts
    482

    Default Re: Got it working!

    Is it possible to configure comparator to do something, without sacrificing main code and cpu time? I mean, to run it in background, without using adcin or other code in loop. Say, only enable hpwm if comparator input is lower than 2 volts.

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

    Default Re: 1st time COMPARATOR use...

    Might be, on a chip with CLC module(s).
    But with a little bit of code it can be done either by polling the comparator output in the main loop or using a comparator interrupt.

    /Henrik.

  11. #11
    Join Date
    Feb 2013
    Posts
    482

    Default Re: 1st time COMPARATOR use...

    Just my code has to multiplex many things, and I want as fast response as possible. How fast is comparator is generally? I don't like 50us wasted on ADC sampling time....

  12. #12
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,231

    Default Re: 1st time COMPARATOR use...

    On the 16F1789 (which happened to be what I was looking at just now) the typical response time is listed as 50ns but it may vary depending on the device you choose.

    The 16F1507 have both a CLC module and CWG module. The CWG module has hardware shutdown inputs(s) which will disable the output without software overhead. That shutdown input can come from the CLC modul. The 16F1507 doesn't have comparators so it obviously won't work for you but I suggest you find a PIC with comparators and CLC module and have a look at the datasheet.

  13. #13
    Join Date
    Feb 2013
    Posts
    482

    Default Re: 1st time COMPARATOR use...

    Thanks, so I understand, that using "standard" MCU, like 16F886, it won't be possible to do what I need in pure hardware way, right?

  14. #14
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,231

    Default Re: 1st time COMPARATOR use...

    Well, I just opened the datasheet for '886 (again, I suggest you do as well) and as it turns out one of the headlines in the comparator section happens to be PWM Shutdown. When using the PWM module in enhanced mode the output of one of the comparators can be used to shut down the PWM.

  15. #15
    Join Date
    Feb 2013
    Posts
    482

    Default Re: 1st time COMPARATOR use...

    Thanks, I checked in detail, and "PWM shutdown" in datasheet is mentioned only once, no further explanation given

  16. #16
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,231

    Default Re: 1st time COMPARATOR use...

    Seriously? You just have to look harder then.

  17. #17
    Join Date
    Feb 2013
    Posts
    482

    Default Re: 1st time COMPARATOR use...

    Absolutely. Page 85, no more.

  18. #18
    Join Date
    May 2013
    Location
    australia
    Posts
    1,661

    Default Re: 1st time COMPARATOR use...

    This is more entertaining than Free to Air TV

  19. #19
    Join Date
    Feb 2013
    Posts
    482

    Default Re: 1st time COMPARATOR use...

    haha found
    I was searching for "pwm auto shutdown", but later in datasheet it is "pwm auto-shutdown"
    so ctrl+f was not helping

Similar Threads

  1. I don't understand this code!
    By Russ Kincaid in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 13th February 2008, 02:55
  2. Measuring time
    By AugustoPedrone in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 30th July 2007, 23:46
  3. Serout2/serin2 Pbp Problem
    By SOMRU in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 11th December 2006, 19:55
  4. Alarm Time
    By Santana in forum Code Examples
    Replies: 1
    Last Post: - 8th December 2006, 13:58
  5. Timer in real time
    By martarse in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 29th July 2005, 14:24

Members who have read this thread : 20

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