Page 427 - Programming Microcontrollers in C
P. 427

412    Chapter 8  MCORE, A RISC Machine

                       {
                          put(‘\n’);
                          put(‘\r’);
                       }
                       else
                          put(x);
                   }

                   /* send a string to the serial port */
                   void puts(BYTE *a)
                   {
                       while(*a!=’\0')
                          putchar(*a++);
                   }

                   /* This function reads a string into the buffer a. The
                       length of the buffer is max. If the string is less than
                       max long, the function returns the number of characters
                       entered. If the string is longer than the buffer, the
                       buffer is filled and a -1 is returned. The input string
                       is terminated by either a ‘\n’ or a ‘\r’. */

                   int gets(BYTE *a,int max) /* no echo */
                   {
                       int i=0,c;

                       do  /* read in data a byte at a time */
                       {
                          *a++=c=getch();
                       }while(c!=’\n’&&c!=’\r’&&++i<max-1);
                       *a=’\0';     /* make it a string */
                       return (i>=max)?-1:i;
                   }

                   /* same as gets() but data entered are echoed */
                   int getse(BYTE *a,int max) /* with echo */
                   {
                       int i=0,c;

                       do
                       {
                          *a++=c=getchar();
                       }while(c!=’\n’&&c!=’\r’&&++i<max-1);
                       *a=’\0';
                       return (i>=max)?-1:i;
                   }
                              Listing 8-8: General Serial Input/Output Functions
   422   423   424   425   426   427   428   429   430   431   432