T3STY
Full Member level 4
I'm experimenting with assembly delays and I have come across a workflow model that can uses 3 8-bit registers for delaying up to 16581375 cycles (without considering the instructions delay). You can find the diagram in the attachment. I've then implemented the model in assembly and works fine except for 1 problem: it's 4 times slower than it should. I have also calculated that if I have a 4MHz PIC clock the real clock is 1MHz, so there are 1milion instructions in a second. By using the model before I can make timers do 250*250*16 loops which makes exactly 1000000 loops so 1 second delay. Although, when I run the code in ISIS it makes a 4 seconds delay (left out the error caused by not considering the delay of the instructions themselves).
I know this delay routine is still not perfect because it does not consider situations where timer0 is 0 and the others will not be executed at all, that's an issue I'm going to deal with when using it. But the problem here seems to be calculus, somehow...
Can someone tell me if I miscalculated the loops? Or did I wrongly consider the 1MHz real clock?
p.s. Running on a 16MHz PIC clock I get the 1 second delay and changing values also gives me the required delay.
Code:
#DEFINE T0 0xFA ;250
#DEFINE T1 0xFA ;250
#DEFINE T2 0x40 ; 16
Delay
CBLOCK 0x0C
TIMER_0: 1
TIMER_1: 1
TIMER_2: 1
ENDC
MOVLW T0
MOVWF TIMER_0
MOVLW T1
MOVWF TIMER_1
MOVLW T2
MOVWF TIMER_2
loop_t0 ; 1ST loop
DECFSZ TIMER_0,f;
GOTO loop_t1 ; while not 0: go to loop_t1
RETURN ; else: the delay has finished, return from call
loop_t1
DECFSZ TIMER_1,f; 2ND loop
GOTO loop_t2 ; while not 0: go to loop_t2
MOVLW T1 ; else: restore value into TIMER_1
MOVWF TIMER_1 ; needed for any further loop
GOTO loop_t0 ; then go to loop_t0
loop_t2 ; 3RD loop
DECFSZ TIMER_2,f;
GOTO loop_t2 ; while not 0: DEC TIMER_2
MOVLW T2 ; else: restore value into TIMER_2
MOVWF TIMER_2 ; needed for any further loop
GOTO loop_t1 ; then go to loop_t1
END
Can someone tell me if I miscalculated the loops? Or did I wrongly consider the 1MHz real clock?
p.s. Running on a 16MHz PIC clock I get the 1 second delay and changing values also gives me the required delay.