PROGRAM M ;****************************** ; written by John Morton * ; date: 10/01/05 * ; version 1.0 * ; file saved as: dice.asm * ; for P12F508 * ; clock frequency: Int. 4MHz * ;****************************** ; PROGRAM FUNCTION: A pair of dice list P=12F508 include "c:\pic\p12f508.inc" __config _MCLRE_OFF & _CP_OFF & _WDT_OFF & _IntRC_OSC ;============== ; Declarations: Die1num equ 10h Die2num equ 11h Mark60 equ 12h PostX equ 13h PostVal equ 14h Ran1 equ 15h Ran2 equ 16h General equ 17h Random equ 18h #define slow General, 0 org 0 ; first instruction to be executed movwf OSCCAL ; calibrates internal oscillator goto Start ; ;======================================================== ; Subroutines: Init movlw b'100000' ; turns off all LEDs movwf GPIO ; movlw b'001000' ; sets up which pins are inputs tris GPIO ; and which are outputs movlw b'01000111' ; enable wake-on-change, disable weak option ; pull-ups, TMR0 prescaled by 256 movlw d'4' ; sets up postscalers movwf PostX ; movwf PostVal ; clrf Die1num ; clears display registers clrf Die2num ; clrf Ran1 ; clears random number registers clrf Ran2 ; bcf slow ; clears Ôslow-downÕ flag retlw 0 ; ;==================================================== Display btfss TMR0, 4 ; uses bit 4 of TMR0 to choose die goto Die2 ; movfw Die1num ; gets number to display call Code1 ; converts to code movwf GPIO ; outputs retlw 0 ; Die2 movfw Die2num ; gets number to display call Code2 ; converts to code movwf GPIO ; outputs retlw 0 ; ; arrangement for dice 1 is : CTLR, D, -, A, C, B Code1 addwf PCL, f ; retlw b'100000' ; all off retlw b'100100' ; 1 retlw b'100001' ; 2 retlw b'100101' ; 3 retlw b'100011' ; 4 retlw b'100111' ; 5 retlw b'110011' ; 6 retlw b'110111' ; all on ; arrangement for dice 2 is : CTLR, C, -, B, A, D Code2 addwf PCL, f ; retlw b'010111' ; all off retlw b'010101' ; 1 retlw b'010011' ; 2 retlw b'010001' ; 3 retlw b'000011' ; 4 retlw b'000001' ; 5 retlw b'000010' ; 6 retlw b'000000' ; all on ;================================================== Timing movfw Mark60 ; 1/40th second delay subwf TMR0, w ; btfss STATUS, Z ; retlw 0 ; movlw d'60' ; resets marker addwf Mark60, f ; decfsz PostX, f ; variable further delay retlw 0 ; call RandomGen ; generate new pseudo-random swapf Random, w ; number andlw b'00000111' ; converts to 0-7 and moves movwf Die1num ; into Die1num call RandomGen ; generate new pseudo-random swapf Random, w ; number andlw b'00000111' ; converts to 0-7 and moves movwf Die2num ; into Die2num btfsc slow ; should this slow down? call Slowdown ; yes movfw PostVal ; updates variable delay length movwf PostX ; retlw 0 Slowdown incf PostVal, f ; increases delay length btfsc PostVal, 5 ; has PostVal reached 32? clrf PostVal ; resets, telling ÔReleasedÕ section retlw 0 ; that the dice have stopped rolling RandomGen movlw d'63' ; newRandom = addwf Random, w ; 63 + oldRandom x 3 addwf Random, w ; addwf Random, f ; retlw 0 ; ;==================================================== RandomScroll incf Ran1, f ; v. quickly scrolls through movlw d'6' ; has Ran1 reached 6? subwf Ran1, w ; btfss STATUS, Z ; retlw 0 ; no, so returns clrf Ran1 ; incf Ran2, f ; movlw d'6' ; has Ran1 reached 6? subwf Ran2, w ; btfss STATUS, Z ; retlw 0 ; no, so returns clrf Ran2 ; retlw 0 ; ;====================================================== ; PROGRAM START Start call Init ; initial settings Pressed btfsc GPIO, 3 ; tests button goto Released ; branches when released call RandomScroll ; quickly scrolls through no.s call Timing ; keeps flashing going call Display ; keeps displays changing goto Pressed ; Released bsf slow ; tells Timing to slow down call Timing ; keeps flashing going call Display ; keeps displays going movf PostVal, f ; have dice stopped rolling? btfss STATUS, Z ; goto Released+1 ; no, so keeps looping incf Ran1, w ; moves 1+ the random number movwf Die1num ; into the display regs. incf Ran2, w ; movwf Die2num ; movlw d'240' ; 240 x 1/40th second = 6 second movwf PostX ; delay EndLoop call Display ; 6 second delay, after which all movfw Mark60 ; LEDs are turned off subwf TMR0, w ; btfss STATUS, Z ; goto EndLoop ; movlw d'60' ; addwf Mark60, f ; decfsz PostX, f ; goto EndLoop ; movlw b'100000' ; turns off all LEDs movwf GPIO ; sleep ; goes to low power mode END