[ resending: I forgot the tag, sorry :( ]
Hi,
although my knowledge of the dsPIC (without DSP) is getting better, I am now
faced with a problem I must do absolutely bug free, because otherwise precious
data would be corrupted, so I'm seeking help here.
In short I have 24bits coming from an ADC, and I have to make them 16bits, but
inside a certain range of these 24bits.
So, to say it in pseudo C:
unsigned long int adc;
unsigned long int min;
unsigned long int max;
unsigned short int out;
if (adc<=min) {
out=0;
} else if (adc>=max) {
out=65535;
} else {
out=(adc-min)*65536/(max-min); /* needs 64bit intermediary precision */
}
pretty simple problem, if it wasn't for my knowledge of the MUL instruction
and also because of the necessary extended precision intermediary results,
but MUL and DIV are 16bit/32but instructions only. So the best I've done so
far is (given that adc, min and max are "only" 24bit values after all):
out=((adc-min)*255)/(((max-min)*255+32768)/65536);
which doesn't need more than the normal usage of the MUL and DIV instructions.
But this is not ideal, how could I perform it in multiprecision, to implement
the "real" formula?
Could anybody help please?
With kind regards,
Mario
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist