7.3 Register Configuration
The Timebase register for the Analog Signal Conditioning (OPAMP) peripheral has to be configured first. The user must program the equivalent number of clock cycles that amount to 1 μs, so the contents are dependent on the operating CPU clock frequency.
#define OPAMP_TIMEBASE_US (ceil(F_CPU /1e6)-1) OPAMP.TIMEBASE = OPAMP_TIMEBASE_US;
1
’ to the
Input Range Select (IRSEL) bit in the Power Control (PWRCTRL) register. For the basic op
amp applications in the technical brief the power saving option will not be used.OPAMP.PWRCTRL = OPAMP_PWRCTRL_IRSEL_FULL_gc;
For a basic op amp configuration operation, the op amp is configured to be always on. It is assumed the ENABLE/DISABLE events are not used in this scenario to enable/disable the op amp. Similarly, the output driver is enabled by selecting the normal output mode. However, in a different application, the op amp could be enabled or disabled based on a certain event generator (TCA, TCB, TCD, RTC, PORT, CCL, etc.). One area where such functionality could be useful is power saving, where that op amp would be enabled only when needed, rather than having it on from the moment power is applied to the circuitry.
OPAMP.OP0CTRLA = OPAMP_OP0CTRLA_OUTMODE_NORMAL_gc | OPAMP_ALWAYSON_bm;
For a basic op amp operation, the inputs and output of the op amp are connected directly to the pins of the device. The multiplexer settings required to achieve the basic op amp configuration are:
MUXPOS | MUXNEG | MUXBOT | MUXWIP | MUXTOP | |
---|---|---|---|---|---|
OPn | INP | INN | OFF | WIP0 | OFF |
In the case of the basic op amp configuration, the resistor ladder
configuration will remain 0x00
.
OPAMP.OP0RESMUX = OPAMP_OP0RESMUX_MUXBOT_OFF_gc | OPAMP_OP0RESMUX_MUXWIP_WIP0_gc | OPAMP_OP0RESMUX_MUXTOP_OFF_gc;
The negative and positive inputs to the op amp are connected straight to the I/O pins.
OPAMP.OP0INMUX = OPAMP_OP0INMUX_MUXNEG_INN_gc | OPAMP_OP0INMUX_MUXPOS_INP_gc;
The settling time depends on a variety of factors, including the load on the op amp, that
may not be known until the later stages of design and development. If the settling time
is unknown, the maximum value of ‘0x7F
’ (127 microseconds) should be
written to the SETTLE bit field.
OPAMP.CTRLA = OPAMP_ENABLE_bm;
A value of ‘0
’ of the SETTLED bit indicates that the settling time has
elapsed:
while (OPAMP.OP0STATUS & OPAMP_SETTLED_bm) { ; }
Putting it all together, the basic op amp initialization code will look as follows:
void OPAMP0_init (void)
{
/* Configure the Timebase */
OPAMP.TIMEBASE = OPAMP_TIMEBASE_US;
/* Configure the voltage input range */
OPAMP.PWRCTRL = OPAMP_PWRCTRL_IRSEL_FULL_gc;
/* Configure the Op Amp n Control A */
OPAMP.OP0CTRLA = OPAMP_OP0CTRLA_OUTMODE_NORMAL_gc | OPAMP_ALWAYSON_bm;
/* Configure the Op Amp n Input Multiplexer */
OPAMP.OP0INMUX = OPAMP_OP0INMUX_MUXNEG_INN_gc | OPAMP_OP0INMUX_MUXPOS_INP_gc;
/* Configure the Op Amp n Resistor Wiper Multiplexer */
OPAMP.OP0RESMUX = OPAMP_OP0RESMUX_MUXBOT_OFF_gc | OPAMP_OP0RESMUX_MUXWIP_WIP0_gc | OPAMP_OP0RESMUX_MUXTOP_OFF_gc;
/* Configure the Op Amp n Settle Time*/
OPAMP.OP0SETTLE = 0x7F;
/* Enable OPAMP peripheral */
OPAMP.CTRLA = OPAMP_ENABLE_bm;
/* Wait for the operational amplifiers to settle */
while (OPAMP.OP0STATUS & OPAMP_SETTLED_bm)
{
;
}
}