Continue to Site

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.

[SOLVED] switch statement in implementing an fsm in c

Status
Not open for further replies.

winnie mong

Junior Member level 3
Junior Member level 3
Joined
Sep 8, 2013
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
366
hi guys,
i have a question concerning the use of the switch statement.i am wondering if case zero corresponds with state 0 and case1 corresponds with state1.with my code below, i want case 0 to execute,which will then state=1 and for state 1 i want i want case 2 to execute.so i am not sure if thats how thw switch statement works.some body help!!


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int i,j,state;
void geyser_state_check();
void do_set_time ( const char *const menu2[]);
 
do{
    state=0;
    {
        switch (state){
            case 0:
                geyser_state_check();
                if (mode()){
                    state=1;
                }
                break;
            case 1:
                j=choice(menu1);
                if (menu1[j]=SET TIME){
                    do_set_time();
                break;
                }
                else if(menu1[j]=VIEW TIME){
                    do_view_time(); 
                    break;
                }
                else {
                    state=0;
                    break;
                }
                break;
        }
    }
}while(1);

 
Last edited by a moderator:

it might depend on the compiler, but if my memory serves it doesn't work that way - it depends on the 'initial' value given to the switch.

is this what you want within the DO loop?

Code:
geyser_state_check();
if (mode())
  { j=choice(menu1);
    if (menu1[j]=SET TIME){do_set_time();}
    else if(menu1[j]=VIEW TIME){do_view_time(); 
  }
 
Last edited:

i want case 0 to execute,which will then state=1 and for state 1 i want i want case 2 to execute

I really didn't understand what you are asking.

According to your code case 0 is executed when start is 0, case 1 when state is 1,


If you want case 0 to execute when state is 1 and case 2 to execute when state is 2.

then change code to


Code C - [expand]
1
switch(state + 1) or



change case 0 to case 1 and case 1 to case 2.


If you want case 0 to execute when state is 1 and then case 1 to execute when state is 1 then


Code C - [expand]
1
2
3
4
5
6
7
switch(case)
 
    case 0:
 
    case 1:
 
        break;




Your expalnation is not clear. If you need a better answer then explain what you want clearly.
 

The switch statement evaluates the value of variable state and jumps to the respective case. As state is set to 0 in the line before, only case 0 will be ever executed.
 

oh there was a mistake, yes what i want is for it to do case 0 when in state 0, and case 1 after updating state=1.i want the whole code to be executing infinitely.
 
Last edited:

Try this


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//value of state change elsewhere in while(1) loop or ISR.
 
while(1) {
        
    
        switch (state) {
                    
        case 0:
                    //code to execute when state is 0
                    break;
                case 1:
                    //code to execute when state is 1
                    break;                
                    
        };      
 
}

 
i tried to do like you advised, it makes sense now.thanks

HTML:
while(1) {
		switch (state)
		{
			case 0:
				geyser_state_check();
				if (mode()){
					state=1;
				}
				break;
			case 1:
				j=choice(menu1);
				if (menu1[j]=SET TIME){
					do_set_time();
				break;
				}
				else if(menu1[j]=VIEW TIME){
					do_view_time();	
					break;
				}
				else {
					state=0;
					break;
				}
				break;
		};
	
}

- - - Updated - - -

is it ok that i have case 0, taking me to case 1 through the statement "state=1"?because for case 1 to execute, mode button has to have been pressed.
 

You don't need the break; statement in else { } block.


Code C - [expand]
1
2
3
4
else {
    state=0;    
}
break;

 
is it ok that i have case 0, taking me to case 1 through the statement "state=1"?because for case 1 to execute, mode button has to have been pressed.

- - - Updated - - -

oh, i removed the break there and i have an error saying that same else is inappropriate.
what could be the problem?
 

If state has to be 0 before pressing mode button then it is correct. switch statements work like this... if case is 0 then code related to case 0 is executed and then switch exits even if mode was pressed and state was changed to 1. In the next while(1) loop case 1 code executes as state was set to 1 previously.
 
wow, this is so help full.thanx a lot.
atleast i have move a step forward with my project.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top