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.

state machine very beautiful example

Status
Not open for further replies.

zia

Full Member level 5
Joined
Sep 24, 2010
Messages
284
Helped
27
Reputation
54
Reaction score
26
Trophy points
1,318
Location
Islamabad Pakistan
Activity points
2,746
state machine very beautiful example for microcontrollers


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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//State Machine
//state_machine.c
//Test state machine framework
 
#include<stdio.h>
 
typedef enum {ST_1=0 , ST_2=1, ST_3=3, ST_4=4} State_Type;
 
//## Prototypes ##//
void Init_StateTBL(void);
void st_1(void);
void st_2(void);
void st_3(void);
void st_4(void);
 
 
void (*state_table[])(void) = {st_1, st_2, st_3, st_4};
 
State_Type curr_state;
 
 
void main(void){
    printf("Start /n");
    Init_StateTBL();    
 
    while(1){
 
        state_table[curr_state]();
 
    }
 
return;
}
 
//#### Functions  ####//
 
void Init_StateTBL(void){
 
    curr_state = ST_1;
    printf("Init_StateTBL /n");
 
return;
}
 
void st_1(){
    
    curr_state = ST_2;
        printf("ST_1 /n");
 
return;
}
 
void st_2(){
 
        curr_state = ST_3;
        printf("ST_2 /n");
 
return;
}
 
void st_3(){
 
        curr_state = ST_4;
        printf("ST_3 /n");
 
return;
}
 
void st_4(){
 
        curr_state = ST_1;
        printf("ST_4 /n");
 
return;
}

 
Last edited:

thats amazing. you wrote a case/ switch statement which does nothing at all. And does it badly too !!
Wow


cheers!
 
you wrote a case/ switch statement which does nothing at all. And does it badly too !!
"Does nothing" isn't the point, it's apparently meaned as a template.

But I'm missing a substantiation. Why is it "beautiful"?
And what's bad? Wasting code and data space? Wearing the computer keyboard? Or even worse?
 
They say, "Beauty is in the eye of the beholder."

However, I'm inclined to agree with FvM as I fail to see the beauty in the posted framework.

BigDog
 

"Does nothing" isn't the point, it's apparently meaned as a template.

But I'm missing a substantiation. Why is it "beautiful"?
And what's bad? Wasting code and data space? Wearing the computer keyboard? Or even worse?

Its bad because it wasted all of our time. And its hardly a template. It's a nothing.
Whats the matter with you guys ?
 

Its bad because it wasted all of our time. And its hardly a template. It's a nothing.
Whats the matter with you guys ?

Now I think you are overreacting.
Sharing an opinion for the posted code is one thing but we are not going to contempt zia for making that post!
zia has just shared what he considered as useful, I don't see why you have to spend time with this post and then hold it against the OP, you can just bypass it.
 

hi i posted it just for optimizationof code and making code a professional touch which is highly used in lcd gui with buttons.
 

Now I think you are overreacting.
Sharing an opinion for the posted code is one thing but we are not going to contempt zia for making that post!
zia has just shared what he considered as useful, I don't see why you have to spend time with this post and then hold it against the OP, you can just bypass it.

Yes Sir !!
(hangs head in shame)
I apologise.....

It IS a beautifully written template which can be immediately applied to many state-machine applications with suitable modifications & expansions. The mods would be very easy to do using this template and of course changing the state transitions depending on whatever inputs are relevant, and not necessarily limited to Kbd+LCD menu's

In the form shown, it simply steps through the 4 states sequentially, displaying the state as it goes.

cheers!
 

Yes Sir !!
(hangs head in shame)
I apologise.....

It IS a beautifully written template which can be immediately applied to many state-machine applications with suitable modifications & expansions. The mods would be very easy to do using this template and of course changing the state transitions depending on whatever inputs are relevant, and not necessarily limited to Kbd+LCD menu's

In the form shown, it simply steps through the 4 states sequentially, displaying the state as it goes.

cheers!


I don't find your irony funny, as a matter of fact is pisses me off and I don't appreciate people who make fun of others.
 

Its bad because it wasted all of our time. And its hardly a template. It's a nothing.
Whats the matter with you guys ?

each has their way of expressing it is wrong for us to belittle them

any way i agree with @alexan_e: and @bigdogguru: in their views

- - - Updated - - -

don't forget that each of us will be in the other side of the table once
 

You're very welcome.
Sorry about earlier.
:)

- - - Updated - - -

I don't find your irony funny, as a matter of fact is pisses me off and I don't appreciate people who make fun of others.

But i meant it sincerely ... really !! :sad:

Even saw & tried to explain all the useful things for the benefit of the greater good, and am planning to use the template myself sometime soon.
And EVEN have recomended it to someone on this forum as a useful starting point.

What more do you want ?
cheers!
 

But i meant it sincerely ... really !! :sad:

This is what you wrote yesterday

thats amazing. you wrote a case/ switch statement which does nothing at all. And does it badly too !!
Wow

Its bad because it wasted all of our time. And its hardly a template. It's a nothing.
Whats the matter with you guys ?

And this is what you said today after my post

Yes Sir !!
(hangs head in shame)
I apologise.....

It IS a beautifully written template which can be immediately applied to many state-machine applications with suitable modifications & expansions. The mods would be very easy to do using this template and of course changing the state transitions depending on whatever inputs are relevant, and not necessarily limited to Kbd+LCD menu's

In the form shown, it simply steps through the 4 states sequentially, displaying the state as it goes.

So what you considered as garbage wasting your time yesterday suddenly now is brilliant code, that makes perfect sense.
If that seemed ironic only in my eyes then I guess I am the crazy one imagining things.
 

I think the template example could be improved by showing sample of each state having a decision point that chooses the
next state. As such the example is just a sequential set of states that many just don't get why bother.

I've been programming for a long time, and outside the engineering world (except for those that know how to write parsers)
you would be surprised how many so called professional programmers are completely ignorant of the finite state machine
concept. I recall once I was abused in code review for making code overly complicated and hard to maintain ?@#!! and I saw only
a thing of beauty, where I could easily visualise the deterministic states of what the code is doing. No they wanted/expected
massive if/then/else nested loops...oh crap!

With the example above, for embedded small code devices, what I concern with is all the function use, this can be an overhead.

I think I have a fairly good example of finite state machine in my blog at https://www.edaboard.com/blog/1750/
 

Curiously the least arguments are about pro an cons of the FSM style presented in the original post, just claims of beauty and badness.

zia added on
hi i posted it just for optimizationof code and making code a professional touch which is highly used in lcd gui with buttons.

The essential point is enclosing functionality for each state in spearate functions. As shown it's lacking specific state machine action, but everybody can imagine how it would be added.

I wasn't yet tempted to write FSM code in a similar style. As already mentioned, IMHO it's blowing up code. But as every style decision it's at matter of taste. To me, it looks more like an automatically generated code from a state machine tool. It's straightforward in some regards, but not optimized.
 
Last edited:

very usefull FSM-Finity State Machine creator C source code from excel:
https://www.ti.com/mcu/docs/litabsm...eNumber=slaa402a&docCategoryId=1&familyId=342
not only TI MSP430 micro! I use this excel file to create my 8051 variants!
https://www.ti.com/litv/zip/slaa402a
1. define all states
2. define events
3. define transition functions
click on: GENERATE CODE

You get 3 files: fsm.h fsm.c and fsm_transition.c

You only edit transition functions for your needs ..... and when event occure call event function! Thats all!

p.s.
read PDF and see examples....
https://www.ti.com/general/docs/lit/getliterature.tsp?literatureNumber=slaa402a&fileType=pdf
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top