Electron wrote:
> In short I have 24bits coming from an ADC, and I have to make them
> 16bits, but inside a certain range of these 24bits.
>
> ...
>
> if (adc<=min) {
> out=0;
> } else if (adc>=max) {
> out=65535;
> } else {
> out=(adc-min)*65536/(max-min); /* needs 64bit intermediary
> precision */ }
Take your input value and subtract off MIN. Now the rest is a scale factor
problem. If this remaining value is still wider than 16 bits, then left
justify it in a 32 bit number and divide to get the 16 bit result. If
ADC-MIN always fits in 16 bits, then multiply it by a scale factor such that
the result you want ends up in 16 bits near the high end of the 32 bit
product.
********************************************************************
Embed Inc, Littleton Massachusetts,
http://www.embedinc.com/products(978) 742-9014. Gold level PIC consultants since 2000.
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist