> As has been pointed out by a few others, do some research on state-machines. A state-machine should not be feared as something complex - it is simply a structured way of looking at a set of conditional jumps or case statements. Despite this it is a very powerful and useful model of many types of reality. Another major aspect of state machines is that they lend themselves absolutely ideally to interrupt driven operation. Trying to perform multiple tasks ("multitasking") without using state machines (or a Real Time Operating System (RTOS) which effectively does the same thing) is very very hard. With state machines, on each interrupt you generally increment (or decrement) a timer and then inspect a list of activities to see if each requires "service" at the resultant timer value. (Rather than sharing a single timer, you may have one "timer" or software counter per task and process the related task when the timer reaches zero.) Any task requiring service directs you to its current state. Appropriate activities (if any) are performed. A decisions is made as to what the next state should be and the appropriate timer is reset to wake you up again in this new state in due course. It may sound a little complex if you've never done it but it makes life much much easier than alternatives. As a typical example I have a multi state-machine application (versions happen to be on AVR & SX but that's irrelevant) that - reads a 9600 baud hardware UART and carries out basic actions - * implements a 4800 baud software UART to talk to a GPS unit - * reads a magnetic card swipe reader (2 channel clock and level data inputs), - switches on and off data and power to a printer and GPS, - * reads the speed of a serial square wave (vehicle speed), , - Operates a serial input shift register to add extra input lines. - Writes to a parallel data port. , (* Optionally implements another software UART to talk to a terminal, ) - * Implements a circular buffer to buffer GPS and magnetic card reader data to hw UART. - Sends a subset or all or no GPS data to hw UART. Items marked with a * are interrupt driven state machines that operate entirely autonomously as "virtual peripherals" (to coin a phrase :-) ). They need no program intervention to operate and may as well, from the programmer's point of view) be hardware peripherals (once you've got their software working :-) ). Example of an interrupt driven software UART Receiver This requires ONLY 2 states! Assume An IRQ occurs at 4 x the bit rate At each IRQ process the current state. A timer named Timer (just a memory location) is decremented once per interrupt This is indicative of state machine method - not of good programming style :-) (ie this is to show method, not how it would necessarily be done in detail). NO bells and whistles here (no start bit recheck, stop bit check etc) Data "appears" in DatatOut when a byte is received and flag ReadyFlag is set to 1. User must retrieve byte before another byte is received and reset ReadyFlag to 0. (Could as easily be interrupt on output) Data is N81 (8 data bits no parity, 1 start, 1 stop). E&OE (written on the fly). ____________ Initialisation ; call me once at start up State = 0 ReadyFlag=0 Timer = 1 ; __________________ ; INTERRUPT CODE: ; once per interrupt Decrement Timer If Timer > 0 then goto otherstuff If State = 0 then goto State0 If State = 1 then goto State1 Otherstuff: .................. ; ______ / State0: If input = high then ; Loop forever while line idle Timer = 1 ; do state 0 again in 1 tick If input = low then ; start bit Timer = 6 ; no debounce check State = 1 ; next time do bit inputting Counter = 8 ; inputting 8 bits Data = 0 goto otherstuff ; or some other common end point / State1: ; input a bit Place input bit into carry Rotate Data right with carry ; input bit moves into MSB Decrement Counter If Counter = 0 then State = 0 ReadyFlag = 1 DataOut = Data Timer = 1 ; half bit time If Counter >0 then timer = 4 ; wait one bit time then come back here goto otherstuff ; Djkstra would be sad :-) ; That's it. ;Try it, you'll like it. ; Russell McMahon -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu