Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[HELP] PIC18F4550 -- C Programming Problems

Status
Not open for further replies.

scdvom

Member level 1
Joined
Feb 8, 2010
Messages
34
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,524
Hi everyone, I got a question here.

void fbStateMachine()
{
switch(fb.state)
{
case 0: if(isForward)
fb.nextState = 1;
case 1: .....
.
.
}
fb.state = fb.nextState;
}

how do I defined fb.state and fb.nextState in my C code????
thx 4 helping.
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
fb.state and fb.nextstate must be part of a structure.

typedef struct
{
int state;
int nextstate;
int someotherstuff;
}FB;

volatile FB fb;

One approach to a state machine is that each state is atomic, ie: changing the code in the state has no side effects on any other part of the program. You can have a seperate file for each state.

The state can also only switch to another state when something happens that needs a state change.

An elegant way to implement this is to have a constant array of function pointers. The state functions return the next state to switch to as the index in the array.

Here is a trivial example of the stratagy.

Code:
/*--- State Machine ---*/

enum STATE{STATE_1, STATE_2, STATE_3, STATE_4}; /* State enumeration */

/*--- State machine function prototypes. ---*/

enum STATE state_1(void);  /* Functions return next state */
enum STATE state_2(void);
enum STATE state_3(void);
enum STATE state_4(void);

/*--- Program entry ---*/

int main(void)
  {
  enum STATE State;
  enum STATE(*const Active_State[])(void) =   /* Constant array of function pointers */ 
  {state_1, state_2, state_3, state_4}; 

  State = STATE_1;
 
  for(;;){     
    State = Active_State[State]();  /* execute state machine */  
    }
 
  return 0;
  }

/*--- State 1 ---*/

enum STATE state_1(void)
  {
   enum STATE State = STATE_1;

  while(State == STATE_1)
    {
    /* Code for state 1

    if(Somethings happened){
        State = STATE_2;
        }

    */
    }

  return STATE_2; /* Return next state */
  }

/*--- State 2 ---*/

enum STATE state_2(void)
  {

  return STATE_3;
  }

/*--- State 3 ---*/

enum STATE state_3(void)
  {

  return STATE_4;
  }

/*--- State 4 ---*/

enum STATE state_4(void)
  {

  return STATE_1;
  }

/*--- End of File ---*/
 

scdvom

Member level 1
Joined
Feb 8, 2010
Messages
34
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,524
Hi, i'm still facing problem, "syntax error". wats going wrong on my code. thx for helping.


volatile fb, lr;

typedef struct
{
int state;
int nextState;
int controlBits;
}fb;

typedef struct
{
int state;
int nextState;
int controlBits;
}lr;

void masterStateMachine()
{
int fbBits, lrBits, masterBits;

fbBits = fb.controlBits; ***** syntax error
lrBits = lr.controlBits; ***** syntax error
masterBits = (fbBits<<2)+(lrBits);
.
.
.
.
}
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
volatile fb, lr; ????

You cant declare these before defining the types! The compiler doesn't know what you are talking about.

You need to study structures a bit more. There is a difference in just defining a structure and defining a type that is a structure.
Also, if you are shifting bits, you should be using 'unsigned int' or 'unsigned char' if it's 8-bits.

Code:
/*--- Normal structure ---*/

struct example    /* Structure tag */
  {
  unsigned int variable_1;
  unsigned int variable_2;
  };

/*--- A structure that is a type ---*/

typedef struct 
  { 
  int state; 
  int nextState; 
  int controlBits; 
  }FB;

volatile FB fb; /* Global instance of type FB */ 

typedef struct 
  { 
  int state; 
  int nextState; 
  int controlBits; 
  }LR;

volatile LR lr; /* Global instance of type LR */

/*--- Program entry ---*/ 

void main(void)
  {
  struct example ex; /* Instance of structure 'example' */
  unsigned int test;

  ex.variable_1 = 10;
  ex.variable_2 = 20;

  test = ex.variable_1 + ex.variable_2;
  } 

  
void masterStateMachine() 
  { 
  int fbBits, lrBits, masterBits; 

  fbBits = fb.controlBits;   /* Using global fb type */  
  lrBits = lr.controlBits;   /* Using global lr type */ 
  masterBits = (fbBits<<2)+(lrBits);  /* Sign is lost? */ 
  }

/*--- End of File ---*/
 

scdvom

Member level 1
Joined
Feb 8, 2010
Messages
34
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,524
Hi Btbass, thx 4 ur guide. jz solve it. ^^ thx.
erm..i got 1 more problem here..once i compile my code. it declare that "old style function declarations not supported". wat d probems on my code here actually. thx 4 helping.

Code:
void setMovement(a, b, c, d)
{        ****
	int a, b, c, d;
	isForward = a;
	isReverse = b;
	isLeft =c;
	isRight =d;
}        ****old style function declarations not supported
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top