DE | EN | CN

ESE Block 4 GPIO

This lession is intended to illustrate the implementation of simple inputs. First we want to briefly discuss the specifics of input via mechanical buttons. The picture shows that a button represents an antenna with its connection to the controller as long as it is not pressed.

Open CMOS inputs are sensitive receivers for all kinds of electro smog. The input delivers virtually arbitrary signals. So-called pull-down or pull-up resistors provide a remedy here. These pull the line to a defined potential and thus suppress electromagnetic interference. In the past, these pull-up resistors were explicitly built into the circuit (activated with the soldering iron, so to speak). Modern microcontrollers have internal pull-up and pull-down resistors. If necessary, these simply have to be activated via software.

A microcontroller application is to be developed in which an LED is switched on by pressing a button.

The task is: When the user presses a key, an LED should be switched on. Otherwise the LED is off. Develop a solution for this in which when the function key on pin A0 is pressed, the indicator LED on pin B0 lights up.

srs_gpio.pdf

If you have still opened a class diagram, select the menu item “up” in the context menu (right mouse button) of the diagram. If the project is no longer open, open the SiSy UML project again. Perform the following preparatory work:

  • create a new class diagram
  • Target language ARM C ++
  • Target platform STM32F042 mySTM32 Board light HAL
  • Load diagram template application framework for PEC applications (XMC, STM32, AVR)
  • Assign driver package for STM32F0
  • optionally assign template stm32F042_48Mhz

The task is that when the function key is pressed, the indicator LED should light up. If we again follow the object-oriented concept, the function key is another component of our system. As already discussed, we map system modules as classes in the UML. To clarify, here are the system modules relevant for this task:

The button and the LED must be connected to the controller. We have already got to know the aggregation to model it. Here again the individual abstraction steps: Real world → Thought model → UML model

For the output we already know the library element PecPinOutput .
For the simple evaluation of the button we can use PecPinInput from the library. If we look at this library component, we will see that it is suitable for the task.

In addition, we have to note that we have to suppress electromagnetic interference from this pin with a PullUp . For this we use the library element pinPullUp . On the right you will find the rough draft of the expected system behavior as an activity model.

REMEMBER: Abstraction = real world → Thought model (natural language) → UML model = software construction drawing

The implementation should include the elements described in the rough draft above. In addition, the Pin B0 must be linked to the implementation of the IndicatorLED and the Pin A0 to the implementation of the FunctionKey . You can find the corresponding PEC block via the Explorer. You can also use the small question mark at PecPinOutput and PecPinInput . Use the following illustration as a guide when realizing your design:

Use the above illustration as a guide and model your class diagram accordingly.

We build the behavior logic back into the onWork () operation of the Controller class. Let's get used to first writing the desired solution as a comment in the source code.

Controller::onWork():void
// continuous event from the Mainloop
// IF FunctionKey is pressed THAN
//      IndicatorLED on
// ELSE
//      IndicatorLED off

Then we can enter the associated C++ code.

Controller::onWork():void
// continuous event from the Mainloop
// IF FunctionKey is pressed THAN
if (functionKey.getState()==0) 
{
	indicatorLED.on(); // IndicatorLED on
}
else   // ELSE
{
	indicatorLED.off(); // IndicatorLED off
}

We can now extend this solution so that the LED flashes when the button is pressed. Compare this variant with the first solution

Controller::onWork():void
// continuous event from the Mainloop
// IF FunctionKey is pressed THAN
if (functionKey.getState()==0) 
{
	indicatorLED.toggle(); // IndicatorLED blinking
	waitMs(200);
}
else   // ELSE
{
	indicatorLED.off(); // IndicatorLED off
}

Build the program. Correct typographical errors if necessary. Transfer the executable program to the program memory of the controller.

  1. Build (compile and link)
  2. uploade
  3. connect pin B0 to the red LED and the button to pin A0

Video summary

Learned and established work steps:

  1. Create and open class diagram
  2. Select and load diagram template for PEC application and insert driver package for STM32F0
  3. Look for PEC blocks from the library in Explorer
  4. Drag the desired library blocks and into the diagram
  5. Connect classes (aggregation, realization)
  6. create the necessary source code in the operations
  7. Create and burn an STM32 application in the class diagram

And because it was so nice here the whole thing again as a video.

or the fast version without voice comments

additional exercise

To practice, expand the application by another button on pin A1. Orientate yourself to the work steps above. The extension of the solution should meet the following requirements:

  • When the function key is pressed, the display LED should light up
  • when the additional key is pressed, the display LED should flash
  • if both buttons are pressed, the additional button has priority
  • if no key is pressed, the display LED should be off

Continue with: