The SAR2 driver provides an API to configure the SAR2 ADC.
The SAR2 has up to 4 ADCs and up to 32 channels for each ADC module. About actual number of supported modules and channels, refer to the technical reference manual (TRM) and device datasheet.
Configuration Considerations
Typical usage:
- Call Cy_SAR2_Init to initialize the ADC module and its channels
- Call Cy_SAR2_Channel_SetInterruptMask if you need to use the interrupt. After initializing channel(s) call, trigger a call by the software (calling Cy_SAR2_Channel_SoftwareTrigger) or start a peripheral configured for the HW trigger.
- Note
- It is not recommended to set the triggerSelection structure member of the cy_stc_sar2_channel_config_t to CY_SAR2_TRIGGER_CONTINUOUS if cy_en_sar2_preemption_type_t is set to CY_SAR2_PREEMPTION_ABORT_CANCEL. It may work to the effect that the channel or channel group will not be able to complete without advanced priority tuning.
- Note
- If you use an interrupt for ADC, create an interrupt handler and register it by using the sysint module. In the handler, you can check which interrupt is occurred by Cy_SAR2_Channel_GetInterruptStatusMasked and can get the conversion result by Cy_SAR2_Channel_GetResult. If you do not use the interrupt, you can poll the conversion status by Cy_SAR2_Channel_GetInterruptStatus. For both of polling and interrupt, clear the interrupt flags by Cy_SAR2_Channel_ClearInterrupt after handling data.
- Note
- SAR2 block can operate in Active or Sleep mode only.
Configuration Example
First step would be to configure clock:
Group of two SAR2 ADC channels are configured, one for bandgap voltage and one for the AN0 analogue input:
{
.channelPriority = 0U,
.isGroupEnd = false,
.extMuxEnable = true,
.extMuxSelect = 0U,
.sampleTime = 120U,
.averageCount = 1U,
.rightShift = 0U,
.rangeDetectionLoThreshold = 0U,
.rangeDetectionHiThreshold = 65535U,
};
{
.channelPriority = 0U,
.isGroupEnd = true,
.extMuxEnable = true,
.extMuxSelect = 0U,
.sampleTime = 120U,
.averageCount = 1U,
.rightShift = 0U,
.rangeDetectionLoThreshold = 0U,
.rangeDetectionHiThreshold = 65535U,
};
{
.powerupTime = 0U,
.enableIdlePowerDown = false,
.enableHalfLsbConv = false,
.sarMuxEnable = true,
.adcEnable = true,
.sarIpEnable = true,
.channelConfig = {&channel_vbg_config, &channel_an0_config,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
};
Initialization
#define CH_VBG (0u)
#define CH_AN0 (1u)
Obtaining results in counts
uint16_t resultVBG;
uint16_t resultAN0;
Calculating results in V, mV or uV
volts = (resultAN0 * 0.9f) / resultVBG;
mVolts = (resultAN0 * 900.0f) / resultVBG;
uVolts = (resultAN0 * 900000.0f) / resultVBG;
Die temperature
uint16_t resultTemp;
double dieTemperature;
Once done, in the variable, returned by the Cy_SAR2_CalculateDieTemperature function will contain the die temperature value in Celsius degrees.
The accuracy can be increased by executing the calibration procedure (see Calibration procedure section) before doing the conversion.
In the function call, the first argument is the enumeration value, representing the VDDA value range supplied to this particular chip being used, see cy_en_sar2_vdda_range_t.
Channel grouping
The SAR2 driver supports the channel grouping. Each channel can be either in an individual group or in a group of 2 or more channels. By default, the hardware will execute the next channel if it exists and is in the Enabled state if the current channel does not have the flag 'Group End' set. You can set triggers to start either the first channel or any channel of the group to execute all the next channels.
More Information
Refer to the technical reference manual (TRM) and the device datasheet.
Changelog
Version | Changes | Reason for Change |
1.1 | Adding support for devices with up to 3 SAR slices. | |
1.0 | Initial version | |