CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

State Machine Implementation using CCS

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
AnthonyF
Guest







State Machine Implementation using CCS
PostPosted: Wed Oct 10, 2007 3:34 pm     Reply with quote

I am using CCS with a PIC 16F877. I am implementing a state machine, in this application the state variable is a byte, so 255 possible states.

Right now omitting #defines it would look something like this:

Code:
switch(state){

case 0x01 :
   command_01();
   break;
   
case 0x02 :
   command_02();
   break;

...

case 0xFF :
   command_FF();
   break;
}


I am trying to optimize this logic. I am familiar with x86 assembly and in assembly I would do something like:

mov register, state
jmp register
jmp command_01
jmp command_02
jmp command_03
...
jmp command_FF

This avoids up to 255 comparisons.

....Any suggestions on how to do this using the CCS compiler and a PIC16F877?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 10, 2007 3:41 pm     Reply with quote

Here are some threads about switch-case and if-else efficiency.
http://www.ccsinfo.com/forum/viewtopic.php?t=28643
http://www.ccsinfo.com/forum/viewtopic.php?t=21373
http://www.ccsinfo.com/forum/viewtopic.php?t=20721
http://www.ccsinfo.com/forum/viewtopic.php?t=17868
http://www.ccsinfo.com/forum/viewtopic.php?t=28970
guestplanet
Guest







PostPosted: Wed Oct 10, 2007 9:15 pm     Reply with quote

removing the default statement from switch-case, compiler will then generate a 'table jump' codes but this in turn will cause your microcontroller to run an approximate 30 instructions before executing functions within each case
AnthonyF
Guest







PostPosted: Fri Oct 12, 2007 3:17 pm     Reply with quote

Thanks,

Looks like if you are comparing with constants as I am the lookup table is hard coded exactly as I want requiring minimal processing during each run.

004C: GOTO 01D
004D: GOTO 024
004E: GOTO 02B
004F: GOTO 032
0050: GOTO 039
0051: GOTO 047
0052: GOTO 040
guestplanet
Guest







PostPosted: Mon Oct 15, 2007 6:48 pm     Reply with quote

I think we all would like to have minimal processing before executing each task.
how do you propose to code 'a table read using computed GOTO method' in C?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group