Page 423 - Programming Microcontrollers in C
P. 423

408    Chapter 8  MCORE, A RISC Machine

                   #define CLOCK_FREQ 32000000L

                   /* Initialize the UART1 */
                   void inituart(int baud)
                   {
                       /* Parity is disabled at reset by default */
                       /* stop bits is set to 1 by default */
                       /* assumes a 32 mHz system clock */
                       U1CR1.UARTEN=ON;  /* Turn the uart on */
                       U1CR1.TXEN=ON;       /* enable transmitter and receiver */
                       U1CR1.RXEN=ON;
                       U1CR2.IRTS=ON;       /* ignore request to send */
                       U1CR2.WS=ON;         /* 8 bit word */
                       /* Baud rate=CLOCK_FREQ/16/baud= */
                       U1BRGR.CD=(UHWORD)(CLOCK_FREQ/16/baud);
                       U1PCR.PC3=ON;        /* connect all i/o pins to the uarts */
                       U1PCR.PC2=ON;
                       U1PCR.PC1=ON;
                       U1PCR.PC0=ON;
                       U1DDR.PDC1=ON;       /* make output pin for the uart */
                   }
                              Listing 8-6: The inituart() Function
                              Each of these functions will have the same three header functions
                          shown in Listing 8-6. In this case, the function requires the clock
                          speed of the chip. This speed can be anything, and in our particular
                          case, it is 32 MHz. The macro definition CLOCK_FREQ makes this
                          value 32000000. If you ever change the clock speed of the computer,
                          this value should be changed. The several instructions in the function
                          are all commented with their operations. This function should be
                          executed whenever the serial I/O system is to be used by a program.
                          Pass the desired baud rate to the function as an integer. The integer
                          size on the DIAB compiler used for this system is 32 bits; therefore,
                          there will be no overflow problem for any of the usual choices for
                          baud rates.
                              Following the inituart() function above, there is the series
                          of five functions shown in Listing 8-7. These functions are all placed
                          together because they each access on-board features of the MCORE
                          chip. Almost always, it is best to collect operations that access on-
                          board registers that control peripheral devices into individual functions
                          that can be called from the applications portion of the program. This
                          way, the detailed features of the chip are tightly contained in functions
   418   419   420   421   422   423   424   425   426   427   428