LPC UART baud rate calculator
In this post I will describe a small project I published some time ago, while working on a NXP microcontroller. It helps in setting the correct register values for proper UART initialization in LPC family MCUs.
The source file is fully available on github, under a permissive MIT-style license:
https://github.com/colosimo/lpc-uart-calc
It implements the algorithm written in the user manual (Fig. 35, pag. 218, paragraph 13.5.15). I tested it on a LPC111x chip, I guess it will work for other LPC chips, but did not test it yet.
The idea of the algorithm is to find the values of DLM, DLL, DIVADDVAL and
MULVAL having the minimum delta with respect to desired baudrate. In the first
loop, the dl_est
(DLM|DLL) is found, together with the fr_est
value:
do { dl_est = uart_pclk / (16 * br * fr_est); fr_est = uart_pclk / (16 * br * dl_est); } while (fr_est <= 1.1 && fr_est >= 1.9);
Then, a call to float_to_fraction
function follows.
float_to_fraction
implements the algorithm computing the fractional
divider look-up table at page 219 (Table 201).
void float_to_fraction(float ft, unsigned *mul, unsigned *div, unsigned maxdiv) { int m, d; float err = ft; float newerr; for (m = 0; m < maxdiv; m++) for (d = 1; d <= maxdiv; d++) { newerr = fabs(((float)m) / ((float)d) - ft); if (newerr < err) { *mul = m; *div = d; err = newerr; } } }