uwezi

wormfood's tables

Nov 17th, 2019
686
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. //
  3. // after http://wormfood.net/avrbaudcalc
  4. // reprogrammed by Uwe Zimmermann, 2019-11-17
  5. //
  6.   function bg_color($err = 0)
  7.   {
  8.     if (abs($err) < 0.1)
  9.     {
  10.       $bg = "#51ff00";
  11.     }
  12.     else
  13.     {
  14.       if (abs($err)<=1)
  15.       {
  16.         $bg = "#42cc02";
  17.       }
  18.       else
  19.       {
  20.         if (abs($err)<=2)
  21.         {
  22.           $bg = "#00990a";
  23.         }
  24.         else
  25.         {
  26.           if (abs($err)<=3)
  27.           {
  28.             $bg = "#fff71c";
  29.           }
  30.           else
  31.           {
  32.             if (abs($err)<=4)
  33.             {
  34.               $bg = "#ff9d1c";
  35.             }
  36.             else
  37.             {
  38.               $bg = "#ff1c1c";
  39.             }
  40.           }
  41.         }
  42.       }
  43.     }
  44.     return $bg;
  45.   }
  46.  
  47. function wormfood_table($F_CPU=16000000)
  48. {
  49. $i = 0;
  50. $baud=array(300,600,1200,2400,4800,9600,14400,19200,28800,57600,76800,115200,128000,230400,256000);  
  51.  
  52. echo "<table border=\"1px\">";
  53. $FCPU = number_format ( $F_CPU , 0 , '.' , '&thinsp;' );
  54. printf("<tr><th colspan=\"7\" style=\"text-align:center\"><i>f<sub>osc</sub></i> = %s Hz</th></tr>",$FCPU);
  55. echo "<tr><th colspan=\"1\" rowspan=\"2\">nominal Bit rate</th><th colspan=\"3\">U2Xn=0</th><th colspan=\"3\">U2Xn=1</th></tr>";
  56. echo "<tr><th>UBRR</th><th>actual Bit rate</th><th>error</th><th>UBRR</th><th>actual Bit rate</th><th>error</th></tr>";  
  57.  
  58. for ($i=0;$i<count($baud);$i++)
  59. {
  60.   $UBRR   = round(($F_CPU / (16 * $baud[$i])) -1);  
  61.   $UBRR2x = round(($F_CPU  / (8 * $baud[$i])) -1);
  62.   if ($UBRR < 0)
  63.   {
  64.       $UBRR = 0;
  65.   }
  66.   if ($UBRR2x < 0)
  67.   {
  68.       $UBRR2x = 0;
  69.   }
  70.   $realbaud = round(($F_CPU/16)/($UBRR+1));
  71.   $err = 100*($realbaud-$baud[$i])/$baud[$i];
  72.   $realbaud2x = round(($F_CPU/8)/($UBRR2x+1));
  73.   $err2x = 100*($realbaud2x-$baud[$i])/$baud[$i];
  74.   echo "<tr><td style=\"text-align=right;\">$baud[$i]</td>";
  75.  
  76.   if ($UBRR > 4095)
  77.   {
  78.     $bg = "#c7c7c7";
  79.   }
  80.   else
  81.   {
  82.     $bg = bg_color($err);
  83.   }
  84.  
  85.   printf("<td style=\"background-color:%s;text-align=right;\">%d</td><td style=\"background-color:%s;text-align=right;\">%d</td><td style=\"background-color:%s;text-align=right;\">%.1f%%</td>",$bg,$UBRR,$bg,$realbaud,$bg,$err);
  86.   if (($UBRR2x > 4095) or ($UBRR2x < 0))
  87.   {
  88.     $bg = "#c7c7c7";
  89.   }
  90.   else
  91.   {
  92.     $bg = bg_color($err2x);
  93.   }
  94.   printf("<td style=\"background-color:%s;text-align=right;\">%d</td><td style=\"background-color:%s;text-align=right;\">%d</td><td style=\"background-color:%s;text-align=right;\">%.1f%%</td>",$bg,$UBRR2x,$bg,$realbaud2x,$bg,$err2x);
  95.   echo "</tr>";
  96.  }
  97.  echo "</table>";
  98. }
  99. ?>
RAW Paste Data