PDA

View Full Version : Exact delay for a loop



mesamune80
- 16th October 2006, 04:49
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.

amindzo
- 16th October 2006, 06:00
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.

sougata
- 16th October 2006, 06:10
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

mesamune80
- 16th October 2006, 06:57
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.

sougata
- 16th October 2006, 07:47
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.

mesamune80
- 16th October 2006, 08:31
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.

sougata
- 16th October 2006, 08:53
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.

mesamune80
- 17th October 2006, 02:02
Thanks sougata for the info,i'll try and work it out then .thanks!

mesamune80
- 19th October 2006, 10:09
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.