Looking for a little help Ping module and LCD
rgmullins
Posts: 1
I'm using a BS2e with a ping module to keep track of the water in my cistern without having to go outside and open the lid. I have very little programming experience but after way too many hours have managed to get it to work, but I can only display whole numbers. I can't figure out how to handle the fractional part of the depth calculation. The LCD module I'm using is the 2 line x 8 character AppMod 29121. Any help would be greatly appreciated!
' =========================================================================
'
' File...... Parallel LCD Vars Appmod.bs2
' Purpose... Display Variables On Parallel LCD
' Author.... Chris Savage - Savage///Circuits
' E-mail.... chris@savagecircuits.com
' Started... 01-12-2009
' Updated...
' License... See End of Code
'
' {$STAMP BS2e}
' {$PBASIC 2.5}
'
' =========================================================================
'
[ Program Description ]
' This program demonstrates how to display up to a 16-bit numeric variable
' on a parallel LCD using the StampWorks Parallel LCD code as framework.
' To use this code, connect your LCD AppMod as per the documentation and
' then download and run this code. This version strips leading zeros in
' values printed.
'
[ 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
'
[ Variables ]
char VAR Byte ' character sent to LCD
idx VAR Byte ' loop counter
counter VAR Word ' counter variable
index VAR Byte ' index counter
flag VAR Bit ' flag variable
time VAR Word ' returned ping value
depth VAR Byte ' calculated depth
depth_ft VAR Byte ' calculated depth in ft
'
[ EEPROM Data ]
Msg DATA "DEPTH FT:", 0 ' store message
'Two DATA "#:", 0 ' message two
'
[ Initialization ]
Reset:
DIRL = %11111110 ' setup pins for LCD
PAUSE 100 ' let the LCD settle
LCD_Init:
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 ' 2-line mode
GOSUB LCD_Cmd
char = %00001100 ' on, no crsr, no blink
GOSUB LCD_Cmd
char = %00000110 ' inc crsr, no disp shift
GOSUB LCD_Cmd
'
[ Program Code ]
Main:
char = LcdCls ' clear the LCD
GOSUB LCD_Cmd
PAUSE 500
idx = Msg ' get EE address of message
GOSUB Write_Message
PAUSE 2000 ' wait 2 seconds
char = LcdLine2 ' switch to line 2
GOSUB LCD_Cmd
'PAUSE 500
'idx = Two ' get EE address of message
'GOSUB Write_Message
'FOR counter = 0 TO 10 ' count from 0 to 10
DO
PULSOUT 15, 5
PULSIN 15, 1, time
depth = time ** 890
depth_ft = depth / 12
char = LcdLine2 ' position cursor after
GOSUB LCD_Cmd ' the word counter
GOSUB Write_Var ' pass value in counter
PAUSE 100
'NEXT
LOOP
STOP
'
[ Subroutines ]
Write_Message:
DO
READ idx, char ' get character from EE
IF (char = 0) THEN EXIT ' if 0, message is complete
GOSUB LCD_Out ' write the character
idx = idx + 1 ' point to next character
LOOP
RETURN
Write_Var:
flag = 0
' FOR index = 4 TO 0 ' handle 5 digits
char = depth_ft ' check value of 5 digits
IF char THEN flag = 1 ' is digit a zero?
IF flag = 1 THEN ' strip leading zeros only
char = char + $30 ' if not add ASCII offset
GOSUB LCD_Out ' print it
ENDIF
'NEXT
RETURN
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
' =========================================================================
' Copyright (c) 2013 Chris Savage - Savage///Circuits
'
' Permission is hereby granted, free of charge, to any person obtaining a
' copy of this Software and associated documentation files, to deal in the
' Software without restriction, including without limitation the rights to
' use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
' The above copyright notice and this permission notice shall be included
' in all copies or substantial portions of the Software.
' =========================================================================
'
' File...... Parallel LCD Vars Appmod.bs2
' Purpose... Display Variables On Parallel LCD
' Author.... Chris Savage - Savage///Circuits
' E-mail.... chris@savagecircuits.com
' Started... 01-12-2009
' Updated...
' License... See End of Code
'
' {$STAMP BS2e}
' {$PBASIC 2.5}
'
' =========================================================================
'
[ Program Description ]
' This program demonstrates how to display up to a 16-bit numeric variable
' on a parallel LCD using the StampWorks Parallel LCD code as framework.
' To use this code, connect your LCD AppMod as per the documentation and
' then download and run this code. This version strips leading zeros in
' values printed.
'
[ 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
'
[ Variables ]
char VAR Byte ' character sent to LCD
idx VAR Byte ' loop counter
counter VAR Word ' counter variable
index VAR Byte ' index counter
flag VAR Bit ' flag variable
time VAR Word ' returned ping value
depth VAR Byte ' calculated depth
depth_ft VAR Byte ' calculated depth in ft
'
[ EEPROM Data ]
Msg DATA "DEPTH FT:", 0 ' store message
'Two DATA "#:", 0 ' message two
'
[ Initialization ]
Reset:
DIRL = %11111110 ' setup pins for LCD
PAUSE 100 ' let the LCD settle
LCD_Init:
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 ' 2-line mode
GOSUB LCD_Cmd
char = %00001100 ' on, no crsr, no blink
GOSUB LCD_Cmd
char = %00000110 ' inc crsr, no disp shift
GOSUB LCD_Cmd
'
[ Program Code ]
Main:
char = LcdCls ' clear the LCD
GOSUB LCD_Cmd
PAUSE 500
idx = Msg ' get EE address of message
GOSUB Write_Message
PAUSE 2000 ' wait 2 seconds
char = LcdLine2 ' switch to line 2
GOSUB LCD_Cmd
'PAUSE 500
'idx = Two ' get EE address of message
'GOSUB Write_Message
'FOR counter = 0 TO 10 ' count from 0 to 10
DO
PULSOUT 15, 5
PULSIN 15, 1, time
depth = time ** 890
depth_ft = depth / 12
char = LcdLine2 ' position cursor after
GOSUB LCD_Cmd ' the word counter
GOSUB Write_Var ' pass value in counter
PAUSE 100
'NEXT
LOOP
STOP
'
[ Subroutines ]
Write_Message:
DO
READ idx, char ' get character from EE
IF (char = 0) THEN EXIT ' if 0, message is complete
GOSUB LCD_Out ' write the character
idx = idx + 1 ' point to next character
LOOP
RETURN
Write_Var:
flag = 0
' FOR index = 4 TO 0 ' handle 5 digits
char = depth_ft ' check value of 5 digits
IF char THEN flag = 1 ' is digit a zero?
IF flag = 1 THEN ' strip leading zeros only
char = char + $30 ' if not add ASCII offset
GOSUB LCD_Out ' print it
ENDIF
'NEXT
RETURN
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
' =========================================================================
' Copyright (c) 2013 Chris Savage - Savage///Circuits
'
' Permission is hereby granted, free of charge, to any person obtaining a
' copy of this Software and associated documentation files, to deal in the
' Software without restriction, including without limitation the rights to
' use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
' The above copyright notice and this permission notice shall be included
' in all copies or substantial portions of the Software.