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.

Figure 7-3. OPAMP.TIMEBASE - Set TIMEBASE
#define OPAMP_TIMEBASE_US     (ceil(F_CPU /1e6)-1)
OPAMP.TIMEBASE = OPAMP_TIMEBASE_US;

In applications where a rail-to-rail input voltage range is not needed, the OPAMP peripheral may be configured to save power, by writing a ‘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.
Figure 7-4. OPAMP.PWRCTRL - Set Input Range
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.

Figure 7-5. OPAMP.OPnCTRLA - Configure the Op Amp Control A
 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:

Table 7-1. Op Amp Connected Directly to Pins
MUXPOSMUXNEGMUXBOTMUXWIPMUXTOP
OPnINP INN OFFWIP0OFF

In the case of the basic op amp configuration, the resistor ladder configuration will remain 0x00.

Figure 7-6. OPAMP.OPnRESMUX - Configure the Resistor Ladder Multiplexer
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.

Figure 7-7. OPAMP.OPnINMUX - Configure the Input Multiplexer
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.

Figure 7-8. OPAMP.OPnSETTLE - Configure the Settle Time
The operational amplifier module is enabled by setting the ENABLE bit in the OPAMP.CTRLA register:
Figure 7-9. OPAMP.CTRLA - Enable OPAMP Peripheral
OPAMP.CTRLA = OPAMP_ENABLE_bm;

A value of ‘0’ of the SETTLED bit indicates that the settling time has elapsed:

Figure 7-10. OPAMP.OP0SnATUS - OPAMP Status
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)
    {
        ;
    }
}
The code for this example is available in the simple-opamp folder in these github repositories
Note: This example is not available for Atmel START or MPLAB® X MCC