Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

17
Chapter 10 Chapter 10 Glass Bliss Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays

Transcript of Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Page 1: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Chapter 10 Chapter 10 Glass BlissGlass Bliss

Using the Parallel Master Port to communicate with Alphanumeric

LCD displays

Page 2: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

Alphanumeric LCD ModulesAlphanumeric LCD Modules

Page 3: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

Instruction Code Description / Execution timeRS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0

Clear display 0 0 0 0 0 0 0 0 0 1 Clears display and returns cursor to the home position (address 0).

1.64mSCursor home 0 0 0 0 0 0 0 0 1 * Returns cursor to home position (address 0).

Also returns display being shifted to the original position. DDRAM contents remains unchanged.1.64mS

Entry mode set 0 0 0 0 0 0 0 1 I/D S Sets cursor move direction (I/D), specifies to shift the display (S). These operations are performed during data read/write. 40uS

Display On/Off 0 0 0 0 0 0 1 D C B Sets On/Off of all display (D), cursor On/Off (C) and blink of cursor position character (B).40uS

Cursor/display shift 0 0 0 0 0 1 S/C R/L * * Sets cursor-move or display-shift (S/C), shift direction (R/L). DDRAM contents remains

unchanged.40uS

Function set 0 0 0 0 1 DL N F * * Sets interface data length (DL), number of display line (N) and character font(F). 40uS

Set CGRAM addr 0 0 0 1 CGRAM address Sets the CGRAM address. CGRAM data is sent and received after this setting. 40uS

Set DDRAM addr 0 0 1 DDRAM address Sets the DDRAM address. DDRAM data is sent and received after this setting. 40uS

Read busy-flag and address ctr 0 1 BF CGRAM / DDRAM address Reads Busy-flag (BF) indicating internal

operation is being performed and reads CGRAM or DDRAM address counter contents (depending on previous instruction). 0uS

Write to CGRAM 1 0 write data Writes data to CGRAM or DDRAM. 40uS(or DDRAM)

Read CGRAM 1 1 read data Reads data from CGRAM or DDRAM. 40uS(or DDRAM)

HD44780 Instruction SetHD44780 Instruction Set

Page 4: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

HD44780 Instruction Set (cont.)HD44780 Instruction Set (cont.)

Bit name Setting / StatusI/D 0 = Decrement cursor position 1 = Increment cursor positionS 0 = No display shift 1 = Display shiftD 0 = Display off 1 = Display onC 0 = Cursor off 1 = Cursor onB 0 = Cursor blink off 1 = Cursor blink onS/C 0 = Move cursor 1 = Shift displayR/L 0 = Shift left 1 = Shift rightDL 0 = 4-bit interface 1 = 8-bit interfaceN 0 = 1/8 or 1/11 Duty (1 line) 1 = 1/16 Duty (2 lines)F 0 = 5x7 dots 1 = 5x10 dotsBF 0 = Can accept instruction 1 = Internal operation in progress

Page 5: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

Character Generator TableCharacter Generator Table

Page 6: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

Parallel Master PortParallel Master Port

Page 7: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

PMCONPMCON

PMCON Register 20-1 (DS61143)

Page 8: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

LCD InitializationLCD Initialization#define LCDDATA 1 // RS = 1 ; access data register#define LCDCMD 0 // RS = 0 ; access command register#define PMDATA PMDIN1 // PMP data buffer

void LCDinit( void){ // PMP initialization PMCON = 0x83BF; // Enable the PMP, long waits PMMODE = 0x3FF; // Master Mode 1 PMPEN = 0x0001; // PMA0 enabled  PMADDR = LCDCMD; // command register (ADDR = 0) PMDATA = 0x38; // set: 8-bit interface, 2 lines, 5x7 TMR1 = 0; while( TMR1<8); // 8 x 6us = 48us PMDATA = 0x0c; // ON, no cursor, no blink TMR1 = 0; while( TMR1<8); // 8 x 6us = 48us PMDATA = 0x01; // clear display TMR1 = 0; while( TMR1<300); // 300 x 6us = 1.8ms PMDATA = 0x06; // increment cursor, no shift TMR1 = 0; while( TMR1<300); // 300 x 6us = 1.8ms } // LCDinit

Page 9: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

Reading the LCDReading the LCD

char readLCD( int addr){ int dummy; while( PMMODEbits.BUSY); // wait for PMP to be available PMADDR = addr; // select the command address dummy = PMDATA; // init read cycle, dummy read 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)

Page 10: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

Writing to the LCDWriting to the LCD

void writeLCD( int addr, char c) { while( busyLCD()); while( PMMODEbits.BUSY); // wait for PMP to be available PMADDR = addr; PMDATA = c;} // writeLCD

#define busyLCD() readLCD( LCDCMD) & 0x80#define addrLCD() readLCD( LCDCMD) & 0x7F#define getLCD() readLCD( LCDDATA)

Page 11: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

Extending the “Include Search Path”Extending the “Include Search Path”

Page 12: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

LCD Control Using the peripheral library LCD Control Using the peripheral library void initLCD( void){ // PMP initialization mPMPOpen( PMP_ON | PMP_READ_WRITE_EN | 3, PMP_DATA_BUS_8 | PMP_MODE_MASTER1 | PMP_WAIT_BEG_4 | PMP_WAIT_MID_15 | PMP_WAIT_END_4, 0x0001, // only PMA0 enabled PMP_INT_OFF); // no interrupts used // wait for >30ms Delayms( 30); //initiate the HD44780 display 8-bit init sequence PMPSetAddress( LCDCMD); // select command register PMPMasterWrite( 0x38); // 8-bit int, 2 lines, 5x7 Delayms( 1); // > 48 us PMPMasterWrite( 0x0c); // ON, no cursor, no blink Delayms( 1); // > 48 us PMPMasterWrite( 0x01); // clear display Delayms( 2); // > 1.6ms PMPMasterWrite( 0x06); // increment cursor, no shift Delayms( 2); // > 1.6ms } // initLCD

Page 13: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

LCD Control Using the peripheral library LCD Control Using the peripheral library

char readLCD( int addr){ PMPSetAddress( addr); // select register mPMPMasterReadByte(); // initiate read sequence return mPMPMasterReadByte();// read actual data} // readLCD  void writeLCD( int addr, char c) { while( busyLCD()); PMPSetAddress( addr); // select register PMPMasterWrite( c); // initiate write sequence} // writeLCD

Page 14: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

putsLCD()putsLCD()void putsLCD( char *s){ char c; while( *s) { switch (*s) { case '\n': // point to second line setLCDC( 0x40); break; case '\r': // home, point to first line setLCDC( 0); break; case '\t': // advance next tab (8) positions c = addrLCD(); while( c & 7) { putLCD( ' '); c++; } if ( c > 15) // if necessary move to second line setLCDC( 0x40); break; default: // print character putLCD( *s); break; } //switch s++; } //while } //putsLCD

Page 15: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

Advanced LCD ControlAdvanced LCD Control

#define setLCDG( a) writeLCD( LCDCMD, (a & 0x3F) | 0x40)

Page 16: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

Progress BarProgress Barvoid newBarTip( int i, int width){ char bar; int pos; // save cursor position while( busyLCD()); pos = addrLCD();  // generate a new character at position i // set the data pointer to the LCD CGRAM buffer setLCDG( i*8); // as a horizontal bar (0-4)x thick moving left to right // 7 pixel tall if ( width > 4) width = 0; else width = 4 - width; for( bar=0xff; width > 0; width--) bar<<=1; // bar >>= 1; if right to left  // fill each row (8) with the same pattern putLCD( bar); putLCD( bar); putLCD( bar); putLCD( bar); putLCD( bar); putLCD( bar); putLCD( bar); putLCD( bar); // restore cursor position setLCDC( pos);} // newBarTip 

Page 17: Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.

Di Jasio - Programming 32-bit Microcontrollers in C

Progress Bar (cont.)Progress Bar (cont.)void drawProgressBar( int index, int imax, int size){ // index is the current progress value // imax is the maximum value // size is the number of character positions available int i; // scale the input values in the available space int width = index * (size*5) / imax; // generate a character to represent the tip newBarTip( TIP, width % 5); // user defined character 0   // draw a bar of solid blocks for ( i=width/5; i>0; i--) putLCD( BRICK); // filled block character  // draw the tip of the bar putLCD( TIP); // use character 0 } // drawProgressBar