180 likes | 248 Views
Chapter 9 Alphanumeric Displays. “ Glass = Bliss ”. Checklist. The following tools will be used in this lesson: MPLAB X, Integrated Development Environment (v1.8 or later, free) MPLAB XC16, C compiler (v1.11 or later, free)
E N D
Chapter 9 Alphanumeric Displays “Glass = Bliss”
Checklist The following tools will be used in this lesson: • MPLAB X, Integrated Development Environment (v1.8 or later, free) • MPLAB XC16, C compiler (v1.11 or later, free) 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 Additional Hardware Tools: • Explorer 16/32 evaluation board • or equivalent board with 2x16 alphanumeric display (HD44780 compatible) • ICD3 In circuit Programmer Debugger (or ICD4, Real-ICE)
Initializing the Display #define LCDDATA 1 #define LCDCMD 0 #define PMDATA PMDIN1 void InitLCD( void) { // PMP initialization PMCON = 0x83BF; // Enable the PMP, long waits PMMODE = 0x3FF; // Master Mode 1 PMAEN = 0x0001; // PMA0 enabled // init TMR1 T1CON = 0x8030; // Fosc/2, 1:256, 16us/tick // wait for >30ms TMR1 = 0; while( TMR1<2000);// 2000 x 16us = 32ms PMADDR = LCDCMD; // command register PMDATA = 0b00111000; // 8-bit, 2 lines, 5x7 TMR1 = 0; while( TMR1<3); // 3 x 16us = 48us PMDATA = 0b00001100; // ON, cursor off, blink off TMR1 = 0; while( TMR1<3); // 3 x 16us = 48us PMDATA = 0b00000001; // clear display TMR1 = 0; while( TMR1<100); // 100 x 16us = 1.6ms PMDATA = 0b00000110; // increment cursor, no shift TMR1 = 0; while( TMR1<100); // 100 x 16us = 1.6ms } //InitLCD
Reading and Writing char ReadLCD( intaddr) { int dummy; while( PMMODEbits.BUSY); // wait for PMP to be available PMADDR = addr; // select the command address dummy = PMDATA; // initiate a read cycle, dummy while( PMMODEbits.BUSY); // wait for PMP to be available return( PMDATA); // read the status register } // ReadLCD #define BusyLCD() ReadLCD( LCDCMD) & 0x80 #define AddrLCD() ReadLCD( LCDCMD) & 0x7F #define getLCD() ReadLCD( LCDDATA) void WriteLCD( intaddr, char c) { while( BusyLCD()); while( PMMODEbits.BUSY); // wait for PMP to be available PMADDR = addr; PMDATA = c; } // WriteLCD
Testing the Display #define putLCD( d) WriteLCD( LCDDATA, (d)) #define CmdLCD( c) WriteLCD( LCDCMD, (c)) #define HomeLCD() WriteLCD( LCDCMD, 2) #define ClrLCD() WriteLCD( LCDCMD, 1) void putsLCD( char *s) { while( *s) putLCD( *s++); } // putsLCD main( void) { // initializations InitLCD(); // put a title on the first line putsLCD( "Flying the PIC24"); // main loop, empty for now while ( 1) { } } // main
Defining Custom Characters // useful macros #define SetLCDG( a) writeLCD( LCDCMD, (a & 0x3F) | 0x40) // position cursor in CGRAM #define setLCDC( a) writeLCD( LCDCMD, (a & 0x7F) | 0x80) // position cursor in DDRAM // generate two new characters SetLCDG(0); putLCD( 0b00010); putLCD( 0b00010); putLCD( 0b00110); putLCD( 0b11111); putLCD( 0b00110); putLCD( 0b00010); putLCD( 0b00010); putLCD( 0); // alignment putLCD( 0b00000); putLCD( 0b00100); putLCD( 0b01100); putLCD( 0b11100); putLCD( 0b00000); putLCD( 0b00000); putLCD( 0b00000); putLCD( 0); // alignment
Flying the PIC24 // fly airplane, fly! while( 1) { // the entire plane appears at the right margin setLCDC(0x40+14); putLCD( 0); putLCD( 1); DELAY(); // fly fly fly (from right to left) for( i=13; i>=0; i--) { setLCDC(0x40+i); // cursor to next position putLCD(0); putLCD(1); // new airplane putLCD(' '); // erase the previous tail DELAY(); } // the tip disappears off the left margin, // only the tail is visible setLCDC(0x40); putLCD( 1); putLCD(' '); DELAY(); // erase the tail setLCDC(0x40); // point to left margin 2nd line putLCD(' '); // and draw just the tip appearing from the right setLCDC(0x40+15); // point to right margin 2nd line putLCD( 0); DELAY(); } // repeat the main loop
Notes for User Interface Experts The PIC24 family of microcontrollers has expanded considerably in the last few years and includes now a number of models (PIC24F DA2) that offer two important new features: • The Charge Time Measurement Unit (CTMU), which allows very precise control of capacitive inputs as required in touch screen applications • An integrated Graphic Controller, including 3 hardware acceleration units that allow for very effective interface to C-STN and TFT (color) graphic displays For all PIC24 microcontrollers that offer the basic PMP port there is also the possibility to interface directly to Mobile Displays that integrate both the graphic controller and all the RAM required (several hundred Kbytes) to maintain a 16-bit color QVGA or larger image. The complexity of developing any advanced graphical user interface is greatly reduced by the availability of a complete (object oriented) graphic library (GOL) as part of the Microchip Libraries for Application (MLA).
Notes for C Experts it is possible to replace the low level I/O routines defined in the stdio.h library, and in particular write.c, to re-direct the output to the LCD display as in the example code below: #include <p24fxxxx.h> #include <stdio.h> #include <LCD.h> int write(int handle, void *buffer, unsigned int len) { int i, *p; const char *pf; switch (handle) { case 0: case 1: case 2: for (i = len; i; --i) putLCD( *(char*)buffer++); break; default: break; } return(len); }
Tips and Tricks Since the LCD display is a slow peripheral, waiting for its commands to be completed in tight (locking) loops constitute a waste of MCU cycles. A better scheme allows for LCD commands to be cached in a FIFO buffer and an interrupt mechanism to periodically schedule their execution in the background of the main application execution. Hint: An example of such a mechanism is provided in the LCD.c example code provided with the Explorer16 demonstration board.
Suggested Excercises Enhance the putLCD() function to interpret correctly the following characters: • ‘\n’: advance to the next line • ‘\r’: re-position cursor to the beginning of current line • ‘\t’: advance to a fixed tabulation position Enhance the putLCD() function to interpret the following ANSI escape codes: • ‘\x1b[2J’: clear entire screen • ‘\x1b[1;1H’: home cursor • ‘\x1b[n;mH’: position the cursor at row ‘n’, column ‘m’
Recommended Readings Di Jasio, L. (2013), “Graphics, Touch, Sound and USB”, Lulu.com • This book contains an introduction to use and configuration/customization of the Primitives and the Graphic Object Library (GOL) contained in the Microhip Libraries for Applications (MLA). Wilbert O. Galitz, “The essential guide to User Interface Design”, Wiley • Designing user interfaces (graphical or not) is a science, a little planning can goes a long way in making your application more usable and effective.
Online Resources • http://www.microchip.com/graphicsThis is the design center dedicated to graphic displays interfaces • http://www.microchip.com/mlaThis is the Microchip Libraries for Applications page. It contains the Graphic Library that offers an object oriented library for the creation of graphical user interfaces using 16-bit color diplays and touch sensing inputs. • http://www.microchip.com/PICTailPlusThis is the catalog of expansion boards available for the Explorer16/32. Among them the Graphics PICTail Plus (AC164127) will allow you to quickly evaluate color graphic applications.