# Counts Conversion Functions¶

group group_sar_functions_countsto

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.

The equation used is:

Counts = (RawCounts/AvgDivider - Offset)


where,

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.

Function Usage

Parameters
Returns

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 funtion is only valid when result alignment is right aligned.


/* 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));

Function Usage

Parameters
Returns

Result in Volts.

• If channel number is invalid, 0 is returned.

• If channel is left aligned, 0 is returned.

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 funtion is only valid when result alignment is right aligned.

    /* 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));

Function Usage

Parameters
Returns

Result in millivolts.

• If channel number is invalid, 0 is returned.

• If channel is left aligned, 0 is returned.

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 funtion is only valid when result alignment is right aligned.

    /* 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));

Function Usage

Parameters
Returns

Result in microvolts.

• If channel number is valid, 0 is returned.

• If channel is left aligned, 0 is returned.

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
• base – Pointer to structure describing registers

• chan – The channel number, between 0 and CY_SAR_INJ_CHANNEL.

• offset – The count value measured when the inputs are shorted or connected to the same input voltage.

Returns

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.

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
• base – Pointer to structure describing registers

• chan – The channel number, between 0 and CY_SAR_INJ_CHANNEL.

• adcGain – The gain in counts per 10 volt.

Returns