Microchip PIC Delay Calculator

Delay Calc Banner
Delays are very essential in the operation of a microcontroller. Because microcontrollers execute instructions at a very fast rate (in microseconds), programmers need to apply delay so that it can interact properly with humans, for example, delay must be used to create software debouncing. Unfortunately, the MPLAB IDE does not have a built-in delay function unlike the Arduino IDE (Arduino stuffs coming soon 🙂 ) so the programmer have to write his/her own delay assembly code.

Let’s say the MCU is running at 4MHz. That’s a period of 250 nanoseconds. And every instruction cycle takes 4 periods. That’s 1 microsecond per instruction. If the programmer wants to make a delay of 5ns, that’s simple. Just execute the nop (no operation) instruction 5 times. But what if it’s 100ns or even 1 second? That’s where instructions like decfsz comes in. We can create longer delays by compounding a short section of delay. The idea is there but we need to manually calculate the number of times we should compound the short delay. That is why I wrote this program to simplify the calculation.

First and foremost, I should mention that I got the inspiration from a delay calculation program found on the Cytron website. The program was written by Kong Wai Weng in October 2004. I saw some improvements that have to be made so I wrote a new one myself.

The program is written in Visual Basic 2010 and should work fine on Windows 7, Vista or XP. For linux users, it will even run on Ubuntu or Debian with wine installed. The main highlight of my program is that the user can see the delay subroutine being generated and the program can copy the subroutine to the clipboard by just clicking a simple button. That way the programmer can paste it over at the MPLAB IDE without having to type the long subroutine code and substituting the values of d1, d2 and d3 manually.


For those wondering how the program works, basically the user have to use the trial and error approach. There are three values of d1, d2 and d3, so the user have to try and guess these numbers, and slowly adjusting them to match the delay duration they desire. I tried to write the program in a way that it can predict the values of d1, d2 and d3 but it will consume a lot of processing time as the computer have to try each possible combination of d1, d2 and d3. That’s 255 times 255 times 255 which is 16.6 million possibilities. And not to mention that it has to be run with a few decimal point so as to get a closer value (which will be rounded off in the end). I figured out the code but most of the time it just locks up the program because the process is just simply too long.

Finally, I hope the program is useful to you. To download it, click on the button below.
To show your support, ‘Like’ my website. If you’ve come across any bugs and errors while using the program, please inform me at [email protected]

Thank you.

.exe  110KB

Print Friendly
  1. FranQ03-18-2014

    It’s a really very good fine website, no tricks, no adds, all 100 % trusted. Thank you , Share this web for thankfully

  2. lall03-06-2014

    hi Siew Wai Hung wonderful job..it is great important to me………Thanks a lot

  3. Good day! I just wish to give an enormous thumbs up for the great information you could have right here
    on this post. I can be coming back to your weblog
    for extra soon.

Leave a Reply