Dear friends

Recently I came across an AVR application note about an intelligent charger
design based on their controller line.
(not explained in detail ) which was used for getting valid ADC readings.
The following is copy-paste from there.
.................................................. ..................
The stable_ADC function is used when measuring battery
voltage or temperature. It makes sure the ADC values are
stable inside a defined area. This is important for an accurate
measurement. The function loops until it gets three
ADC values where the highest is no more than one step
higher than the lowest.
.............................................
There was a pseudo c flowchart but I was not able to follow it through.

|----------------- V[0] > V[1]+1 No ----------------------------- > return
| Yes
| V[5] = V[4]
| V[4] = V[3]
| V[3] = V[2]
| ADC done? No ------>-continue conversion
| Yes
| V[1] = highest value of V[2] to V[5]
| V[0] = lowest value of V[2] to V[5]
| |
| |
-----------------------
I wonder if someone can help in implementing this for picbasic as I would like to
use it in order to improve ADC readings on a PIC12F675 .
At the moment I am adding 64 consecutive values in order to approximate a 16
bit result and by floating point math to end up to my final reading which is not
very stable.

Bill
Last edited by gebillpap; - 4th February 2008 at 23:42.

2. You probably need to clean up your circuit if you are getting that much noise. For example, I was taking an a2d reading, and it was varying by about 8 counts on a 10-bit measurement. That is a lot. I put a 1uF cap on the power pin of the amp, and now it only varies by one or two counts.

3. ## junk the top and bottom four readings

Hi Bill,

Melanie’s great idea:
Originally Posted by Melanie
take say 16 ADC readings, sort them in sequential order of value, junk the top and bottom four readings as being 'out-of-range', sum and average the middle eight... this makes ADC readings in industrial applications pretty much bomb-proof...

It would be interesting to see a “real world” working example of this technique written for a PIC’s 10 bit ADC.

4. One advantage to Melanie's technique is very obvious when you have a moving a/d value to read. For slow moving, if you have a quality circuit with low noise, you can usually just average. I used an a/d port to track the charging of a battery once, using 10 point averaging. I could almost get better resolution than the PIC advertised because with a steady and slow moving v-in, the readings changed very slowly... that is, on a 10-bit reading, I'd get 5 readings at, say, 480, and 5 at 481. The next time, it would be 4 at 480 and 6 at 481... then 3 & 7, etc.

If you really want a "rolling average" then you can take a reading and add it to your average, and subtract your oldest rolling number. That saves time in computation if it matters. Otherwise, just adding all of your numbers every time may be easier to read and comprehend.

5. Thank you for responding
I will try to describe the problem I face better in order to justify why I think that something like the stable ADC mentioned in my first post might solve it.
A 12f675 chip gets ADC readings of a battery undergoing charging with currents 0,1-4A.
The voltage is monitored and displayed every 1 second with resolution of two decimals (for example 8,17 V).
Since the battery is under charge it's voltage is climping up steadily and the display should show (at 1 sec rate ) voltages like 8,17 - 8,17 -8,17-some time -8,17 -8,17 -8,18 -8,18-some time-8,18- 8,19- some time-etc.
But what happens is that I get the voltage increase in "bursts" of three like 8,17 - 8,18 -8,19- 8,19 - 8,19 -8,19- some time-etc. every time there is a slight increase in voltage.
This is probably due to my manipulation of ADC data with adding 64 readings to achieve a 16 bit result and so on .
Í believe that with the stable adc routine ,mentioned in my first post, I can improve my display of voltages.

Anyway can anyone propose some picbasic equivelant for the stable ADC routine???

Bill

6. OK Adam, you asked for it... here it is... quickly thrown together for an 18F2420... (you can probably optimise it with a bit of thought, but it's a starter just to get going)...

The variables...
Code:
```	ADCValue var WORD			' Final ADC Result
CounterA var BYTE			' Just a BYTE Temporary working variable
DataW var WORD				' Just a WORD Temporary working variable
RawData var WORD [16]			' Array holding ADC Result```
Some Initialisation Set-up's for the PIC....
Code:
```	ADCON1=%00001110			' ADC Control 1
' 	7=0 - 0 Unused
' 	6=0 - 0 Unused
' 	5=0 - VRef=Vss
' 	4=0 - VRef=Vdd
' 	3=1 )
' 	2=1 ) RA0 set to Analogue
' 	1=1 ) All others set to Digital
'	0=0 )
' 	7=1 - Right Justified - Read 10-Bits
' 	6=0 - 0 Unused
' 	5=1 )
' 	3=1 )
' 	2=0 )
' 	1=0 ) Fosc/8
'	0=1 )```
Now for the real program... First TAKE your SAMPLES...
Code:
```		'
'	Stuff 16 Element WORD Array full of ADC values
'	----------------------------------------------
For CounterA=0 to 15
ADCON0=%00000001		' Select Channel, Turn-On A/D
'	7=0 Unused
'	6=0 Unused
'	5=0 )
'	4=0 )
'	3=0 ) selects AN0
'	2=0 )
'	1=0 Go-done Bit
Pauseus 50			' Wait for channel to setup
ADCON0.1 = 1			' Start conversion
While ADCON0.1=1:Wend		' Wait for conversion
RawData(CounterA)=DataW
Next CounterA```
Code:
```		'
'	--------------
CounterA=0
If RawData(CounterA+1) < RawData(CounterA) then
DataW=RawData(CounterA)
RawData(CounterA)=RawData(CounterA+1)
RawData(CounterA+1)=DataW
If CounterA>0 then CounterA=CounterA-2
endif
CounterA=CounterA+1
Finally EXTRACT SOMETHING USEFUL from what you've got...
Code:
```	'
'	Quanticise discarding top and bottom FOUR elements
'	----------------------------------------------------
DataW=0
For CounterA=4 to 11
DataW=DataW+RawData(CounterA)
Next CounterA
ADCValue=DataW>>3			' Divide Result by EIGHT```
Ain't so difficult when you break it down into little steps is it?

7. ## my co-workers have noticed

Hi Melanie,

Thanks a bunch, this is really easy to understand.
You are spot on, it is overwhelming looking at the problem. Trying to get a foothold.
But not so bad step by step.

Of coarse it is easy with the code as an example.

I will study the whole thing. I learn, not just from your excellent example of how to code it. But, from the style of logic flow and layout. It is good, the way you comment lines of code that seem obvious to you but cause a noobe to pause and check. This allows us to get beyond the mechanics quickly, and see the part you are showing us.

I have saved this example and will try to learn and build from it. You’re the greatest!

(notice the "spot on", my co-workers have noticed my accent growing!)

8. Originally Posted by Pic_User
Of coarse it is easy ... my co-workers have noticed my accent growing!
You're getting there - but you're still a little coarse (of course!).

9. ## Thanks.

Thanks.
Originally Posted by Melanie
You're getting there - but you're still a little coarse (of course!).
Of course, embarrassed, I now wander off in search of the “misused words” forum…..

10. Dear All
I replaced my own routine for ADC readings with Melanie's just to see if there would be any difference in displayed voltages.
I found out that there was always an increase in steps of 30mV(remember that I display with a resolution of two decimal digits) instead of my previous output in short bursts of gradually increasing values by 10mV.
So I came to the conclusion that my problem lies with the resistive divider that I use in order to be able to read voltages up to 30V.This is division by 6 and with the 0,004887 V / step(due to 5V max./1023 for 12F675) we arrive at apx. 30mV / new step which is unavoidable.
I think that I have to use some OPamp method(differential) to read voltage without stepping down through a ressistive divider.
I would welcome any thoughts and/or suggestions on the subject.
I thank all of you and especially Melanie(for the exceptional/disciplined programming techniques she uses) for the time you dedicated for me.
Also please forgive any grammar mistakes I make since I am only a native Greek .

Bill

11. ## thoughts on the subject

Originally Posted by gebillpap
Dear All
I replaced my own routine for ADC readings with Melanie's just to see if there would be any difference in displayed voltages.
I found out that there was always an increase in steps of 30mV(remember that I display with a resolution of two decimal digits) instead of my previous output in short bursts of gradually increasing values by 10mV.
So I came to the conclusion that my problem lies with the resistive divider that I use in order to be able to read voltages up to 30V.This is division by 6 and with the 0,004887 V / step(due to 5V max./1023 for 12F675) we arrive at apx. 30mV / new step which is unavoidable.
I think that I have to use some OPamp method(differential) to read voltage without stepping down through a ressistive divider.
I would welcome any thoughts and/or suggestions on the subject.
I thank all of you and especially Melanie(for the exceptional/disciplined programming techniques she uses) for the time you dedicated for me.
Also please forgive any grammar mistakes I make since I am only a native Greek .

Bill
Hi Bill

Take a look at this post.

You will have to extrapolate the Voltage up from the 12 Volt example given. Hope it helps….

Melanie doesn’t really make fun of grammar. We have a running joke about the difference between REAL English and American English. She was just catching a goof of mine. Your English is better than mine!

12. Bill

You're pretty much at the limit of conventional measurement (using a potential divider). The best you can accomplish as you have discovered will be steps of...

5v/1024*6 = 0.02929 (29.2mV)

Where 6 is your Potential Divider ratio.

Adam has suggested Zener Diodes, but beware using those, as no two Zener Diodes are the same and their tollerances are quite large.

You can always use an external 12-bit ADC rather than the PICs internal 10-bit one. That will then give you a resolution of about 4.4mV which is inside your two decimal places.

Trouble is I can guarantee your 5v reference probably isn't 5.0000v (and are you using 0.1% Resistors?) so we're splitting hairs about millivolt ADC tollerances anyway.

The only software solution... which is valid for slow-moving voltages (sampling alone in the below example will probably take about 4-5mS)... just massively over-sample, but I think you're using that already... Even with older versions of PBP, you are able to take 64 samples and add them together before you spill out of a WORD.

Code:
```		'
'	Sample 64 times
'	---------------
For CounterA=0 to 63
ADCON0=%00000001		' Select Channel, Turn-On A/D
'	7=0 Unused
'	6=0 Unused
'	5=0 )
'	4=0 )
'	3=0 ) selects AN0
'	2=0 )
'	1=0 Go-done Bit
Pauseus 50			' Wait for channel to setup
ADCON0.1 = 1			' Start conversion
While ADCON0.1=1:Wend		' Wait for conversion
Next CounterA```
You now have 16-bits resolution... well, not really, but the bigger the sample the better.

Need more? Just use PBP 2.50 where you can have more bits... but it will take much longer to sample - how much time do you have to spare?

13. Melanie you are right that I already use this "fake 16 bit" method which in turn made me use the FP routines in order to have math results.The reason is that although I display 2 decimals I calculate using 3 (1mV prec.).I have to multiply the 16 bit result by 5000 and divide by 65535 as you understand of course.
You are also correct in pointing out that it is a POTENTIAL divider and not, as I incorrectly put it, a resistive one.

I made a simple differential amplifier circuit using 1/4 of LM324 having a gain of 0,5 and the output was very very very close to half the input once I managed to get close in resistor values matching R and 2R (1% used).
I hope that by using 0,1% resistors and suitable resistor values I can get rid of the inaccuracies of the potential divider.On the other hand I will be facing another similar problem having to closely match the already difficult to find resistor values(0,1%) in order to make the correct division.
Did anyone followed the dif.amp route and with what results?

Another interesting way to get around the problem would be something like the Microchip suggestion(in an older app.note I have seen) in using a digital(eeprom) potentiometer to apply apropriate voltage for subtruction on a dif-amp.
Any further thoughts?
Bill

14. Bill
Using LM324 or LM358 op-amps are probably some of the worst choices to use for ADC voltage buffer/follower applications (uA741 is also a poor choice). The better op-amps would be rail-to-rail op-amps They give much better performance and far more stable in these applications.

Something like MCP6022 C-MOS OP-OP-AMP or any that can supply rail-to-rail on the input/output, Well I found them to work great

15. Bill, you mention that you had to use FP to calculate, that's not necessary. You can save a lot of time and codespace by using ** instead. This operator makes your multiplication and an "invisible" division by 65536. Replace all your calculations with ......

to get 0 to 5.000 volts or .......

.... if yo want it to go from 0 to 15.000V instead.

16. Ingvar I think you are absolutely RIGHT and I will test it as soon as I can.
Since the ** multiplication places the result in the upper 16 bits it is I believe the same with divide by 65536 as you say so in fact I can get rid of the FP routines.
I think the slight miscalculation of using 65536 instead of 65535 is not worth to mention.
Am I right?
This proves that although I read the manual I didn't fully understand it.(of course it is not the first time )

Thank you for pointing out this for me.
Bill

17. I'd say that it is 2^16(65536) that is the right value to use but that's not really important since the error we're talking about is 0,0015%. Your resistors and ADreference will produce a far greater error than that.

Happy coding
/Ingvar

18. Ingvar I did test what you suggested and it was RIGHT.
In other words it does exactly the same as FP routines do and at lesser code space cost.
Ofcourse it doesnt solve my problem but it can be used to save space effectively.
I believe that using a converter with greater than 10 bits resolution is a must and if there is a chip suggestion, to make things easy, I am waiting for it.
Thanks again
Bill

19. ## 18F8723 - 12 bit A/D ???

Originally Posted by gebillpap
Ingvar I did test what you suggested and it was RIGHT.
In other words it does exactly the same as FP routines do and at lesser code space cost.
Of course it doesnt solve my problem but it can be used to save space effectively.
I believe that using a converter with greater than 10 bits resolution is a must and if there is a chip suggestion, to make things easy, I am waiting for it.
Thanks again
Bill
18F8723 - 12 bit A/D ???

20. If you choose a Pic that has the capability to use external Vref you can get about twice the resolution(above 15V). Put a 50/50 voltagedivider(2.5V) on Vref-, feed it with Vdd. Connect Vref+ to Vdd. The AD will now give you a zero reading for everything below 15V. 30V will still produce maxreading. Your calculation would look like .......

ScaledResult = (AccumulatedADvals ** 15000) + 15000

Unfortunatley the 12F675 doesn't have this capability.

21. Ingvar
Thanks for the clever suggestion.Did you use it and with what results/observations?
I suppose that a 2,5V stable voltage ref is the obvious need BUT maybe a volage divider would be better because it will follow the VDD ripple.Is it so?
I think that having one AD channel with Vref 5V for measuring 0-15V and another AD channel configured with 2,5V Vref (following your trick with 2,5V bias in respect to ground) for measuring 15-30V is what I should try.

Pic_User
Thanks also.Maybe after giving up with the regular stuff and methods I might follow this route.
Bill

22. Well, i haven't used it exactly like this in exactly this application, but yes, i have used an AD like this. This is one of the reasons why there is the possibility to use external Vrefs.
It's always a good practice to use the same source for both Vrefs for exactly the reason you mentioned. However, in this application that's not really necessary since the voltage you're measuring is not dependant on your reference. This means that you need a very stable(ripple free) and accurate(known voltage) reference. Vdd is most likley not good enough. There are about a gazillion different ways of creating this Vref, easiest is to buy a chip from Maxim. Trimming of your potentialdividers is also(probably) necessary to get the precision you're after. If it's a "one off" i'd do that with potentiometers, if it's going to be made in big quantities i'd do the trimming in software.

To finish this off, i'd like to say that you're shooting WAY over the target for a batterycharger. Millivolts is not important in this case, tenths of a volt would be enough.

But that's just my humble opinion .......

Good luck,
/Ingvar

I tried to use Melanie's great piece of code (Thanks !) for sorting the values I read with 12F675 ...
But the results are ...strange ; the schematic wont work as I wish.
It's something wrong in my code ? Thanks in advance !

Code:
```CounterA var BYTE			' Just a BYTE Temporary working variable
DataW var WORD				' Just a WORD Temporary working variable
RawData var WORD [10]			' Array holding ADC Result
............
Main:
For CounterA=0 to 9
Pauseus 50			' Wait for channel to setup
ADCON0.1 = 1			' Start conversion
While ADCON0.1=1:Wend		' Wait for conversion
RawData(CounterA)=DataW
Next CounterA
gosub getsort
CounterA=0

IF (ADvalue < 80)  THEN gosub do_5
endif

Goto Main

;=========================
; here are the commands
GetSort:
If RawData(CounterA+1) < RawData(CounterA) then
DataW=RawData(CounterA)
RawData(CounterA)=RawData(CounterA+1)
RawData(CounterA+1)=DataW
If CounterA>0 then CounterA=CounterA-2
endif
CounterA=CounterA+1
If CounterA<9 then goto GetSort
DataW=0
For CounterA=2 to 9
DataW=DataW+RawData(CounterA)
Next CounterA
return```

Me ... again ...
Tried to use Mr.Darell (RIP ) routine :
Code:
```include "D:\PBP\average_DT.pbp"

Main:
Pauseus 50			' Wait for channel to setup
ADCON0.1 = 1			' Start conversion
While ADCON0.1=1:Wend		' Wait for conversion
gosub Average
IF (advalue > 450) AND (advalue < 640) THEN gosub do_1 ...
IF (advalue > 320) AND (advalue < 440) THEN gosub do_2 ...
...
endif

Goto Main```
...but something is wrong - always is executed do_1 subroutine

Note : the code in his "simple" variant, works just fine . Just I want more "accuracy" ...
Code:
```advalue  var word

pause 200

Main:

PAUSEuS 50                      	' Wait for A/D channel acquisition time
for advaloop = 1 to 20
WHILE ADCON0.1 = 1 : WEND

...
endif
Goto Main```
Last edited by fratello; - 10th May 2015 at 13:15.

Your implementation of Mel's sort algorithm has a problem in it.

After you read the ADC values in the "For CounterA = 0 to 9" loop, CounterA equals 10.

You then issue the "Gosub GetSort" statement.
Since CounterA = 10 the following line is pointing at memory locations beyond the RawData array.
"If RawData(CounterA+1) < RawData(CounterA) then"
This says If RawData(11) < RawData(10) then....
You need to set CounterA to 0 before calling GetSort.

Also you did not declare "Advalue" as a variable either.

Thanks !
In post #6 of Mel's I see the same sort procedure as mine ... why mine is wrong ?!

In post #6 of Mel's I see the same sort procedure as mine ... why mine is wrong ?!
Simple because, ad mentioned by Tabsoft, you have forgotten to clear the variable 'CounterA' before entering the sorting routine.

Cheers

Al.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts