Chapter 9 Asynchronous Communication

  • Upload
    nishan

  • View
    32

  • Download
    2

Embed Size (px)

DESCRIPTION

Chapter 9 Asynchronous Communication. Using the UART module. Simplified UART block diagram. figure 19-1 (DS61143). Baud Rate setting. In our case this translates to the following expression: U2BREG = (36,000,000 / 4 / 115,200) -1 = 77.125 - PowerPoint PPT Presentation

Citation preview

  • Chapter 9 Asynchronous CommunicationUsing the UART module

    Di Jasio - Programming 32-bit Microcontrollers in C

    Simplified UART block diagramfigure 19-1 (DS61143)

    Di Jasio - Programming 32-bit Microcontrollers in C

    Baud Rate settingIn our case this translates to the following expression:U2BREG = (36,000,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 / (U2BREG + 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 RS232 port (+/- 2%) .We can therefore define the constant BRATE as:

    #define BRATE 77 // 115,200 Bd (BREGH=1)

    Di Jasio - Programming 32-bit Microcontrollers in C

    UxMODE registerregister 19-5 (DS61143)

    Di Jasio - Programming 32-bit Microcontrollers in C

    UxSTA registerregister 19-6 (DS61143)

    Di Jasio - Programming 32-bit Microcontrollers in C

    Initializing UART2 #include // I/O definitions for the Explorer16#define CTS _RF12 // Clear To Send, input#define RTS _RF13 // Request To Send, output#define TRTS TRISFbits.TRISF13 // Tris control for RTS pinvoid initU2( void){ U2BRG = BRATE; // initialize the baud rate generator U2MODE = U_ENABLE; // initialize the UART module U2STA = U_TX; // enable the Transmitter TRTS = 0; // make RTS an output pin RTS = 1; // set RTS default status (not ready)} // initU2

    Di Jasio - Programming 32-bit Microcontrollers in C

    Sending and Receiving Dataint putU2( int c){ while ( CTS); // wait for !CTS, clear to send while ( U2STAbits.UTXBF); // wait while Tx buffer full U2TXREG = c; return c;} // putU2

    char getU2( void){ RTS = 0; // assert Request To Send !RTS while ( !U2STAbits.URXDA); // wait for a new char to arrive RTS = 1; return U2RXREG; // read char from receive buffer}// getU2

    Di Jasio - Programming 32-bit Microcontrollers in C

    HyperTerminal Setup

    Di Jasio - Programming 32-bit Microcontrollers in C

    Tips and TricksTo re-direct the output stream of the standard C library (stdio.h) functions such as printf() to a UART:

    Define the function: _mon_putc() Note that a weak definition is already provided in the library to send the default output stream (stdout) to UART2 (convenient for all Explorer16 users).

    Similarly define: _mon_getc() A default weak version is already provided in the library as well, connecting UART2 receiver to the input stream (stdin).Weak means that the compiler wont complain when you define a new function with the same name, it will simply replace it with the new one you provide.

    NOTEYou are responsible for the UART initialization! Before the first call to any stdio function (printf()) make sure the UART2 is enabled and the baud rate is set correctly.

    *