Page 422 - Programming Microcontrollers in C
P. 422

Serial Input/Output   407

                   /* return TRUE when a key has been hit and FALSE otherwise */
                   int kbhit(void)
                   {
                       return U1RX.CHARRDY;
                   }



                   /* convert a long to a series of ascii characters
                      and send it out the serial port */
                   void printd(unsigned long x)
                   {
                       if(x/10)
                          printd(x/10);
                       putchar(x%10+’0');
                   }

                   /* same as above but output hexidecimal */
                   void printx(unsigned long x)
                   {
                       if(x/16)
                          printx(x/16);
                       putchar(x%16+((x%16>9)?(‘a’):(‘0’)));
                   }
                                 Listing 8-5: Serial Input/Output Functions

                              At the beginning of the program, the usual headers are included.
                          In this case, mmc2001.h is needed to identify the address of the
                          UART, the header uart.h contains all of the definitions needed for
                          the UART registers, and the serial.h header contains function
                          prototypes for all of the serial input/output functions. These functions
                          are for demonstration purposes only. They all work. I have used these
                          functions at 115200 bit rates and had no problems. None of them
                          have any built-in tests for errors on reception like parity tests, framing
                          errors, or overrun errors. These tests need to be included and the
                          errors handled properly if you want to have a production quality
                          input/output library.
                              In the text that follows, each of the several small functions
                          contained in the above listing will be described individually. The
                          first function is the inituart() function.

                   #include “mmc2001.h”
                   #include “uart.h”
                   #include “serial.h”
   417   418   419   420   421   422   423   424   425   426   427