Add the necessary modules and create a function
configure_opamp2() in an
empty Atmel Studio ASF Board Project for SAM L21 or within an existing SAM L21
project. Configure the OPAMP2 as a Non-Inverting PGA:
Inverting input connected to resistor ladder
Non-inverting input connected to reference signal
R1 enabled and connected to Ground (GND)
R2 connected to OPAMP2 output
In addition, the resistor ladder must be configured to achieve a
desired gain. In this example the potentiometer option R1 = 12R and R2 = 4R
is selected. Since the output signal from the OPAMP2 will be used by the
ADC, it must be enabled by software to be made available. The configuration
function should follow the ASF standard by creating a configuration struct,
loading default settings, applying necessary settings, and enabling the
module.
/* Configure OPAMP2 and I/O PORT */
void configure_opamp2(void)
{
/* Creates a new configuration structure for the OPAMP2. */
struct opamp2_config conf;
/* Initializes OPAMP module. */
opamp_module_init();
/* Settings and fill with the default settings. */
opamp2_get_config_defaults(&conf);
/* Set the the OPAMP2 in "Non-Inverted PGA" mode. */
conf.negative_input = OPAMP2_NEG_MUX_TAP2;
conf.positive_input = OPAMP2_POS_MUX_PIN2;
conf.r1_connection = OPAMP2_RES1_MUX_GND;
conf.config_common.potentiometer_selection = OPAMP_POT_MUX_12R_4R;
conf.config_common.r1_enable = true;
conf.config_common.r2_out = true;
conf.config_common.analog_out = true;
Further, the I/O pin must be configured to enable the input signal. As
previously mentioned, PA05 corresponds to the positive input for OPAMP2:
/* Set up OA2POS pin as input. */
struct system_pinmux_config opamp2_input_pin_conf;
system_pinmux_get_config_defaults(&opamp2_input_pin_conf);
opamp2_input_pin_conf.direction = SYSTEM_PINMUX_PIN_DIR_INPUT;
opamp2_input_pin_conf.mux_position = OPAMP_INPUT_MUX;
system_pinmux_pin_set_config(OPAMP_INPUT_PIN, &opamp2_input_pin_conf);
Note:OPAMP_INPUT_MUX and OPAMP_INPUT_PIN is
defined in conf_example.h which must be included.
Finally, apply the settings and enable OPAMP2:
/* Initialize and enable the OPAMP2 with the user settings. */
opamp2_set_config(&conf);
opamp_enable(OPAMP_2);
/* Wait for the output ready. */
while(!opamp_is_ready(OPAMP_2));
The resulting configure_opamp2() function should be like the
following:
/* Configure OPAMP2 and I/O PORT */
void configure_opamp2(void)
{
/* Creates a new configuration structure for the OPAMP2. */
struct opamp2_config conf;
/* Initializes OPAMP module. */
opamp_module_init();
/* Settings and fill with the default settings. */
opamp2_get_config_defaults(&conf);
/* Set the the OPAMP2 in "Non-Inverted PGA" mode. */
conf.negative_input = OPAMP2_NEG_MUX_TAP2;
conf.positive_input = OPAMP2_POS_MUX_PIN2;
conf.r1_connection = OPAMP2_RES1_MUX_GND;
conf.config_common.potentiometer_selection = OPAMP_POT_MUX_12R_4R;
conf.config_common.r1_enable = true;
conf.config_common.r2_out = true;
conf.config_common.analog_out = true;
/* Set up OA2POS pin as input. */
struct system_pinmux_config opamp2_input_pin_conf;
system_pinmux_get_config_defaults(&opamp2_input_pin_conf);
opamp2_input_pin_conf.direction = SYSTEM_PINMUX_PIN_DIR_INPUT;
opamp2_input_pin_conf.mux_position = OPAMP_INPUT_MUX;
system_pinmux_pin_set_config(OPAMP_INPUT_PIN, &opamp2_input_pin_conf);
/* Initialize and enable the OPAMP2 with the user settings. */
opamp2_set_config(&conf);
opamp_enable(OPAMP_2);
/* Wait for the output ready. */
while(!opamp_is_ready(OPAMP_2));
}
The online versions of the documents are provided as a courtesy. Verify all content and data in the device’s PDF documentation found on the device product page.