Chapter 8 Asynchronous Serial Half and Full Duplex

















- Slides: 17

Chapter 8 Asynchronous Serial Half and Full Duplex Communication with Terminals and other Devices

Checklist The following tools will be used in this lesson: q q q MPLAB X IDE (v 1. 8 or later, free) MPLAB XC 16, C compiler (v 1. 11 or later, free) Explorer 16/32 demo board (or equivalent) Serial to USB adapter (or an old laptop with RS 232 ports) Tera. Term, Cool Term, Hyperterm (or any equivalent terminal application for your operating system) The following pieces of documentation will be used during this lesson: q PIC 24 FJ 128 GA 010 Datasheet –DS 39747 (latest rev. ) Make sure they are available and/or installed and ready to use on your computer. You can download them from Microchip web site at: http: //www. microchip. com/mplabx And http: //www. microchip. com/xc 16 Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)

UART Block Diagram Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)

Configuration Parameters q The first step is the definition of the transmission parameters, the options include: q q q Baud rate Number of data bits Parity bit, if present Number of stop bits Handshake protocol For our demo we will choose the fast and convenient configuration: “ 115200, 8, N, 1, CTS/RTS”, that is: q q q 115, 200 baud 8 data bit No parity 1 stop bit Hardware handshake using the CTS and RTS lines Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)

Special Function Registers Ux. MODE –Mode Control Register Ux. STA –Status Register Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)

UART Configuration example /* ** Serial. c ** UART 2 RS 232 asynchronous communication demonstration */ #include <config. h> // I/O definitions for the Explorer 16 #define CTS _RF 12 // Cleart To Send, in, handshake #define RTS _RF 13 // Request To Send, out, handshake #define TRTS TRISFbits. TRISF 13 // tris control for RTS pin #define BRATE 34 #define U_ENABLE 0 x 8008 #define U_TX 0 x 0400 void Init. U 2( void( } U 2 BRG = BRATE ; U 2 MODE = U_ENABLE; U 2 STA = U_TX; RTS = 1; TRTS = 0; // {Init. U 2 // 115, 200 Baud (BREGH=1) // enable the UART peripheral // enable transmission // set RTS default status // make RTS output Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)

Sending and Receiving Example // serial. c continued int put. U 2( int c) { while ( CTS); while ( U 2 STAbits. UTXBF); U 2 TXREG = c; return c; // wait for !CTS, clear to send // wait while Tx buffer full } // put. U 2 char get. U 2( void) { RTS = 0; while ( !U 2 STAbits. URXDA); RTS = 1; return U 2 RXREG; }// get. U 2 // assert Request To Send !RTS // wait // read from the receive buffer main() } char c; . 1 // init the UART 2 serial port Init. U 2; (). 2 // prompt put. U 2; ('<' ) . 3 // main loop while ( 1( } 3. 1 // 3. 2 // // { //{main wait for a character c = get. U 2; () echo the character put. U 2( c; ( main loop Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)

Sending and Receiving Lines void puts. U 2( char *s) { while( *s) put. U 2( *s++); put. U 2( 'r'); put. U 2( 'n'); } // puts. U 2 // loop until *s == '