(This time I added the subject tag correctly ... ) Roman wrote: >Hi Greg, a good way to handle this is to use a fast >timer interrupt routine, which checks the timer and >makes an "event" every 100mS. This one-sec timer >routine at: >http://www.ezy.net.au/~fastvid/one_sec.htm > >can be easily adapted to do that. Then with such >a long (70mS) task I suggest doing it OUTSIDE the >int routine, so the int routine just sets a bit, then >in your main non-int code you detect that bit and >start the 70mS task. That way the timer int keeps >perfect time while the task is being performed. >:o) If you're writing in HI-TECH C, you can use the freeeware version of our RTOS (called Salvo Lite) to do exactly this very quickly. Here are two ways to do it: 1) Call the system timer (OSTimer()) every, say, 10ms from inside an ISR. Put the "big code" in a task. Have the task delay itself for 100ms (10 system ticks, via OS_Delay(10)), and you're done. About 20 lines of C plus your "big task". Code looks like this: #define BIGTASK_P OSTCBP(1) _OSLabel(BigTask1) void BigTask( void ) { /* put 70ms task's initialization code here */ for (;;) { /* delay 100ms */ OS_Delay(10, BigTask1); /* put rest of 70ms task here */ } } void main( void ) { /* do global inits (e.g. setup timer TMR2) here */ /* initialize OS */ OSInit(); /* create a task with priority 5 */ OSCreateTask(BigTask, BIGTASK_P, 5); /* enable interrupts */ ei(); /* start multitasking */ for (;;) OSSched(); } interrupt MyISR( void ) { /* clear interrupt flag, e.g. TMR2IF */ /* call system timer */ OSTimer(); } 2) Setup a 100ms interrupt that signals a binary semaphore via OSSignalBinSem(). In the task, wait for the event (i.e. wait for the binary semaphore to be signaled). Again, about 20 lines of C: #define BIGTASK_P OSTCBP(1) #define BINSEM1_P OSECBP(1) _OSLabel(BigTask1) void BigTask( void ) { /* put 70ms task's initialization code here */ for (;;) { /* wait until signaled from ISR */ OS_WaitBinSem(BINSEM1_P, BigTask1); /* put rest of 70ms task here */ } } void main( void ) { /* do global inits (e.g. setup timer TMR2) here */ OSInit(); OSCreateTask(BigTask, BIGTASK1_P, 5); /* event has initially not happened */ OSCreateBinSem(BINSEM1_P, 0); ei(); for (;;) OSSched(); } interrupt MyISR( void ) { /* clear interrupt flag, e.g. TMR2IF */ /* signal event */ OSSignalBinSem(BINSEM1_P); } Method 2) above is the method Roman is suggesting, using an RTOS to handle the occurrence of an event. Salvo Lite handles up to 3 tasks and 4 events on the PIC16. Count on around 600 words for the RTOS in the examples above -- with the full version, you can shrink that down considerably. Since BigTask() is hogging resources for 70ms (what on earth takes 70ms = 70,000 cycles at 4MHz, BTW?), this is not such a good thing in an RTOS environment. But it's easy to fix -- just add a couple of OS_Yield()'s to BigTask() and then other, higher-priority tasks will have a chance to execute if they become eligible sometime between when BigTask() starts and when it finishes. Their total time to execute must be less than 30ms (easily done) so that BigTask() can run every 100ms for 70ms. Even if you're not writing in C, the above examples may give you an idea of how to use the concept of an event in the foreground (i.e. interrupt) to trigger the execution of code in the background (i.e. the main loop). The trouble you face is how to "chop up" the 70ms task so that other things can happen. By putting it into the background, then other interrupts can still occur, but you still are faced with the problem of how to do "bigger" processing while BigTask() is running. The RTOS solves that elegantly for you by managing the "big" processing as tasks. Regards, -- ______________________________________ Andrew E. Kalman, Ph.D. Salvo(TM), The RTOS that runs in tiny places(TM) Pumpkin, Inc. 750 Naples Street San Francisco, CA 94112 tel: (415) 584-6360 fax: (415) 585-7948 web: http://www.pumpkininc.com email: aek@pumpkininc.com -- ______________________________________ Andrew E. Kalman, Ph.D. aek@pumpkininc.com -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.