Rolling die MPASM code
#include "p10f200.inc"
; OSCCAL 0CD6
; Simulate rolling of a die
; CONFIG
__CONFIG _WDT_OFF & _CP_OFF & _MCLRE_OFF
CBLOCK 0x10
stage1 ; GPIO settings for the die
random ; random num using TMR0
j ; delay loop
k ; delay loop
l ; delay loop
m ; delay loop
ENDC
ORG 0x0000
INIT
MOVLW 0xD2 ; 1101 0010 no weak pull-up (6), PSA(3) pre(2-0) 1:8
OPTION
MOVLW 0x08 ; 1000 set GP0, GP1, GP2 as outputs
TRIS GPIO
CLRF GPIO
LOOP
BTFSC GPIO, GP3 ; key pressed (pull-up gives problems)
GOTO LOOP ; else
MOVF TMR0, 0 ; if
MOVWF random
CALL MODULO6 ; if: get TMR0 modulo 6 -> 0 to 5
CALL DISPLAY
GOTO LOOP
MODULO6
MOVLW 6
SUBWF random,F ; random - 6, result in W
BTFSC STATUS, C ; if operant < W carry bit = 0, skip next
GOTO $ + 2 ; else
GOTO $ + 2 ; if
GOTO MODULO6
MOVLW 6
ADDWF random ; add 6 results in 0 to 5
MOVF random, W ;
CALL TABLE
MOVWF stage1 ; copy W into stage1
RETLW 0
TABLE
ADDWF PCL
RETLW 0x01 ; 0001 2L 1L 0H
RETLW 0x05 ; 0101 2H 1L 0H
RETLW 0x03 ; 0011 2L 1H 1H
RETLW 0x25 ; 0101 2H 1L 1H 0010 2L 1H 0L
RETLW 0x35 ; 0101 2H 1L 0H 0011 2L 1H 0H
RETLW 0x24 ; 0100 2H 1L 0L 0010 2L 1H 0L
DISPLAY ; for 2 sec
MOVLW 8 ;Load initial value for the delay
MOVWF j ;Copy the value to the register 0x11
MOVWF k ;Copy the value to the register 0x12
DISPLAY_LOOP
MOVF stage1, 0 ; 4 five 2H 1L 0L 0100
MOVWF GPIO
CALL SHORT_D
; MOVF stage2, 0 ; 2 five 2L 1H 0L 010
SWAPF stage1, 0 ; into W
MOVWF GPIO
CALL SHORT_D
DECFSZ j, F ;Else decrement the register 0x11, check if it is not 0
GOTO DISPLAY_LOOP ;If not then go to the DELAY_LOOP label
DECFSZ k, F ;Else decrement the register 0x12, check if it is not 0
GOTO DISPLAY_LOOP ;If not then go to the DELAY_LOOP label
CLRF GPIO ; if
RETLW 0 ; else return from the subroutine
;-----------------------------------------------------------------------------------------
SHORT_D ;Start DELAY subroutine here
MOVLW 2 ;Load initial value for the delay
MOVWF l ;Copy the value to the register 0x11
MOVWF m ;Copy the value to the register 0x12
DELAY_LOOP ;Start delay loop
DECFSZ l, F ;Else decrement the register 0x11, check if it is not 0
GOTO DELAY_LOOP ;If not then go to the DELAY_LOOP label
DECFSZ m, F ;Else decrement the register 0x12, check if it is not 0
GOTO DELAY_LOOP ;If not then go to the DELAY_LOOP label
RETLW 0 ;Else return from the subroutine
END