I require a delay of about 30 minutes using timers in PIC 18F4520. Using an internal crystal of 16 MHz, I would not be get desired delay from internal clock source. I thought of using external crystal for this purpose preferably 32.768 kHz. Actually (30 minutes) is a time where the micro controller has to perform it's function and after that it has to stop the operation of process. So, the idea was to use timer interrupt. Using 32 KHz oscillator and using timer0 as 16 bit counter we can get

2^16/32768 = 2 second delay (assuming prescaler = 1)

if pre scaler = 256 --> then max delay = 512 seconds , which is still less than 30 minutes.

Then how can we get delay in minutes? Thanks in advance.

  • \$\begingroup\$ There are other questions at hand. Do you require 16 MHz when the PIC is running? We understand the delay between moments of running. But do you really want such a slow clock when running every 30 minutes? Also, for cases like this, processors like the MSP430 provide you with both worlds. You can have an external high speed crystal clock, a low speed 32.768 kHz clock and buckets of internal timers to use them. And they wake up VERY fast. They also include a DCO so you can ditch the 16 MHz crystal, if you want. \$\endgroup\$ Jul 27, 2023 at 12:28
  • \$\begingroup\$ Did you search the datasheet and application note? Microchip has fairly good documentation, i doubt you can't find that easily. Furthermore, it his possible for a 16MHz clock to drive a timebase that allows delays in this order of magnitude. You might not need a rtc at all. \$\endgroup\$
    – Julien
    Jul 27, 2023 at 12:39
  • \$\begingroup\$ I was wondering that if can we use other oscillator of 32 KHz that can provide external pulse at TOCKI pin. What do you think? \$\endgroup\$
    – newbie
    Jul 27, 2023 at 13:30
  • 1
    \$\begingroup\$ You need to understand the difference between an external oscillator and an external crystal. If you have a 32kHz oscillator then you can connect it to T0CKI. But if you have an external 32kHz crystal then you need to use Timer1 pins T1OSO & T1OSI. \$\endgroup\$
    – brhans
    Jul 27, 2023 at 14:51
  • 1
    \$\begingroup\$ @newbie why not increment a variable every x second and check it in your interrupt? By doing that, you get the best of both world. Incrementing a variable and checking if it reached a certain value don't take a lot of instructions so you should be good. \$\endgroup\$
    – Julien
    Jul 28, 2023 at 12:22

1 Answer 1


You can use any clock you need for operation, even if the internal timer does not give you the desired delay directly.

Instead, use the timer to trigger a periodic routine at a convenient rate. Count the number of triggers in software. When the appropriate count is reached, execute the final function.

This way you can achieve delays in the range from very small fractions of seconds to the end of the universe.

You can choose between multiple options to realize the triggering. Some are:

  • "Polling": Loop while waiting for the timer flag to raise.
  • "Interrupt": Implement an interrupt service routine.

Due the simple task to solve, I would go for the polling solution.

With the interrupt solution you could try to put the MCU into sleep to save power. However, we don't know your requirements concerning power.

Final note: Since you did provide neither the specific MCU type nor a minimal program attempt, we cannot present you suggestions in code.

  • \$\begingroup\$ thanks for the answer. i think that instead of polling, interrupt shall be used in my case as my controller needs to work on some task during this delay duration and from what i have read just now i found that polling would keep the controller busy during the whole time.What do you think? \$\endgroup\$
    – newbie
    Jul 28, 2023 at 6:57
  • \$\begingroup\$ @newbie That's right. If my post answers your question, feel free to mark it as accepted. This is supposed to do instead of "thank you" comments. SE/EE is not a forum. ;-) \$\endgroup\$ Jul 28, 2023 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.