Olin wrote: > > I want to scale an incoming byte 00 - FF into a smaller range of > > values so that 00 still is 00 but FF gets cut down to say 28h with > > all the values in between filled in. I though of a few ways to do it; > > 1) Divide by a constant & round the result. > > Maybe the best but I may need to change the curve of the scaling. > > 2) A 256 byte table. > > Very definable but large & a pain to change scale. > > 3) Lots of compares. > >You don't say what machine you're on, how critical code space is, and how >critical execution speed is. I think there are two basic solutions, a table >and a fixed point multiply. Sorry, forgot to say it's on a 16F876 but may have to jam it into a 16F628 whenever they get released and readily available. (or even a 16F84A) Not sure how much code space I will need - still early days yet. Execution needs to be fairly quick as there is the possibility of multiple commands arriving from the host system in quick succession. >Since you are multiplying by a number less than 1, this works well when >expressed in fixed point with 8 fraction bits. Multiplying this value by >the original byte yields the scaled value in the high byte. This solution >is especially attractive if you are on a machine with a hardware multiplier. I've never needed to do math before in a PIC - might be time to learn! >The table is more general and probably the fastest solution if you don't >have a hardware multiplier, but it can eat up code space. However, note >that you don't need to index into the table with all 8 input bits. If the >result is only in the 0 - 40 range you should look at using only the high 6 >bits of the input value as the table index for exapmle. I like to use >assembler loops to write such tables for me. It prevents error in typing in >individual values, and it makes it simple to change the overall scale or >whatever of the table if needed. I think the table is probably the most quick & portable solution as present. Just have to brush up on those page boundary crossing details. :-) Regards... -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.