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.

understanding a delays

Status
Not open for further replies.

timothyjackson

New Member
Hello. I'm trying to understand nigels delay routine (still new to pic's). Is some able to explain whats happening here in baby language for me please? its a 250ms delay routine, used in pulsing led's on and off.

Delay movlw d'250'
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb

Delay_0
decfsz counta,f
goto $+2
decfsz countb,f
goto Delay_0

decfsz count1,f
goto d1
retlw 0x00

end

many thanks.
 
....forgot to mention,,,,

at the line "movlw 0xC7 ", what is this, and why?

why is decimal used at the beginning i.e. 250, and hex here?

I also don't understand "how" its creating a 250ms delay, and what needs to be changed to create longer, shorter delays. For example, wht needs to be changed to create a 100ms delay.........

would like to understand. :?
 
Code:
Delay    movlw    d'250'          //
            movwf    count1         // count1 = 250 
d1         movlw    0xC7          //
            movwf    counta        // counta = 199
            movlw    0x01           // 
            movwf    countb        // countb = 1

Delay_0
            decfsz    counta,f       // counta--, if(counta == 0) skip next line
            goto       $+2             // go forward 2 instructions from current
            decfsz    countb,f       // countb--, if(countb == 0) skip next line
            goto       Delay_0       // goto Delay_0
            
            decfsz    count1,f       // count1--, if(count1 == 0) skip next line
            goto       d1               // goto d1
            retlw      0x00            // return 0

            end
 
thanks checkmate.

I dont understand how its achieving 250 ms? I understand the individual instructions, its the "whole".

I apreciate the reply none the less.

:)
 
This is actually a 1mS delay routine, generated by the online program on the PICList - I wrapped an extra counter (count1) round it, in order to have an adjustable routine, giving from 1 to 255mS.

Many of the delay routines in the tutorials have an extra entry point, DelayW - where it delays the number of milliseconds passed in W. They often also have a number of preset delay entry points, just to make life easy :D

I used a decimal number because it makes more sense, 250mS is a LOT clearer than 0xFAmS.
 
Code:
// Call Delay takes 2 cycles (1 iteration)
Delay    movlw    d'250'          || 
            movwf    count1         // 2 cycles (1 iteration)
d1         movlw    0xC7          ||
            movwf    counta        || 
            movlw    0x01           || 
            movwf    countb        // 4 cycles (250 iterations)

Delay_0
            decfsz    counta,f       || 
            goto       $+2             || 
            decfsz    countb,f       || 5 cycles (250 * 198 iterations)
            goto       Delay_0       // + 4 cycles (250 * 1 iterations)
            
            decfsz    count1,f       || 
            goto       d1               || 3 cycles (249 iterations)
            retlw      0x00            // + 4 cycles (1 iteration)

            end
So total instruction cycles per call is
2*1 + 2*1 + 4 * 250 + 5*250*198 + 4*250 + 3*249 + 4*1 = 250255
Assuming you use a 4MHz oscillator, that's 1 MIPs(Million instructions per second). So each clock cycle lasts 1us. 250255 clock cycles take 250.255 ms.
The number of clock cycles taken by each instruction is given in the ref manual and datasheets under Instruction Set.
 
d1
mov ;1 cicle
mov ;1 cicle
Delay_0
decfsz counta,f ;1 cicle
goto $+2 ;2 cicle
decfsz countb,f
goto Delay_0 ;2 cicle
decfsz count1,f ;1 cicle
goto d1 ;2 cicle
retlw 0x00
;250x(199x5 cicle+5)=250x1000 cicle=250ms
 
Thanks to you all. Im learning that delays are fundamental to PIC programming. If I wanted to use nigels delay routine, as posted above, yet, adjust certain values to achieve different delay durations, what are the values which should be changed??

Take for example, changing this 250ms delay, into a 175 ms delay routine?
 
timothyjackson said:
Thanks to you all. Im learning that delays are fundamental to PIC programming. If I wanted to use nigels delay routine, as posted above, yet, adjust certain values to achieve different delay durations, what are the values which should be changed??

Take for example, changing this 250ms delay, into a 175 ms delay routine?

That's VERY easy (and explained in my previous post), the outer loop (which I wrote) executes the inner (1mS) delay 250 times - you just need to change the 250 to 175, this value is the delay time in mS.
 
Google around for PICLOOPS.
But it's important to understand how to count execution time when dealing with time-critical interrupts.
 
i have no sensitivity(right?) on time, when i use the timer, i always make mistake :(
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top