David Williams wrote: > I've got to set a pin every 24 hours plus or minus 10 minutes. I need > the timer to work while I'm doing a bunch of other things Care to explain the "other things"? It makes it far easier to suggest code. > (I guess this means I've got to use interrupts). This appears to be a common misconception. There are *THREE* ways to achieve this functionality on a PIC with timer and interrupts, *two* on one with timer and no interrupts (16C5xx, 12c5xx). The three ways are: 1} Isosynchronous code - loops performing all functions take a measured time to cycle. Very tedious, virtually impossible in "C". 2} Interrupt-driven. On interrupt, counters are incremented/ decremented and overflow to other counters. The main routine can "peek" at these counters and determine when to perform the necessary task which it does outside of "interrupt time", thus avoiding problems with interrupts overlapping. To resolve whether the task has been performed at each required interval however, the interrupt routine can set a (series of) flag(s) on certain overflows e.g., each hour. The mainline code then checks if the flag is set *and* the hour is right, if so clears the flag (so it doesn't perform the task twice) and performs the action. Hint: These flags are all bit flags within a byte; the interrupt routine sets the whole byte. Easier if worked the other way - it clears all the bits at once. Along this line, it simplifies things a bit if the interrupt routine only deals with say, seconds, setting a flag each second which the main routine uses to advance the second count and count minutes, hours, days etc. But this does lead to the next proposal: 3} Timer-polling. This is the simplest and easiest to code, and can be done on the 12-bit core devices without interrupt hardware. That makes it a very *mature* approach! The basis is to set up the timer and prescaler so that the timer overflows every so many thousand clock cycles, more than enough to perform either the whole of the "other" functions, or a substantial module of those functions. After performing a function group known to take such a time, you wait polling for the overflow, perform the timing count when it happens and go on to the next function group. Thus for one function: While TRUE Do main_function wait_for_rollover timer_count Loop and for multiple parts of code each taking less *on average* than the overflow time: While TRUE Do main_function1 wait_for_rollover timer_count main_function2 wait_for_rollover timer_count main_function3 wait_for_rollover timer_count main_function4 wait_for_rollover timer_count Loop For example, function 1 can take 1¸ times the overflow time while function 2 is very short, the first overflow is serviced before another overflow occurs, and the short second function brings the next overflow in on time. You can even cut it slightly finer than that. The "rollover" code (should be a subroutine) on the devices with an interrupt flag (T0IF) consists of polling for that flag *and clearing it when found*, but note: no interrupt, specific or global is enabled. (Other interrupts can however be enabled as long as they will not make the mainline code modules run significantly over-time as above.) The timing cycles are each of the form: timer macro unit,unit_max,end_time decfsz unit goto end_time movlw unit_max movwf unit endm e.g., for seconds, minutes decfsz seconds goto done_time movlw 60 movwf seconds decfsz minutes goto done_time movlw 60 movwf minutes could be macro coded as: timer seconds,60,done_time timer minutes,60,done_time ..more timers.. ..payload; i.e. what you do at the end.. done_time > Anyway, anyone have any ideas how to set up a 24 hour count? C code > is preferred. Sorry, I'm not good on "C", and don't think it suits timing applications. Further hint on using interrupts and timer in general: IMNSHO don't even *think* of setting/ re-setting/ altering the timer value within the interrupt or poll routine. Always let it count un-impeded, use "nice" numbers. *Far* less heartache that way. KISS! -- Cheers, Paul B.