' ========================================================================= ' File....... SW21-EX10-Clock.SXB ' Purpose.... Simple digital clock with software multiplexing ' Author..... (C) 2000 - 2005, Parallax, Inc. ' E-mail..... support@parallax.com ' Started.... 01 SEP 2005 ' Updated.... 23 MAR 2007 by TSaavik for SX28 ' ========================================================================= ' -----[ Program Description ]--------------------------------------------- ' This program combines a 7-segment display and pushbutton input to form ' a digital die that displays numbers 1 - 6. This program will work, ' unmodified, on any BS2-family module. '------{ Device Settings ]------------------------------------------------- DEVICE SX28,TURBO,BANKS8,OSCHS3,SYNC,OPTIONX FREQ 50_000_000 ID "EX09" ' -----[ I/O Definitions ]------------------------------------------------- Segs VAR RB ' Segments on RB.0 - RB.7 RollBtn VAR RA.1 ' roll button for die ' -----[ Constants ]------------------------------------------------------- BugLen CON 8 ' calc animation length Button_Down CON 0 Button_Up CON 1 ' -----[ Variables ]------------------------------------------------------- rndVal VAR Byte ' random number dieVal VAR Byte ' new die value spinPos VAR Byte ' spinner position doSpin VAR Byte ' spinner update control ' -----[ Initialization ]------------------------------------------------- PROGRAM Start watch Segs, 8, UBIN watch RollBtn, 1, UDEC watch rndVal, 8, UDEC ' random number watch dieVal, 8, UDEC ' new die value watch spinPos, 8, UDEC ' spinner position watch doSpin, 8, UDEC ' spinner update control ' -----[Subs ]------------------------------------------------------------- Tumble_Die SUB ' -----[ Program Code ]---------------------------------------------------- Start: Tris_B = %10000000 ' make LEDs outputs Main: DO GOSUB Tumble_Die ' shake the die 'Break PAUSE 5 ' loop pad ' check for button press ''BUTTON Pin,DownState, Delay, Rate, Workspace, TargetState, Address 'BUTTON RollBtn, 0, 255, 5, swData, 1, Show_Die IF RollBtn = Button_Down THEN Pause 10 IF RollBtn = Button_Down THEN Goto Show_Die ENDIF ENDIF LOOP Show_Die: READ Digit0 + dieVal, Segs ' transfer die to segments PAUSE 3000 ' hold for viewing GOTO Main ' start again ' -----[ Subroutines ]----------------------------------------------------- Tumble_Die: RANDOM rndVal ' stir random value dieVal = rndVal // 6 ' get random val, 0 - 5 inc dieVal ' get die val, 1 - 6 inc doSpin ' inc spin timer doSpin = doSpin // 10 ' update spin timer IF doSpin = 0 THEN ' time for update inc spinPos spinPos = spinPos // BugLen ' yes, point to next pos READ Bug0 + spinPos, Segs ' output to segments ENDIF RETURN ' -----[ EEPROM Data ]----------------------------------------------------- ' %.GFEDCBA Digit0: DATA %00111111 Digit1: DATA %00000110 Digit2: DATA %01011011 Digit3: DATA %01001111 Digit4: DATA %01100110 Digit5: DATA %01101101 Digit6: DATA %01111101 Digit7: DATA %00000111 Digit8: DATA %01111111 Digit9: DATA %01100111 ' %.GFEDCBA Bug0: DATA %00000001 ' animated "bug" frames Bug1: DATA %00000010 Bug2: DATA %01000000 Bug3: DATA %00010000 Bug4: DATA %00001000 Bug5: DATA %00000100 Bug6: DATA %01000000 Bug7: DATA %00100000