Chapter 8 Asynchronous Serial

Slides:



Advertisements
Similar presentations
Chapter 3 Message in a Bottle Programming Loops in C.
Advertisements

Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
INPUT-OUTPUT ORGANIZATION
Chapter 4 NUMB3RS.
Slides created by: Professor Ian G. Harris PIC Development Environment MPLAB IDE integrates all of the tools that we will use 1.Project Manager -Groups.
Chapter 6 Under the Hood. Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Checklist The following tools will be used in this lesson:
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
1 Homework Reading –Review previous material on “interrupts” Machine Projects –MP4 Due today –Starting on MP5 (Due at start of Class 28) Labs –Continue.
7-1 Digital Serial Input/Output Two basic approaches  Synchronous shared common clock signal all devices synchronised with the shared clock signal data.
Chapter 8 Overview Programmed I/O Introduction to Interrupt Driven I/O Project 3.
Serial Communications Standards (Partly Excerpted from Simpl Primer) Cabling Configuration Protocol.
ECE 371- Unit 11 Introduction to Serial I/O. TWO MAJOR CLASSES OF SERIAL DATA INTERFACES ASYNCHRONOUS SERIAL I/O - USES “FRAMING BITS” (START BIT AND.
Chapter 8 Communication Introduction to serial communication interfaces. Examples of use of the SPI module.
INPUT-OUTPUT ORGANIZATION
University of Tehran 1 Interface Design Serial Communications Omid Fatemi.
Chapter 1 The First Flight Creating the first project and saying “Hello to the World”
Computers in Surveying SVY2301 / E4006 Automated Surveying.
Silicon Labs ToolStick Development Platform
Chapter 5 Interrupts.
Serial Communication Lab 12 Module M21.1. Asynchronous Serial I/O ASCII code 54H = (“T”) sent with odd parity.
UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)
Chapter 3 More Loops. Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Checklist The following tools will be used in this lesson:
Chapter 2 A Loop in the Pattern Designing the Main Loop and Timing.
Page 1 D&C EBV Seminar June 2003 Motor Demo C868 Chevillot/Jansen June 2003 N e v e r s t o p t h i n k i n g. Infineon C868 Hands On Training CAPCOM6.
Industrial Reference Design Platform RS-232 Interface Developed by the TSC Americas Release 1.0.
EEE527 Embedded Systems Lecture 9: Chapter 9: UARTs (version 2 – 25/11/13 changed slides 11-17, added new at end) Ian McCrumRoom 5B18, Tel: voice.
CPSC1301 Computer Science 1 Overview of Dr. Java.
UniMAP 1 Interfacing Peripherals. UniMAP 2 Interfacing devices on Embedded Linux In general, to interface to a device connected to an embedded Linux platform.
Universal Asynchronous Receiver/Transmitter (UART)
EEE527 Embedded Systems Lecture 2: Chapter 2: C and Using more MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: voice mail on 6 th ring
Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.
PPI-8255.
Chapter 5 - Interrupts.
Microcontrollers JULES CALELLA. Microcontrollers vs Microprocessors  Microprocessors – do not contain RAM, ROM, I/O  Microcontrollers – The whole package.
Tiva C TM4C123GH6PM UART Embedded Systems ECE 4437 Fall 2015 Team 2:
Lecture 15. Modem Controller Register 4310 DTR 0 = Polling Operator 1 = Interrupts Enabled RTS 1 =Self Test 0 =Normal.
DALCON RFID IMPROVEMENT ECE 599, SPRING 2011 Brad Gasior, ECE Mike Fradkin, ECE Richard Young, ECE Sean Rinehart, ECE.
Digital Logic Design Alex Bronstein
The HCS12 SCI Subsystem A HCS12 device may have one or two serial communication interface. These two SCI interfaces are referred to as SCI0 and SCI1. The.
Homework Reading Machine Projects
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Lecture 2 Interrupts.
Serial mode of data transfer
Programming and Debugging with the Dragon and JTAG
CS-401 Computer Architecture & Assembly Language Programming
I/O SYSTEMS MANAGEMENT Krishna Kumar Ahirwar ( )
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Microcontroller basics
Microcontroller Applications
Operating Systems (CS 340 D)
Serial I/O and Data Communication.
Chapter 9 Alphanumeric Displays
Serial Communications
1 Input-Output Organization Computer Organization Computer Architectures Lab Peripheral Devices Input-Output Interface Asynchronous Data Transfer Modes.
Homework Reading Machine Projects Labs
Asynchronous Serial Communications
UART Serial Port Programming
Serial Communication Interface: Using 8251
Microcontrollers and Microprocessors
Chapter 10 It’s an Analog World
Serial Communication Interface
Debugging Debug environments Debug via serial
Transmitter Interrupts
ECE 3567 Microcontroller Lab
Software Setup & Validation
PIC Serial Port Interfacing
Chapter 13: I/O Systems.
PIC Serial Port Interfacing
Workshop for Programming And Systems Management Teachers
ECE 3567 Microcontrollers Lab
Presentation transcript:

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) 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

UART Block Diagram

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

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

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

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 }// getU2 main() 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

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

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

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 // 3.3 send a carriage return pcr(); } // main loop }// main

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 int i,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 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; default:// unchanged } // switch } // for } // main loop } // main

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: } 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!

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.

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.

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

Recommended Readings Axelson, Jan (2013), “USB Complete”, 5rd ed., Lakeview Research, Madison, WI Jan’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.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.

Online Resources http://en.wikipedia.org/wiki/ANSI_escape_code This is a link to the complete table of ANSI escape codes as implemented by the VT100 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 PIC18, PIC24 and PIC32. This is a treasure trove of examples for all device and host applications.