Using the LCD: making a news ticker
In this tutorial we explain how text can be shown on the LCD of the Dwengo board and how to operate the backlight of this display. The first result of this tutorial is a program that shows "Dwengo Board" on the first line of the display and on the second line a slowly increasing number. As an extension we show how you can display a text that is longer than 16 characters on one line of the screen by using a news ticker effect (commonly used for displaying stocks).
Requirements
- One Dwengo board
- One Dwengo programmer
- Enclosed USB cable
Using the LCD library
Dwengo provides a library for using the LCD. We will use this library to write a small program that writes Dwengo Board to the first line and shows a number on the second line that slowly increases.
Start by making a new project as explained in step 1 of the tutorial about the blinking LEDs. Choose a suitable name (e.g. LcdDisplay) and a location for the project.
Now add a C file (LcdDisplay.c) to the project. This file will contain the main function of the program.
In order to have the necessary functionality available wel also add the correct header files. You can do this by adding the following code at the top of your file. In this case the header file dwengoLCD.h provides all the functions you need to use the LCD display.
#include <dwengoConfig.h> #include <dwengoBoard.h> #include <dwengoDelay.h> #include <dwengoLCD.h>
The main loop
The main loop of the program looks like this:
void main(void) { unsigned char counter; initBoard(); initLCD(); clearLCD(); backlightOn(); setCursorLCD(0, 0); appendStringToLCD(" Dwengo Counter"); counter=0; while (TRUE) { setCursorLCD(1, 0); appendIntToLCD(counter); appendStringToLCD(" "); counter+=1; delay_ms(200); } }
In the first line of the program we define the variable counter. This variable is of the type unsigned char and can contain numbers between 0 and 255. In this program we use counter to store the number we want to display on the second line of the LCD.
Next we call the function initBoard() to initialize the base functionality of the obard. The library function initLCD() initializes the LCD, in other words initLCD() makes sures that the display is ready to receive text from the microcontroller and show it on the screen. The function clearLCD() erases all contents shown on the display. We call this function in the beginning of the program to make sure that we start with a blank screen. backlightOn() turns the backlight of the display on.
Line 9 and 10 print the text "Dwengo Counter" in the center of the first line of the display. On line 9 the statement setCursorLCD(0, 0); is used to place the cursor at the beginning (position 0) of the first line (line 0). The statement on line 10 prints the text "Dwengo Counter" on the display, starting from the current position of the cursor. By placing a space in front of the text, the text appears in the middle of the display.
The next lines of code contain the main loop of the program. On line 12 we initialize the variable counter to 0. The loop is formed by the code between lines 13 and 19. The following steps in the loop are repeated infinitely:
- line 14: Set the cursor at the beginning of the second line (line 1).
- line 15: Write the contents of the variable counter to the screen.
- line 16: Write two extra spaces to the screen.
- line 17: Increase the contents of the variable counter by one, so that the next iteration of the loop shows the next number.
- line 18: Wait for 200ms before we show the next number
The result of this code is that the top line of the screen shows " Dwengo Counter " while the bottom line of the screen is showing a number that increases every 200ms. Note that the counting starts again at zero after the variable reaches the number 255. By writing two extra spaces after counter we make sure that the last two digits of 255 are erased once we restart counting from 0.
Making a news ticker
An extension to the previous program is instead of showing the static text "Dwengo counter" in the middle of the screen, to let this text scroll slowly from right to left as you can see in news tickers. In this way you can display larger texts on a small screen.
The main function of the program looks like this:
void main(void) { unsigned char position; unsigned char counter; initLCD(); clearLCD(); backlightOn(); counter=0; position=0; while (TRUE) { setCursorLCD(0, 0); position=newsticker("The Dwengo Counter counts from 0 to 255. ", position); setCursorLCD(1, 0); appendIntToLCD(counter); appendStringToLCD(" "); delay_ms(200); counter+=1; } }
We define an extra variable position. This variable is used to store the starting position of the text that should be displayed.
The main loop of the program (lines 12 to 20) now writes both lines of the display because now also the contents of the first line will change every iteration. Writing the second line (lines 15-17) remains unchanged from our initial program. Writing the first line of the screen is done in lines 13 and 14. The function in line 13 sets the cursor to the beginning of line 1. Line 14 calls the function newsticker() which writes the correct text to the screen.
The function newsticker() has two arguments: the complete text that has to be displayed ("The Dwengo Counter counts from 0 to 255. ") and the current position. The function writes only 16 characters of the complete text. The current position variable provides the position of the first character that should be displayed. For example, if the position is 5, then the function writes "wengo Counter co" to the screen. When we reach the end of the text, the functions continues by displaying the beginning of the text again. For example, if we reach position 35, the text that will be displayed on the screen is "255. The Dwengo". Besides writing text to the screen, the function newsticker() also returns the next value of position.
The code of function newsticker() is displayed below:
unsigned char newsticker(rom char *text, unsigned char positie) { char i,j; j=positie; for (i=0; i<16; i++) { if (text[j]==0) { j=0; } appendCharToLCD(text[j]); j++; } if (text[positie+1]==0) return 0; else return positie+1; }
The function uses the property of how strings (a sequence of characters) are stored in memory. After the memory location of the last character of the string there is always a null character (0x00). This way the function can easily detect the end of the string.
Lines 2 to 11 write 16 characters to the screen by using the library function appendCharToLCD(). This function writes one character to the screen. In lines 13 to 16 we calculate and return the new position.
Additional information about the LCD
The LCD with backlight is included in the standard package of the Dwengo board. This LCD is HD44780 compatible. The LCD uses a standard 16 pins interface:
| Pin | Symbol | Description |
|---|---|---|
| 1 | VCC | Vcc (+5V) |
| 2 | GND | Ground |
| 3 | V0 | Contrast adjustment |
| 4 | RS | Register select |
| 5 | R/W | Read (high) or write (low) |
| 6 | E | Enable (active high) |
| 7 | DB0 | Data-bus bit 0 (not in 4-bit mode) |
| 8 | DB1 | Data-bus bit 1 (not in 4-bit mode) |
| 9 | DB2 | Data-bus bit 2 (not in 4-bit mode) |
| 10 | DB3 | Data-bus bit 3 (not in 4-bit mode) |
| 11 | DB4 | Data-bus bit 4 |
| 12 | DB5 | Data-bus bit 5 |
| 13 | DB6 | Data-bus bit 6 |
| 14 | DB7 | Data-bus bit 7 |
| 15 | LED+ | Backlight (positive) |
| 16 | LED- | Backlight (negative) |

Your shopping cart