I am working on an embedded project where I have to write the timeout value in two byte registers of some microchips.
A timeout is defined as:
timeout = REG_a * (REG_b +1)
I want to program these registers using an integer in the range from 256 to 60,000. I am looking for an algorithm that, given the timeout value, computes REG_a and REG_b.
If an exact solution is not possible, I would like to get the next possible larger timeout value.
What i have done so far:
My current solution calculates:
temp = integer_square_root (timeout) +1;
REG_a = temp;
REG_b = temp-1;
This leads to values that work well in practice. However, I would like you to come up with a better solution.
Oh, and I'm limited by memory, so large tables are out of the question. Running time is also important, so I can’t just reinstall the solution.