Integrator and Comparator

Thread Starter

espire

Joined Dec 18, 2007
24
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.
 

beenthere

Joined Apr 20, 2004
15,819
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.
 

Thread Starter

espire

Joined Dec 18, 2007
24
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
 

beenthere

Joined Apr 20, 2004
15,819
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.
 

Thread Starter

espire

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

Tks
 

Thread Starter

espire

Joined Dec 18, 2007
24
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
 

beenthere

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

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

Thread Starter

espire

Joined Dec 18, 2007
24
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..
 

bloguetronica

Joined Apr 27, 2007
1,544
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
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:
Rich (BB code):
double Comparator (double v_plus, double v_minus, double v_prail, double v_nrail)
{
     constant GAIN = 10000;
     double v_out;
     v_out = GAIN * (v_plus - v_minus);
     return v_out < v_nrail ? v_nrail : vout > v_prail : v_prail : v_out;
}
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.
 

Thread Starter

espire

Joined Dec 18, 2007
24
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:
Rich (BB code):
double Comparator (double v_plus, double v_minus, double v_prail, double v_nrail)
{
     constant GAIN = 10000;
     double v_out;
     v_out = GAIN * (v_plus - v_minus);
     return v_out < v_nrail ? v_nrail : vout > v_prail : v_prail : v_out;
}
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.

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

Thread Starter

espire

Joined Dec 18, 2007
24
I am confused - why should the uC function as an op amp? By the way, which uC?

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