Codificação compatível com Arduino 12: Rolagem de texto longo no LCD de caracteres usando Arduino

Arduino 12 compatible coding: Scrolling long text on character LCD using Arduino

In the previous tutorial, we discussed:

  • The functional blocks of a character LCD
  • How Character LCD Works and Interfaces with a Controller
  • How to Display Text on a Character LCD

In this tutorial, we'll learn how to scroll long strings of text on a character LCD, which can be tricky. An LCD character module has a small amount of RAM that can store text and supports continuous scrolling of text on an LCD display. However, this requires careful design of the embedded program.

In this tutorial, we will display a long string of text on an Arduino character LCD and review the different steps and concerns that must be considered.

Displaying long text
Displaying long strings of text on a character LCD is far from simple. The reason for this is that the display data RAM of character LCD modules is limited. For example, the display data RAM (DDRAM) of a 16x2 character LCD can only store 80 characters. In two-line mode, DDRAM can only store 40 characters per line. This means that any attempt to store a text string longer than 40 characters will overwrite the other line.

If you want to display and scroll text longer than 40 characters on a 16x2 LCD line, the only way to do this is to break the original string into parts and write these parts of the text string to DDRAM one by one by scrolling each part of the text string.

Another concern is maintaining the continuity of the text that runs across the LCD. If only 40 characters per line can be written to an LCD module at a time, then only 16 characters can be displayed on the LCD at a time. To maintain text scrolling continuity on the LCD, some characters from the previous text segment need to be repeated in the next text segment written to DDRAM.

For example, on a 16x2 character LCD, if the text is scrolled one character at a time, 15 characters of the current text segment will need to be repeated in the next text segment written to DDRAM. For other LCD modules, one less than the number of characters per line needs to be repeated in the text segments. The size of the text segment that can be written to DDRAM at a time depends on the number of characters per line that can be stored in the DDRAM of this LCD module.

So, let's assume that the following text string is to be scrolled on a line of LCD characters: “Quick brown fox jumps over lazy dog ​​Quick brown fox jumps over lazy dog ​​Quick brown fox jumps over lazy dog”

The string above has 120 characters. Since only 40 characters can be written to DDRAM at a time, 15 characters must be repeated from the previous text segment to maintain text continuity on a 16×2 LCD.

This means that the above string must be split into the following strings to scroll down a 16×2 LCD line:

1: “Quick brown fox jumps over the lazy dog”
2: “R the lazy dog ​​Quick brown fox jumps”
3: “A fox jumps over the lazy dog, quick eyebrow”
4: ”fast brown fox dog jumps on the lazy one”
5: “I finished off the lazy dog”

To display and scroll the 120-character text string given above on a 16×2 LCD, you need to divide it into five 40-character text segments (except the last one) – with 15 (16-1) characters repeating in each consecutive segment. This is because each line displays 16 characters at a time while scrolling one character at a time.

Text segments 1 to 4 must be scrolled 24 (40-16) times as 40 characters are written to DDRAM at a time. These characters are scrolled on a 16-character line, while the last segment of text must be scrolled as many times as the length of the segment in characters is – plus additional padding (spaces) between each repeat of the scroll.

If you are not adding any padding between repeated scrolling of the text, the “5” text segment will need to be scrolled 21 times (the length of the segment) or at least five times (21-16) as per your choice.

Scrolling long text
Arduino's LiquidCrystal library has a number of functions that can be used to scroll text on a character LCD.

These methods are useful for scrolling text on an LCD display:

1. scrollDisplayLeft
2. scrollDisplayRight
3. auto scroll
4. Do not Autoscroll
5. from left to right
6. right to left

The print function is used to write textual data to the DDRAM of the character LCD. To scroll long text on a character LCD, the text can be split, as explained above, and each segment can be written to DDRAM using the print function.

After writing each segment, it can be scrolled with the help of any of the functions listed above.

It should be noted, however, that to scroll text in the LCD, S/C and RL bits of the “cursor or display change”, the LCD command is defined. Visit the previous tutorial to learn about LCD commands.

The scrollDisplayLeft method
This function scrolls the display content (text and cursor) one space to the left. No arguments needed. It simply scrolls the text written to DDRAM one space from the current DDRAM address (as in the LCD address counter).

This function has this syntax:

lcd.scrollDisplayLeft // lcd is an object of the LiquidCrystal class

Additionally, it has this source code:

This function uses a “command function”, which is used to send commands from the LCD to the character LCD.

The scrollDisplayRight method
This function scrolls the display content (text and cursor) one space to the right. No arguments needed. It simply scrolls the text written to DDRAM one space from the current DDRAM address (in the LCD address counter).

This function has this syntax:

lcd.scrollDisplayRight //lcd is an object of the LiquidCrystal class

Additionally, it has this source code:

The leftToRight method

This function sets the direction of text on a character LCD, from left to right. It is the default text direction. After calling this function, when characters are written to the LCD's DDRAM, the characters will be displayed from left to right. This function does not affect any previously displayed text. It only affects the text that is passed to the LCD (via the print or write function) after calling it.

It has this syntax:

lcd.leftToRight //lcd is an object of the LiquidCrystal class

This is the source code:

The rightToLeft method

This function sets the direction of text on a character LCD from right to left. The default text direction is left to right. After calling this function, when characters are written to the DDRAM of the LCD, the characters will be displayed from right to left. This function does not affect any previously displayed text. It only affects the text that is passed to the LCD (via the print or write function) after calling it.

It has this syntax:

lcd.rightToLeft //lcd is an object of the LiquidCrystal class

This is the source code:

The autoscroll method
This function is to automatically scroll the LCD text. The default text direction is left to right. The text scrolling direction can be set using the leftToRight function or the rightToLeft function.

When calling this function, the text is scrolled from left to right or right to left by one space, according to the direction of the text. Furthermore, this function scrolls the text written in DDRAM. Does not change the contents of DDRAM.

It has this syntax:

lcd.autoscroll //lcd is an object of the LiquidCrystal class

This is the source code:

The noAutoscroll method
This function disables automatic text scrolling on the LCD.

It has this syntax:

lcd.noAutoscroll //lcd is an object of the LiquidCrystal class

This is the source code:

The command , write and send methods
All functions used to scroll the LCD character screen use the command method. This method is used to pass LCD commands to the LCD module.

This method also uses another method — send — to pass commands to the LCD. The command method is used to pass commands to the LCD while the write method is used to pass data to the LCD module (for writing to DDRAM).

The command , write and send methods have these source codes:

The recipe
In this recipe, we will scroll long string texts on a 16x2 character LCD. Two different strings will be scrolled on two lines of the 16×2 LCD.

One of the strings will scroll across the top line of the character LCD and repeatedly display “EEWORLDONLINE”. The second string will scroll on the bottom line of the 16×2 LCD. It displays all EE Network sites with WTWH Media LLC.

Required components
1. Arduino UNO x1
2. 16×2 character x1 LCD
3. 10K Pot x1
4. 330 Ohm resistor or any low value resistor x1
5. Test board x1
6. Male-to-male bonding wires or connecting wires

Circuit Connections
The LCD module used in this project is the JHD162A. This is a 16×2 LCD module with 5×8 character dots. The LCD module has a 16-pin interface. The LCD interfaces with the Arduino in 4-bit mode. Pins 1 (GND) and 16 (LED) of the LCD module are connected to ground. Pin 2 (VCC) is connected to VCC.

Pin 15 (LED+) of the LCD module is connected to VCC through a small value resistor. Pin 3 (VEE) is connected to the variable terminal of a potentiometer, while the potentiometer's fixed terminals are connected to ground and VCC.

The R/W pin is connected to ground because the Arduino will only write data to the LCD module. RS, EN, DB4, DB5, DB6 and DB7 pins of LCD are connected to pins 13, 11, 7, 6, 5 and 4 of Arduino UNO respectively. The breadboard is powered to the common ground and 5V power rail of one of the ground pins and the 5V pin of the Arduino UNO.

Circuit Diagram

Arduino Sketch

How the project works

The objective is to display two strings on a 16×2 LCD module. One of the strings just displays the text “EEWORLDONLINE” on line 0 of the LCD. While scrolling on the display, this text must also scroll continuously on line 0. The text is repeated in a long sequence with two spaces between each repetition of the text.

The second string contains the names of EE Network websites with WTWH Media LLC, which include:

  1. eeworldonline.com
  2. edaboard.com
  3. electro-tech-online.com
  4. engineersgarage.com
  5. analogictips.com
  6. Connectortips.com
  7. microcontrollertips.com
  8. powerelectronictips. with
  9. sensortips.com
  10. testandmeasurementtips.com
  11. wireandcabletips.com
  12. designfast.com
  13. 5gtechnologyworld.com

The second string is 269 characters long and contains this text:

“eeworldonline.com edaboard.com electro-tech-online.com engineersgarage.com analogictips.com connectortips.com microcontrollertips.com powerelectronictips.com sensortips.com testandmeasurementtips.com wireandcabletips.com designfast.com 5gtechnologyworld.com”

To display scrolling text on line 0, the following text segments are routed to the character LCD:

1. ”EEWORLDONLINE EEWORLDONLINE EEWORLDO”

2. “NLINE EEWORLDONLINE EEWORLDONLINE EEW”

3. “ORLDONLINE EEWORLDONLINE EEWORLDONLINE”

After the third line segment, string segments 1 through 3 begin repeating to allow repeated display of the text, “EEWORLDONLINE”. All three text segments above are 40 characters long and need to be scrolled 24 times (40-16) on one line of the 16×2 LCD.

To display scrolling text on line 1, the 269-character string is divided into the following 40-character text segments.”

1. “eeworldonline.com engineersgarage.com”

2. “ersgarage.com edaboard.com electro-tec”

3. “om electro-tech-online.com analog tip”

4. “om analogictips.com connectortips.com”

5. “nectortips.com microcontrollertips.com”

6. “rollertips.com powerelectronictips.com”

7. “ronictips.com sensortips.com testandme”

8. “.com testandmeasurementtips.com wirean”

9. “ips.com wireandcabletips.com designfas”

10. “.com designfast.com 5gtechnologyworld”

11. “technologyworld.com”

All text segments are 40 characters long except the last one, because the maximum capacity of LCD DDRAM is 40 characters per line. Each subsequent text segment begins by repeating the last 15 characters of the previous text segment to ensure continuity of the scrolling text on the display.

A total of 15 characters are repeated because each line of the display has 16 characters and when scrolling a space, the last 15 characters must match the previous text segment whenever new data is written to DDRAM. Each text segment is scrolled 24 times (40-16) as there are 40 characters stored in DDRAM at a time and 16 characters are displayed on one line at a time on the LCD. The last line is scrolled according to the number of characters contained therein. This means it is rolled less than 24 times.

The result is a 16x2 LCD scrolling screen that highlights EEWORLDONLINE in line 0 and all sites that are part of the EE network in line 1.

Programming Guide
The Arduino sketch starts importing the LiquidCrystal library. Next, an object defined by the “lcd” variable of the LiquidCrystal class is defined.

#include
//LCD Liquid Crystal(RS, E, D4, D5, D6, D7);
Liquid Crystal LCD (13, 11, 7, 6, 5, 4);

A global variable “counter” is defined to scroll the text and the setup function is defined. In the configuration function, the LCD is initialized with the size 16×2 using the Begin method as follows:

null configuration
{
lcd.begin(16, 2);
}

In the loop function, the LCD display is first cleared using the clear method, and the cursor is placed in column 0 of row 0 using the setCursor method.

The text “EEWORLDONLINE EEWORLDONLINE EEWORLDO” is printed using the print method on the “lcd” object. Similarly, the text “”eeworldonline.com engineersgarage.com” is printed in column 0 of line 1.

Then a for loop is executed in which the entire display is shifted to the left using the scrollDisplayLeft method 24 times. After scrolling on the LCD, a delay of 300 milliseconds at a time is provided using the delay function.

empty loop
{
lcd.claro;
lcd.setCursor(0, 0);
lcd.print(”EEWORLDONLINE EEWORLDONLINE EEWORLDO”);
lcd.setCursor(0, 1);
lcd.print (“eeworldonline.com engineersgarage.com“);
for(counter = 0; counter <24; counter++)
{
lcd.scrollDisplayLeft;
delay(300);
}

Likewise, the other text segments are displayed and scrolled. In line 0, three text segments are repeated continuously until the last cut text segment is displayed. In line 1, 11 text segments are displayed and scrolled. The last text segment on both lines is scrolled five times, according to the length of the text segments.

For simplicity, the last text segments on lines 0 and 1 are also kept the same length.

The body of the loop function keeps repeating until the Arduino is turned off. Therefore, both long strings keep scrolling on lines 0 and 1 of the LCD module.

The Arduino does not support C-style string manipulation functions. Otherwise, the sketch could be reduced to a few lines by storing the entire string in a variable and then cutting the string at each step to pass to the LCD module's DDRAM . Even if the standard C string library could have been supported, using the string manipulation function would have increased the space occupied by the embedded program.

In the next tutorial, we will discuss how to display custom characters on the Character LCD.

Demo video

(tagsToTranslate)Arduino

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.