Home ] Up ] #1 ] #2 ] [ #3 ] #4 ] #5 ] #7 ] #8 ]
#3

Up ]



Tasks and State programming

 

 

Last updated: 07-09-11

 
 
 
 
bullet

State programming with:  switch( ) case 

bullet

Count_ver1_case

This type of programming also refered as "run to complete" programming, even the use of while( true) {    } loops

   
 

The switch / case statement will be the most common way to implement a State Machine.
Please note that Counter1 and Counter2 will execute in “parallel”

   
 
 
   
 
bulletIn order to define statenames like, Idle and Press1 will the enum statement be usefull.
The variable State of the type States can the be used in a switch( State) to executed the code connected to a perticular state.
   
 
   
 
bulletWhen the button pressed (B==1) will a state change take place. Namely a state changes from Idle to Press1 (Detection of B 0 => 1 shift)
 
   
bullet

State programming with:  goto

bullet

Count_ver1_goto

   
 
 

 

 
bullet

State programming with:  task ..  Exit_To

bullet

 Count_ver1_task

   
 
   
 
   
bullet

1 System tick = 1 millisecond
bullet

The Wait( time) call will leave the task "sleeping" for the given time.
This will not be a good solution if the task also should be able to detect changes of input signals during the wait time.

bullet

The CurrentTick( ) function returns the actual millisecond counter of the system.
This enable a task the calculate the actual time passed.

   
   
 

Take a closer look at the while loop - will there be a problem here?
Consider using a
do
{
         dT = CurrentTick() - Time;
 } while ( dT<1000)
statement - will the this solve the problem.

   
bullet

Count_ver2_case

This counter version program will wait for a button press (B==1) and increment Count1.
If the button keep being pressed will the counter be incremented once each 1000 millisecond.

   
 
   
bullet

Count_ver2_goto

Even the goto statement got "bad standing" among professional programmers, could it be useful for "state programming".

   
 
  The same code with comments
 
   
bullet

Count_ver2_task

The task Counter1 will be called by the Precedes( ..) statement and hence start the state-machine.
The
ExitTo( ..) function works like the goto statement and force a shift between tasks.