4 OPAMP Initialization

For all the following examples, the OPAMP will use a basic initialization preparing it for use. The following steps are needed to initialize the OPAMP:
  1. Disable the digital input buffer of the output pin of the OPAMP to reduce noise and power consumption.
  2. Set the TIMEBASE register to be ≥ 1 µs.
  3. Set the ALWAYSON bit to enable the OPAMP to run continuously.
  4. Set the OUTMODE to NORMAL, to connect the I/O of the OPAMP to their respective pins.
  5. Activate OPAMP in standby sleep (optional).
  6. Enable the OPAMP by writing a ‘1’ to the ENABLE bit.

The above steps, including optional steps, are implemented in the function below. This function will be used in all the following examples.

#define OPAMP_MAX_SETTLE_TIME 0x7F

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.OP0CTRLA = OPAMP_RUNSTBY_bm | OPAMP_ALWAYSON_bm | OPAMP_OP0CTRLA_OUTMODE_NORMAL_gc;
       OPAMP.OP0SETTLE = OPAMP_MAX_SETTLE_TIME; //As the settle time is unknown, the maximums should be set
}