In this blog, we will be going through what a State Machine is and how to design a State Machine in Simulink using Stateflow. We will be designing a traffic light State Machine and program this State Machine to the STM32F103C8T6 using Embedded Coder app in Simulink.Note: If you are new to programming STM32 using Simulink and arm Keil-MDK IDE, check out the previous blog Programming STM32 using Simulink, where I have covered generating configuration files in STM32CubeMX, code generation using embedded coder, and building the project using arm Keil-MDK IDE.
- STM32F103C8T6 (Blue pill) microcontroller
- STLink v2
- 3* 1KΩ resistors
- 1* 10KΩ resistor
- 3* LEDs (different color)
- Push Button
Stateflow is a graphical modeling environment that allows you to design and simulate logic based on events, time-based conditions, or external signals. In Stateflow, logical systems are modeled as state machines which are systems with specific operating modes.
Traffic Light State Machine
The most popular and commonly found example is traffic lights.
At any point in time, a traffic sign has a defined state. Typically, it is either, Has the red light on, and the other 2 lights off, has yellow light on, and the other 2 lights off, has the green light on, and the other 2 lights off.
In State machine terminology, the individual lights are called outputs, and those 3 scenarios are called state.
The traffic sign will change state when it receives an input. Typically, an external signal or a timer condition decides how much time the traffic light should be red, yellow, and green. The external signal or the timer is, in this case, is the input of the system.
Program the State Machine to the STM32We will be doing traffic light State machine and program the State machine to the STM32. Following are the required blocks of programming the STM32
Double click on the white space and search for “chart”. A blank chart will be created, enter into the chart, drag and drop the empty states, and add the input and output variables.
Select the Symbols pane option and set the variables data type as Input or Output, respectively.
Double click on the variable, you will be getting a list of data types in the dropdown. Choose the correct data type and save the changes. After changes, input and output ports will be created to the chart. Give connections as shown below.
After giving connections, build the project using the embedded coder app in Simulink and load the project to the STM32 using arm Keil-MDK IDE. After loading the project to the STM32, toggle back the BOOT0 pin back to its position and press the reset button. The following images show the output; by pressing the push button, the state of the three LEDs changes.
Conclusion“Simulink”, “Stateflow” and “Embedded Coder” made prototyping complex State Machine easier. We can test the State Machine in Simulink for the errors and performance before deploying the binaries of the State Machine into the hardware.
Get instant access to the code, model, or application of the video or article you found helpful! Simply purchase the specific title, if available, and receive the download link right away! #MATLABHelper #CodeMadeEasy
Ready to take your MATLAB skills to the next level? Look no further! At MATLAB Helper, we've got you covered. From free community support to expert help and training, we've got all the resources you need to become a pro in no time. If you have any questions or queries, don't hesitate to reach out to us. Simply post a comment below or send us an email at [email protected].
And don't forget to connect with us on LinkedIn, Facebook, and Subscribe to our YouTube Channel! We're always sharing helpful tips and updates, so you can stay up-to-date on everything related to MATLAB. Plus, if you spot any bugs or errors on our website, just let us know and we'll make sure to fix it ASAP.
Ready to get started? Book your expert help with Research Assistance plan today and get personalized assistance tailored to your needs. Or, if you're looking for more comprehensive training, join one of our training modules and get hands-on experience with the latest techniques and technologies. The choice is yours – start learning and growing with MATLAB Helper today!
Education is our future. MATLAB is our feature. Happy MATLABing!