Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

help with delay routine

Status
Not open for further replies.
I am a total newbie with pics and I am trying to get my head around assembly.

I have been reading "PIC in Practice" by D.W. Smith.

In it he uses a 0.5 second delay routine.
This routine dosent make sense to me, could someone please
explain it to me.

The circuit uses a 32.768kHz xtl, prescalar is /256, timer is 1/32 secs.
Code:
;0.5 second delay.
DELAYP5        CLRF         TMR0                        ;START TMR0.
LOOPB          MOVF         TMR0,W                     ;READ TMR0 INTO W.
               SUBLW        .16                        ;TIME - 16
               BTFSS        STATUS,ZEROBIT            ;Check TIME-W =0
               GOTO         LOOPB                      ;Time is not =16.
               RETLW        0                          ;Time is 16, return.



As I see it the routine will * clear tmr0
*put tmr0 in w
*subtract 16 from W
*see if answer is 0 if so skip next instruction and then return to program, otherwise it will goto LOOPB.

As I understand it once TMR0 is reset it will start count up with each clock cycle. In this instance that would be 1/32 of a second. Does this mean that for this to really be a 0.5 second delay that it should only take 16 clock cycles from the time the delay is called to the time it returns to the program.

Does this routine acheive this?
 
Use the Code button to keep the source code in the original format!

Like this:
Code:
test
                                     test
See it stays there
 
Python your loop B is in the wrong spot..
Code:
;0.5 second delay. 
DELAYP5        CLRF         TMR0                        ;START TMR0. 
               MOVF         TMR0,W                     ;READ TMR0 INTO W. 
LOOPB          SUBLW        .16                        ;TIME - 16 
               BTFSS        STATUS,ZEROBIT            ;Check TIME-W =0 
               GOTO         LOOPB                      ;Time is not =16. 
               RETLW        0                          ;Time is 16, return.
try this..
 
come on, I don't even use assembly and I'm the first one to answer his question for real?

first off, williB, his loopb was in the right spot. if he moved it down a line like you said, it would never check TMR0, it would just continuously decrement the W register until it equaled zero, which would occur in about 16 loop cycles, instead of 16 TMR0 increments, and would be far less than 0.5 seconds.

you have the right idea. the single purpose of the loop is to wait until TMR0 reaches a value of 16. (in this case, by subtracting 16 and seeing when the result equals zero)

with the clock frequency/prescaler you stated, it will be 1/32 second per increment of TMR0 (as you also stated) however, just to be clear, the prescaler applies to incrementing of TMR0, not to the execution of your program, so everything else is running at normal clock speed; thus, this loop will occur many more than the 16 times that TMR0 will increment. it just keeps checking and checking and checking until TMR0 reaches 16.

and since it waits for TMR0 to increment 16 times,

therefore, 16*1/32 = 0.5 seconds. so it starts the timer, loops for 0.5 seconds, and then returns to what it was doing before; ie - a 0.5 second delay routine. so it does not wait 16 CLOCK cycles, it waits for 16 increments of TMR0. (which, with the prescaler, is actually 16*256 = 4096 clock cycles, plus a couple for code overhead)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top