Shop OBEX P1 Docs P2 Docs Learn Events
Help adding and subtracting with the MAX7219 and the Basic Stamp 2 — Parallax Forums

Help adding and subtracting with the MAX7219 and the Basic Stamp 2

kingspudkingspud Posts: 128
edited 2007-06-11 16:42 in BASIC Stamp
Hello all,

I play steel tip darts and no one makes an electronic·score board without the dart board so...

I am using the MAX7219 chip with the BS2 chip and have come across a problem that hopefully all you out there can help me with!

Example:· Lets say you are playing a game of 401 and need to subtract from this score.

The BS2 chip sends data in the form;

··· Dig0 = $0101
··· Dig1 = $0200
··· Dig2 = $0304

to represent a····· 1 in the first 7-segment display
················ a····· 0 in the second 7-segment display
··············· and a 4 in the third·7-segment display.

I can debug the values by useing the following command:

····· DEBUG··· DEC Dig0, " " ,DEC DIG1, " ",DEC DIG2

and this gives me 1 0 4 in the debug window but...

if I use the following;

one = Dig0
ten = Dig1
hundred = Dig2

I dont get any value from these numbers!!!

how do I get the values from these numbers so I can add,subtract, or multiply them????

any thouthts?

thanks in advance

P.S.· Does any one have a program that will add and subtract using the MAX7219 and the BS2?· If so this would save me a lot of time!!!

Oh and by the way... here is the code so far if any one is interested.

I am using the PDBoard with three of the five 7-segment displays and the 8 switches to test the following:

When you hit·one of the eight·buttons you get the following so far:

Cr············ 'for Cricket
P 1··········· 'for player 1
501···········' for a 501 game
401·········· ' for a 410 game
reset········ ' to reset the three displays
the other three are for testing use


' {$STAMP BS2}
' {$PBASIC 2.5}

'
[noparse][[/noparse] I/O Definitions ]
'0 sets pin as input
'button PIN 0 makes the word button = pin 0
DIRS = $000E············ 'pins 1 2 3 output,all other pins outputs
DOUT PIN 1·············· 'pin 1 of stamp to pin 1 of 7219
Load PIN 2·············· 'pin 2 of stamp to pin 12 of 7219
CLK PIN 3··············· 'pin 3 of stamp to pin 13 of 7219

'
[noparse][[/noparse] Constants ]
IsLow CON 0
IsHigh CON 1

'
[noparse][[/noparse] Variables ]
Dig0 VAR Byte
Dig1 VAR Byte
Dig2 VAR Byte
btn·· VAR Bit
Value VAR Nib
total VAR Byte
temp VAR Byte
one VAR Byte
ten VAR Byte
hundred VAR Byte
'
[noparse][[/noparse] Initialization ]
Dig0 = $010F
Dig1 = $020F
Dig2 = $030F
Value = 0



main:

··· GOSUB DecodeMode
··· GOSUB ShutDownMode
··· GOSUB ScanLimit
··· GOSUB Intensity
··· GOSUB reset

loop1:
· BUTTON 5,0,255,250,btn,1,press
· BUTTON 15,0,255,250,btn,1,press1
· BUTTON 6,0,255,250,btn,1,press2
· BUTTON 4,0,255,250,btn,1,press3
· BUTTON 7,0,255,250,btn,1,press4
· BUTTON 11,0,255,250,btn,1,press5
· BUTTON 12,0,255,250,btn,1,press6
· BUTTON 13,0,255,250,btn,1,press7
· GOTO loop1

· press:············· 'Press button [noparse][[/noparse]1]
· GOSUB DecodeMode
· GOTO Begin_401
· press1:············ 'Press button [noparse][[/noparse]2]
· GOSUB DecodeMode
· GOTO Begin_501
· press2:············ 'Press button [noparse][[/noparse]0]
· GOSUB DecodeMode
· GOTO reset
· press3:············ 'Press button [noparse][[/noparse]7]
· GOSUB DecodeMode
· GOTO Player_1
· press4:············ 'Press button [noparse][[/noparse]3]
· GOTO Cricket
· press5:············ 'Press button [noparse][[/noparse]6]
· GOTO Add_20
· press6:············ 'Press button [noparse][[/noparse]5]
· GOTO Add_19
· press7:············ 'Press button [noparse][[/noparse]4]
· GOTO Subtract_20
'
[noparse][[/noparse] GOSUB RESET ]
reset:
· Dig0 = $010F
· Dig1 = $020F
· Dig2 = $030F
· GOSUB board_display:
GOTO loop1
'
[noparse][[/noparse] GOSUB BEGIN_401 ]
Begin_401:
··· Dig0 = $0101
··· Dig1 = $0200
··· Dig2 = $0304
··· GOSUB board_display:
··· GOTO loop1
'
[noparse][[/noparse] GOSUB BEGIN_501 ]
Begin_501:
··· Dig0 = $0101
··· Dig1 = $0200
··· Dig2 = $0305
··· GOSUB board_display:
··· GOTO loop1
'
[noparse][[/noparse] GOSUB Player_1 ]
Player_1:
··· Dig0 = $0101
··· Dig1 = $020F
··· Dig2 = $030E
··· GOSUB board_display:
··· GOTO loop1
'
[noparse][[/noparse] GOSUB Cricket ]
Cricket:
··· LOW Load
· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$0900\16]
· HIGH Load
··· Dig0 = $0100··· 'THIS IS BLANK
··· Dig1 = $0205··· 'THIS IS THE LETTER r
··· Dig2 = $034E··· 'THIS IS THE LETTER C
··· GOSUB board_display:
··· GOTO loop1
'
[noparse][[/noparse] GOSUB Add_20 ]
Add_20:
··· 'If total is 401 then Add 20 to the total
··· Dig0 = $0101
··· Dig1 = $0200
··· Dig2 = $0306

··· GOSUB board_display:
··· GOTO loop1
'
[noparse][[/noparse] GOSUB Add_19 ]
Add_19:
··· Dig0 = $0101
··· Dig1 = $0200
··· Dig2 = $0304

··· 'GOSUB hundreds:
··· 'GOSUB tens:
······ DEBUG··· DEC Dig0, " " ,DEC DIG1, " ",DEC DIG2
··· GOSUB board_display:
··· GOTO loop1
'
[noparse][[/noparse] GOSUB Subtract_20 ]
Subtract_20:
··· 'If total is 401 then subtract 20 from total
··· Dig0 = $0101
··· Dig1 = $0200
··· Dig2 = $0304

··· GOSUB board_display:
··· GOTO loop1




'
[noparse][[/noparse] Subroutines ]
'Decode mode for no decoding to BCD
'Least sig nibble:
'0 = no decode
'1 = decode digit 0
'F = decode digits 0-3
'FF = decode digits 0-7
DecodeMode:
· LOW Load
· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$090F\16]
· HIGH Load
· RETURN
'Set shutdown mode so device is active
ShutDownMode:
· LOW Load
· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$0C01\16]
· HIGH Load
· RETURN

'Set scan limit mode for three digits
ScanLimit:
· LOW Load
· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$0B02\16]
· HIGH Load
· RETURN
'Set intensity of LEdDs
'LSNibble 0 = min intensity and F = max intensity
Intensity:
· LOW Load
· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$0A09\16]
· HIGH Load
· RETURN

board_display:

··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Dig0\16]
··· HIGH Load
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Dig1\16]
··· HIGH Load
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Dig2\16]
··· HIGH Load
··· PAUSE 400
RETURN

Comments

  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-06-11 04:15
    I'm not familiar with the dart games, so I can't help much there and it is hard for me to follow what you want to accomplish with the program.

    But I noticed that you have Dig0 Dig1 and Dig2 allocated as bytes, but then you make assignments like:
    Dig2 = $0304

    Since Dig2 is a byte, it becomes $04, and the upper part $03 gets dropped. That is true of all the digN assignments. I don't know what purpose the $03 was supposed to serve. Maybe position, but the pointer was automatically incremented by the '7219.

    It might be better to store the score values as a word in decimal so that you can do the math on them, and have a little subroutine that converts the decimal value to a string to send to the display. For example,
    score = 401
    score = score - 20
    dig0 = score dig 0
    dig1 = score dig 1
    dig2 = score dig 2

    Look up dig in the Stamp help. It is a math operator that extracts the decimal digits from a variable.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • kingspudkingspud Posts: 128
    edited 2007-06-11 05:28
    Hi Tracy,

    Actually the Dig0, Dig1, and Dig2 are word and not byte.· I guess I had modified the code for a test and posted it before I put it back to normal.

    So... a value like $0304 is how the MAX7219 chip displays digits on a 7-segment display...· it is broken down to... 03 for the # of the 7-segment display used and

    the 04 is the value shown on the display.

    So the values

    ··· Dig0 = $0101
    ··· Dig1 = $0200
    ··· Dig2 = $0304

    represents three 7-segment displays and on these three displays is 401

    I understand what you are giving me but how do I go from:

    · score = 401
    · score = score - 20
    · dig0 = score dig 0
    · dig1 = score dig 1
    · dig2 = score dig 2

    to...

    ··· Dig0 = $0101
    ··· Dig1 = $0208
    ··· Dig2 = $0303

    Thanks

    Joe
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-06-11 14:48
    Hi Joe,

    score = 401
    score = score - 20
    dig0 = score dig 0 + $0100
    dig1 = score dig 1 + $0200
    dig2 = score dig 2 + $0300

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • kingspudkingspud Posts: 128
    edited 2007-06-11 16:42
    Hi Tracy,

    Worked like a charm!· The problem I was having when I was testing your code was I forgot to change the VAR of score to a word size instead of byte.

    Because it was set to byte it wasn't big enought to hold anything over 255 in value and I kept getting odd values!· This was really driving me crazy!

    Thanks a bunch.

    When I get the whole thing finished I will post it on this site!

    Joe

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    “Intellectual growth should commence at birth and cease only at death”
    Albert Einstein
Sign In or Register to comment.