MTB CAT1 Peripheral driver library
Counts Conversion Functions

General Description

This set of functions performs counts to *volts conversions.

Functions

int16_t Cy_SAR_RawCounts2Counts (const SAR_Type *base, uint32_t chan, int16_t adcCounts)
 Convert the channel result to a consistent result after accounting for averaging and subtracting the offset. More...
 
float32_t Cy_SAR_CountsTo_Volts (const SAR_Type *base, uint32_t chan, int16_t adcCounts)
 Convert the ADC output to Volts as a float32. More...
 
int16_t Cy_SAR_CountsTo_mVolts (const SAR_Type *base, uint32_t chan, int16_t adcCounts)
 Convert the ADC output to millivolts as an int16. More...
 
int32_t Cy_SAR_CountsTo_uVolts (const SAR_Type *base, uint32_t chan, int16_t adcCounts)
 Convert the ADC output to microvolts as a int32. More...
 
cy_en_sar_status_t Cy_SAR_SetChannelOffset (const SAR_Type *base, uint32_t chan, int16_t offset)
 Store the channel offset for the voltage conversion functions. More...
 
cy_en_sar_status_t Cy_SAR_SetChannelGain (const SAR_Type *base, uint32_t chan, int32_t adcGain)
 Store the gain value for the voltage conversion functions. More...
 

Function Documentation

◆ Cy_SAR_RawCounts2Counts()

int16_t Cy_SAR_RawCounts2Counts ( const SAR_Type base,
uint32_t  chan,
int16_t  adcCounts 
)

Convert the channel result to a consistent result after accounting for averaging and subtracting the offset.

The equation used is:

Counts = (RawCounts/AvgDivider - Offset)

where,

Parameters
basePointer to structure describing registers
chanThe channel number, between 0 and CY_SAR_INJ_CHANNEL
adcCountsConversion result from Cy_SAR_GetResult16
Returns
adcCounts after averaging and offset adjustments. If channel number is invalid, adcCounts is returned unmodified.
Function Usage

This function is used by Cy_SAR_CountsTo_Volts, Cy_SAR_CountsTo_mVolts, and Cy_SAR_CountsTo_uVolts. Calling this function directly is usually not needed.

◆ Cy_SAR_CountsTo_Volts()

float32_t Cy_SAR_CountsTo_Volts ( const SAR_Type base,
uint32_t  chan,
int16_t  adcCounts 
)

Convert the ADC output to Volts as a float32.

For example, if the ADC measured 0.534 volts, the return value would be 0.534. The calculation of voltage depends on the channel offset, gain and other parameters. The equation used is:

V = (RawCounts/AvgDivider - Offset)*TEN_VOLT/Gain

where,

Note
This function is only valid when result alignment is right aligned.
Parameters
basePointer to structure describing registers
chanThe channel number, between 0 and CY_SAR_INJ_CHANNEL
adcCountsConversion result from Cy_SAR_GetResult16
Returns
Result in Volts.
  • If channel number is invalid, 0 is returned.
  • If channel is left aligned, 0 is returned.
Function Usage
/* Scenario: ADC conversion has completed and result is valid.
* Retrieve the result on channel 0 and convert it to volts. */
uint32_t chan = 0UL;
float32_t resultVolts;
resultVolts = Cy_SAR_CountsTo_Volts(SAR_HW, chan, Cy_SAR_GetResult16(SAR_HW, chan));

◆ Cy_SAR_CountsTo_mVolts()

int16_t Cy_SAR_CountsTo_mVolts ( const SAR_Type base,
uint32_t  chan,
int16_t  adcCounts 
)

Convert the ADC output to millivolts as an int16.

For example, if the ADC measured 0.534 volts, the return value would be 534. The calculation of voltage depends on the channel offset, gain and other parameters. The equation used is:

V = (RawCounts/AvgDivider - Offset)*TEN_VOLT/Gain
mV = V * 1000

where,

Note
This function is only valid when result alignment is right aligned.
Parameters
basePointer to structure describing registers
chanThe channel number, between 0 and CY_SAR_INJ_CHANNEL
adcCountsConversion result from Cy_SAR_GetResult16
Returns
Result in millivolts.
  • If channel number is invalid, 0 is returned.
  • If channel is left aligned, 0 is returned.
Function Usage
/* Scenario: ADC conversion has completed and result is valid.
* Retrieve the result on channel 0 and convert it to millivolts. */
uint32_t chan = 0UL;
int16_t resultmVolts;
resultmVolts = Cy_SAR_CountsTo_mVolts(SAR_HW, chan, Cy_SAR_GetResult16(SAR_HW, chan));

◆ Cy_SAR_CountsTo_uVolts()

int32_t Cy_SAR_CountsTo_uVolts ( const SAR_Type base,
uint32_t  chan,
int16_t  adcCounts 
)

Convert the ADC output to microvolts as a int32.

For example, if the ADC measured 0.534 volts, the return value would be 534000. The calculation of voltage depends on the channel offset, gain and other parameters. The equation used is:

V = (RawCounts/AvgDivider - Offset)*TEN_VOLT/Gain
uV = V * 1000000

where,

Note
This function is only valid when result alignment is right aligned.
Parameters
basePointer to structure describing registers
chanThe channel number, between 0 and CY_SAR_INJ_CHANNEL
adcCountsConversion result from Cy_SAR_GetResult16
Returns
Result in microvolts.
  • If channel number is valid, 0 is returned.
  • If channel is left aligned, 0 is returned.
Function Usage
/* Scenario: ADC conversion has completed and result is valid.
* Retrieve the result on channel 0 and convert it to microvolts. */
uint32_t chan = 0UL;
int32_t resultuVolts;
resultuVolts = Cy_SAR_CountsTo_uVolts(SAR_HW, chan, Cy_SAR_GetResult16(SAR_HW, chan));

◆ Cy_SAR_SetChannelOffset()

cy_en_sar_status_t Cy_SAR_SetChannelOffset ( const SAR_Type base,
uint32_t  chan,
int16_t  offset 
)

Store the channel offset for the voltage conversion functions.

Offset is applied to counts before unit scaling and gain. See Cy_SAR_CountsTo_Volts for more about this formula.

To change channel 0's offset based on a known V_offset_mV, use:

Cy_SAR_SetOffset(0UL, -1 * V_offset_mV * (1UL << Resolution) / (2 * V_ref_mV));
Parameters
basePointer to structure describing registers
chanThe channel number, between 0 and CY_SAR_INJ_CHANNEL.
offsetThe count value measured when the inputs are shorted or connected to the same input voltage.
Returns

◆ Cy_SAR_SetChannelGain()

cy_en_sar_status_t Cy_SAR_SetChannelGain ( const SAR_Type base,
uint32_t  chan,
int32_t  adcGain 
)

Store the gain value for the voltage conversion functions.

The gain is configured at initialization in Cy_SAR_Init based on the SARADC resolution and voltage reference.

Gain is applied after offset and unit scaling. See Cy_SAR_CountsTo_Volts for more about this formula.

To change channel 0's gain based on a known V_ref_mV, use:

Cy_SAR_SetGain(0UL, 10000 * (1UL << Resolution) / (2 * V_ref_mV));
Parameters
basePointer to structure describing registers
chanThe channel number, between 0 and CY_SAR_INJ_CHANNEL.
adcGainThe gain in counts per 10 volt.
Returns