# [SOLVED][Moved] how to generate 1,2,4,7 hour of delay in mikro c

Status
Not open for further replies.

#### i_chaitanya

##### Member level 2
hello freinds
i want to generate an interrupt after 1,2,4,7 hours if any body could help me with the code in mikro c it will be very helpful.
actual codes will oblige me a lot .
thanks a lot

#### oyvdahl

##### Member level 5
Hi
You need to enable interrupts and configure a timer.
You can probably find example code in the datasheet of your microcontroller.

Oyvind

#### Tahmid

If you aren't going to be doing anything during that long period, you can use software delay, using the delay_ms() function and for loop.

If you are going to be doing something during that period, you need to use timer as Oyvind has said.

I don't know which PIC you'll be using. But you can use Timer 1 as most PICs have a Timer 1 and Timer 1 is a 16-bit counter (8-bit PICs). Similarly, you can make do with Timer 0 as well.

Calculate how long it'll take for the timer to overflow - the interrupt to occur. Then, you can calculate how many times the interrupt must occur for the required time. Use a counter in the ISR to check this.

Hope this helps.
Tahmid.

vinodhembedded

### vinodhembedded

Points: 2

#### i_chaitanya

##### Member level 2
thanks a lot guys
but this did not helped me
what i want to do is that i am trying to make a motor timer controller .at the heart of setup i am using pic 16f877a with a 24 Hz crystal (it can be changed) it has three switches attached which controls the time for which the motor will run if i press first key the motor should run for 1 hour if i press second then it should run for 2 hours if i press third it should run for 4 hour . i am new to controller and its programming so having problem with the concept of timer . even after going through the data sheet of controller i cannot understand the math used by the micro controller to calculate time. once i understand the concept how time is calculated by Uc i think i will be able to write the code.
if you could help me with this(math) then it will be very helpful.
thanks

#### oyvdahl

##### Member level 5
Can you post the math that you do not understand here? Then maybe we can help you understand it.

#### milind.a.kulkarni

You need to post your schematic to the forum to get better clarity of your application ....Also I have a question r u planning to automatic power detection....as I think there is a frequent power cut... ??

#### Tahmid

thanks a lot guys
but this did not helped me
what i want to do is that i am trying to make a motor timer controller .at the heart of setup i am using pic 16f877a with a 24 Hz crystal (it can be changed) it has three switches attached which controls the time for which the motor will run if i press first key the motor should run for 1 hour if i press second then it should run for 2 hours if i press third it should run for 4 hour . i am new to controller and its programming so having problem with the concept of timer . even after going through the data sheet of controller i cannot understand the math used by the micro controller to calculate time. once i understand the concept how time is calculated by Uc i think i will be able to write the code.
if you could help me with this(math) then it will be very helpful.
thanks

Are you sure it's 24Hz?

Anyways, since you say it can be changed, I'll explain with 4 MHz crystal in mind.

If you have 4Mhz crystal, the instruction cycle is [( 1/(4 * 10^6) ) * 4] seconds = 1 µs. So, each instruction takes 1 µs. Each timer increment occurs each 1µs.

Let's say we're using Timer 1.

It is a 16-bit timer. So, it increments from 0 to (2^16)-1 = 65535. Each increment occurs each 1 µs. When the timer reaches 65535, the timer overflows. Since each increment takes 1µs, 65535 increments takes 65535 µs. Then, it takes 1 more µs for the timer to overflow to zero. This is when the interrupt flag is raised. So, starting from zero, interrupt occurs each 65.536 ms.

Using the prescaler, we can prolong this time. If we use the 1:8 prescale setting, each timer increment now takes (1 µs * 8) = 8µs. So, the interrupt occurs every (65.536 ms * 8) = 524.288ms.

Have you understood this much? This is more or less all the math required to calculate the time for one interrupt to occur.

If you are clear regarding this, I will try to explain interrupt implementation in this case(if you need help with these).

Hope this helps.
Tahmid.

Last edited:

Points: 2

### vinodhembedded

Points: 2

#### papunblg

Are you sure it's 24Hz? Anyways, since you say it can be changed, I'll explain with 4 MHz crystal in mind.Tahmid.

i_chaitanya, Since you have written that you are new to microcontroller programming, pay some attention to the above comment by Thamid.
It means so much. It that 24Hz(?) crystal (& attached capacitors values) is/are mentioned in the datasheet?

Last edited:

#### bigdogguru

I believe we can all be fairly certain that i_chaitanya is mistaken when he states the crystal frequency is 24Hz, unless of course, it is in fact an oscillator. Although an uncommon frequency, a 24Hz Oscillator is possible.

And while a 24kHz crystal is certainly possible, it is not one of the more common crystal frequencies encountered.

@i_chaitanya What exactly are the markings on your crystal? What is the package configuration?

If it has something like "24.000" it is most likely a 24MHz crystal. In which case you are overclocking your PIC16F877A which has a maximum operating frequency of 20MHz.

You may want to post a photo of the crystal in question, without knowing the exact clock frequency, establishing a reasonably accurate delay will be extremely difficult.

BigDog

#### i_chaitanya

##### Member level 2
sorry for the mistake guys i wrote 24Hz instead of 24MHz ....
Tahmid. thanks a lot ... it helped me a lot but still there is one query regarding the equation "1/(4 * 10^6) ) /4" i did not understand why you have used the last "4" (i understand this much that 4*10^6 is the frequency i supplied )will it remain constant for every Uc or it will change and if it can change then how do we decide what its value will be .thanks a lot BigDog for reminding me of the over-clocking the PIC i didn't knew about this situation (after reading your post i went through the data sheet and then i came to know)

#### Tahmid

*4 in the last part: [/4 was a typo, fixed it now]

Code:
[( 1/(4 * 10^6) ) * 4]

will be constant for all PIC 16 and PIC 18 microcontrollers where it takes four oscillator cycles to complete one instruction cycle.

Since, in the PIC16F877A, it takes four oscillator cycles to complete one instruction cycle, the frequency becomes 1/4 th and the time is increased 4 times. And hence * 4.

Hope this helps.
Tahmid.

vinodhembedded

### vinodhembedded

Points: 2

#### bigdogguru

thanks a lot BigDog for reminding me of the over-clocking the PIC i didn't knew about this situation (after reading your post i went through the data sheet and then i came to know)

No problem. It always helps to reference the device's datasheet.

The following are a few tutorials specific to MikroC concerning Timers:

PIC Microcontrollers Chapter 4: Timers

Lab 7: PIC Timers and Counters (Part 1)

Lab 16: Understanding Interrupts

00 to 99 minute timer using PIC16F628A microcontroller

BigDog

Status
Not open for further replies.