Page 362 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 362

11.8  UART Protocol                                                 339


            Again, we offer a C program to send and receive serial data using gadfly
        synchronization; we leave the assembly-language program as an exercise for the reader. In
        main, the SCI is initialized for gadfly synchronization of 9600 baud and eight data bits
        without parity. Reading status and data registers twice clears the RDRF flag. The put
        procedure gadflies on transmitter data register empty (TDRE); when it is empty, put
        outputs its argument. The get procedure gadflies on the receive data register full
        (RDRF); when the receive register is full, get returns the data in data port SCODRL.
        These procedures are the get and put procedures we have mentioned many times in this
        book:

        enum{ PE=Ox200, PT=OxlOO, TIE=Ox80, RIE=Ox20, TE=8, RE=4 };
        enum{ TDRE = -32768, RDRF =0x2000, OR = 8, FE = 2, PF = 1 };
        void main() { char i;
             SCOBD = 52; 7*9600 baud*/SCOCR « TE + RE;/* enable Xmt, Rev devices */
             i=SCOSR; i=SCODRL; i=SCOSR; i=SCODRL;/* clear RDRF */
             put(0x55); i = get();
        }
        put(char d) { while( ( SCOSR & TDRE ) == 0) ; SCODRL = d; }

        char get() { while( ( SCOSR & RDRF ) == 0) ; return SCODRL; }
            An interrupt-based C program below will send a character vector and receive serial
        data into a buffer; we again leave the assembly-language program as an exercise for the
        reader. In main, the SCI is initialized for interrupt synchronization at 4800 baud and
        eight data bits without parity. Reading status and data registers twice clears the RDRF
        flag. The interrupt handler outputs a byte from the character string whenever a transmitter
        data register empty (TDRE) bit is set. The handler also moves a byte from the SCIO data
        port whenever receive data register full (RDRF) bit is asserted. In this simplified example,
        we do not worry about overrunning the input buffer or output character string.

        enum{ PE = 0x200, PT = 0x100, TIE = 0x80, RIE = 0x20, TE=8, RE=4 };
        enum{ TDRE « -32768, RDRF =0x2000, OR = 8, FE = 2, PF = 1 };

        char outString[] = "Hi thereXr", inStringflO], *in, *out;
        void main() { char i;
             SCOBD = 104;/*4800baud*/SCOCR = TE + RE + RIE + TIE; //en. int.
             i=SCOSR; i=SCODRL; i=SCOSR; i=SCODRL ;/* clear RDRF */
             in = inString; out = outString; asm CLI
             do ; while(l);
        /
        void interrupt 20 hndlr(void){
             if( SCOSR & TDRE ) SCODRL = *OUt++;
             if( SCOSR & RDRF ) *in++ = SCODRL;
        }
   357   358   359   360   361   362   363   364   365   366   367