Chapter 9 Asynchronous Communication Using the UART module

  • Slides: 9
Download presentation
Chapter 9 Asynchronous Communication Using the UART module

Chapter 9 Asynchronous Communication Using the UART module

Simplified UART block diagram figure 19 -1 (DS 61143) Di Jasio - Programming 32

Simplified UART block diagram figure 19 -1 (DS 61143) Di Jasio - Programming 32 -bit Microcontrollers in C

Baud Rate setting In our case this translates to the following expression: U 2

Baud Rate setting In our case this translates to the following expression: U 2 BREG = (36, 000 / 4 / 115, 200) -1 = 77. 125 To decide how to best round out the result, use the reverse formula to calculate the actual baud-rate and determine the percentage error: Error = ((Fpb / 4 / (U 2 BREG + 1)) – baud rate) / baud rate % With a value of 77 -> 115, 384 Baud with an error of just 0. 2%, With a value of 78 -> 113, 924 baud, 1. 1% error, Both are within the acceptable tolerance range for a standard RS 232 port (+/- 2%). We can therefore define the constant BRATE as: #define BRATE 77 Di Jasio - Programming 32 -bit Microcontrollers in C // 115, 200 Bd (BREGH=1)

Ux. MODE register 19 -5 (DS 61143) Di Jasio - Programming 32 -bit Microcontrollers

Ux. MODE register 19 -5 (DS 61143) Di Jasio - Programming 32 -bit Microcontrollers in C

Ux. STA register 19 -6 (DS 61143) Di Jasio - Programming 32 -bit Microcontrollers

Ux. STA register 19 -6 (DS 61143) Di Jasio - Programming 32 -bit Microcontrollers in C

Initializing UART 2 #include <p 32 xxxx. h> // I/O definitions for the Explorer

Initializing UART 2 #include <p 32 xxxx. h> // I/O definitions for the Explorer 16 #define CTS _RF 12 // Clear To Send, input #define RTS _RF 13 // Request To Send, output #define TRTS TRISFbits. TRISF 13 // Tris control for RTS pin void init. U 2( { U 2 BRG = U 2 MODE = U 2 STA = TRTS = } // init. U 2 void) BRATE; U_ENABLE; U_TX; 0; 1; Di Jasio - Programming 32 -bit Microcontrollers in C // // // initialize the baud rate generator initialize the UART module enable the Transmitter make RTS an output pin set RTS default status (not ready)

Sending and Receiving Data int put. U 2( int c) { while ( CTS);

Sending and Receiving Data int put. U 2( int c) { while ( CTS); while ( U 2 STAbits. UTXBF); U 2 TXREG = c; return c; } // put. U 2 // wait for !CTS, clear to send // wait while Tx buffer full char get. U 2( void) { RTS = 0; // assert Request To Send !RTS while ( !U 2 STAbits. URXDA); // wait for a new char to arrive RTS = 1; return U 2 RXREG; // read char from receive buffer }// get. U 2 Di Jasio - Programming 32 -bit Microcontrollers in C

Hyper. Terminal Setup Di Jasio - Programming 32 -bit Microcontrollers in C

Hyper. Terminal Setup Di Jasio - Programming 32 -bit Microcontrollers in C

Tips and Tricks To re-direct the output stream of the standard C library (stdio.

Tips and Tricks To re-direct the output stream of the standard C library (stdio. h) functions such as printf() to a UART: q Define the function: _mon_putc() q q Note that a “weak” definition is already provided in the library to send the default output stream (stdout) to UART 2 (convenient for all Explorer 16 users). Similarly define: _mon_getc() q q A default “weak” version is already provided in the library as well, connecting UART 2 receiver to the input stream (stdin). Weak means that the compiler won’t complain when you define a new function with the same name, it will simply replace it with the new one you provide. NOTE q You are responsible for the UART initialization! q Before the first call to any stdio function (printf()…) make sure the UART 2 is enabled and the baud rate is set correctly. Di Jasio - Programming 32 -bit Microcontrollers in C