Push Button and LED
This example shows you how to use the GPIO blocks in the STMicroelectronics STM32F4-Discovery library to control the push-button and the LED's on the STMicroelectronics STM32F4-Discovery board.
Embedded Coder Support Package for STMicroelectronics Discovery Boards enables you to create and run Simulink models on STM32F4-Discovery board. The target includes a library of Simulink blocks for configuring and accessing the STM32F4-Discovery board sensors, actuators and communication interfaces.
In this example you will learn how to use the GPIO blocks in the STM32F4-Discovery library.
We recommend completing the Getting Started with Embedded Coder Support Package for STMicroelectronics Discovery Boards example.
To run this example you will need the following hardware:
- STMicroelectronics STM32F4-Discovery board
- USB type A to Mini-B cable
Task 1 - Configure GPIO blocks
In this task, you will configure the GPIO blocks in the STM32F4-Discovery library for controlling the LEDs and for reading the state of the user push button on the STM32F4-Discovery board. There are four user LED's and a user push button on the STM32F4-Discovery board. The user push button and the LEDs are connected to GPIO pins. The blue user push button is connected to GPIOA pin 0. The four LEDs labelled on the board as LD3, LD4, LD5 and LD6 are connected to GPIOD pin 13, pin 12, pin 14 and pin 15 respectively.
1. Open the Push Button and LED model. This model is configured for the STM32F4-Discovery target. A pulse generator is used to feed three of the LEDs with a periodic pulse. The period of the pulse is set to 1 second and the duty cycle to 0.5 second. The output of the pulse generator is converted to a logical value to match the GPIO Write block's input port specifications. A GPIO Read block is used to sense the state of the user push button. The output is fed to the red LED (LED5) on the board so that it will glow when the push button is pressed.
2. The settings used in each of the GPIO blocks are annotated below the block in the model. For example, the User Push Button block is configured for GPIOA pin 0. You can view the GPIO port and pin number configuration by double clicking on the block. Additional settings are available in the Configuration Parameters > Hardware Implementation > Target Hardware Resources > GPIO A. The GPIO A pin 0 is configured for push pull mode with no pull-up and no pull-down internal resistor enabled. Follow the steps below to visualize these settings:
3. Change the Build action to Build, load and run to download and run the generated hex file, stm32f4discovery_pb_led.hex, on the STM32F4-Discovery board. Follow the steps below to set the Build action:
4. Build the model by clicking on the Build model button.
NOTE: If you select Build only as your Build action like described in step 3, you have to use a flash programming tool, such as the ST-Link Utility, to load and run the generated hex file, stm32f4discovery_pb_led.hex, on the STM32F4-Discovery board.
5. Observe that the orange, green and blue LEDs on the board are ON for 0.5 second and OFF for 0.5 second. Press the blue push button to see the red LED glow.
This example illustrated how to use the GPIO blocks for controlling the LEDs and reading the state of the push button on the STM32F4-Discovery board.