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

  1. One Dwengo board
  2. One Dwengo programmer
  3. 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. Add the header files and configure the fuses as explained in the step 2 of the tutorial about the blinking LEDs.

Adding the LCD library to the project

Now download the LCD library: lcd.h and lcd.c (available at the bottom of this page, you may have to log in first). Add these files to the project. Do this by selecting the Project menu and choosing Add Files to Project. Choose in the field Files of type: for All Source And Header Files (*.asm, *.c, *.h), navigate to your project folder and select both files. Click Open. Now the files become visible in the Project view. The library will be compiled by the compiler the moment you compile your project.

To make sure that the library functions are available and known in the main file (LcdDisplay.c), we have to include the header file lcd.h. Do this by adding the following code at the top of your file.

  1. #include <p18f4550.h>
  2. #include <delays.h>
  3. #include "lcd.h"

In addition you have to explicitly add the folder that contains lcd.h to the options of your project. For this, click in the menu Project on the Build options / Project. On the tab Directories select Show directories for Include Search Path. Choose New and add your path of the folder that contains lcd.h ([path]/library/).

The main loop

The main loop of the program looks like this:

  1. void main(void) {
  2. unsigned char counter;
  3.  
  4. initLCD();
  5. clearLCD();
  6. LCDBacklightOn();
  7.  
  8. setLineOnLCD(0);
  9. appendStringToLCD(" Dwengo Counter");
  10.  
  11. counter=0;
  12. while (TRUE) {
  13. setLineOnLCD(1);
  14. appendIntToLCD(counter);
  15. appendStringToLCD(" ");
  16. Delay10KTCYx(240);
  17. counter+=1;
  18. }
  19. }

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.

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. LCDBacklightOn() turns the backlight of the display on.

Line 8 and 9 print the text "Dwengo Counter* in the center of the first line of the display. On line 8 the statement setLineOnLCD(0); is used to place the cursor at the beginning of the first line. The statement on line 9 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 11 we initialize the variable counter to 0. The loop is formed by the code between lines 12 and 18. The following steps in the loop are repeated infinitely:

  • line 13: Set the cursor at the beginning of the second line.
  • line 14: Write the contents of the variable counter to the screen.
  • line 15: Write two extra spaces to the screen.
  • line 16: Wait for (240*10,000)/12,000,000 = 200ms before we show the next number
  • line 17: Increase the contents of the variable counter by one, so that the next iteration of the loop shows 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:

  1. void main(void) {
  2. unsigned char position;
  3. unsigned char counter;
  4.  
  5. initLCD();
  6. clearLCD();
  7. LCDBacklightOn();
  8.  
  9. counter=0;
  10. position=0;
  11.  
  12. while (TRUE) {
  13. setLineOnLCD(0);
  14. position=newsticker("The Dwengo Counter counts from 0 to 255. ", position);
  15. setLineOnLCD(1);
  16. appendIntToLCD(counter);
  17. appendStringToLCD(" ");
  18. Delay10KTCYx(200);
  19. counter+=1;
  20. }
  21. }

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:

  1. unsigned char newsticker(rom char *text, unsigned char positie) {
  2. char i,j;
  3. j=positie;
  4.  
  5. for (i=0; i<16; i++) {
  6. if (text[j]==0) {
  7. j=0;
  8. }
  9. appendCharToLCD(text[j]);
  10. j++;
  11. }
  12.  
  13. if (text[positie+1]==0)
  14. return 0;
  15. else
  16. return positie+1;
  17. }

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)
Source codeSource code files are only available to Dwengo customers. If you have bought with us before, please log in to download source files.