DE | EN | CN

ESE Block 12 LCD

Use a text LCD with the mySTM32 light. For this exercise you need a Text-LCD Add-On 3V version. We often find small displays on embedded systems. A distinction is made, for example, between text and graphic displays. This exercise shows the use of a text display with 16 characters in two lines. This type of display can almost be described as quasi industrie standard due to its frequent use. In any case, text outputs as a user interface are much more powerful and user-friendly than blink codes of LEDs.

The task

A microcontroller solution is to be developed in which an analog value is visualized on a text display.

Requirements for the solution:

  • Text display with HD44780 display controller or compatible
  • Display in 4 bit mode, save pins
  • Display the analog value as a number in the first line
  • Display the analog value as a bar in the second line

SRS LCD

Preparation

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.

This time the preparatory work differs slightly from the previous procedure. So that we have little writing effort for the necessary source code, this time we use a special board package from the SiSy online LibStore instead of loading the general driver package for the STM32F0. Since the connection of the display requires several pins and these can be arranged very differently in the target systems, the library component is generally valid. The developer usually has to implement the assignment of the specific pins for the display himself by overwriting the corresponding pin operations. If a board and its pin configuration is known, the pin adaptation can be loaded as a prepared component. That is exactly what we will do for the mySTM32 Board light.

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)
  • Load the board package for the STM32 Board light from the SiSy LibStore
  • optionally assign template stm32F042_48Mhz

ATTENTION: For this exercise please download the driver package “Board Package mySTM32 Board light” from the SiSy LibStore

Solution idea

If we take a closer look at the task, we notice that some of the requirements in previous exercises have already been processed.

  • Load packages from the LibStore
  • read in an analog value
  • Format numerical values as texts

We can build on that. The task reminds of the output of the values via UART. This time we don't send the output to the PC but to the display. Let's take a look at the PecTextLcd16x2 library block.

The texts are output using the write (“…”) operation. The setPos (x, Y) operation is available to us for output at certain positions. The display can be cleared with clear () . That should be enough for the task.

If we apply the principles of object orientation to the task, the following system modules result:


These system modules can be put together as a possible rough draft to form the following application architecture:

This architectural draft can be read as “natural” text as follows:

  • The controller has a sensor and a display
  • The sensor is a PecAdcChannel with adcResolution8bit
  • The display is a PecTextLcd16x2 with lcd_useBacklight

The specific pins, channels and other hardware configurations must be assigned during implementation. For this it is necessary to deal with the Text LCD Add-On in more detail. A look at the technical description is helpful.

The circuit diagram (see page 5, technical description myAVR LCD Add-On) shows that the first 8 pins on the extension connection are occupied by the add-on. These are pins A0 to A7. These are therefore no longer available for other tasks. The sensor must be connected to a different pin. On the Reference card we can determine the next possible analog input.

REMEMBER: Special driver packages for known boards = use LibStore board packages

Realization

The implementation should contain the elements described in the above draft. In addition, we still have to allocate the specifically used resources of our controller. For the display we use the configuration from the board package textLcdSTMlight . We should use PinB0 for the ADC. The steps for the correct configuration of the text display are shown in more detail below.

Complete your class model so that it corresponds to the following illustration. Arrange the elements clearly.

As a first careful test, we put out simple welcome texts on line one and two, which we delete after two seconds. Note the following code in the onStart () operation of the Controller class:

Controller::onStart():void
// boot sequence after start SysTick
display.setPos(1,1);
display.write("Hello mySTM32.de");
display.setPos(1,2);
display.write("================");
waitMs(2000);
display.clear();

It is advantageous to create, transfer and test this in advance.

The actual solution of the task takes place in the operation onWork () of the class Controller . For more complex solutions that obviously require more than three lines of code, first write down the solution idea as comments.

Controller::onWork():void
// continuous event from the main loop
// read in analog value
// format the text output
// Text output analog value in the first line
// show a bar on the second line
// REPEAT FROM 0 TO 16
//   IF VALUE < MAX / 16 THEN write character "*" 
//   ELSE write character " "
// give the eye time to read the display

You are welcome to discuss this detailed draft with someone who may already have some experience with programming microcontrollers. At least you should calmly internalize this draft solution for yourself and play it through in your mind.

Now the actual source code can be written. Add the code for the onWork () operation of the Controller class as follows:

Controller::onWork():void
// continuous event from the Mainloop ---------------
// read in analog value
uint8_t value;
value=sensor.getValue();
// format the text output ----------------------------
String txt;
txt.format("Value=%d  ",value);
// Text output analog value in the first line --------
display.home();
display.write(txt);
// show a bar on the second line ---------------------
display.setPos(1,2);
for (uint8_t i=0; i<16; i++ )
{
	if (i<=value/16) display.write("*");
	else             display.write(" ");
}
// give the eye time to read the display -------------
waitMs(100);

You are now ready to build, deploy, and test the application.

Test

Compile the program. Correct typographical errors if necessary. Transfer the executable program to the controller's program memory.

  1. Build (compile and link)
  2. burning
  3. connect …

Variant

Change the application so that a 10 bit analog value (0..1023) is visualized on the display. Change the class model so that the sensor delivers 10-bit analog values.

Change the source code of the OnWork () operation of the Controller class as follows:

Controller::onWork():void
// continuous event from the Mainloop ---------------
// read in 10 BIT analog value
uint16_t value;
value=sensor.getValue();
// format the text output 16 Bit integer ------------
String txt;
txt.format("Value=%d  ",value);
// Text output analog value in the first line -------
display.home();
display.write(txt);
// show a bar second line --------------------------
display.setPos(1,2);
for (uint16_t i=0; i<16; i++ )
{
	if (i<=value/64) display.write("*");
	else             display.write(" ");
}
// give the eye time to read the display ---------
waitMs(100);

Create the program, transfer the solution to the microcontroller and test the application. Compare both solutions.

Video summary

Learned and established work steps:

  1. Create and open class diagram
  2. Select diagram template for PEC application
  3. load the correct board driver package from the SiSy Online LibStore
  4. Search for the desired modules in the Explorer and drag them into the diagram
  5. Aggregate classes
  6. create the necessary source code in the operations
  7. Create and burn an ARM application in the class diagram

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

or the fast version without voice comments

Exercise

Add an alarm LED to the application. The alarm LED should flash quickly (flicker) for sensor values below 100.
For values above 100, the LED is off. Use pin B4 for the alarm LED. Also show the information “OK” or “ALARM” on the display.

Continue with:

Search keywords