I am using MK10DN512VLL10 microcontroller. And I have a problem with measuring some values with the ADC (in this case it is about measuring current consumption with a shunt).
Randomly, every second/third time when I turn on the system the ADC displays a different value. It always switches between two values.
For example: Value_1 is measured first and second time when I turn on the system, and the third time the Value_2 is measured. After that the fourth time the Value_1 is again measured, and then again in some random N turn_on the Value_2 is again measured. Value_1 and Value_2 are different but not so much (like Value_1 is ~10005 and Value_2 is ~10015). The correct value should be between Value_1 and Value_2
I debugged and the value that I get from ADC is simply different. It has nothing to do with the math after, that transforms this value to a current....
For programming I am using CodeWarrior 10.6
I think that is a problem with a frequency....
Before we used FEI (FLL output clock from LSI), but now we use PEE (PLL output clock form HSE) which is much better, faster, stable... BUT when we used FEI there were no problem like this. It started with the PEE option of the clock.
Also I find out that I have no problems when the ADC is 16bit resolution, with 8MHz clock (180us conversion time...), BUT with 4MHz or 2MHz ADC clock or with any frequency when the ADC is 12bit resolution I have these random results.
About the code:
The MCU frequency is 100MHz.
The ADC gets its clock from the HSE(8 MHz) and then divides it with a prescaler...
The ADC measurement is initialized in the beginning, after that when the measurement is done, the interrupt AD1_OnMeasurementComplete is called (it has the highest priority in the system). In this same interrupt the conversion from ADC value to current representation is done, and at the end the ADC measurement is started again.
Also I tried using StartCalibration function but it does not help.
I spend much time with this problem and I cannot figure it out why is this happening.