' ============================================================================== ' ' File...... Ex12 - LCD Characters.BS2 ' Purpose... Custom LCD Characters ' Author.... Parallax ' E-mail.... support@parallax.com ' Started... ' Updated... 01 JAN 2003 ' ' {$STAMP BS2} ' {$PBASIC 2.5} ' ' ============================================================================== ' -----[ Program Description ]-------------------------------------------------- ' This program demonstrates custom character creation and animation on a ' character LCD. ' ' The connections for this program conform to the BS2p LCDIN and LCDOUT ' commands. Use this program for the BS2, BS2e or BS2sx. There is a ' separate program for the BS2p and BS2pe. ' -----[ I/O Definitions ]------------------------------------------------------ E PIN 1 ' LCD Enable pin (1 = enabled) RW PIN 2 ' Read/Write (0 = Write) RS PIN 3 ' Register Select (1 = char) LCDbus VAR OUTB ' 4-bit LCD data bus ' -----[ Constants ]------------------------------------------------------------ ClrLCD CON $01 ' clear the LCD CrsrHm CON $02 ' move cursor to home position CrsrL CON $10 ' move cursor left CrsrR CON $14 ' move cursor right DispL CON $18 ' shift displayed chars left DispR CON $1C ' shift displayed chars right DDRam CON $80 ' Display Data RAM control CGRam CON $40 ' Custom character RAM Line1 CON $80 ' DDRAM address of line 1 Line2 CON $C0 ' DDRAM address of line 2 ' -----[ Variables ]------------------------------------------------------------ char VAR Byte ' character sent to LCD newChar VAR Byte ' new character for animation idx1 VAR Byte ' loop counter idx2 VAR Byte ' loop counter ' -----[ EEPROM Data ]---------------------------------------------------------- Msg1 DATA "THE BASIC STAMP ", 0 ' preload EEPROM with messages Msg2 DATA " IS VERY COOL! ", 3, 0 CC0 DATA $0E, $1F, $1C, $18, $1C, $1F, $0E, $00 ' character 0 CC1 DATA $0E, $1F, $1F, $18, $1F, $1F, $0E, $00 ' character 1 CC2 DATA $0E, $1F, $1F, $1F, $1F, $1F, $0E, $00 ' character 2 Smiley DATA $00, $0A, $0A, $00, $11, $0E, $06, $00 ' smiley face ' -----[ Initialization ]------------------------------------------------------- Initialize: DIRL = %11111110 ' setup pins for LCD LCD_Init: PAUSE 500 ' let the LCD settle LCDbus = %0011 ' 8-bit mode PULSOUT E, 1 : PAUSE 5 PULSOUT E, 1 : PAUSE 0 PULSOUT E, 1 : PAUSE 0 LCDbus = %0010 ' 4-bit mode PULSOUT E, 1 char = %00101000 ' multi-line mode GOSUB LCD_Command char = %00001100 ' disp on, crsr off, blink off GOSUB LCD_Command char = %00000110 ' inc crsr, no disp shift GOSUB LCD_Command Download_Chars: ' download custom chars to LCD char = CGRam ' point to CG RAM GOSUB LCD_Command ' prepare to write CG data FOR idx1 = CC0 TO (Smiley + 7) ' build 4 custom chars READ idx1, char ' get byte from EEPROM GOSUB LCD_Write ' put into LCD CG RAM NEXT ' -----[ Program Code ]--------------------------------------------------------- Main: char = ClrLCD ' clear the LCD GOSUB LCD_Command PAUSE 250 FOR idx1 = 0 TO 15 ' get message from EEPROM READ (Msg1 + idx1),char ' read a character GOSUB LCD_Write ' write it NEXT PAUSE 2000 ' wait 2 seconds Animation: FOR idx1 = 0 TO 15 ' cover 16 characters READ (Msg2 + idx1), newChar ' get new char from 2nd message FOR idx2 = 0 TO 4 ' 5 characters in animation cycle char = Line2 + idx1 ' set new DDRAM address GOSUB LCD_Command LOOKUP idx2, [0, 1, 2, 1, newChar], char GOSUB LCD_Write ' write animation character PAUSE 50 ' delay between animation chars NEXT NEXT PAUSE 1000 GOTO Main ' do it all over END ' -----[ Subroutines ]---------------------------------------------------------- LCD_Command: LOW RS ' enter command mode LCD_Write: LCDbus = char.HIGHNIB ' output high nibble PULSOUT E, 1 ' strobe the Enable line LCDbus = char.LOWNIB ' output low nibble PULSOUT E, 1 HIGH RS ' return to character mode RETURN