Exact delay for a loop


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2006
    Posts
    14

    Default Exact delay for a loop

    Hi there,i would like to use a timer delay for my loop,
    which mean to say that when i go in a loop a timer like 1 minutes(actual time)
    will start to run.
    After 1 minutes elapsed then the program will automatically go out from that loop,anyone knows which method to use for this kind of function?Thanks.

  2. #2
    Join Date
    Aug 2006
    Location
    Iran
    Posts
    94


    Did you find this post helpful? Yes | No

    Default

    Hi,
    if you don't want do anything in one minute ,you can use pause instruction:

    pause 60000 'wait for one minute

    or you can use the timers.

  3. #3
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default Mention your PIC

    Hi,

    Just before entering the loop start your timer. Make sure you clear it and setup the prescale right before starting it. Take a counter variable and clear it too.

    Now inside your loop poll for the timer overflow bit (mention PIC I will clarify or read DATASHEET). If it is one clear it and increment your counter variable. Depending on your oscillator and prescaler you know how long it takes for the timer to count till overflow. When your counter inside the loop exceeds a preset jump to anywhere you like.

    Only caution is your loop should not take more time to execute than one timer cycle

    I use this in my menu routines for autoexit when a user forgets to get out of the menu.

    Hope this helps
    Regards

    Sougata

  4. #4
    Join Date
    Apr 2006
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    Thank you very much to sougata for this useful information,now i got the rough idea on how to do the job,but for PBP there is only ON Interrupt for any interrput command,is it neccessary or should i said is a must for me to use assembley language?
    For PBP is any way which is possible for me to reload the timer as you mention earlier ? Thanks.

  5. #5
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default No need to use interrupt

    Hi,

    Even if you turn-off the Global Interrupt enable bit or the respective peripheral interrupt enable bits the Interrupt Flag for respective peripherals do get set on a PIC. So you just need to poll the Flag and all of them are accessible in PBP. Since your timer is rolling over you have enough time to poll the flags between rollovers. If you use less prescaler and a tight loop then possibilly the delay in testing the condition would suffice your need. As I mentioned in the previous post please mention the PIC and the Oscillator frequency to give you a better idea.
    For example if you are using Timer0 and your prescaler is around 1:8 (divide by 256) then your timer will overflow 15.25 times every second. So if your loop polls the flag 15 times you increase the seconds counter. Remember to clear the flag just after polling and getting a set bit. By adjusting the offset and using interrupt it is possible to get more accurate time base. But if your application permits you can choose this way. You can keep the prescaler low so that errors in every cycle is reduced however that solely depends how much code you need to excute in your loop. If the loop is lengthy in terms of execution time then you can put in frequent checking through gosubs and calling your check routine every now and then. Kind'a PBP On-Interrupt Scheme.
    Regards

    Sougata

  6. #6
    Join Date
    Apr 2006
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    Actually i just need it to use in my RPM counter program ,the loop which need to be executed is a repeated count loop that add a variable everytime a pin becomes LOW.(which i use a photosensor to scan the revolution per minutes for my DC motor).i use PIC16F877 and 4MHZ crystal for my application
    So for a certain time (1 minutes ) i know that how many times my motor slice has through my sensor ,by this way i can know the RPM of the motor,or is there any other way i can achieve that?Thanks.

  7. #7
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default Better to do with Interrupt

    Hi,

    Don't tell me you will be counting pulses for 1 minute and update your display. Too boring for a RPM counter. Why not check it every second and multiply the reading by 60. 1 sec refresh rate for your display. It is best to use Timer1 in counter mode and dumping the results every 1 second. For that you need a 1 sec timebase. If you are not accustomed with interrupts then this is high time to get going. Darrel has done a great job look here http://www.picbasic.co.uk/forum/showthread.php?t=3251 or http://darreltaylor.com/DT_INTS-14/intro.html and here http://darreltaylor.com/DT_INTS-14/elapsed.html for your timebase thing.
    Regards

    Sougata

  8. #8
    Join Date
    Apr 2006
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    Thanks sougata for the info,i'll try and work it out then .thanks!

  9. #9
    Join Date
    Apr 2006
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    Hi there ,yesterday i had tried the timer interrupt function but ,i think i need one more interrupt for my sensor to detect (i use RB0 to trigger the pin) but i encounter 1 problem ,which is in my program i need 2 interrupt with differnt priority,any idea how to accomplish this ?thanks.

Similar Threads

  1. Controlsystem for compact sporting (clay shooting)
    By Fredrick in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th July 2009, 16:48
  2. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  3. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  4. Memory Space of the PIC16F84...
    By Tear in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 1st July 2005, 19:55
  5. Problem with saving to EEPROM...
    By Tear in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st July 2005, 00:10

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts