On 21/04/2012 15:33, Goran Hosinsky wrote: > I am perhaps going at this in the wrong way. It is a watering system and= =20 > I need delays in the range of 10's of > minutes up to many hours. The chip is a 16F887 and for the delays I use=20 > the HI-TEC C __delay_ms() macro. > This macro can handle delays up to about 50 seconds and I use a loop=20 > with it to get the necessary total delay. > I would like to be able to interrupt via RS323 and change the parameters= =20 > for the watering. The precision of the > timing is not important. If I was aiming to do this I would probably approach it: 1) Calculate a fixed period delay that divides into your minimum "off" time. Ideally you should be able to use a hardware timer for this that can work while the device is in sleep mode. 2) Sleep for 99% of the time. Great for batteries. 3) Wake the device when the timer fires, increment a counter. Set a flag, defined like 'volatile char timer_flag'[1]. I would do this in the ISR. 4) Check for the timer_flag. If set, see if the number of interrupts (e.g. 5 * 1 minute =3D 5 minutes) is now greater than your "off" time. If so start watering. I would do all of this in the main loop. 5) Go back to sleep. Using interrupts makes it easier to add other features. If you want to wake on RS232 you can, it's just another interrupt and another variable "flag". Timer reloads and peripheral (e.g. TMR0IF) resets obviously need to be accounted for in the ISR. Note you don't actually have to "sleep" the PIC hardware if you don't want to. I have used this approach for everything from decoding IR (microseconds) to sleeping for hours (waking once a minute for 3 above). To me this is a very simple version of the "state machine" that Alan mentioned. I'd draw a flowchart, perhaps there's a good website that can produce them quickly. If you'd like I can throw up some code on pastebin that illustrates the above. David 1 - Whilst not ANSI C compliant Hi-Tech will allow unsigned bit timer_flag, which is what I'd normally use. --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .