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

Simple State Machine Concept

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



Joined: 21 Nov 2006
Posts: 16

View user's profile Send private message

Simple State Machine Concept
PostPosted: Wed Feb 07, 2007 10:23 am     Reply with quote

Here is some simple state machine concept, maybe someone finds it
as usefull.Any comment?

Code:
////////////////////////////////////////////////////////////////////////////
///////                                                              ///////
///////                   State Machine Concept                      ///////
///////                                                              ///////
////////////////////////////////////////////////////////////////////////////
///////                                                              ///////
///////   State Machine Name = STM                                   ///////
///////   State Machine States = STMST1, STMST2, ...STMST10          ///////                                 
///////   State Machine Events = STMEV1, STMEV2,....STMEV10          ///////
////////////////////////////////////////////////////////////////////////////
///////                        Transitions                           ///////
////////////////////////////////////////////////////////////////////////////
///////   STMST1==>STMST5  for STMEV1                                ///////
///////   STMST1==>STMST3  for STMEV4                                ///////                                 
///////--------------------------------------------------------------///////
///////   STMST2==>STMST1  for STMEV3                                ///////
///////   STMST2==>STMST10 for STMEV7                                ///////
///////--------------------------------------------------------------///////
///////   STMST3==>STMST4  for STMEV8                                ///////
///////   STMST3==>STMST7  for STMEV2                                ///////                                 
///////--------------------------------------------------------------///////
///////   etc........................                                ///////
///////   etc........................                                ///////                                 
////////////////////////////////////////////////////////////////////////////
///////   When some extern event happens, one should call            ///////
///////   STMHandler (some event from defined set of events).        ///////
///////   Will State machine change or not current state             ///////
///////   depends whether transition is defined or not for current   ///////
///////   state and event passed as parameter to STMHandler().       ///////
////////////////////////////////////////////////////////////////////////////


//--------------------- Define states on State Machine STM -----------------

enum state{STMST1,STMST2,STMST3,STMST4,STMST5,STMST6,STMST7,STMST8,STMST9,STMST10}currentState;

//--------------------- Define events on State Machine STM -----------------

enum event{STMEV1,STMEV2,STMEV3,STMEV4,STMEV5,STMEV6,STMEV7,STMEV8,STMEV9,STMEV10}currentEvent;

//--------------------------- Define states handler ------------------------
void STMST1Handler(event eventHappened)
{
   switch(eventHappened)
   {
      case STMEV1:   //Do some action
                     currentState=STMST5;
                     break;
      case STMEV4:   //Do some action
                     currentState=STMST3;
                     break;
     // Other cases

      default:       break;
   }
}

void STMST2Handler(event eventHappened)
{
   switch(eventHappened)
   {
      case STMEV3:   //Do some action
                     currentState=STMST1;
                     break;
      case STMEV7:   //Do some action
                     currentState=STMST10;
                     break;
     // Other cases

      default:       break;
   }
}

void STMST3Handler(event eventHappened)
{
   switch(eventHappened)
   {
      case STMEV8:   //Do some action
                     currentState=STMST4;
                     break;
      case STMEV2:   //Do some action
                     currentState=STMST7;
                     break;
     // Other cases

      default:       break;
   }
}

void STMST4Handler(event eventHappened)
{
;  //Same as for other states transitions
}

void STMST5Handler(event eventHappened)
{
;  //Same as for other states transitions
}

void STMST6Handler(event eventHappened)
{
;  //Same as for other states transitions
}

void STMST7Handler(event eventHappened)
{
;  //Same as for other states transitions
}

void STMST8Handler(event eventHappened)
{
;  //Same as for other states transitions
}

void STMST9Handler(event eventHappened)
{
;  //Same as for other states transitions
}

void STMST10Handler(event eventHappened)
{
;  //Same as for other states transitions
}

//------------------------ Define state machine handler ---------------------

void STMHandler (event eventHappened)
{
    switch(currentState)
    {
       case STMST1:  STMST1Handler(eventHappened);
                     break;
       case STMST2:  STMST2Handler(eventHappened);
                     break;
       case STMST3:  STMST3Handler(eventHappened);
                     break;
       case STMST4:  STMST4Handler(eventHappened);
                     break;
       case STMST5:  STMST5Handler(eventHappened);
                     break;
       case STMST6:  STMST6Handler(eventHappened);
                     break;
       case STMST7:  STMST7Handler(eventHappened);
                     break;
       case STMST8:  STMST8Handler(eventHappened);
                     break;
       case STMST9:  STMST9Handler(eventHappened);
                     break;
       case STMST10: STMST10Handler(eventHappened);
                     break;
       default:
                     break;
    }
}
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Wed Feb 07, 2007 5:29 pm     Reply with quote

Nested switch state machine. I think, it's the only type of state machine that can be practically implemented on a PIC with stable versions of the CCS compiler. As a result, this concept is very useful.

You might find this post interesting too http://www.ccsinfo.com/forum/viewtopic.php?t=28656

P.S. Here's a link to some standard state machine implementation concepts http://www.quantum-leaps.com/writings/PSiCC_Excerpts.pdf (chapter 3). It's outside the context of PICs, however all of these consepts have been implemented on ARM uCs.
zilot



Joined: 21 Nov 2006
Posts: 16

View user's profile Send private message

PostPosted: Thu Feb 08, 2007 1:50 am     Reply with quote

I became frustrated when realised I cant do almost any modification of generated code from some embedded UML based software, to work with CCS compiler and PIC. There is visual state (IAR systems), but hard (maybe impossible) to be modified for CCS.

So seems this is for now only solution for some UML based work, but with no automatic code generation, just fingers and keyboard. Sadness but we must wait for some better times, or change controller and tools.
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Thu Feb 08, 2007 2:24 am     Reply with quote

Things should get better, after CCS debugs v4, and it will actually support function pointers.

Automatic code generation (ACG) is not a bad idea. For example, MS Visio Technical has UML stencils with all necessary fields. Visio is relatively cheap, compared to IAR suites. Visio supports Visual Basic for Applications (VBA). May be, ACG can be coded in VBA for Visio.
zilot



Joined: 21 Nov 2006
Posts: 16

View user's profile Send private message

PostPosted: Thu Feb 08, 2007 5:15 am     Reply with quote

My first idea was to realise State Machine as structure, actually to define State Machine type, and to define States as type too. But was disapointed when realised that CCS doesnt support pointers to function. I wanted to encapsulate handler function into State, etc.... to be more generic, and to make easy for many State Machines definition in one project.

Dont like VB, just C, so this will be only way for now to deal with states.
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Thu Feb 08, 2007 9:17 am     Reply with quote

It does support pointer to function. At least it says it does.
http://www.ccsinfo.com/faq.php?page=pointer_to_function
jma_1



Joined: 08 Feb 2005
Posts: 147
Location: Wisconsin

View user's profile Send private message

PostPosted: Thu Feb 08, 2007 1:04 pm     Reply with quote

Howdy,

If you like state machines, check out the compiler support for the Rabbit line of microprocessors. Search for 'costate' or 'cofunction'. It's interesting to view competitive products with built in multi-tasking support and features. I've never tried the CCS rtos, but the uC rtos available from Rabbit has been around for quite some time.

Cheers,
JMA
jma_1



Joined: 08 Feb 2005
Posts: 147
Location: Wisconsin

View user's profile Send private message

PostPosted: Thu Feb 08, 2007 1:13 pm     Reply with quote

Greetings,

If you like UML (including 2.0) there are half a dozen free software tools which generate UML. The Eclipse framework alone has at least a dozen plug-ins for UML. One or more of these plug-ins supports code generation (c/c++ and/or java). Some of the commericial plug-ins, like Rational Rose will even do reverse engineering.

I have not researched anything recently, put ArgoUml was a freely available, stand-alone, java application, which allowed diagrams and code generation.

Cheers,
JMA
zilot



Joined: 21 Nov 2006
Posts: 16

View user's profile Send private message

PostPosted: Fri Feb 09, 2007 2:12 am     Reply with quote

Till today I've tried Matlab RTW code generator for State Machine, and some models from simulink, VisualState IAR product, and Rhapsody. None of producted c codes cant be easily modified for PIC CCS, when calculate time eventualy I'll spend in code modification, and time I'll spend for manualy code writting, I'm on the same.

Rhapsody is very powerfull tool, you have complete image of whats happening in system, Object diagrams, Statecharts, Colaboration diagrams, good visual simulator of events and states in one word complete embedded UML environment. But rather to use with powerfull microcontroler (processor) and compiler than with PIC and CCS.
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