Examples Index

' =========================================================================
'
'   File...... DICE.SXB
'   Purpose... A Pair of Digital Dice
'   Author.... (c) Parallax, Inc. -- All Rights Reserved
'   E-mail.... support@parallax.com
'   Started...
'   Updated... 05 JUL 2006
'
' =========================================================================

' -------------------------------------------------------------------------
' Program Description
' -------------------------------------------------------------------------
'
' Simple digital dice program.  Uses outputs from RB and RC to dice pattern
' on seven LEDs (for each port) as shown below:
'
'   (0)     (1)
'   (5) (6) (2)
'   (4)     (3)
'
' A button input on RA.0 is used to "roll" the dice.  When rolling is
' stopped, display will stay solid for at least one second, then the
' program will wait for the Roll button to be pressed again.

' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------

DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ            4_000_000
ID              "DICE"

' -------------------------------------------------------------------------
' IO Pins
' -------------------------------------------------------------------------

Roll            VAR     RA.3                    ' roll button
Die0            VAR     RB                      ' LEDs out for die #1
Die1            VAR     RC                      ' LEDs out for die #2

' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------

No              CON     1                       ' button not pressed
Yes             CON     0

' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------

d1Val           VAR     Byte                    ' value of die #1
d2Val           VAR     Byte                    ' value of die #2
pattern         VAR     Byte                    ' dice pattern

tmpB1           VAR     Byte                    ' work variables
tmpB2           VAR     Byte
tmpB3           VAR     Byte
tmpW1           VAR     Word

' =========================================================================
  PROGRAM Start
' =========================================================================

' -------------------------------------------------------------------------
' Subroutine Declarations
' -------------------------------------------------------------------------

GET_DIE         SUB     1                       ' pass seed and index addr
DELAY           SUB     1, 2                    ' delay in milliseconds

' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------

Start:
  TRIS_B = %10000000                            ' make LED ports outputs
  TRIS_C = %10000000
  PLP_A = %1000                                 ' pull-up unused pins
  PLP_B = %01111111
  PLP_C = %01111111

  d1Val = $12                                   ' initialize seeds
  d2Val = $34

Main:
  DO
    Die0 = GET_DIE @d1Val                       ' randomize die values
    Die1 = GET_DIE @d2Val
    DELAY 75                                    ' delay between rolls
  LOOP WHILE Roll = No                          ' wait for button
  DELAY 1000                                    ' show dice (1 sec min)

Wait_For_Press:
  DO
    DELAY 10
  LOOP UNTIL Roll = Yes

Wait_For_Release:
  DO WHILE Roll = Yes
    DELAY 10
  LOOP
  DELAY 100
  GOTO Main

' -------------------------------------------------------------------------
' Subroutine Code
' -------------------------------------------------------------------------

' Use: pattern = GETDIE @seed
' -- randomizes 'seed' (must pass address as 'seed' is updated)
' -- returns die display in 'pattern'

GET_DIE:
  tmpB1 = __PARAM1                              ' get seed address
  tmpB2 = __RAM(tmpB1)                          ' get seed value
  RANDOM tmpB2                                  ' randomize seed
  __RAM(tmpB1) = tmpB2                          ' update seed
  tmpB2 = tmpB2 / 43                            ' make = 0 to 5
  READ Pips + tmpB2, tmpB1                      ' get LED pattern
  RETURN tmpB1                                  ' return pattern

' -------------------------------------------------------------------------

' Use: DELAY ms
' -- 'ms' is delay in milliseconds, 1 - 65535

DELAY:
  IF __PARAMCNT = 1 THEN
    tmpW1 = __PARAM1                            ' save byte value
  ELSE
    tmpW1 = __WPARAM12                          ' save word value
  ENDIF
  PAUSE tmpW1
  RETURN

' =========================================================================
' User Data
' =========================================================================

Pips:
  DATA  %01000000                               ' 1
  DATA  %00010010                               ' 2
  DATA  %01010010                               ' 3
  DATA  %00011011                               ' 4
  DATA  %01011011                               ' 5
  DATA  %00111111                               ' 6