Welcome to EDAboard.com

Welcome to our site! EDAboard.com 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.

[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
Joined
Jun 4, 2012
Messages
48
Helped
4
Reputation
10
Reaction score
5
Trophy points
1,288
Location
pune
Activity points
1,569
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
Joined
Apr 26, 2010
Messages
86
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,288
Location
Oslo, Norway
Activity points
1,810
Hi
You need to enable interrupts and configure a timer.
You can probably find example code in the datasheet of your microcontroller.

Oyvind
 

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,543
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.
 

i_chaitanya

Member level 2
Joined
Jun 4, 2012
Messages
48
Helped
4
Reputation
10
Reaction score
5
Trophy points
1,288
Location
pune
Activity points
1,569
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
Joined
Apr 26, 2010
Messages
86
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,288
Location
Oslo, Norway
Activity points
1,810
Can you post the math that you do not understand here? Then maybe we can help you understand it.
 

milind.a.kulkarni

Advanced Member level 3
Joined
Oct 6, 2011
Messages
923
Helped
214
Reputation
428
Reaction score
208
Trophy points
1,333
Location
Bangalore
Activity points
7,438
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

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,543
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:

papunblg

Advanced Member level 3
Joined
Oct 22, 2010
Messages
726
Helped
172
Reputation
344
Reaction score
165
Trophy points
1,343
Location
Kolkata India
Activity points
6,437
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

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,422
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
Joined
Jun 4, 2012
Messages
48
Helped
4
Reputation
10
Reaction score
5
Trophy points
1,288
Location
pune
Activity points
1,569
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

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,543
*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.
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,422
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

Good luck with your project.

BigDog
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top