This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Baudrate Generation

Hi,
I want to create a Function to approximate a custom Baudrate.
The Function depends on two Variables:
Baudrate = f(x,y) = (FDV * 40) / (8192 * (BG + 1)), FDV = 1..511, BG = 1..8192
Unfortunately I am not that good in maths so anyone who has an idea how to solve this ? One could do it iterative but that will be too time consuming.

Best Regards,
Marcus

  • It's not time-consuming at all if you don't try to run the code on the microcontroller itself. And even if you do run it there, it's not much of a problem --- you're not going to be changing the baudrate once per second, or are you?

    The plan would be to loop over one variable and calculate the best possible solution for the other, i.e.:

    for (FDV=0; FDV<512; FDV++) {
      short BG = round(8192.0 * BaudRate / 40 / FDV) - 1;
    
      float BaudRateError = 40.0 * FDV / 8192 / (BG + 1) - BaudRate;
    
      if (BaudRateError < 0)
        BaudRateError = -BaudRateError;
    
      if (BaudRateError < OptimalBaudRateError)
         updateOptimalBaudRate(FDV,BG,BaudRateError);
    }

    Note that this exercise has been gone through by others quite a lot, and code to do it is available on the net. IIRC, the Keil site and the CD has some, too.