Proton BASIC Compiler - A Simple State Machine

  • 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.

    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. (
    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.


    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.
    $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.


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

    Download the manuals and code HERE.

    Alberto Freixanet
    July 2017