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.
Firstly, I have checked the data sheet, the fADCK(ADC conversion clock frequency) ranges from 1.0MHz to 18MHz, I think it is okay to use 2MHz or 4MHz ADC clock with 12 bit resolution.
If you sample current with a shunt resistor, does the current can flow in reverse direction? if the current flows in reverse direction, the voltage on the shunt resistor ends will be in negative, in the case, you have to use OP AMP to offset the voltage or use differential ADC channels.
As a test, if you test a DC voltage with low output impedance for example 1.6V, are there any random samples?
If you still have issue, pls post only the ADC code which can duplicate the issue.
Hope it can help you
tnx for the reply.
I tried different frequencies(2MHz -> 8MHz) and resolution(12bit,16bit) but ALMOST (with 8MHz and 16bit no problem - mentioned before) in every case I have this "random" problem. I designed the hardware in a way that I have an offset that gives me the possibility to sense current in both directions.
In the hardware design there is an voltage follower that connects the differential amplifier with the ADC input of the MCU.
So I already have a low impedance on the input of the ADC, but I also tried without the voltage follower but the "random" problem continues to reappear.
Here are the settings of the ADC + the code:
But I also note that when I change the source clock of the ADC, from extern oscillator(8MHz) to BusClock then I have no problems with this "random" issue, regardless of the frequency or the bit resolution. Below is the settings of the source clock set to BusClock:
I have checked your code, you start the ADC conversion in ADC ISR, maybe it has issue, because the ADC conversion rate is too high.
I suggest you use PIT to generate an interrupt, which you can set the ADC sampling frequency with, in the PIT ISR, you can start the ADC to convert, in the ADC ISR, you just need to read the ADC sample and compute based on your application.
Pls have a try.
BTW, if just the current channel has random sample, maybe the shunt resistor voltage have spike, in the case, I suggest you use a RC circuit.
Pls have a try.