PIC12F629 Comparator with delay

Discussion in 'Embedded Systems and Microcontrollers' started by Twitch, Jun 14, 2010.

  1. Twitch

    Thread Starter New Member

    Jun 14, 2010
    3
    0
    Hi, I am new to PIC programming, only started a couple of days ago. I am currently using the following comparator code:

    What I want do is to add a 2 second delay before the LED is ON and before the LED is OFF

    In some pseudo-code:

    If CIN+ < CIN- is true for a minimum of 2 seconds
    Turn LED ON
    Else, do nothing

    If CIN+ > CIN- is true for a minimum of 2 seconds
    Turn OFF LED
    Else, do nothing

    So the LED will only turn on if CIN+ < CIN- is true for more than 2 seconds. Once the LED is ON, if CIN+ > CIN- for a minimum of 2 seconds, it will turn the LED OFF.

    Thanks for the help, I'm a newbie at this.
     
  2. ELECTRONERD

    Senior Member

    May 26, 2009
    1,146
    16
    I'm not sure if your compiler will accept this delay command, but try it:

    Code ( (Unknown Language)):
    1. Delay10KTCYx(Delay);
    You'll have to include the header file "delays.h".
     
  3. Twitch

    Thread Starter New Member

    Jun 14, 2010
    3
    0
    Does this line mean "Wait a certain time" before continuing or does it mean " check if CIN+ < CIN- is true for a certain time before continuing"?

    Sorry for my ignorance, I am new at this.
     
  4. ELECTRONERD

    Senior Member

    May 26, 2009
    1,146
    16
    Yes. It tells the processor to burn up clock cycles for however long you choose, or simply a delay. Your latter statement doesn't make sense if you think about it. Microcontrollers (uC) are very fast when it comes to processing, so it would only take a very short duration for your uC to test if that statement is true. Why have it test slower?

    Don't worry about it, we all had to start from the ground up. Just keep working on it and you'll get better. ;)
     
  5. eng1ne

    Member

    Dec 4, 2009
    97
    3
    Which compiler are you using?

    A delay is probably your best solution as Electronerd says. Bear in mind however, that if you check is A < B at time 0, then check again at t = 2s, you can't guarantee that A < B for the whole 2s.

    A < B might begin at t = -0.1s and end at t = 0.1s, then might begin again at t = 1.9s and end at t = 2.1s; thus you have a false reading.

    Are you trying to debounce a switch?

    Like Electronerd says, you can read much faster than every 2 seconds.
     
  6. Twitch

    Thread Starter New Member

    Jun 14, 2010
    3
    0
    I am using the HI-TECH compiler. Yes, I am trying to debounce a switch

    There is a code in the tutorial:
    But I do not know how to put it into my current code. My CIN- is set at 1.5V, the CIN+ will go to 0.00V then the LED turns on.

    The reason for the "delay" prevent the LED from turning on and off quickly if the CIN+ suddenly flickers above 1.5V, it doesn't happen all the time, it's just a preventive measure. The the CIN+ must be lower than CIN- for more than 2 seconds before the LEd turns on
     
Loading...