+ Post New Thread
Results 1 to 12 of 12
  1. #1
    Member level 2
    Points: 452, Level: 4

    Join Date
    Jun 2012
    Location
    pune
    Posts
    48
    Helped
    5 / 5
    Points
    452
    Level
    4

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

    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

  2. #2
    Member level 5
    Points: 937, Level: 6
    oyvdahl's Avatar
    Join Date
    Apr 2010
    Location
    Oslo, Norway
    Posts
    86
    Helped
    6 / 6
    Points
    937
    Level
    6

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

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

    Oyvind



    •   AltAdvertisement

        
       

  3. #3
    Advanced Member level 5
    Points: 38,590, Level: 48
    Achievements:
    Created Album pictures Created Blog entry 7 years registered
    Tahmid's Avatar
    Join Date
    Jun 2008
    Location
    Silicon Valley, California, USA (from Dhaka, Bangladesh)
    Posts
    4,758
    Helped
    1799 / 1799
    Points
    38,590
    Level
    48
    Blog Entries
    22

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

    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.


    1 members found this post helpful.

  4. #4
    Member level 2
    Points: 452, Level: 4

    Join Date
    Jun 2012
    Location
    pune
    Posts
    48
    Helped
    5 / 5
    Points
    452
    Level
    4

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

    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



  5. #5
    Member level 5
    Points: 937, Level: 6
    oyvdahl's Avatar
    Join Date
    Apr 2010
    Location
    Oslo, Norway
    Posts
    86
    Helped
    6 / 6
    Points
    937
    Level
    6

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

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



    •   AltAdvertisement

        
       

  6. #6
    Advanced Member level 3
    Points: 7,837, Level: 21
    milind.a.kulkarni's Avatar
    Join Date
    Oct 2011
    Location
    Bangalore
    Posts
    923
    Helped
    215 / 215
    Points
    7,837
    Level
    21
    Blog Entries
    1

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

    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... ??
    Hope the post will be helpful to you....
    "Sharing is the best way of Learning"



  7. #7
    Advanced Member level 5
    Points: 38,590, Level: 48
    Achievements:
    Created Album pictures Created Blog entry 7 years registered
    Tahmid's Avatar
    Join Date
    Jun 2008
    Location
    Silicon Valley, California, USA (from Dhaka, Bangladesh)
    Posts
    4,758
    Helped
    1799 / 1799
    Points
    38,590
    Level
    48
    Blog Entries
    22

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

    Quote Originally Posted by i_chaitanya View Post
    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 by Tahmid; 13th June 2012 at 07:38. Reason: Typo in expression [( 1/(4 * 10^6) ) * 4]


    2 members found this post helpful.

  8. #8
    Advanced Member level 3
    Points: 4,828, Level: 16
    papunblg's Avatar
    Join Date
    Oct 2010
    Location
    Kolkata India
    Posts
    726
    Helped
    172 / 172
    Points
    4,828
    Level
    16
    Blog Entries
    9

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

    Quote Originally Posted by Tahmid View Post
    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 by papunblg; 12th June 2012 at 20:01.
    Rajsekhar Nag
    -------------..--------
    Knowledge Grows When Shared



  9. #9
    Administrator
    Points: 69,512, Level: 64
    Achievements:
    Created Album pictures Created Blog entry 7 years registered
    bigdogguru's Avatar
    Join Date
    Mar 2010
    Location
    Southwest, USA
    Posts
    9,832
    Helped
    2358 / 2358
    Points
    69,512
    Level
    64
    Blog Entries
    4

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

    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
    The Edge... there is no honest way to explain it, because the only people who really know where it is are the ones who have gone over.
    -- Hunter S. Thompson



  10. #10
    Member level 2
    Points: 452, Level: 4

    Join Date
    Jun 2012
    Location
    pune
    Posts
    48
    Helped
    5 / 5
    Points
    452
    Level
    4

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

    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)



  11. #11
    Advanced Member level 5
    Points: 38,590, Level: 48
    Achievements:
    Created Album pictures Created Blog entry 7 years registered
    Tahmid's Avatar
    Join Date
    Jun 2008
    Location
    Silicon Valley, California, USA (from Dhaka, Bangladesh)
    Posts
    4,758
    Helped
    1799 / 1799
    Points
    38,590
    Level
    48
    Blog Entries
    22

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

    *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.


    1 members found this post helpful.

    •   AltAdvertisement

        
       

  12. #12
    Administrator
    Points: 69,512, Level: 64
    Achievements:
    Created Album pictures Created Blog entry 7 years registered
    bigdogguru's Avatar
    Join Date
    Mar 2010
    Location
    Southwest, USA
    Posts
    9,832
    Helped
    2358 / 2358
    Points
    69,512
    Level
    64
    Blog Entries
    4

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

    Quote Originally Posted by i_chaitanya View Post
    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
    The Edge... there is no honest way to explain it, because the only people who really know where it is are the ones who have gone over.
    -- Hunter S. Thompson



--[[ ]]--