CAT2 Peripheral Driver Library
Counts Conversion Functions

This set of functions performs counts to *volts conversions. More...

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...
 
int16_t Cy_SAR_CountsTo_degreeC (const SAR_Type *base, uint32_t chan, int16_t adcCounts)
 Converts the ADC output to degrees Celsius. More...
 
int16_t Cy_SAR_CountsTo_tenthDegreeC (const SAR_Type *base, uint32_t chan, int16_t adcCounts)
 Converts the ADC output to tens of degrees Celsius. More...
 
cy_en_sar_status_t Cy_SAR_SetChannelOffset (const SAR_Type *base, uint32_t chan, int16_t offsetCount)
 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...
 

Detailed Description

This set of functions performs counts to *volts conversions.

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(SAR0, chan, Cy_SAR_GetResult16(SAR0, 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(SAR0, chan, Cy_SAR_GetResult16(SAR0, 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 / 10) / (Gain / 10)
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 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 microvolts. */
uint32_t chan = 0UL;
int32_t resultuVolts;
resultuVolts = Cy_SAR_CountsTo_uVolts(SAR0, chan, Cy_SAR_GetResult16(SAR0, chan));

◆ Cy_SAR_CountsTo_degreeC()

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

Converts the ADC output to degrees Celsius.

Parameters
basePointer to structure describing registers
chanThe channel number, between 0 and CY_SAR_INJ_CHANNEL
adcCountsConversion result from Cy_SAR_GetResult16
Note
In case of ADC Vref doesn't match the DieTemp sensor Vref this function automatically corrects the adcCounts to match the DieTemp Vref.
Returns
The die temperature in degrees Celsius. If any of base or chan parameters is valid, 0 is returned.
Function Usage
int16_t temp = Cy_SAR_CountsTo_degreeC(SAR0, CY_SAR_INJ_CHANNEL, injResult);
/* now temp contains the die temperature is degrees Celsius */
Also please refer the Die temperature measurement

◆ Cy_SAR_CountsTo_tenthDegreeC()

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

Converts the ADC output to tens of degrees Celsius.

For example, converts 12.3C to 123. This is used for converting data from the dietemp sensor.

Parameters
baseThe pointer to the structure, which describes registers.
chanThe channel number, between 0 and CY_SAR_INJ_CHANNEL
adcCountsConversion result from Cy_SAR_GetResult16
Note
If ADC Vref does not match the DieTemp sensor Vref, this function automatically adjusts adcCounts to match DieTemp Vref.
Returns
The die temperature in tens of degrees Celsius. If any base or channel parameter is valid, 0 is returned.
Function Usage
int16_t temp = Cy_SAR_CountsTo_degreeC(SAR0, CY_SAR_INJ_CHANNEL, injResult);
/* now temp contains the die temperature is degrees Celsius */
Also please refer the Die temperature measurement

◆ Cy_SAR_SetChannelOffset()

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

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 positive offset of V_offset_mV, use:

Cy_SAR_SetChannelOffset(base, chan, -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.
offsetCountThe 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_SetChannelGain(SAR0, 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