Ihave been working on an assignment to implement the timer0 function to be used as a delay subroutine rather than looping the program. I appear to be going around in circles! When I animate the program it continuously loops between the BTFSS INTCON,T0IF and GOTO Timerdel. From the view watch panel (MPLAB IDE) i can see the OPTION_REG never changes from 0xFF. Also when ever i write to the WREG and move it to somewhere else eg. PORTB the associate register stays s 0x00. I tried using the MOVF command instead of the MOVWF but the results were the same.
The pic is a 16F84. If someone an point my in the right direction that would be very helpful.
Code:
DELAY:
MOVLW 0X07 ; use 0000 0111 for timer0 prescaler
MOVWF OPTION_REG ; set prescaler to max 256
CLRF TMR0 ; clear timer register
Timerdel BTFSS INTCON,T0IF ; check timer flag, skip next instuction is set
GOTO Timerdel ; loops until timer flag is set T0IF
BCF INTCON,T0IF ; clear timer flag ready for next delay subroutine
RETURN ; will return to main program (line after the Delay was called from)
END ; end of program
The code should work so I suspect the problem is elsewhere.
Things to look for:
1. You are polling the Timer 0 interrupt flag, are you sure the interrupt itself isn't being triggered? (INTCON,T0IE should be 0)
2. Is the watchdog timer enabled? It could be causing a reset while you are waiting for T0IF to set.
3. OPTION_REG is in register bank 1 so you should switch banks when writing to it then go back to bank 0 for the rest of the code. Like this:
Code:
DELAY:
banksel OPTION_REG
MOVLW 0X07 ; use 0000 0111 for timer0 prescaler
MOVWF OPTION_REG ; set prescaler to max 256
banksel TMR0
CLRF TMR0 ; clear timer register
Timerdel BTFSS INTCON,T0IF ; check timer flag, skip next instuction is set
GOTO Timerdel ; loops until timer flag is set T0IF
BCF INTCON,T0IF ; clear timer flag ready for next delay subroutine
RETURN ; will return to main program (line after the Delay was called from)
END ; end of program
Note that you probably only want to write to OPTION_REG once in the whole program instead of every time you call "DELAY" so it might be better to move that code to the top of the program instead.
Sorry if editing messed up the display format - but you should get the idea.