Opamp (Operational Amplifier)¶
-
group
group_hal_opamp
High level interface for interacting with the Operational Amplifier (Opamp).
Features
Each opamp can operate in one of two modes:
Opamp: Bare opamp with two input pins.
Follower: Also known as unity gain. Buffers the signal on a single input pin and drives the same voltage on the output.
In both modes, the output is driven off chip via another pin.
Quickstart
Call cyhal_opamp_init to initialize an opamp instance by providing the opamp object (obj), non-inverting input pin (vin_p), inverting input pin (vin_m), and output pin (vout). If follower mode is desired, pass NC for vin_m.
Use cyhal_opamp_set_power to configure the opamp power.
Code Snippets
note
Error checking is omitted for clarity
Snippet 1: Bare opamp initialization
The following snippet initializes a bare opamp. Note that any passive components (e.g. resistive feedback) must be implemented off-chip.
cy_rslt_t rslt; cyhal_opamp_t opamp_obj; // Initialize opamp, using pin P9_0 for the non-inverting input, pin P9_1 for the inverting // input, and P9_2 for the output rslt = cyhal_opamp_init(&opamp_obj, P9_0, P9_1, P9_2); // Power on the opamp rslt = cyhal_opamp_set_power(&opamp_obj, CYHAL_POWER_LEVEL_DEFAULT); // Release opamp object after use cyhal_opamp_free(&opamp_obj);
Snippet 2: Opamp follower initialization
The following snippet initializes an opamp as a follower.
cy_rslt_t rslt; cyhal_opamp_t opamp_obj; // Initialize opamp as follower, using pin P9_0 for the input and P9_2 for the output rslt = cyhal_opamp_init(&opamp_obj, P9_0, NC, P9_2); // Power on the opamp rslt = cyhal_opamp_set_power(&opamp_obj, CYHAL_POWER_LEVEL_DEFAULT); // Release opamp object after use cyhal_opamp_free(&opamp_obj);
Snippet 3: Opamp powering-off and on
The following snippet demonstrates temporarily powering-off the opamp without freeing it.
// This assumes that the opamp has already been initialized as shown in snippet 1 or 2 // Power on the opamp rslt = cyhal_opamp_set_power(&opamp_obj, CYHAL_POWER_LEVEL_DEFAULT); rslt = cyhal_opamp_set_power(&opamp_obj, CYHAL_POWER_LEVEL_OFF); // When the opamp is needed again, power it back on rslt = cyhal_opamp_set_power(&opamp_obj, CYHAL_POWER_LEVEL_DEFAULT);
Functions
-
cy_rslt_t
cyhal_opamp_init
(cyhal_opamp_t *obj, cyhal_gpio_t vin_p, cyhal_gpio_t vin_m, cyhal_gpio_t vout)¶ Initialize the opamp peripheral.
If vin_m is NC, the opamp will be initialized in follower mode (unity gain).
The opamp will be initialized but not powered-on until cyhal_opamp_set_power is called.
- Return
The status of the init request
- Parameters
[out] obj
: Pointer to an opamp object. The caller must allocate the memory for this object but the init function will initialize its contents.[in] vin_p
: Non-inverting input[in] vin_m
: Inverting input[in] vout
: opamp output
-
void
cyhal_opamp_free
(cyhal_opamp_t *obj)¶ Deinitialize the opamp peripheral and free associated resources.
This will disconnect all inputs and outputs, including internal feedback.
- Parameters
[in] obj
: The opamp object
-
cy_rslt_t
cyhal_opamp_set_power
(cyhal_opamp_t *obj, cyhal_power_level_t power)¶ Changes the current operating power level of the opamp.
If the power level is set to CYHAL_POWER_LEVEL_OFF, the opamp will be powered-off but it will retain its configuration, so it is not necessary to reconfigure it when changing the power level from CYHAL_POWER_LEVEL_OFF to any other value.
- Return
The status of the set power request
- Parameters
[in] obj
: Opamp object[in] power
: The power level to set