This is a multi-part message in MIME format. ------=_NextPart_000_0000_01C2D8C7.AC2829C0 Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: 7bit The state-machines type of programming really has me very interested. I have been using that type of approach in my interrupt routines. I was worried though that I was writing poor code as the flow was in trapped in my head and could never document that flow easily with just comments at the end of the line. The fact that it is a well recognised system and that there are proper ways to generate a "neat table" for the permutations is some thing I have to follow up. This is really what I have been looking for! The fact that Timers are an integral part of the system is really at the heart of my programming philosophy. I never use a DELAY routine, it is such a waste of processor time. And as a result I have not yet had to go above 4Mhz except on speed critical systems such as encoder readers. In some of my programs I have up to 36 timers running. As a thank you to every one I have attached a timer routine that I use. It is very compact (only for the 18 series) and very expandable. The code in the file is configured for 24 16 bit timers. To use load the timer with the value you wish to count down from. Set the appropriate bit in TMRFLAGSX.X and away it goes. Just check the bit to see if it has timed up. Thanks Tim -----Original Message----- From: pic microcontroller discussion list [mailto:PICLIST@MITVMA.MIT.EDU]On Behalf Of John Sanderson Sent: Thursday, February 20, 2003 9:08 AM To: PICLIST@MITVMA.MIT.EDU Subject: [PIC]: Programming logic solving Hello Timothy & PIC.ers, As has been pointed out by a few others, do some research on state-machines. I had only a vague idea of the concept when I started using PICs, and somehow (eesshh.. brain-burning stuff) produced my original products without them. The project definitions would not differ vastly from your own. One day, browsing through the p*clist, I came across Andy Kunz's treatise on his use of the things. He gave us some very tightly written asm code, which I scratched my hair out trying to follow, but the concept was very clear :-- LOOP_1 looping in a known state, give it a number (eg. 0-100). | \/ monitor all possible trigger conditions which could kick the program to another state. | \/ on recognition of any such trigger (stimulus) call a transition subroutine, followed by a destination state. The transition subroutine *and* destination state are both optional, the destination state can even be the original one. | \/ no stimuli? >> goto LOOP_1 The whole shebang gets formalised in a neat table, I've been adapting the technique for +/- 4 years now & can't consider using any other way of writing code for target apps. with multiple stable states. I'll send you more info, off-list if you want. ..mmm... If Andy's around maybe he could comment & give us the latest skinny on these.. best regards, John >Date: Tue, 18 Feb 2003 12:06:33 -0000 >From: Timothy Box >Subject: [PIC]: Programming logic solving > >Hi all > >Being a self taught programmer I often waste a lot of time converting "I >WANT TO DO XXXX" into code. Do our learned gentlemen have any pointers to >how they would tackle the task or perhaps a good site. > >I know it all depends on exactly wha .. eMail from the desk of John Sanderson. JS Controls, PO Box 1887, Boksburg 1460, Rep. of S. Africa. Tel / Voice / Fax : 011 893 4154 email : jsand @pixie.co.za Cell : 082 741 6275 Manufacturer & Purveyor of laboratory force testing apparatus and related products & services. -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu ------=_NextPart_000_0000_01C2D8C7.AC2829C0 Content-Type: application/octet-stream; name="timer16.asm" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="timer16.asm" ; 16 bit timer routine By Timothy Box any questions mail to = tim@tjbsystems.com include p18f452.inc cblock 0x00 TIMER0:2 TIMER1:2 TIMER2:2 TIMER3:2 TIMER4:2 TIMER5:2 TIMER6:2 TIMER7:2 TIMER8:2 TIMER9:2 TIMER10:2 TIMER11:2 TIMER12:2 TIMER13:2 TIMER14:2 TIMER15:2 TIMER16:2 TIMER17:2 TIMER18:2 TIMER19:2 TIMER20:2 TIMER21:2 TIMER22:2 TIMER23:2 TMRTEMP INT_TEMP TMRFLAGS1 TMRFLAGS2 TMRFLAGS3 endc ;------- 16 BIT COUNT DOWN TIMER ROUTINE ------------------------ START =20 MOVFF TMRFLAGS1, INT_TEMP LFSR 0, TIMER0 =20 TSTFSZ TMRFLAGS1 ; Don't = bother checking each one if there are no CALL UPDATE_TIMERS_16BIT ; = Timers running 0 - 7 MOVFF INT_TEMP, TMRFLAGS1=20 =20 MOVFF TMRFLAGS2, INT_TEMP LFSR 0, TIMER8 =20 TSTFSZ TMRFLAGS2 ; Don't = bother checking each one if there are no CALL UPDATE_TIMERS_16BIT ; = Timers running 8 - 15 MOVFF INT_TEMP, TMRFLAGS2=20 MOVFF TMRFLAGS3, INT_TEMP LFSR 0, TIMER16 =20 TSTFSZ TMRFLAGS3 ; Don't = bother checking each one if there are no CALL UPDATE_TIMERS_16BIT ; = Timers running 16 - 23 MOVFF INT_TEMP, TMRFLAGS3=20 GOTO START ;BRA END_TMRS=20 =20 UPDATE_TIMERS_16BIT MOVLW 9 ; Load a = loop counter MOVWF TMRTEMP =20 UPDATE_TMR_LOOP CLRWDT ; Tickle = the dog DCFSNZ TMRTEMP ; Dec = loop counter and are we at zero yet RETURN RRNCF INT_TEMP,F ; No so = shift some flags BTFSS INT_TEMP,7 ; Is = this timer running BRA NEXTTMR2 ; No CALL TMR_DEC_N_CHK_16 ; Yes so = go and dec timer counter TSTFSZ WREG,0 ; Has = the timer timed out yet ? BCF INT_TEMP,7 ; Yes NEXTTMR1 INCF FSR0L ; Next = timer please BRA UPDATE_TMR_LOOP NEXTTMR2 INCF FSR0L ; No = action needed so move pointers to next timer INCF FSR0L BRA UPDATE_TMR_LOOP TMR_DEC_N_CHK_16 DECF POSTINC0,1,0 ; Lower = the timer pointed to by fsr0l and move pointer to timer + 1 BTFSC STATUS,C ; Did we = roll under ? RETLW 0 ; No so = return TSTFSZ INDF0 ; Is = timer + 1 already 0 ? DCFSNZ POSTDEC0 ; No so = dec timer + 1 and move pointer back to timer=20 TSTFSZ INDF0 ; Now = check if timer is =3D 0 BRA NOTZERO ; No = branch to notzero exit routine=20 DECF FSR0L ; Yes = both timer & timer + 1 =3D 0 INCF POSTINC0,1,0 ; Inc = timer to bring back to 0 and move frs0 pointer to next timer RETLW 1 ; Set = flag to say timer up and return NOTZERO INCF FSR0L ; Move = frs0 pointer to next timer=20 RETLW 0 ; Set = timer not up flag and return = ; Set flag to say timer up and return END_TMRS GOTO $ END ------=_NextPart_000_0000_01C2D8C7.AC2829C0--