Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Delay Timing for PIC16f family

Not open for further replies.


Newbie level 1
Apr 10, 2013
Reaction score
Trophy points
Activity points
Hello. Can you please help me on my program? I would like to generate 5ms, 2ms and 10ms delay and after that I will blink my LED. I can't fully understand the resources around the web. I hope you can teach me or provide me links that have clear explanations for beginners like me about the calculations needed to be acquired so I can design on my own.

First a few specific details:

What is the specific PIC16F that you will be utilizing in your design, PIC16F877A, PIC16F886, etc?

What will be the planned System Clock frequency (Fosc) of the design 4MHz, 8MHz, etc? Is this Fosc derived from a crystal or external oscillator, if so what is its frequency?

And, what language and compiler or assembler will you be utilizing to develop your code, XC8, MikroC, MPASM, MPASMX, etc?

Delays typically fall into to categories, software and hardware generated delays.

Each have their advantages and disadvantages, which should be weighted carefully when considering their application within your app code.

The delays required for visual perception of the changes of state in an LED are typically exceed 50ms, often in the 100ms range.

Most compilers offer builtin delay routines which are generated in Assembly language code to provide a certain level of deterministic precision, which cannot be typically obtained if generated by C language code.

A better choice would be to utilize a timer, timer interrupt and its associated Interrupt Service Routine (ISR). Changes can be made to the timer value within the ISR to affect a change in "blink" frequency.


Here is a simple assembly language delay routine with timing calculations in the comments. You may want to copy and paste it into a text editor as the "code" box mangles formatting of text.
This addresses timing issues of a software delay.
DELAY1		EQU	H'20'	; 3, 1 byte RAM locations Used for delay routine
DELAY2		EQU	H'21'	; General purpose ram begins at H'20'	  
DELAY3          EQU     H'22'
                            ; SET UP DELAY FUNCTION                          clocks to call delay       2
DELAY   CLRF   DELAY1       ; 1 clock cycle      delay1 = 0 so will decrement 256 times until 0         1
        CLRF   DELAY2       ; 1 clock cycle      delay2 = 0 so will decrement 256 times untii 0         1
        MOVLW  D'12'        ; 1 clock cycle                                                             1
        MOVWF  DELAY3       ; 1 clock cycle      delay3 = 12 so will derement  12 times until 0         1 
                            ;                                                                   total   6 clock cycles
                            ; DELAY LOOP
               ; This section runs 'delay1' x 'delay2' x 'delay3' times = 786432 
LOOP    DECFSZ DELAY1,1     ; 1 or 2 clocks - runs 255 times x 1 clock + 1 times 2 clocks = 257 
        GOTO   LOOP         ; 2      clocks - runs 256 times x 2 clock3 =                   512    
                            ;                                                        total  769 
                            ;                                                  769 X 256 X 12 = 2,362,368 clock cycles
               ; This section runs 'delay2' x 'delay3' times = 3072
        DECFSZ DELAY2,1     ; 1 or 2 clocks - runs 255 times x 1 clock + 1 times 2 clocks = 257     
        GOTO   LOOP         ; 2      clocks - runs 256 times x 2 clocks =                   512  
                            ;                                                        total  769 
                            ;                                                             769 X 12 = 9228 clock cycles 
               ; This section runs 'delay3' times = 12
        DECFSZ DELAY3,1     ; 1 or 2 clocks - runs 12 times x 1 clock + 1 times 2 clocks = 13                   
        GOTO   LOOP         ; 2      clocks - runs 12 times x 2 clocks  =                  24               
                            ;                                                             total  37 =  37 clock cycles
        RETLW  0	    ; 2 clock cycles                                                            2 clock cycles  
                            ;                                                       grand total 2,371,641 clock cycles

                            ;  formula: clock cycles x instruction time = delay time
                            ;              2,371,641 x (1 / 5,000,000)  = .4743282 second delay

                            ;  On a PICC16F877A the oscillator frequency is divided by 4 for the instruction clock
                            ;  a 20mhz crystal gives a 5mhz instruction clock

Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to