- See examples/differential_amplifier
int output = analogDiffRead(A0, A1, GAIN_8);
// output = (A1 - A0) * 8
int invalid_output = analogDiffRead(A0, A5, GAIN_8);
// invalid_output = -1
- Available pin combinations
-\+ | A0 | A1 | A2 | A3 | A4 | A5 | A6 | A7 | A8 | A9 |
---|---|---|---|---|---|---|---|---|---|---|
A0 | - | ✓ | ||||||||
A1 | ✓ | - | ||||||||
A2 | ✓ | ✓ | - | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
A3 | ✓ | ✓ | ✓ | - | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
A4 | ✓ | ✓ | - | |||||||
A5 | ✓ | ✓ | - | |||||||
A6 | ✓ | ✓ | - | |||||||
A7 | ✓ | ✓ | - | |||||||
A8 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | - | ✓ |
A9 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | - |
Also see the examples in the differential_amplifier
library provided with this core.
The differential amplifier (DAP) consists of
inverting
input (-)non_inverting
input (+)gain
valueoutput
.
where
output = gain * (non_inverting - inverting)
And the output can be read through the ADC (like a normal analog read).
Both inputs and output must be between gnd and vcc.
All information was extracted from this translation of the datasheet: (English) LGT8FX8P_databook_V1.04 (thanks to metallurge for the translated doc here)
-
Setup the inputs and gain and turn DAO on through the DAPRC (Differential amplifier control register):
DAPCR = bit(DAPEN) | gain | inverting | noninverting;
-
Configure the ADC input to use the Differential amplifier instead of reading directly from the analog pins
ADCSRC |= DIFS_DIFFAMP;
-
Setup the multiplexer if necessary and make the reading
int res = analogRead(inverting == INVERTING_MXER ? pin1 : pin2);
- For this, I'm just reusing the standard
analogRead(pin)
function which also configures the multiplexer. - Note: for some pin combinations it is not necessary to use the multiplexer, but for the sake of simplicity I still use the normal
analogRead(pin)
function. In this case it doesn't matter which pin is passed to the function, as the DAO won't use the output of the multiplexer.
- For this, I'm just reusing the standard
-
Cleanup:
-
Turn DAO off:
DAPCR &= ~bit(DAPEN);
-
Undo step (3) (reconnect ADC to multiplexer for future normal analogRead calls)
ADCSRC &= ~DIFS_DIFFAMP;
-