2
$\begingroup$

I have a question regarding solving values Period and Prescaller from the equation below

$$F_k = \frac{F_{mcu}}{(\text{Period} + 1) (\text{Prescaller} + 1)}$$

where $F_{mcu}$ and $F_k$ are known. Also, $F_{mcu} > F_k$. Period and Prescaller are 16 bit, they can take values from 0 to 65535. The point is to find values of Period and Prescaller so the equation will give as close value to known $F_k$ as possible. What would be the algorithm here that is more efficient than checking all possible values in a loop? I would appreciate all help.

  • 0
    It is an integer (32 bit)2017-02-23
  • 0
    $\frac{48000000}{247400} \approx 194.018$ is not quite integral.2017-02-23
  • 0
    Its an integral in the end. Operations can be done on floats and cut off in the end2017-02-23
  • 0
    $194 = 97 \cdot 2$. Done.2017-02-23
  • 0
    How about $\text{Period} = 96$ and $\text{Prescaller} = 1$? It yields $F_k \approx 247422.68$.2017-02-23

1 Answers 1

1

Let $x=Period$ and $y=Prescaller$. Your problem is equivalent to finding a feasible solution to \begin{cases} \frac{F_{mcu}}{F_k} = (x+1)(y+1) \\ x \le 65535 \\ y \le 65535 \\ x,y \in \mathbb{N} \end{cases}

You can solve this graphically. Draw a $65535$ x $65535$ grid, and the implicit function $\frac{F_{mcu}}{F_k} = (x+1)(y+1)$. Check if the curve passes through one of the points of the grid.

Note that the equation $\frac{F_{mcu}}{F_k} = (x+1)(y+1)$ is equivalent to $$ y= \frac{ \frac{F_{mcu}}{F_k} -1-x}{x+1} $$

so $$(x,y) = \left(0, \left \lfloor \frac{F_{mcu}}{F_k} -1 \right \rfloor \right)$$ is a feasible solution (provided that $\frac{F_{mcu}}{F_k} -1 \le 65535$) that should be close enough to the optimum (the $\left \lfloor \cdot \right \rfloor$ is the floor function).

  • 0
    Could you please elaborate a bit more? Do you mean like Newton method (not sure if this is translated this way to english)?2017-02-23
  • 0
    well, it depends what tools you have. If for example you are using Python, there are free generic non linear solvers out there who can do the job for you. They use methods such as Newton, but also more sophisticated ones.2017-02-23
  • 0
    I need to run the algoritm on a bare metal microcontroller. I dont even have floating point. I am using C.2017-02-23
  • 0
    I have edited the post, as you can solve the problem graphically. It's probably the simplest way to do it. If you give me your values of $F_{mcu}$ and $F_k$ I can do it for you.2017-02-23
  • 0
    Fmcu is 48 Mhz and Fk is 247.4 kHz. But only here, code needs to be able to solve it for different Fmcu and Fk. Fk is always lower than FMcu.2017-02-23
  • 0
    In this case take $x=Period=0$ and $y=Prescaller = 193$: the right hand ratio in your question then equals $247.42$ Mhz.2017-02-23
  • 0
    But what method did you use?2017-02-23
  • 0
    I edited the post again. In the end it is an easy equation to solve. No need to use any algorithms.2017-02-23
  • 0
    So far I was using this solution, I assigned 0 to one of the parameters. But this way the function is not ready to take all possible values, 16 bit will be easilly overflown2017-02-23
  • 0
    Ok, you should have said that earlier. So you want $x>0$ ?2017-02-23
  • 0
    Sorry for that. X could be anything really as soon as in combination with y it gives the closest possible value. The only way i can think of of solving this is to check all possible x and y, for example by using division algoritm, or some simmilar2017-02-23
  • 0
    In this case, if the floor of $F_{mcu}/F_k$ is odd, you can take $x=2$ and if it is even, you can take $x=1$.2017-02-23