PSoC 6 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_SetOffset (uint32_t chan, int16_t offset)
 Override the channel offset stored in the Cy_SAR_offset array for the voltage conversion functions. More...
 
cy_en_sar_status_t Cy_SAR_SetGain (uint32_t chan, int32_t adcGain)
 Override the gain stored in the Cy_SAR_countsPer10Volt array 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_MAX_NUM_CHANNELS - 1
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 contents of Cy_SAR_offset, Cy_SAR_countsPer10Volt, and other parameters. The equation used is:

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

where,

Note
This funtion is only valid when result alignment is right aligned.
Parameters
basePointer to structure describing registers
chanThe channel number, between 0 and CY_SAR_MAX_NUM_CHANNELS - 1
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, chan, Cy_SAR_GetResult16(SAR, 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 contents of Cy_SAR_offset, Cy_SAR_countsPer10Volt, and other parameters. The equation used is:

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

where,

Note
This funtion is only valid when result alignment is right aligned.
Parameters
basePointer to structure describing registers
chanThe channel number, between 0 and CY_SAR_MAX_NUM_CHANNELS - 1
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, chan, Cy_SAR_GetResult16(SAR, 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 contents of Cy_SAR_offset, Cy_SAR_countsPer10Volt, and other parameters. The equation used is:

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

where,

Note
This funtion is only valid when result alignment is right aligned.
Parameters
basePointer to structure describing registers
chanThe channel number, between 0 and CY_SAR_MAX_NUM_CHANNELS - 1
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, chan, Cy_SAR_GetResult16(SAR, chan));

◆ Cy_SAR_SetOffset()

cy_en_sar_status_t Cy_SAR_SetOffset ( uint32_t  chan,
int16_t  offset 
)

Override the channel offset stored in the Cy_SAR_offset array 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
chanThe channel number, between 0 and CY_SAR_MAX_NUM_CHANNELS - 1.
offsetThe count value measured when the inputs are shorted or connected to the same input voltage.
Returns

◆ Cy_SAR_SetGain()

cy_en_sar_status_t Cy_SAR_SetGain ( uint32_t  chan,
int32_t  adcGain 
)

Override the gain stored in the Cy_SAR_countsPer10Volt array 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
chanThe channel number, between 0 and CY_SAR_MAX_NUM_CHANNELS - 1.
adcGainThe gain in counts per 10 volt.
Returns