Chapter 8 Asynchronous Serial Half and Full Duplex

  • Slides: 17
Download presentation
Chapter 8 Asynchronous Serial Half and Full Duplex Communication with Terminals and other Devices

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

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)

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

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

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

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)

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.

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 == '' the end of the string // send the character and point to the next one // terminate with a cr / line feed char *getsn. U 2( char *s, int len( } char *p = s; // copy the buffer pointer do} * s = get. U 2(); // wait for a new character { * if ( *s=='r') break ; s++; len; -while ( len>1 ); s = ''; return p; // {getsn. U 2 // end of line, end loop // increment buffer pointer // until buffer full // null terminate the string // return buffer pointer Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)

VT 100 Terminal Curses q q q These commands are performed by sending so

VT 100 Terminal Curses q q q These commands are performed by sending so called escape sequences Defined in the ECMA-48 standard (ISO/IEC 6429 and ANSI X 3. 64), also referred to as ANSI escape codes. They all start with the characters ESC (ASCII 0 x 1 b) and the character ‘[‘ (left squared bracket). // useful macros #define Clrscr() puts. U 2( "x 1 b[2 J") #define Home() puts. U 2( "x 1 b[1; 1 H") #define pcr() put. U 2( 'r' ); put. U 2( 'n') Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition) // Clear the screen // return cursor home // carriage return

Sending and Receiving Lines /* ** CONU 2 Test. c ** UART 2 RS

Sending and Receiving Lines /* ** CONU 2 Test. c ** UART 2 RS 232 asynchronous communication demonstration */ #include <stdio. h> #include <config. h> #include <CONU 2. h> #define BUF_SIZE 128 main() { char s[ BUF_SIZE]; // 1. init the console serial port Init. U 2(); // 2. text prompt Clrscr(); Home(); puts. U 2( "Learn to fly with the PIC 24!rn"); sprintf( s, "Learn to fly the PIC 24! %drn", 17); puts. U 2( s); // 3. main loop while ( 1) { // 3. 1 read getsn. U 2( s, // 3. 2 send puts. U 2( s); // 3. 3 send pcr(); } // main loop }// main a full line of text sizeof(s)); a string to the serial port a carriage return Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)

The Matrix /* ** The Matrix */ #include <config. h> #include <CONU 2. h>

The Matrix /* ** The Matrix */ #include <config. h> #include <CONU 2. h> #include <stdlib. h> // 3. 1 refresh the screen with random columns for( i=0; i<ROW; i++) { // refresh one row at a time for( j=0; j<COL; j++) { // print random characters for each column length if ( i < v[j]) put. U 2( 33 + (rand()%94)); else put. U 2(' '); put. U 2( ' '); } // for j pcr(); } // for i #define COL 40 #define ROW 23 #define DELAY 3000 main() { int v[40]; int i, j, k; // vector containing length of each string // 1. initializations T 1 CON = 0 x 8030; // TMR 1 on, prescale 256, Tcy/2 Init. U 2(); // initialize the console Clrscr(); // clear the terminal (VT 100 emulation) get. U 2(); // wait for character to randomize sequence srand( TMR 1); 3. 2 //randomly increase or reduce each column lenght for( j=0; j<COL; j(++ } switch ( rand()%3( } case 0: // increase length v[j; ++[ if (v[j]>ROW( v[j]=ROW; break; // 2. init each column length for( j =0; j<COL; j++) v[j] = rand()%ROW; case 1: // decrease length v[j; --[ if (v[j]<1( v[j]=1; break; . 3 // main loop while( 1( } Home; () 3. 1. 1 // delay to slow down the screen update TMR 1 =0; while( TMR 1<DELAY) ; // { default: // unchanged break; switch for // { main loop // {main Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)

Notes for the C Experts In order to use the standard I/O functions defined

Notes for the C Experts In order to use the standard I/O functions defined in the stdio. h library (such as printf ) and to direct the output to the UART 2 peripheral, use the following code: /* ** write. c ** replaces stdio lib write function with UART 2 */ #include <p 24 fxxxx. h> #include <stdio. h> #include <CONU 2. h> int write(int handle, void *buffer, unsigned int len) { int i; switch (handle) { case 0: case 1: case 2: i = len; while( i--) put. U 2( *(char*)buffer++); break; default: break; } return(len); } Save this code in a file called ‘write. c’ in your project directory and add it to the list of source files for the project! Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)

Notes for PIC Expertes q q q Several PIC 24 microcontroller models (the PIC

Notes for PIC Expertes q q q Several PIC 24 microcontroller models (the PIC 24 FJxx. GB 1/2 series for example) incorporate a USB Serial Interface Engine (SIE) as a standard communication interface. They are mostly pin-to-pin compatible with the PIC 24 FJxx. GAxx series used in this book. You can replace the PIM on the Explorer 16 demonstration board. Unfortunately the USB peripheral interface is much more complex to use than a UART. You can find USB support, documentation and example codes in the Microchip Library for Applications. In the book ‘Graphic, Touch, Sound and USB” (Di Jasio, Lulu. com) you will find an introduction to the most common classes of USB applications and a guide to the MLA libraries configuration. Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)

Tips and Tricks q q When debugging an application (using ICD 3 or PICKit

Tips and Tricks q q When debugging an application (using ICD 3 or PICKit 3) after executing each instruction in single-step mode or, upon encountering a breakpoint, the debugger not only stops the CPU execution, but also freezes all the peripherals. When this happens to a UART peripheral that is busy in the middle of a transmission, the output serial line (TX) is also frozen in the current state. If a bit was being shifted out in that precise instant, and specifically if it was a 1, the TX line will be held in the break state (low) indeterminately. Some Terminal applications sense this prolonged break condition and interpret it as a line error. When you resume your program execution, make sure to hit the terminal ‘Disconnect’ button first and then the ‘Connect’ button again. All operations will resume normally. Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)

Suggested Excercises q Write a console library with buffered I/O (using interrupts) to minimize

Suggested Excercises q Write a console library with buffered I/O (using interrupts) to minimize the impact on program execution (and debugging). Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)

Recommended Readings q Axelson, Jan (2013), “USB Complete”, 5 rd ed. , Lakeview Research,

Recommended Readings q Axelson, Jan (2013), “USB Complete”, 5 rd ed. , Lakeview Research, Madison, WI Jan’s book has reached the 5 th edition already. She has kept adding more material at every step and still managed to keep things very simple. q Di Jasio, L. (2013), “Graphics, Touch, Sound and USB”, Lulu. com This book contains an introduction to the most used USB classes of applications and a guide to the configuration and use of the MLA USB Library. Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)

Online Resources q q http: //en. wikipedia. org/wiki/ANSI_escape_code This is a link to the

Online Resources q q http: //en. wikipedia. org/wiki/ANSI_escape_code This is a link to the complete table of ANSI escape codes as implemented by the VT 100 terminal emulation http: //www. microchip. com/mal This is a link to the Microchip Library for Applications (MLA) containing the complete USB Framework for the PIC 18, PIC 24 and PIC 32. This is a treasure trove of examples for all device and host applications. Di Jasio – Programming 16 -bit Microcontrollers in C (Second Edition)