1 / 17

Chapter 8 Asynchronous Serial

Chapter 8 Asynchronous Serial. Half and Full Duplex Communication with Terminals and other Devices. Checklist. The following tools will be used in this lesson: MPLAB X IDE (v1.8 or later, free) MPLAB XC16, C compiler (v1.11 or later, free) Explorer 16/32 demo board (or equivalent)

dhealey
Download Presentation

Chapter 8 Asynchronous Serial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Checklist The following tools will be used in this lesson: • MPLAB X IDE (v1.8 or later, free) • MPLAB XC16, C compiler (v1.11 or later, free) • Explorer 16/32 demo board (or equivalent) • Serial to USB adapter (or an old laptop with RS232 ports) • TeraTerm, Cool Term, Hyperterm (or any equivalent terminal application for your operating system) The following pieces of documentation will be used during this lesson: • PIC24FJ128GA010 Datasheet –DS39747 (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/xc16

  3. UART Block Diagram

  4. Configuration Parameters • The first step is the definition of the transmission parameters, the options include: • 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: • 115,200 baud • 8 data bit • No parity • 1 stop bit • Hardware handshake using the CTS and RTS lines

  5. Special Function Registers UxMODE – Mode Control Register UxSTA – Status Register

  6. UART Configuration example /* ** Serial.c ** UART2 RS232 asynchronous communication demonstration */ #include <config.h> // I/O definitions for the Explorer16 #define CTS _RF12 // Cleart To Send, in, handshake #define RTS _RF13 // Request To Send, out, handshake #define TRTS TRISFbits.TRISF13 // tris control for RTS pin #define BRATE 34 // 115,200 Baud (BREGH=1)#define U_ENABLE 0x8008 // enable the UART peripheral#define U_TX 0x0400 // enable transmission void InitU2( void) { U2BRG = BRATE; U2MODE = U_ENABLE; U2STA = U_TX; RTS = 1; // set RTS default status TRTS = 0; // make RTS output } // InitU2

  7. Sending and Receiving Example // serial.c continued int 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 RTS = 1; return U2RXREG; // read from the receive buffer }// getU2main() { char c; // 1. init the UART2 serial port InitU2(); // 2. prompt putU2( '>'); // 3. main loop while ( 1) { // 3.1 wait for a character c = getU2(); // 3.2 echo the character putU2( c); } // main loop }// main

  8. Sending and Receiving Lines void putsU2( char *s) { while( *s) // loop until *s == '\0' the end of the string putU2( *s++); // send the character and point to the next one putU2( '\r'); // terminate with a cr / line feed putU2( '\n'); } // putsU2 char *getsnU2( char *s, int len) { char *p = s; // copy the buffer pointer do{ *s = getU2(); // wait for a new character if ( *s=='\r') // end of line, end loop break; s++; // increment buffer pointer len--; } while ( len>1 ); // until buffer full *s = '\0'; // null terminate the string return p; // return buffer pointer } // getsnU2

  9. VT100 Terminal Curses • These commands are performed by sending so called escape sequences • Defined in the ECMA-48 standard (ISO/IEC 6429 and ANSI X3.64), also referred to as ANSI escape codes. • They all start with the characters ESC (ASCII 0x1b) and the character ‘[‘ (left squared bracket). // useful macros #define Clrscr() putsU2( "\x1b[2J") // Clear the screen #define Home() putsU2( "\x1b[1;1H") // return cursor home #define pcr() putU2( '\r' ); putU2( '\n') // carriage return

  10. Sending and Receiving Lines /* ** CONU2Test.c ** UART2 RS232 asynchronous communication demonstration */ #include <stdio.h> #include <config.h> #include <CONU2.h> #define BUF_SIZE 128 main() { char s[ BUF_SIZE]; // 1. init the console serial port InitU2(); // 2. text prompt Clrscr(); Home(); putsU2( "Learn to fly with the PIC24!\r\n"); sprintf( s, "Learn to fly the PIC24! %d\r\n", 17); putsU2( s); // 3. main loop while ( 1) { // 3.1 read a full line of text getsnU2( s, sizeof(s)); // 3.2 send a string to the serial port putsU2( s); // 3.3 send a carriage return pcr(); } // main loop }// main

  11. The Matrix /* ** The Matrix */ #include <config.h> #include <CONU2.h> #include <stdlib.h> #define COL 40 #define ROW 23 #define DELAY 3000 main() { int v[40]; // vector containing length of each string inti,j,k; // 1. initializations T1CON = 0x8030; // TMR1 on, prescale 256, Tcy/2 InitU2(); // initialize the console Clrscr(); // clear the terminal (VT100 emulation) getU2(); // wait for character to randomize sequence srand( TMR1); // 2. init each column length for( j =0; j<COL; j++) v[j] = rand()%ROW; // 3. main loop while( 1) { Home(); // 3.1.1 delay to slow down the screen update TMR1 =0; while( TMR1<DELAY); // 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]) putU2( 33 + (rand()%94)); else putU2(' '); putU2( ' '); } // for j pcr(); } // for i // 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; case 1: // decrease length v[j]--; if (v[j]<1) v[j]=1; break; default:// unchanged break; } // switch } // for } // main loop } // main

  12. 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 UART2 peripheral, use the following code: /* ** write.c ** replaces stdio lib write function with UART2 */ #include <p24fxxxx.h> #include <stdio.h> #include <CONU2.h> int write(int handle, void *buffer, unsigned int len) { int i; switch (handle) { case 0: case 1: case 2: i = len; while( i--) putU2( *(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!

  13. Notes for PIC Expertes • Several PIC24 microcontroller models (the PIC24FJxxGB1/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 PIC24FJxxGAxx series used in this book. You can replace the PIM on the Explorer16 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.

  14. Tips and Tricks • When debugging an application (using ICD3 or PICKit3) 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.

  15. Suggested Excercises • Write a console library with buffered I/O (using interrupts) to minimize the impact on program execution (and debugging).

  16. Recommended Readings • Axelson, Jan (2013), “USB Complete”, 5rd ed., Lakeview Research, Madison, WIJan’s book has reached the 5th edition already. She has kept adding more material at every step and still managed to keep things very simple. • Di Jasio, L. (2013), “Graphics, Touch, Sound and USB”, Lulu.comThis 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.

  17. Online Resources • http://en.wikipedia.org/wiki/ANSI_escape_codeThis is a link to the complete table of ANSI escape codes as implemented by the VT100 terminal emulation • http://www.microchip.com/malThis is a link to the Microchip Library for Applications (MLA) containing the complete USB Framework for the PIC18, PIC24 and PIC32. This is a treasure trove of examples for all device and host applications.

More Related