.st0{fill:#FFFFFF;}

Programming STM32 using Stateflow 

 September 10, 2021

By  Addepalli Dolendra Vikas

Introduction

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.


Hardware Required

  • STM32F103C8T6 (Blue pill) microcontroller
  • STLink v2
  • Breadboard
  • 3* 1KΩ resistors
  • 1* 10KΩ resistor
  • 3* LEDs (different color)
  • Push Button

Stateflow

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.

State Machine

State Machine

Traffic Light State Machine

The most popular and commonly found example is traffic lights.

Traffic Light

Traffic Light

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.

Different States

Different States

In State machine terminology, the individual lights are called outputs, and those 3 scenarios are called state.

State Machine Outputs

State Machine Outputs

State Machine States

State Machine States

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.

Stateflow of Traffic Light

Stateflow of Traffic Light

Program the State Machine to the STM32

We will be doing traffic light State machine and program the State machine to the STM32. Following are the required blocks of programming the STM32
Simulink Blocks

Simulink Blocks

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.

Traffic Light State Machine

Traffic Light State Machine

Select the Symbols pane option and set the variables data type as Input or Output, respectively.

State Machine Variables

State Machine Variables

State Machine Variables Datatype

State Machine Variables Datatype

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.

Blocks Connections

Blocks Connections

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.

Red State

Red State

Yellow State

Yellow State

Green State

Green State

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.

Did you find some helpful content from our video or article and now looking for its code, model, or application? You can purchase the specific Title, if available, and instantly get the download link.

Thank you for reading this blog. Do share this blog if you found it helpful. If you have any queries, post them in the comments or contact us by emailing your questions to [email protected]. Follow us on LinkedIn Facebook, and Subscribe to our YouTube Channel. If you find any bug or error on this or any other page on our website, please inform us & we will correct it.

If you are looking for free help, you can post your comment below & wait for any community member to respond, which is not guaranteed. You can book Expert Help, a paid service, and get assistance in your requirement. If your timeline allows, we recommend you book the Research Assistance plan. If you want to get trained in MATLAB or Simulink, you may join one of our training modules. 

If you are ready for the paid service, share your requirement with necessary attachments & inform us about any Service preference along with the timeline. Once evaluated, we will revert to you with more details and the next suggested step.

Education is our future. MATLAB is our feature. Happy MATLABing!

About the author 

Addepalli Dolendra Vikas

I’m an electronics hobbyist and tech enthusiast. I keep roaming around the stuff that are related to embedded systems, IoT and robotics. I love prototyping stuff and make it work.

  • Duc Tuan Nguyen says:

    Can I use this method for big program? Compared to traditional programming style, what limitations does it have?

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Connect with MATLAB Helper ®

    Follow: YouTube Channel, LinkedIn Company, Facebook Page, Instagram Page

    Join Community of MATLAB Enthusiasts: Facebook Group, Telegram, LinkedIn Group

    Use Website Chat or WhatsApp at +91-8104622179

    >