' ============================================================================== ' ' File...... Ex11 - LCD Demo.BS2 ' Purpose... Essential LCD control ' Author.... Parallax ' E-mail.... stamptech@parallaxinc.com ' Started... ' Updated... 01 MAY 2002 ' ' {$STAMP BS2} ' ' ============================================================================== ' ------------------------------------------------------------------------------ ' Program Description ' ------------------------------------------------------------------------------ ' This program demonstrates essential character LCD control. ' ' 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. ' ------------------------------------------------------------------------------ ' I/O Definitions ' ------------------------------------------------------------------------------ E CON 8 ' LCD Enable pin (1 = enabled) RS CON 3 ' Register Select (1 = char) LCDbus VAR OUTB ' 4-bit LCD data bus DQ CON 0 ' DS1620.1 (data I/O) Clock CON 1 ' DS1620.2 Reset CON 2 ' DS1620.3 ' ------------------------------------------------------------------------------ ' Constants ' ------------------------------------------------------------------------------ ClrLCD CON $01 ' clear the LCD CrsrHm CON $02 ' move cursor to home position 'CrsrLf CON $10 ' move cursor left 'CrsrRt CON $14 ' move cursor right DispLf CON $18 ' shift displayed chars left DispRt CON $1C ' shift displayed chars right DDRam CON $80 ' Display Data RAM control RdTmp CON $AA ' read temperature WrHi CON $01 ' write TH (high temp) WrLo CON $02 ' write TL (low temp) RdHi CON $A1 ' read TH RdLo CON $A2 ' read TL StartC CON $EE ' start conversion StopC CON $22 ' stop conversion WrCfg CON $0C ' write config register RdCfg CON $AC ' read config register ' ------------------------------------------------------------------------------ ' Variables ' ------------------------------------------------------------------------------ char VAR BYTE ' character sent to LCD index VAR BYTE ' loop counter tempIn VAR WORD ' raw temperature sign VAR tempIn.BIT8 ' 1 = negative temperature tSign VAR BIT tempC VAR WORD ' Celsius tempF VAR WORD ' Fahrenheit ' ------------------------------------------------------------------------------ ' EEPROM Data ' ------------------------------------------------------------------------------ Msg DATA "THE BASIC STAMP!", 0 ' preload EEPROM with message ' ------------------------------------------------------------------------------ ' Initialization ' ------------------------------------------------------------------------------ Initialize: DIRL = %11111101 ' setup pins for LCD HIGH Reset ' alert the DS1620 SHIFTOUT DQ, Clock, LSBFIRST, [WrCfg, %10] ' use with CPU; free-run LOW Reset PAUSE 10 HIGH Reset SHIFTOUT DQ, Clock, LSBFIRST, [StartC] ' start conversions LOW Reset LCD_Init: PAUSE 500 ' let the LCD settle LCDbus = %0011 ' 8-bit mode PULSOUT E, 1 PAUSE 5 PULSOUT E, 1 PULSOUT E, 1 LCDbus = %0010 ' 4-bit mode PULSOUT E, 1 char = %00001100 ' disp on, crsr off, blink off GOSUB LCD_Command char = %00000110 ' inc crsr, no disp shift GOSUB LCD_Command ' ------------------------------------------------------------------------------ ' Program Code ' ------------------------------------------------------------------------------ Main: GOSUB Get_Temperature ' read the DS1620 char = ClrLCD ' clear the LCD GOSUB LCD_Command PAUSE 500 index = tempC ' get EE address of message Read_Char: READ index, char ' get character from EEPROM IF (char = 0) THEN Msg_Done ' if 0, message is complete GOSUB LCD_Write ' write the character index = index + 1 ' point to next character GOTO Read_Char ' go get it Msg_Done: ' the message is complete PAUSE 10 ' wait 2 seconds char = CrsrHm ' move the cursor home GOSUB LCD_Command char = %00001110 ' turn the cursor on GOSUB LCD_Command PAUSE 10 char = CrsrRt FOR index = 1 TO 15 ' move the cursor accross display GOSUB LCD_Command PAUSE 10 NEXT FOR index = 14 TO 0 ' go backward by moving cursor char = DDRam + index ' to a specific address GOSUB LCD_Command PAUSE 10 NEXT char = %00001101 ' cursor off, blink on GOSUB LCD_Command PAUSE 10 char = %00001100 ' blink off GOSUB LCD_Command FOR index = 1 TO 10 ' flash display char = char ^ %00000100 ' toggle display bit GOSUB LCD_Command PAUSE 10 NEXT PAUSE 10 FOR index = 1 TO 16 ' shift display char = DispRt GOSUB LCD_Command PAUSE 10 NEXT PAUSE 10 FOR index = 1 TO 16 ' shift display back char = DispLf GOSUB LCD_Command PAUSE 10 NEXT PAUSE 10 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 Get_Temperature: HIGH Reset ' alert the DS1620 SHIFTOUT DQ, Clock, LSBFIRST, [RdTmp] ' give command to read temp SHIFTIN DQ, Clock, LSBPRE, [tempIn\9] ' read it in LOW Reset ' release the DS1620 tSign = sign ' save sign bit tempIn = tempIn / 2 ' round to whole degrees IF (tSign = 0) THEN No_Neg1 tempIn = tempIn | $FF00 ' extend sign bits for negative No_Neg1: tempC = tempIn ' save Celsius value tempIn = tempIn */ $01CC ' multiply by 1.8 IF (tSign = 0) THEN No_Neg2 ' if negative, extend sign bits tempIn = tempIn | $FF00 No_Neg2: tempIn = tempIn + 32 ' finish C -> F conversion tempF = tempIn ' save Fahrenheit value RETURN