5.2 Using External Resistors
By utilizing external resistors, virtually any voltage level from 1.8V to VDD-1V can be supplied to the positive input of the op amp. With resistors connected as shown in the block diagram below, this equation determines the output voltage of the op amp:
It is necessary to connect the positive input of the OPAMP to its respective pin for connecting the OPAMP to the external resistors. Also, the OPAMP's negative input needs to be connected internally to its output.
The following function will perform the necessary setup:
void op_amp_setup_ext_resistors()
{
OPAMP.OP0INMUX = OPAMP_OP0INMUX_MUXNEG_OUT_gc | OPAMP_OP0INMUX_MUXPOS_INP_gc;
}
The code below will set up the OPAMP to output the voltage supplied to the OPnINP pin.
#define F_CPU 4000000ul #define CLK_PER 4000000ul #define OPAMP_MAX_SETTLE_TIME 0x7F #include <avr/io.h> #include <math.h> void op_amp_setup_ext_resistors(); void op_amp_init(); int main(void) { op_amp_init(); op_amp_setup_ext_resistors(); while (1) { /*Your application goes here*/ } } void op_amp_init() { /*Disable input on op amp output pin*/ PORTD.PIN2CTRL = PORT_ISC_INPUT_DISABLE_gc; /*Set up op amp*/ OPAMP.CTRLA = OPAMP_ENABLE_bm; OPAMP.TIMEBASE = (uint8_t) ceil(CLK_PER*0.000001)-1; /*Number of peripheral clock cycles that amounts to 1us*/ OPAMP.OP1CTRLA = OPAMP_RUNSTBY_bm | OPAMP_ALWAYSON_bm | OPAMP_OP1CTRLA_OUTMODE_NORMAL_gc; OPAMP.OP0SETTLE = OPAMP_MAX_SETTLE_TIME; //As the settle time is unknown, the maximums should be set } void op_amp_setup_ext_resistors() { OPAMP.OP0INMUX = OPAMP_OP0INMUX_MUXNEG_OUT_gc | OPAMP_OP0INMUX_MUXPOS_INP_gc; }
The code for this example is available in the
using-external-resistors folder in these github
repositories: