• PicŪ Basic


  • A Simple State Machine

    There is a lot of theory about State Machines (SM) that is sometimes very complicated to put into practice. In this little tutorial I have tried to convey my way of applying a complex theory to simple, practical use for PDS user projects.

    History:
    A few years ago I applied a special SM system to some projects. I found this system so effective that I wanted to use it for large programs. So I set up another State Machine structure described in my Spanish tutorials.

    Reviewing my SM projects, I realized that it was possible to extend the automatic functions of the structure to make it easier for the user to create his code. Here is the result. It would be very useful to refresh your pre-processor knowledge, please read the PDS manual.

    When the code runs with a PIC of the K-series (FOSC = 64Mhz), its efficiency is seen. I have tried with the K20 and FOSC series = 80Mhz and it works very well. As well, I use my 80Mhz bootloader for the K20 series.

    I have accompanied the manuals with sample code so you can try this system. The structure of the machine is designed so that the user can take advantage of the dead times in a program such as during delays, to work in the background without using interrupts. Code is written for the Amicus18 Board; but it is easily applied to any PIC project.

    12/04/2017 - Update State Machine Tutorial Part 4.
    Where I have done a complete project of a Clock/Calendar with the programming of the Contrast, Date/Time and 2 alarms. It is an easily expandable project thanks to its state machine structure.
    Please use the last State Machine library version to develop your project. (STMachine01.inc)
    More SM commands have been added.

    05/06/2017 - Update State Machine Tutorial Part 5. A Simple Multitasking System
    Using the same project I have written a code to make a small multitasking system. Also I have kept the code of State Machine #1. It's a very simple system, you will see that you can reuse a lot of code for other projects. You will be able to see some tricks to reduce the lines of codes so that the system works faster. I have been able to test this project with an Amicus18 Board at 80 Mhz, it is wonderful.

    23/07/2017 - Update for a Local Variables Library.
    It is very important to reuse the variables when they are scarce as the system variables.
    I have been using the local variables for the system variables for years (PIC18). Just go see my libraries or projects
    in the wiki. The system I used was very manual and sometimes not totally efficient. So I was thinking of writing a new system of local variables more automatically.

    Macros work this way...for Libraries or Subroutines.
    LocalByte0(MyVar0)
    LocalByte1(MyVar1)
    LocalByte2(MyVar2)

    LocalWord0(MyVar3)
    LocalWord1(MyVar4)
    LocalWord2(MyVar5)

    Rules to use the Local Variables:
    1- The local Variables could be a Bit, Byte, Word, Dword, SByte, SWord, SDword or Float.
    2- The local variable name must never be repeated.
    3- The local variables must be written before Macros or subroutines.
    4- The local variable must be used once.
    5- If the variable is used in 2 or more subroutines it is a global variable.
    6- The local variable uses the system variables because the acces is more faster for Macros or Subroutines.
    7- In the main code use the SM local standard variables (not System).
    8- The macros can declare 8 variables only. If more variable are needed then you
    can modify the macro.

    These macros are to use the system variables for Macros or subroutines allowing a faster access to them.
    System variables are a scarce resource, within the main program we will use another form if necessary.

    Construction of the macro.
    Code:
    $define LocalByte0(pVar)         '
        $ifndef _LVS_Byte0           '
        $define _LVS_Byte0           '
        Dim LVS_Byte0 As Byte System '
        $endif                       '
        Dim pVar As LVS_Byte0
    Macros work this way...for Main Code.
    SMLocalByte0(MyVar0)
    SMLocalByte1(MyVar1)
    SMLocalByte2(MyVar2)

    SMLocalWord0(MyVar3)
    SMLocalWord1(MyVar4)
    SMLocalWord2(MyVar5)

    Your success will only depend on your imagination! Enjoy working with a State Machine.

    Download the manuals and code HERE.

    Alberto Freixanet
    July 2017
  • Recent Activity

    towlerg-21522

    User Id Location ?

    Thread Starter: H-bert

    hello all, does someone know how to set the User Id Location bytes at address 0x2000-0x2003 in the PIC's ? Greetings & thanks for reading...

    towlerg Today, 18:03 Go to last post
    charliecoultas-16125

    Trouble with arrays, lookup

    Thread Starter: charliecoultas

    I'm going nuts. Will somebody please tell me what I'm doing wrong here: Dim ASCII As Byte = "0", "1", "2", "3", "4", "5" Dim Telec As Byte = ...

    charliecoultas Today, 19:39 Go to last post
  • Tag Cloud