Integrator and Comparator

Discussion in 'Programmer's Corner' started by espire, Dec 19, 2007.

  1. espire

    Thread Starter Member

    Dec 18, 2007
    24
    0
    Hi All,

    I been trying to write some codes to use the uC to work as a integrator and comparator respectively.But has not been successful, has anyone done it before?

    Tks in advance.
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    If you can digitize two voltages, doing a comparator function doesn't seem too difficult.

    Using an external resistor and capacitor (and a controlled switch to dump charge off the capacitor) will let you use the A to D to take in the change of voltage on the external cap over time. just keep your measurement intervals accurate.
     
  3. espire

    Thread Starter Member

    Dec 18, 2007
    24
    0
    I am taking in 2 varying voltages from 2 separate voltage divider and input into 2 separate AD channel of the uC. In order for it to function like a comparator.
    Roughly how should the algorithm be like.

    Tks
     
  4. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    If the value out of one A to D conversion is greater than the other, then that voltage is higher. There may be a COMPARE instruction that will set a flag in the status register to indicate the outcome.
     
  5. espire

    Thread Starter Member

    Dec 18, 2007
    24
    0
    Roger that.
    How about making the uC to perform an integration on the varying voltage applied to one of the AD channel.

    Tks
     
  6. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    See my first reply.
     
  7. espire

    Thread Starter Member

    Dec 18, 2007
    24
    0
    Can I not use any external resistors and capacitors?
    I wonder if I use a loop program to monitor the changing voltage via the ADC and add them up over time will work?

    Tks
     
  8. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    You would need to use an external resistor and capacitor.

    That is the way to program the processor to monitor the integration.
     
  9. espire

    Thread Starter Member

    Dec 18, 2007
    24
    0
    Okie,
    I hope you wouldnt mind if you could provide a sample code for the uC to function as an op-amp.
    I am still quite loss.

    Many Thanks..
     
  10. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    I am confused - why should the uC function as an op amp? By the way, which uC?
     
  11. cumesoftware

    Senior Member

    Apr 27, 2007
    1,330
    10
    It is very difficult to reproduce a comparator in programming. Computers live in an ideal world while true comparators live in a physical world. You should allow an error when comparing two voltages. A reasonable algorithm to reproduce the behavior of an ideal op-amp being used as a comparator should be something like:
    Code ( (Unknown Language)):
    1.  
    2. double Comparator (double v_plus, double v_minus, double v_prail, double v_nrail)
    3. {
    4.      constant GAIN = 10000;
    5.      double v_out;
    6.      v_out = GAIN * (v_plus - v_minus);
    7.      return v_out < v_nrail ? v_nrail : vout > v_prail : v_prail : v_out;
    8. }
    9.  
    Being:
    v_prail - Positive rail voltage;
    v_nrail - Negative rail voltage;
    v_plus - Plus input voltage;
    v_minus - Minus input voltage;
    v_out - Output voltage.
     
  12. espire

    Thread Starter Member

    Dec 18, 2007
    24
    0

    Thanks alot...
    How do you derive the gain>?
     
  13. espire

    Thread Starter Member

    Dec 18, 2007
    24
    0

    I am using the PIC 16F877,
    Trying out the one cycle control, basically its make up of different op-amps blocks.
    Was wondering if the uC can perform the task as well using programming.

    Tks.
     
Loading...