How to set Gain (ADC_SetGain) for PSoC4 ADC SAR Seq

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

In an example application CE95272 there is code in this example that sets the gain of the API ADC_SetGain().  Below is a copy of the code from this example.

    /* Calculate gain for the first channel */

    adcGain = (((int32)ADC_SAR_WRK_MAX_12BIT * ADC_10MV_COUNTS) +

              ADC_DEFAULT_VREF_MV_VALUE) / (ADC_DEFAULT_VREF_MV_VALUE * 2);

    adcGain *= GAIN;

    ADC_SetGain(CHANNEL_1, adcGain);

Does anyone know were I can find information as to the other API used  so i can understand why its there.  I am doing a similar application using a instrumentation amplifier with a gain of 10, but i don't understand this code and why its there.  I don't know if its there for a voltage conversion or a calibration.

Thanks

Scott

0 Likes
1 Solution
Vasanth
Moderator
Moderator
Moderator
250 sign-ins 500 solutions authored First question asked

Hello Scott,

I will try to answer your doubts. The adc_gain parameter is used by the voltage conversion functions in the ADC component. It will remove the effect of the gain from the final voltage value. So the count you have obtained will be according to the input you have given at the input of the ADC. But inside the ADC_CountsTo_mVolts( or any other count to volt API) the result is after removing the adc_gain factor. Which will give you the input value you have given.


Now to answer your questions,

Q1> What is value in this case for (int32)ADC_SAR_WRK_MAX_12BIT?

    It 4096, the number of steps for 12 bit accuracy

Q2> What is value for ADC_10MV_COUNTS ?

     10000                                       

Q3> What is value for ADC_DEFAULT_VREF_MV_VALUE?  

     1024           

Q4> The data sheet for the ADC_SAR_Seq ... says its sets the gain in counts per 10 volts.    .... can this be explained by example?

     adc_gain value is used in the voltage conversion APIs. If you see the internal code of these APIs, you will get a better idea.

Q5> Were does this equation come from?

     I will try to answer this in detail. We are trying to find the ADC count value for 10V. This will give the slope of the curve.

adc_gain = 10 V/ resolution

now ,

resolution = voltage range / maximum count value

So the equation becomes

adc_gain = 10V * maximum count value/ voltage range

For SAR , the range is  2*Vref.

adc_gain = 10V* maximum count value/ 2 * Vref

Now  there is  rounding off in this equation ( half way rounding)

SO if you wan to find A/B, you will find the division (A+ (B/2))/B for rounding of the value to the rail or floor. You can search about the same. If you use the same principle in the above equation,

adc_gain = (10V* maximum count value + Vref )/ 2 * Vref

That is how the equation is created. I hope this answers your question.

Best Regards,
VRS

View solution in original post

0 Likes
5 Replies
Yugandhar
Moderator
Moderator
Moderator
500 solutions authored 1000 replies posted 5 likes given

Hi,

ADC_SetGain(uint32 chan, int32 adcGain) API sets the ADC gain in counts per 10volt for the voltage conversion functions ADC_CountsTo_uVolts(), ADC_CountsTo_mVolts(), and ADC_CountsTo_Volts(). This value is set by default based on the reference and input range settings in the ADC component. It should only be used to further calibrate the ADC with a known input or if an external reference is used. It affects these functions by supplying the correct conversion between ADC counts and the applied input voltage.

Thanks,

P Yugandhar.

0 Likes
Anonymous
Not applicable

Thank you for getting back to me on this but ... my question is what is all the other API doing and why is it structured in the code.  What does it do and why?

Thanks

Scott

0 Likes
Yugandhar
Moderator
Moderator
Moderator
500 solutions authored 1000 replies posted 5 likes given

Hello,


In the above code, adcGain value is used to calibrate the gain. This value is rounding off to nearest integer and is used to give the correct conversion between ADC counts and the applied input voltage. The value will be calculated based on Maximum count value and Vref voltage given to the ADC.
In the above code, ADC_SAR_WRK_MAX_12BIT is the maximum count value and ADC_DEFAULT_VREF_MV_VALUE is the Vref voltage.


Thanks,
P Yugandhar.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Thanks again for trying to help me understand, but I am just not seeing the reasoning for all the variables and the math in the ADC_SetGain() function.  The problem for me is this,  when you run this code it does not report a gain of 10 to the UART on the differential input (CHANNEL_1).  When I run this code/application, and I apply just under 25mV or just over 75mV the LED is supposed to light and it does perfectly based on the Compare mode settings, then it should report that voltage with a gain of 10 applied to the UART, which it does not do!  For example, when I apply  30mV to the input the LED should go off which it does and the UART should report 300mV which it does not do.  I did the math which calculates the Compare Mode settings.  I will attach my spread sheet that others my see, use and review.  It was helpful for me to understand how those compare mode values were derived.  In trying to understand the ADC_SetGain() function and how to make sure the gain is set properly for CHANNEL_1, i do not understand the values of the variables and the math used?  Would it be possible to answer the questions below so i fully understand the calculations used.

Q1> What is value in this case for (int32)ADC_SAR_WRK_MAX_12BIT?    ... is this 2^n-1 or 2048

Q2> What is value for ADC_10MV_COUNTS ?                                            ... is this number of counts for 10mV ?

Q3> What is value for ADC_DEFAULT_VREF_MV_VALUE?                    ... is this 1.024 volts?

Q4> The data sheet for the ADC_SAR_Seq ... says its sets the gain in counts per 10 volts.    .... can this be explained by example?

Q5> Were does this equation come from?

0 Likes
Vasanth
Moderator
Moderator
Moderator
250 sign-ins 500 solutions authored First question asked

Hello Scott,

I will try to answer your doubts. The adc_gain parameter is used by the voltage conversion functions in the ADC component. It will remove the effect of the gain from the final voltage value. So the count you have obtained will be according to the input you have given at the input of the ADC. But inside the ADC_CountsTo_mVolts( or any other count to volt API) the result is after removing the adc_gain factor. Which will give you the input value you have given.


Now to answer your questions,

Q1> What is value in this case for (int32)ADC_SAR_WRK_MAX_12BIT?

    It 4096, the number of steps for 12 bit accuracy

Q2> What is value for ADC_10MV_COUNTS ?

     10000                                       

Q3> What is value for ADC_DEFAULT_VREF_MV_VALUE?  

     1024           

Q4> The data sheet for the ADC_SAR_Seq ... says its sets the gain in counts per 10 volts.    .... can this be explained by example?

     adc_gain value is used in the voltage conversion APIs. If you see the internal code of these APIs, you will get a better idea.

Q5> Were does this equation come from?

     I will try to answer this in detail. We are trying to find the ADC count value for 10V. This will give the slope of the curve.

adc_gain = 10 V/ resolution

now ,

resolution = voltage range / maximum count value

So the equation becomes

adc_gain = 10V * maximum count value/ voltage range

For SAR , the range is  2*Vref.

adc_gain = 10V* maximum count value/ 2 * Vref

Now  there is  rounding off in this equation ( half way rounding)

SO if you wan to find A/B, you will find the division (A+ (B/2))/B for rounding of the value to the rail or floor. You can search about the same. If you use the same principle in the above equation,

adc_gain = (10V* maximum count value + Vref )/ 2 * Vref

That is how the equation is created. I hope this answers your question.

Best Regards,
VRS

0 Likes