Shop OBEX P1 Docs P2 Docs Learn Events
~~!!!!!!Show the total number to the LCD (2x8)><??? I have write a program,but — Parallax Forums

~~!!!!!!Show the total number to the LCD (2x8)><??? I have write a program,but

davidchoidavidchoi Posts: 7
edited 2009-04-20 02:59 in BASIC Stamp
i buy the LCD appmod 2x8 ( http://img151.imagevenue.com/img.php?image=16089_SBII_collect_2X8LCD_122_413lo.JPG )
i use basic stamp to do the coin electronic saving box
and i want to use LCD appmod 2x8 to display total coin in the saving box
coin it will be $1 $2 $5 and $10

The 4 button of the LCD is use to +$1,+$2 ,+$5 ,+ $10,
(When i put the coin into the saving box, the coin will push the button)
When push the 4 button, it will +1, +2,+5,+10, to sum up the total number,
LCD need show the total number.

Now,i write a program, But it just can calculate the total number in the Dubug Terminal when i push the button.
It can't show to the LCD, LCD will show some error code.
SO,i want ask, i how can show the total sum of number to LCD when i push the bottom?
Help me please!!


this is my program:






' {$STAMP BS2}
' {$PBASIC 2.5}
'
' =========================================================================
'
[noparse][[/noparse] Program Description ]
'
' This program demonstrates the use of the Parallax LCD Terminal AppMod
'
'
[noparse][[/noparse] I/O Definitions ]

E PIN 1 ' LCD Enable (1 = enabled)
RW PIN 2 ' Read/Write\
RS PIN 3 ' Reg Select (1 = char)
LcdDirs VAR DIRB ' dirs for I/O redirection
LcdBusOut VAR OUTB
LcdBusIn VAR INB

'
[noparse][[/noparse] 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
LcdScrollTm CON 250 ' LCD scroll timing (ms)

'
[noparse][[/noparse] Variables ]
value1 VAR Word
value2 VAR Word
value3 VAR Word
value4 VAR Word
value5 VAR Word
addr VAR Word ' address pointer
crsrPos VAR Byte ' cursor position
char VAR Byte ' character sent to LCD
idx VAR Byte ' loop counter
scan VAR Byte ' loop counter
buttons VAR Nib
btnA VAR buttons.BIT0 ' left-most button
btnB VAR buttons.BIT1
btnC VAR buttons.BIT2
btnD VAR buttons.BIT3 ' right-most
btnDemo VAR Byte ' loop counter




value1 = 0
value2 = 1
value3 = 2
value4 = 5
value5 = 10


'
[noparse][[/noparse] Initialization ]

Initialize:
NAP 5 ' let LCD self-initialize
DIRL = %11111110 ' setup pins for LCD

LCD_Init:
LcdBusOut = %0011 ' 8-bit mode
PULSOUT E, 3 : PAUSE 5
PULSOUT E, 3 : PAUSE 0
PULSOUT E, 3 : PAUSE 0
LcdBusOut = %0010 ' 4-bit mode
PULSOUT E, 3
char = %00101000 ' 2-line mode
GOSUB LCD_Command
char = %00001100 ' on, no crsr, no blink
GOSUB LCD_Command
char = %00000110 ' inc crsr, no disp shift
GOSUB LCD_Command


'
[noparse][[/noparse] Program Code ]

Main:

char = LcdCls ' clear the LCD
GOSUB LCD_Command
PAUSE 500



DO

FOR btnDemo = 1 TO 100
GOSUB LCD_Get_Buttons


DEBUG SDEC ? value1

char = LcdLine1
GOSUB LCD_Command

FOR idx = 0 TO 3 ' display buttons
IF buttons.LOWBIT(idx) THEN

IF idx=0 THEN value1 = value1 + value2
IF idx=1 THEN value1 = value1 + value3
IF idx=2 THEN value1 = value1 + value4
IF idx=3 THEN value1 = value1 + value5
ENDIF
WRITE_value:
addr = value1
GOSUB LCD_Put_String
NEXT
NEXT
LOOP
'GOTO Main ' run demo again
END

'
[noparse][[/noparse] Subroutines ]
' Writes stored (in DATA statement) zero-terminated string to LCD
' -- position LCD cursor
' -- point to 0-terminated string (first location in 'addr')

LCD_Put_String:
DO
READ addr, char
IF (char = 0) THEN EXIT
GOSUB LCD_Write_Char
addr = addr + 1
LOOP
RETURN

LCD_Put_String_2:
DO
READ addr, char
IF (char = 0) THEN EXIT
GOSUB LCD_Write_Char
addr = addr
LOOP
RETURN


' Send command to LCD
' -- put command byte in 'char'

LCD_Command: ' write command to LCD
LOW RS

' Write character to current cursor position
' -- but byte to write in 'char'

LCD_Write_Char: ' write character to LCD
LcdBusOut = char.HIGHNIB ' output high nibble
PULSOUT E, 3 ' strobe the Enable line
LcdBusOut = char.LOWNIB ' output low nibble
PULSOUT E, 3
HIGH RS ' return to character mode
RETURN




' Reads byte from LCD
' -- put byte address in 'addr'
' -- returns byte read in 'char'

LCD_Read_Char: ' read character from LCD
char = addr ' move cursor
GOSUB LCD_Command
HIGH RS ' data command
HIGH RW ' read
LcdDirs = %0000 ' make LCD bus inputs
HIGH E
char.HIGHNIB = LcdBusIn ' get high nibble
LOW E
HIGH E
char.LOWNIB = LcdBusIn ' get low nibble
LOW E
LcdDirs = %1111 ' return data lines to outputs
LOW RW
RETURN

' Read and debounce the LCD AppMod buttons

LCD_Get_Buttons:

LcdDirs = %0000 ' make LCD bus inputs
buttons = %1111 ' assume all pressed
FOR scan = 1 TO 10
buttons = buttons & LcdBusIn ' make sure button held
PAUSE 5 ' debounce 10 x 5 ms
NEXT
LcdDirs = %1111 ' return bus to outputs
RETURN

Post Edited (davidchoi) : 4/18/2009 2:52:25 PM GMT

Comments

  • FranklinFranklin Posts: 4,747
    edited 2009-04-18 14:36
    get the LCD working with the demo code before you try to use it in a program. Has the LCD ever worked correctly with any program? If so which one?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • davidchoidavidchoi Posts: 7
    edited 2009-04-18 14:48
    Franklin said...
    get the LCD working with the demo code before you try to use it in a program. Has the LCD ever worked correctly with any program? If so which one?





    yes~~My teacher have help me write a demo program~~it is work~
    If put the 4 button,,LCD will show "$1" or "$2" or "$5" or "10"
    But it have not sum all of the total.
    Now, i change it program to When i push button, it will +1, +2,+5,+10, to sum up the total number,
    LCD need show the total number.





    this is my teacher demo program:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    ' =========================================================================
    '
    [noparse][[/noparse] Program Description ]
    '
    ' This program demonstrates the use of the Parallax LCD Terminal AppMod
    '
    '
    [noparse][[/noparse] I/O Definitions ]

    E PIN 1 ' LCD Enable (1 = enabled)
    RW PIN 2 ' Read/Write\
    RS PIN 3 ' Reg Select (1 = char)
    LcdDirs VAR DIRB ' dirs for I/O redirection
    LcdBusOut VAR OUTB
    LcdBusIn VAR INB

    '
    [noparse][[/noparse] 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
    LcdScrollTm CON 250 ' LCD scroll timing (ms)

    '
    [noparse][[/noparse] Variables ]

    addr VAR Word ' address pointer
    crsrPos VAR Byte ' cursor position
    char VAR Byte ' character sent to LCD
    idx VAR Byte ' loop counter
    scan VAR Byte ' loop counter
    buttons VAR Nib
    btnA VAR buttons.BIT0 ' left-most button
    btnB VAR buttons.BIT1
    btnC VAR buttons.BIT2
    btnD VAR buttons.BIT3 ' right-most
    btnDemo VAR Byte ' loop counter

    Msg1 DATA "Coin", 0
    Msg2 DATA "$ 1", 0
    Msg3 DATA "$ 2", 0
    Msg4 DATA "$ 5", 0
    Msg5 DATA "$10", 0

    '
    [noparse][[/noparse] Initialization ]

    Initialize:
    NAP 5 ' let LCD self-initialize
    DIRL = %11111110 ' setup pins for LCD

    LCD_Init:
    LcdBusOut = %0011 ' 8-bit mode
    PULSOUT E, 3 : PAUSE 5
    PULSOUT E, 3 : PAUSE 0
    PULSOUT E, 3 : PAUSE 0
    LcdBusOut = %0010 ' 4-bit mode
    PULSOUT E, 3
    char = %00101000 ' 2-line mode
    GOSUB LCD_Command
    char = %00001100 ' on, no crsr, no blink
    GOSUB LCD_Command
    char = %00000110 ' inc crsr, no disp shift
    GOSUB LCD_Command


    '
    [noparse][[/noparse] Program Code ]

    Main:
    char = LcdCls ' clear the LCD
    GOSUB LCD_Command
    PAUSE 500
    Write_Total:
    addr = Msg1 ' point to message
    GOSUB LCD_Put_String ' write it
    'PAUSE 1000

    'Show_Buttons:
    'char = LcdCls ' clear the LCD
    'GOSUB LCD_Command
    'PAUSE 100
    'addr = Msg4 ' write "Buttons:"
    'GOSUB LCD_Put_String

    DO
    FOR btnDemo = 1 TO 100
    GOSUB LCD_Get_Buttons ' read/debounce buttons
    char = LcdLine2 + 2 ' show on 2nd line
    GOSUB LCD_Command
    FOR idx = 0 TO 3 ' display buttons
    IF buttons.LOWBIT(idx) THEN
    IF idx=0 THEN addr = Msg2
    IF idx=1 THEN addr = Msg3
    IF idx=2 THEN addr = Msg4
    IF idx=3 THEN addr = Msg5 ' button letter if pressed
    ENDIF
    GOSUB LCD_Put_String
    NEXT
    NEXT
    LOOP
    'GOTO Main ' run demo again
    END

    '
    [noparse][[/noparse] Subroutines ]
    ' Writes stored (in DATA statement) zero-terminated string to LCD
    ' -- position LCD cursor
    ' -- point to 0-terminated string (first location in 'addr')

    LCD_Put_String:
    DO
    READ addr, char
    IF (char = 0) THEN EXIT
    GOSUB LCD_Write_Char
    addr = addr + 1
    LOOP
    RETURN

    ' Send command to LCD
    ' -- put command byte in 'char'

    LCD_Command: ' write command to LCD
    LOW RS

    ' Write character to current cursor position
    ' -- but byte to write in 'char'

    LCD_Write_Char: ' write character to LCD
    LcdBusOut = char.HIGHNIB ' output high nibble
    PULSOUT E, 3 ' strobe the Enable line
    LcdBusOut = char.LOWNIB ' output low nibble
    PULSOUT E, 3
    HIGH RS ' return to character mode
    RETURN

    ' Reads byte from LCD
    ' -- put byte address in 'addr'
    ' -- returns byte read in 'char'

    LCD_Read_Char: ' read character from LCD
    char = addr ' move cursor
    GOSUB LCD_Command
    HIGH RS ' data command
    HIGH RW ' read
    LcdDirs = %0000 ' make LCD bus inputs
    HIGH E
    char.HIGHNIB = LcdBusIn ' get high nibble
    LOW E
    HIGH E
    char.LOWNIB = LcdBusIn ' get low nibble
    LOW E
    LcdDirs = %1111 ' return data lines to outputs
    LOW RW
    RETURN

    ' Read and debounce the LCD AppMod buttons

    LCD_Get_Buttons:
    LcdDirs = %0000 ' make LCD bus inputs
    buttons = %1111 ' assume all pressed
    FOR scan = 1 TO 10
    buttons = buttons & LcdBusIn ' make sure button held
    PAUSE 5 ' debounce 10 x 5 ms
    NEXT
    LcdDirs = %1111 ' return bus to outputs
    RETURN

    Post Edited (davidchoi) : 4/18/2009 2:53:22 PM GMT
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-04-19 01:41
    Check your display button loop.

    My·suggestions to you:

    1. Read IF...THEN commad carefully. You are using·"statement" ·where "address" should be.

    ·· The compiler will not flag that as error.

    ·· Your code should read

    ·· IF condition THEN

    ····· statement

    ·· ELSEIF condition THEN

    ···· statement

    ··· etc.

    ·· ENDIF

    2. Always use·DEBUG and STOP to trace your code to the point where it stops working.



    There is probably nothing wrong with your LCD code, I did not check that.

    Good luck in your learning.

    Cheers

    Vaclav
  • davidchoidavidchoi Posts: 7
    edited 2009-04-19 14:01
    vaclav_sal said...
    Check your display button loop.

    My suggestions to you:

    1. Read IF...THEN commad carefully. You are using "statement" where "address" should be.

    The compiler will not flag that as error.

    Your code should read

    IF condition THEN

    statement

    ELSEIF condition THEN

    statement

    etc.

    ENDIF

    2. Always use DEBUG and STOP to trace your code to the point where it stops working.



    There is probably nothing wrong with your LCD code, I did not check that.

    Good luck in your learning.

    Cheers

    Vaclav

    Now, i can't show the variable (which is in "value")in LCD , but it can show in DEBUG....
    I don;t know what happen.
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-04-20 02:59
    Friend,
    In the future please do not repeat in you reply what was·said in the thread. In is not necessary.
    ·
    I do not have my setup operating now so I cannot test your code any further.
    ·
    The sample code you are·using·displays characters to LCD via DATA.
    Therefore you could copy your value to·DATA and reuse same code or you could send individual characters / value to LCD.
    ·
    Ask your teacher for advise how to do that. Maybe it is little ahead of his teaching schedule. ·He should be able to advise·you better·in person than me·in this·forum.
    Keep learning.
    Cheers
    Vaclav


    ·
Sign In or Register to comment.