#include const double BaudTable[] = { 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200, 230400}; int within3percent (double baud) { double minval, maxval; int retval, i; retval = 0; for (i = 0; i < 10; i++) { minval = BaudTable[i] * 0.97; maxval = BaudTable[i] * 1.03; if ((baud >= minval) && (baud <= maxval)) retval = 1; } return (retval); } void main (void) { int SPBRG; double temp1; int spbrg0, spbrg1; double baud0, baud1; float OSCILLATOR; printf ("Enter the crystal speed (ex: 4 MHz = 4000000):"); scanf ("%f", &OSCILLATOR); printf ("CPU Speed = %7.4f MHz\nSPBRG\t BRGH=0\t BRGH=1\n", OSCILLATOR / 1000000); for (SPBRG = 255; SPBRG != 0; SPBRG--) { temp1 = (SPBRG+1.0) * 64.0; baud0 = OSCILLATOR / temp1; spbrg0 = within3percent (baud0); temp1 = (SPBRG+1.0) * 16.0; baud1 = OSCILLATOR / temp1; spbrg1 = within3percent (baud1); if ((spbrg0) || (spbrg1)) { printf ("%3d\t", SPBRG); if (spbrg0) printf ("%9.2f\t", baud0); else printf (" \t"); if (spbrg1) printf ("%9.2f\n", baud1); else printf ("\n"); // printf ("%d\t%9.2f%s\t%9.2f%s\n",SPBRG,baud0,((spbrg0)?" OK":" "),baud1,((spbrg1)?" OK":" ")); } } }