Page 425 - Programming Microcontrollers in C
P. 425

410    Chapter 8  MCORE, A RISC Machine

                              The function getchar() reads a single character from the serial
                          port. This function also mimics the getchar() that you are used
                          to using, because it echoes the character received to the serial port
                          output. There might be an occasion in which you want to read in a
                          character without echoing it to the serial output. In that case, you can
                          use getch() shown below. This function works exactly the same
                          as getchar() but it does not echo the data received.
                   #define U1SRint (*(volatile unsigned short *)(0x1000a086))
                   enum {TRDYint=8192};
                   #define U1RXint (*(volatile unsigned short *)(0x1000a000))
                   enum {CHARRDYint=32768};

                   /* send a character out the serial port when it is ready */
                   static void put(BYTE x)
                   {
                       while((U1SRint & TRDYint)==0)
                          ;  /* wait until register available */
                       U1TX.DATA =x; /* send the data out */
                   }

                   /* read in and echo a character through the serial port */
                   BYTE getchar(void)
                   {
                       BYTE a;

                       while((U1RXint & CHARRDYint)==0)
                          ;  /* wait till character is ready */
                       a=U1RX.DATA;
                       putchar(a);
                       return a;
                   }

                   /* read in a character with no echo */
                   BYTE getch(void)
                   {
                       BYTE a;

                       while((U1RXint & CHARRDYint)==0)
                          ;  /* wait till character is ready */
                       a=U1RX.DATA;
                       return a;
                   }
   420   421   422   423   424   425   426   427   428   429   430