' ========================================================================= ' ' File....... SW21-EX12-LCD_Chars.BS2 ' Purpose.... Create and use customer characters on the LCD ' Author..... (C) 2000 - 2005, Parallax, Inc. ' E-mail..... support@parallax.com ' Started.... ' Updated.... 16 AUG 2006 ' ' {$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-family LCDCMD, ' LCDIN, and LCDOUT instructions. Use this program for the BS2, BS2e, ' or BS2sx. There is a separate program for the BS2p, BS2pe, and BS2px. ' -----[ I/O Definitions ]------------------------------------------------- E PIN 1 ' Enable pin RW PIN 2 ' Read/Write RS CON 3 ' Register Select LcdBus VAR OUTB ' 4-bit LCD data bus ' -----[ Constants ]------------------------------------------------------- LcdCls CON $01 ' clear the LCD LcdHome CON $02 ' move cursor home LcdCrsrL CON $10 ' move cursor left LcdCrsrR CON $14 ' move cursor right LcdDispL CON $18 ' shift chars left LcdDispR CON $1C ' shift chars right LcdDDRam CON $80 ' Display Data RAM control LcdCGRam CON $40 ' Character Generator RAM LcdLine1 CON $80 ' DDRAM address of line 1 LcdLine2 CON $C0 ' DDRAM address of line 2 #DEFINE _LcdReady = ($STAMP >= BS2P) ' -----[ Variables ]------------------------------------------------------- char VAR Byte ' character sent to LCD newChar VAR Byte idx1 VAR Byte ' loop counters idx2 VAR Nib ' -----[ EEPROM Data ]----------------------------------------------------- Msg1 DATA "THE BASIC STAMP " ' preload EE with messages Msg2 DATA " IS VERY COOL! ", 3 CC0 DATA %01110 ' mouth 0 DATA %11111 DATA %11100 DATA %11000 DATA %11100 DATA %11111 DATA %01110 DATA %00000 CC1 DATA %01110 ' mouth 1 DATA %11111 DATA %11111 DATA %11000 DATA %11111 DATA %11111 DATA %01110 DATA %00000 CC2 DATA %01110 ' mouth 2 DATA %11111 DATA %11111 DATA %11111 DATA %11111 DATA %11111 DATA %01110 DATA %00000 Smiley DATA %00000 ' smiley face DATA %01010 DATA %01010 DATA %00000 DATA %10001 DATA %01110 DATA %00110 DATA %00000 ' -----[ Initialization ]-------------------------------------------------- Reset: #IF _LcdReady #THEN #ERROR "Please use BS2p version: SW21-EX12-LCD_Chars.BSP" #ENDIF DIRL = %11111110 ' setup pins for LCD PAUSE 100 ' let the LCD settle Lcd_Setup: LcdBus = %0011 ' 8-bit mode PULSOUT E, 3 PAUSE 5 PULSOUT E, 3 PULSOUT E, 3 LcdBus = %0010 ' 4-bit mode PULSOUT E, 1 char = %00101000 ' multi-line mode GOSUB LCD_Cmd char = %00001100 ' disp on, no crsr or blink GOSUB LCD_Cmd char = %00000110 ' inc crsr, no disp shift GOSUB LCD_Cmd Download_Chars: ' download custom chars char = LcdCGRam ' point to CG RAM GOSUB LCD_Cmd ' prepare to write CG data FOR idx1 = CC0 TO (Smiley + 7) ' build 4 custom chars READ idx1, char ' get byte from EEPROM GOSUB LCD_Out ' put into LCD CG RAM NEXT ' -----[ Program Code ]---------------------------------------------------- Main: char = LcdCls ' clear the LCD GOSUB LCD_Cmd PAUSE 250 FOR idx1 = 0 TO 15 ' get message from EEPROM READ (Msg1 + idx1), char ' read a character GOSUB LCD_Out ' write it NEXT PAUSE 1000 ' wait 2 seconds Animation: FOR idx1 = 0 TO 15 ' cover 16 characters READ (Msg2 + idx1), newChar ' get new char from Msg2 FOR idx2 = 0 TO 4 ' 5 characters in cycle char = LcdLine2 + idx1 ' set new DDRAM address GOSUB LCD_Cmd ' move cursor position LOOKUP idx2, [0, 1, 2, 1, newChar], char ' get animation "frame" GOSUB LCD_Out ' write "frame" PAUSE 100 ' animation delay NEXT NEXT PAUSE 2000 GOTO Main ' do it all over ' -----[ Subroutines ]----------------------------------------------------- LCD_Cmd: LOW RS ' enter command mode LCD_Out: LcdBus = char.HIGHNIB ' output high nibble PULSOUT E, 3 ' strobe the Enable line LcdBus = char.LOWNIB ' output low nibble PULSOUT E, 3 HIGH RS ' return to character mode RETURN