First off, don't forget, PBP only deals in integers, so if your input range is less than your target output range, you'll lose a bit in the translation.
So, here's a quicky thing I can think of to get you what you want...(in pseudo-code)
adcinput var word : minimumvalue var word : maximumvalue var word
range var word : scalefactor var word : newadcinput var word
Loop:
Get adcinput 'get the numbers from the A/D pin
if adcinput < minimumvalue then 'check for a new minimum value
minimumvalue = adcinput 'if it's less than the old one, replace it
endif
if adcinput > maximumvalue then 'check for a new maximum value
maximumvalue = adcinput 'if it's greater than the old one, replace it
endif
range = maximumvalue - minimumvalue 'get the total range of the input so far
scalefactor = 65536 / range 'get the scale factor for multiplying the number
newadcinput = adcinput * scalefactor 'up to a full 16 bit number to help avoid losing any more precision than we have to
newadcinput = newadcinput >> 9 'shift that big number back down to a 7 bit number
goto loop
Of course this could probably all be shortened up into one decent line of code, but you should get the picture.
Since we're working with integers, if you just took the range of the input and multiplied it to get a number range close to 0-127, you'd lose way too much precision. By taking and multiplying it up to a 16 bit number and dividing it down by a constant, you might save a bit of that precision. If we had floating point, we'd be ok, but we don't...




Bookmarks