PDA

View Full Version : Converting 10bit ADC result to 8 bit



jmgelba
- 22nd February 2012, 22:32
I need to write some ADC results in EEprom for calibrating a design. This wont be needed in the final product, its just to calibrate a resistor divider.

I'm using a 10bit ADC result and need the result in 10 bits for elsewhere in the program. If I put the result into a duplicate VAR WORD and divide it by 4 , can I put that result in a new VAR BYTE and write the byte to EEprom?

It *looks* like this works from what I'm reading back from EEprom but I just wanted a second opinion.

Thanks!

Dave
- 22nd February 2012, 23:22
shift the value 2 places to the right.... 10bits shift >> 2, 8 bits....

Art
- 23rd February 2012, 01:25
If you simply shift out the least significant bits you lose any small variations between values
that might not have occurred with a divide, but then when you multiply back to the original range
you also drop resolution.

You can calculate a new range like this:
NewValue = (((OriginalValue - OriginalMin) * (NewMax - NewMin)) / (OriginalMax - OriginalMin)) + NewMin

languer
- 23rd February 2012, 07:53
Unless I am missing the obvious. Shifting right by two, or dividing by two (integer division) is identical. The only difference should be in speed. Shifts should always be faster, and divides may be slower depending on the compiler optimization.

Dave
- 23rd February 2012, 11:47
Lanquer, That is correct...

Art
- 23rd February 2012, 14:50
Ignore my whole post... don't know where my head was,
you're going to drop the same resolution anyway!

jmgelba
- 25th February 2012, 02:37
Correct me if I'm wrong but if you divide a 10 bit result by 2, you get a 9 bit result. EG 1024/2 = 512. 8 bit max is 256 so you'd have to divide it by 4.

languer
- 25th February 2012, 04:10
you're correct, this
or dividing by two was my hands playing catchup with the brain

aratti
- 25th February 2012, 22:26
Why all this manipolation loosing valuable information contained in the 10 bits ADC, when you can store the whole 10 bits in the eeprom.

Write 0,ADC.lowbyte
Write 1,ADC.highbyte

Will store

Read 0, ADC.lowbyte
Read 1,ADC.highbyte

Will retrive your word.

Cheers

Al.

jmgelba
- 5th March 2012, 20:38
It'll also half the number of results that can be stored. Accuracy wasnt an issue, number of results was.