- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I am using CY8C4124AZI-S413 (supplied with 5V) to read a voltage value using CapSense_ADC (release 5.0) block.
I need the formula to calculate raw to mV and viceversa.
Could someone help me, please?
Thanks
Regards
Mauro Berioli
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can read the source code generated by CapSense_ADC component to make it clear, the conversion logic is defined in CapSese_ADC_Adc_INT.c , you can find below code in isr function CY_ISR(CapSense_ADC_1_AdcIntrHandler)
/*******************************************************************************************/
/* Read ADC result and check for ADC_ABORT or ADC_OVERFLOW flags */
tmpResult = CY_GET_REG32(CapSense_ADC_1_ADC_RES_PTR);
if (0uL == (tmpResult & CapSense_ADC_1_AdcADC_RES_ABORT_MASK))
{
if (0uL == (tmpResult & CapSense_ADC_1_AdcADC_RES_OVERFLOW_MASK))
{
/* Read ADC status, define polarity, value and ChId */
adcFsmStatus = (uint8)(CapSense_ADC_1_dsRam.adcStatus & CapSense_ADC_1_AdcSTATUS_FSM_MASK);
/* Select the polarity bit */
polarity = tmpResult & CapSense_ADC_1_AdcADC_RES_HSCMPPOL_MASK;
/* Select the result value */
tmpResult &= CapSense_ADC_1_AdcADC_RES_VALUE_MASK;
tmpChId = CapSense_ADC_1_dsRam.adcStatus & (uint8)(CapSense_ADC_1_AdcSTATUS_LASTCHAN_MASK);
/* ADC could have been converting or calibrating; handle each differently. */
switch (adcFsmStatus)
{
case CapSense_ADC_1_AdcSTATUS_CONVERTING:
/*
* After the converting will calculate an ADC result in mV depending on
* sourcing or sinking mode. Checks for a saturation in all modes.
*/
/* HSCMP polarity is 0:sink, 1:source */
if(0uL != polarity) /* Sourcing */
{
/* Saturate result at tVssa2Vref */
tmpResult = (tmpResult > (uint32)tVssa2Vref) ? (uint32)tVssa2Vref : tmpResult;
/* Scale result to Resolution range with rounding*/
tmpResult = ((((uint32)tVssa2Vref - tmpResult) * CapSense_ADC_1_AdcRES_MAX) +
((uint32)tFull >> 1uL)) / (uint32)tFull;
}
else /* Sinking */
{
#if (CapSense_ADC_1_ADC_FULLRANGE_MODE == CapSense_ADC_1_ADC_MEASURE_MODE)
/* Scale result with sink/source mismatch with rounding */
tmpResult = (((uint32)((uint32)tRecover << 1u) * tmpResult) + ((uint32)tVssa2Vref >> 1u)) / (uint32)tVssa2Vref;
/* Saturate result at t_Vdda2Vref*/
tmpResult = (tmpResult > (uint32)tVdda2Vref) ? (uint32)tVdda2Vref : tmpResult;
/* Scale result to Resolution range with rounding */
tmpResult = ((((uint32)tVssa2Vref + tmpResult) * CapSense_ADC_1_AdcRES_MAX) +
((uint32)tFull >> 1uL)) / (uint32)tFull;
#else /* (CapSense_ADC_1_ADC_FULLRANGE_MODE == CapSense_ADC_1_ADC_MEASURE_MODE) */
/* In vref mode, we are not supposed to be sinking. Saturate */
tmpResult = ((uint32)tVssa2Vref * CapSense_ADC_1_AdcRES_MAX) / (uint32)tFull;
#endif /* (CapSense_ADC_1_ADC_FULLRANGE_MODE == CapSense_ADC_1_ADC_MEASURE_MODE) */
}
/* Store ADC result code */
CapSense_ADC_1_dsRam.adcCode [tmpChId] = (uint16)(tmpResult);
/* Scale result to mV with rounding and store it */
tmpResult = ((uint32)vMaxMv * tmpResult) / CapSense_ADC_1_AdcRES_MAX;
CapSense_ADC_1_dsRam.adcResult[tmpChId] = (uint16)(tmpResult);
CapSense_ADC_1_dsRam.adcStatus = CapSense_ADC_1_AdcSTATUS_IDLE;
break;
.......
......
/*******************************************************************************************/
Please note that the formula include some static variables (tFull, tVssa2Vref, etc.), these variables store the calibration values measure by API CapSense_ADC_AdcCalibrate() which be called in CapSense_ADC_Start() once.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can read the source code generated by CapSense_ADC component to make it clear, the conversion logic is defined in CapSese_ADC_Adc_INT.c , you can find below code in isr function CY_ISR(CapSense_ADC_1_AdcIntrHandler)
/*******************************************************************************************/
/* Read ADC result and check for ADC_ABORT or ADC_OVERFLOW flags */
tmpResult = CY_GET_REG32(CapSense_ADC_1_ADC_RES_PTR);
if (0uL == (tmpResult & CapSense_ADC_1_AdcADC_RES_ABORT_MASK))
{
if (0uL == (tmpResult & CapSense_ADC_1_AdcADC_RES_OVERFLOW_MASK))
{
/* Read ADC status, define polarity, value and ChId */
adcFsmStatus = (uint8)(CapSense_ADC_1_dsRam.adcStatus & CapSense_ADC_1_AdcSTATUS_FSM_MASK);
/* Select the polarity bit */
polarity = tmpResult & CapSense_ADC_1_AdcADC_RES_HSCMPPOL_MASK;
/* Select the result value */
tmpResult &= CapSense_ADC_1_AdcADC_RES_VALUE_MASK;
tmpChId = CapSense_ADC_1_dsRam.adcStatus & (uint8)(CapSense_ADC_1_AdcSTATUS_LASTCHAN_MASK);
/* ADC could have been converting or calibrating; handle each differently. */
switch (adcFsmStatus)
{
case CapSense_ADC_1_AdcSTATUS_CONVERTING:
/*
* After the converting will calculate an ADC result in mV depending on
* sourcing or sinking mode. Checks for a saturation in all modes.
*/
/* HSCMP polarity is 0:sink, 1:source */
if(0uL != polarity) /* Sourcing */
{
/* Saturate result at tVssa2Vref */
tmpResult = (tmpResult > (uint32)tVssa2Vref) ? (uint32)tVssa2Vref : tmpResult;
/* Scale result to Resolution range with rounding*/
tmpResult = ((((uint32)tVssa2Vref - tmpResult) * CapSense_ADC_1_AdcRES_MAX) +
((uint32)tFull >> 1uL)) / (uint32)tFull;
}
else /* Sinking */
{
#if (CapSense_ADC_1_ADC_FULLRANGE_MODE == CapSense_ADC_1_ADC_MEASURE_MODE)
/* Scale result with sink/source mismatch with rounding */
tmpResult = (((uint32)((uint32)tRecover << 1u) * tmpResult) + ((uint32)tVssa2Vref >> 1u)) / (uint32)tVssa2Vref;
/* Saturate result at t_Vdda2Vref*/
tmpResult = (tmpResult > (uint32)tVdda2Vref) ? (uint32)tVdda2Vref : tmpResult;
/* Scale result to Resolution range with rounding */
tmpResult = ((((uint32)tVssa2Vref + tmpResult) * CapSense_ADC_1_AdcRES_MAX) +
((uint32)tFull >> 1uL)) / (uint32)tFull;
#else /* (CapSense_ADC_1_ADC_FULLRANGE_MODE == CapSense_ADC_1_ADC_MEASURE_MODE) */
/* In vref mode, we are not supposed to be sinking. Saturate */
tmpResult = ((uint32)tVssa2Vref * CapSense_ADC_1_AdcRES_MAX) / (uint32)tFull;
#endif /* (CapSense_ADC_1_ADC_FULLRANGE_MODE == CapSense_ADC_1_ADC_MEASURE_MODE) */
}
/* Store ADC result code */
CapSense_ADC_1_dsRam.adcCode [tmpChId] = (uint16)(tmpResult);
/* Scale result to mV with rounding and store it */
tmpResult = ((uint32)vMaxMv * tmpResult) / CapSense_ADC_1_AdcRES_MAX;
CapSense_ADC_1_dsRam.adcResult[tmpChId] = (uint16)(tmpResult);
CapSense_ADC_1_dsRam.adcStatus = CapSense_ADC_1_AdcSTATUS_IDLE;
break;
.......
......
/*******************************************************************************************/
Please note that the formula include some static variables (tFull, tVssa2Vref, etc.), these variables store the calibration values measure by API CapSense_ADC_AdcCalibrate() which be called in CapSense_ADC_Start() once.