drbitboy
Lifetime Supporting Member
What do you mean by 12-bit counter (0-4095)? I thought your counter was rolling over at 32k?
Anyway, if you have a power-of-2 counter, say 1024 (= 210), then you don't have to fiddle about with the rollover offset. Because a bit-wise AND with 1023 (=2n-1, where n=10) of the counter will give you the absolute rotation state, and you can multiply by (360.0/1024.0) to get degrees. When the counter rolls over at some larger power-of-2, the counter.ACC AND 1023 expression result will rollover from 1023 to 0 (or rollunder from 0 to 1023), which will not change the offset.
You will still need to home the system by holding the footplates wherever 0° is and resetting the counter to 0, but after that it is trivial to get the current angle:
Modulo 1024 would be an alternative equivalent to AND 1023, and might be easier to understand.
Anyway, if you have a power-of-2 counter, say 1024 (= 210), then you don't have to fiddle about with the rollover offset. Because a bit-wise AND with 1023 (=2n-1, where n=10) of the counter will give you the absolute rotation state, and you can multiply by (360.0/1024.0) to get degrees. When the counter rolls over at some larger power-of-2, the counter.ACC AND 1023 expression result will rollover from 1023 to 0 (or rollunder from 0 to 1023), which will not change the offset.
You will still need to home the system by holding the footplates wherever 0° is and resetting the counter to 0, but after that it is trivial to get the current angle:
Code:
ActualPosition := (counter.ACC AND 1023) * 360.0 / 1024.0;
Modulo 1024 would be an alternative equivalent to AND 1023, and might be easier to understand.