HP85 GPIB Disk Emulator  1.0
HP85GPIBDiskEmulator
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
baudrate.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 
6 // Usage: baudrate -f F_CPU -b baud
7 // F_CPU = External CPU clock
8 // Displays actual baud rate and register values
9 int main(int argc, char *argv[])
10 {
11  int i;
12  long u2x, actual,fosc,baud;
13  int foscf = 0;
14  int baudf = 0;
15  double div;
16  long ubr_regi;
17  char *ptr;
18 
19  for(i=1;i< argc;++i)
20  {
21 
22  ptr = argv[i];
23  if(*ptr == '-')
24  {
25  ++ptr;
26  if(*ptr == 'f')
27  {
28  ++ptr;
29  if(*ptr || (ptr = argv[++i]) )
30  fosc = atol(ptr);
31  foscf = 1;
32  }
33  if(*ptr == 'b')
34  {
35  ++ptr;
36  if(*ptr || (ptr = argv[++i]) )
37  baud = atol(ptr);
38  baudf = 1;
39  }
40  }
41  }
42  if(!foscf || !baudf)
43  {
44  printf("Usage: %s -f F_CPU -b baud\n", argv[0]);
45  printf(" F_CPU = External CPU clock\n");
46  printf(" Displays actual baud rate and register values\n");
47  exit(1);
48  }
49 
50 // Calculating Baud Rate
51 // (U2X = 0)
52 // BAUD = Fosc/(16*(UBRn+1))
53 // UBRn = Fosc/(16*Baud) -1
54 // (U2X = 1)
55 // BAUD = Fosc/(8*(UBRn+1))
56 // UBRn = Fosc/(8*Baud) -1
57 
59  u2x = 1;
60  div = 8;
61  ubr_regi = round( ((double)fosc/(div*(double)baud)) - 1.0 );
62 
63 // For lower baud rates use 16 divider if the UBRR register overflows
64 // URBRR register is only a 12 bit register!
65  if(ubr_regi > 4095)
66  {
68  u2x = 0;
69  div = 16.0;
70  ubr_regi = round( ((double)fosc/(div*(double)baud)) - 1.0 );
71  }
72 //overflow, baud rate was too low - so we clip to maximum allowed
73  if(ubr_regi > 4095)
74  ubr_regi = 4095;
75 
76  actual = ((double)fosc/(div*((double)(ubr_regi+1))));
77 
78  printf("fosc = %ld, baud = %ld, actual = %ld, URBRR = %ld, U2X bit = %ld\n",
79  fosc, baud, actual, ubr_regi, u2x);
80 
81  return(0);
82 }
printf
MEMSPACE int printf(const char *format,...)
atol
MEMSPACE long atol(const char *str)
Convert ASCII string to number in base 10.
Definition: mathio.c:299
main
int main(int argc, char *argv[])
ATMEGA1284p UART baud rate caluculation tests.
Definition: baudrate.c:9