I have TMR0 which is 10 ms timer
I wish to use GP register as a 1 second timer using TMR0 timer of 10ms
I tried the following code but didnt get the result
btfsc INTCON,2
decf count2,0 // count2 is 100 in decimal
btfsc STATUS,Z
'''''''''''remaining code
after each time the TMR0 overflows i decremented count2 and this will repeat 100 times which should have given 1 second
Please let me know your ideas on this
First off...you need to change the designator on your decriment instruction to 1 or F. With the 0 there, it's saving the decrimented result in W while keeping the number in COUNT2 the same so it will never equal zero, and thus will never branch. If you change it to a 1 or F, it will store the decrimented result back into COUNT2 instead.
Second off, the TMR0 interrupt flag (T0IF in INTCON) must be cleared in software everytime it gets set.
Furthermore, if you use "decfsz" instead of "decf" and "btfss STATUS,Z", you won't have to check the Z flag as decfsz means
DECriment
File proceeding instruction and
Skip next instruction if file equals
Zero. The F after the comma which proceeds the file address/label tells it to store the decrimented result back into the file.
So it should look like this -
Code:
Delay
clrf TMR0 ;start TMR0 counting at 0
bcf INTCON,T0IF ;clear TMR0 interrupt flag
movlw d'100' ;set the timer interrupt counter
movwf count2 ;to decimal 100
W8 btfsc INTCON,T0IF ;check TMR0 interrupt flag
goto W8 ;branch if set
bcf INTCON,T0IF ;clear TMR0 interrupt flag
decfsz count2,F ;decriment interrupt counter by 1
goto W8 ;branch if zero
return
Instead of this -
Code:
btfsc INTCON,2
decf count2,0 // count2 is 100 in decimal
btfsc STATUS,Z
The basic flowchart for my code would be -
**broken link removed**
With the return instruction, you can call that as a subroutine labeled "Delay" anytime you wish to implement the 1 second delay with a simple "call Delay" instruction.
Again you can use the timer prescaler to slow down the timer count.