Page 424 - Programming Microcontrollers in C
P. 424

Serial Input/Output   409

                          that can be easily changed if the chip itself is changed. The defines
                          and enums set at the beginning of Listing 8-7 are from the top of the
                          serial1.c program. The values established by these devices are
                          used in the functions of Listing 8-7. These #defines and enums
                          would not be necessary if the compiler worked as expected. Generally,
                          if you want to wait until it is safe to send a character out the serial
                          port, the code that you might use is
                   while(U1SR.TDRY ==0)
                          ;    /*  wait here until TDRY is 1 */
                          This code will cause the computer to sit in a loop while the TDRY bit
                          found in U1SR is 0. Unfortunately, the code created by the compiler
                          failed to reload the value of the bit inside the loop, so the computer
                          went into an infinite loop when it executed this code.
                              There are always ways around such problems and with C it is not
                          usually necessary to jump to assembly language when such a problem
                          is found. Notice the code below. Rather than the argument shown
                          above, the argument was converted to a simple bit-wise AND, which
                          did compile correctly. Remember, the addition of a #define or an
                          enum in your code does not add or subtract from the executable
                          code in your program.
                              The first two functions shown below are  put() and
                          getchar(). The function put() sends a character to the serial
                          port number 1 on the board. It works with the function putchar(),
                          which works exactly the same as the putchar() function that you
                          are used to using in your programs. Here putchar() sends a
                          character to the serial port rather than to the device stdout. The
                          function getchar() receives a character from the serial port 1. In
                          C, when a ‘\n’ character is sent to the output the program executes
                          a carriage return and a line feed. Therefore, to make getchar()
                          here the same, you will see in the next group of functions that the
                          data received by getchar() is tested. If it is a ‘\n’ character, two
                          characters, ‘\n’ and ‘\r’, are sent to the serial port. Otherwise,
                          the character passed to the function is sent to the serial port. This
                          operation mimics the operation of the normal getchar(), but it
                          also requires a special function to output the character to the serial
                          port. The function put(BYTE x) is that function. Since the function
                          put() is never to be used outside of this file, it is designated as
                          static.
   419   420   421   422   423   424   425   426   427   428   429