---- START NEW MESSAGE --- Received: from cherry.ease.lsoft.com [209.119.0.109] by dpmail10.doteasy.com with ESMTP (SMTPD32-8.05) id A6C9142A0224; Fri, 30 Jan 2004 01:41:29 -0800 Received: from PEAR.EASE.LSOFT.COM (209.119.0.19) by cherry.ease.lsoft.com (LSMTP for Digital Unix v1.1b) with SMTP id <16.00CC4681@cherry.ease.lsoft.com>; Fri, 30 Jan 2004 4:41:19 -0500 Received: from MITVMA.MIT.EDU by MITVMA.MIT.EDU (LISTSERV-TCP/IP release 1.8e) with spool id 3782 for PICLIST@MITVMA.MIT.EDU; Fri, 30 Jan 2004 04:41:13 -0500 Received: from MITVMA (NJE origin SMTP@MITVMA) by MITVMA.MIT.EDU (LMail V1.2d/1.8d) with BSMTP id 1132; Fri, 30 Jan 2004 04:40:46 -0500 Received: from mail.california.com [209.3.224.15] by mitvma.mit.edu (IBM VM SMTP Level 430) via TCP with SMTP ; Fri, 30 Jan 2004 04:40:45 EST X-Warning: mitvma.mit.edu: Host mail.california.com claimed to be california.com Received: from [66.81.77.10] (HELO DELL) by california.com (CommuniGate Pro SMTP 4.1.5) with SMTP id 62327916 for PICLIST@MITVMA.MIT.EDU; Fri, 30 Jan 2004 01:40:47 -0800 References: X-Mailer: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: Date: Fri, 30 Jan 2004 01:40:47 -0800 Reply-To: pic microcontroller discussion list Sender: pic microcontroller discussion list From: Bob Barr Organization: Consultant Subject: Re: [PIC]: Hitech PICCLITE state machine optimization To: PICLIST@MITVMA.MIT.EDU In-Reply-To: Precedence: list X-RCPT-TO: Status: U X-UIDL: 371856461 On Thu, 29 Jan 2004 21:18:54 -0500, Herbert Graf wrote: >Hello all, been trying to implement a state machine using PICCLITE and am >encountering BIG code. I was wondering if anyone might have some ideas as to >how I could implement a state machine more efficiently. > >For example this is an excerpt of my state machine: > > if ((ISR_state == 7) > || (ISR_state == 8) > || (ISR_state == 9) > || (ISR_state == 10) > || (ISR_state == 11) > || (ISR_state == 12) > || (ISR_state == 13)) > { > if (ISR_charBuf == 0x0d) > ISR_state = 0; > else if (ISR_charBuf == ',') > ISR_state++; > } > >Any ideas as to how I might do the same sort of thing without using so much >code? > Are state machines implemented in C that much differently than the way I was taught to do them in assembly language? Isn't there separate code that gets executed based on the state variable? Once a state's code is being executed, there's no need to check the state variable again. If this is the case, you could call a common exit function from each state to detect the carriage-return and comma characters. (At a minimum, you could test for the state variable being greater than 6 and less than 14 rather than testing for the individual values.). >I had a look at the assembly on another section of the state machine where >it's even worse: > >if ((ISR_state == 1 && ISR_charBuf == 'P') > > 380 0036 0822 movf _ISR_state,w > 381 0037 3A02 xorlw 2 > 382 0038 1D03 btfss 3,2 > 383 0039 283E goto u191 > 384 003A 0821 movf _ISR_charBuf,w > 385 003B 3A50 xorlw 80 > 386 003C 1903 btfsc 3,2 > 387 003D 286E goto u440 > I can see how the 'xorlw 80' is checking for the character being 'P' but how in the world can an 'xorlw 2' possibly check for the state variable being 1? That line makes no sense to me. Am I missing something obvious? Regards, Bob -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body .