MPASM code


processor 12F629

    #include 
    __CONFIG _FOSC_INTRCIO & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _BOREN_OFF & _CP_OFF & _CPD_OFF
    errorlevel -302

; This shorthand assigns d1=0x20, d2=0x21, etc.
    cblock 0x20
        d1               ;define 0x20 register 
        d2
        d3
        cnt
    endc
    ORG 0x0000
   		 
INIT
    BSF STATUS,RP0        ; select bank 1            
    MOVLW 0x08     ;set GP0,1,2,4,5 as outputs, GP3 as input  w= 0000 1000
    MOVWF TRISIO
    ;-------timer0----------------        
    MOVLW 0xD6   ; 1101 0110 NO pull-up, tmr0, prescaler 1:128
    MOVWF OPTION_REG ;         
    BCF STATUS,RP0        ; select bank 0
    movlw b'111'  ; 0x03
    movwf CMCON   ; turn off comparator             
    CLRF GPIO
    MOVLW 0x4
    MOVWF TMR0; TMR0=0x4
    CLRF cnt    ; cnt=0
            
LOOP        
    BTFSS INTCON,T0IF  ; timer0 overflow, T0IF is set
    GOTO $ + 4 ; else(1)
    MOVLW 0x4
    MOVWF TMR0
    BCF INTCON, T0IF ; clear T0IF   
    BSF GPIO, GP4 ; (1) turn on GP4          
    BTFSC GPIO,GP3  ; check button press GP3                   
    GOTO LOOP        ; else (2)
    MOVF TMR0, W     ; copy TMR0 to W       
    MOVWF cnt        ; cnt = TMR0, TMR0 keeps changing
    BCF GPIO, GP4      ; turn off GP4
            
SUBTR                 ; modulo 6
    MOVLW 6
    SUBWF cnt,F    ; cnt - 6, result in W
    BTFSC STATUS, C  ; if carry bit cleared, skip next
    GOTO $ + 2       ; else (3)
    GOTO $ + 3       ; if
	GOTO SUBTR
    MOVLW 6
    ADDWF cnt    ; 0 to 5
    MOVF cnt, W  ;
    CALL TABLE  
    CALL SHOW     
    GOTO LOOP  

TABLE
    ADDWF PCL  ; add W to program counter lower byte        
    RETLW 0x01
    RETLW 0x02
    RETLW 0x05
    RETLW 0x06
    RETLW 0x07
    RETLW 0x26
            
SHOW
    MOVWF GPIO
    CALL DELAY ; 2 sec
    CLRF GPIO; turn off
    CLRF cnt
    RETLW 0        

DELAY            
			;~2sec 1999996 cycles
	movlw	0x11
	movwf	d1
	movlw	0x5D
	movwf	d2
	movlw	0x05
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0
    RETLW 0
    
    END