Alex Rivera | Logout

linear interpolation on 8bit microcontroller

Asked 2010-04-18T08:19:46.360
9

I need to do a linear interpolation over time between two values on an 8 bit PIC microcontroller (Specifically 16F627A but that shouldn't matter) using PIC assembly language. Although I'm looking for an algorithm here as much as actual code.

I need to take an 8 bit starting value, an 8 bit ending value and a position between the two (Currently represented as an 8 bit number 0-255 where 0 means the output should be the starting value and 255 means it should be the final value but that can change if there is a better way to represent this) and calculate the interpolated value.

Now PIC doesn't have a divide instruction so I could code up a general purpose divide routine and effectivly calculate (B-A)/(x/255)+A at each step but I feel there is probably a much better way to do this on a microcontroller than the way I'd do it on a PC in c++

Has anyone got any suggestions for implementing this efficiently on this hardware?

Edit
Report

2 Answers

1

You could do it using 8.8 fixed-point arithmetic. Then a number from range 0..255 would be interpreted as 0.0 ... 0.996 and you would be able to multiply and normalize it.

Tell me if you need any more details or if it's enough for you to start.

answered 2010-04-18T08:36:02.253
1

Interpolation

Given two values X & Y , its basically:

(X+Y)/2

or

X/2 + Y/2 (to prevent the odd-case that A+B might overflow the size of the register)

Hence try the following:

(Pseudo-code)

Initially A=MAX, B=MIN

Loop {

    Right-Shift A by 1-bit.

    Right-Shift B by 1-bit.

    C = ADD the two results.

    Check MSB of 8-bit interpolation value

    if MSB=0, then B=C

    if MSB=1, then A=C

    Left-Shift 8-bit interpolation value

}Repeat until 8-bit interpolation value becomes zero.

The actual code is just as easy. Only i do not remember the registers and instructions off-hand.

answered 2010-04-18T08:52:19.927

Your Answer