Page 292 - Programming Microcontrollers in C
P. 292

Timer Operations    277

                          bits, transmit enable and receive enable, that must be set to enable
                          the SCI. The following two lines of code will enable these bits.
                   SCCR2.TE=1;
                   SCCR2.RE=1;

                          These two lines of code must also be added to the initialization portion
                          of the program.
                              The code to read the data in is as follows:

                   if (SCCR.RDRF==1) /* read in data if it is there */
                   {
                       new_character=SCDR;/*get new byte and reset RDRF*/
                       while(SCCR.TDRE==0); /* wait until transmit
                                                       buffer empty */
                       SCDR=new_character;  /* send out byte and reset
                                                       TDRE */
                   }

                              This sequence of code does quite a bit more than you might expect.
                          Before this code sequence can be executed, both the RDRF and TDRE
                          flags must be set and reset. The RDRF is set when a character has
                          been received by the SCI, and the TDRE is set when the SCDR is
                          empty and can receive a character to send. Both of these bits are reset
                          by the sequential read of SCSR with the bit set followed by a read for
                          the RDRF or a write for the TDRE to the SCDR register. The above
                          sequence accomplishes all of the proper bit resets, transmits the newly
                          received character back to the sender, and leaves the new character
                          in the location new_character . After receiving the data, the
                          following sequence converts it into a binary number to be processed
                          by the remainder of the program.
                   if (SCSR.RDRF==1) /* read in data if there */
                   {
                       new_character=SCDR;/*get new byte and reset RDRF*/
                       while(SCSR.TDRE==0); /* wait until transmit
                                                       buffer empty */
                       SCDR=new_character;  /* send out byte and reset
                                                       TDRE */
                       /* if a number, process it */
                       if(new_character>=’0' && new_character <=’9')
   287   288   289   290   291   292   293   294   295   296   297