3 OPAMP Configuration Code
Just a few lines of code are required to configure the OPAMP peripheral to implement the
constant-current
driver:
// Set up the timebase of the OPAMP peripheral
OPAMP.TIMEBASE = 3; // Number of CLK_PER cycles that equal one us, minus one (4-1=3)
//Connect OP0 to wiper and pin connected to sense resistor
OPAMP.OP0INMUX = OPAMP_OP0INMUX_MUXPOS_WIP_gc | OPAMP_OP0INMUX_MUXNEG_INN_gc;
//Connect resistor ladder to VDD and ground so the wiper functions as a voltage divider
OPAMP.OP0RESMUX = OPAMP_OP0RESMUX_MUXBOT_GND_gc | OPAMP_OP0RESMUX_MUXWIP_WIP7_gc | OPAMP_OP0RESMUX_MUXTOP_VDD_gc;
// Configure OP0 Control A
OPAMP.OP0CTRLA = OPAMP_OP0CTRLA_OUTMODE_NORMAL_gc | OPAMP_ALWAYSON_bm;
// Enable the OPAMP peripheral
OPAMP.CTRLA = OPAMP_ENABLE_bm;
After this code has been executed, the op amp stays active, and no further CPU intervention is required.