AN4032 Application Note: Interfacing An HD44780 2-Line LCD Display With The Stm8Svldiscovery
AN4032 Application Note: Interfacing An HD44780 2-Line LCD Display With The Stm8Svldiscovery
Application note
Interfacing an HD44780 2-line LCD display
with the STM8SVLDISCOVERY
Introduction
This application note describes how to use a firmware example that drives a standard
HD44780 2-line LCD display with the STM8SVLDISCOVERY.
The example uses an LCD display driver that has been developed to be as generic as
possible, so it can be reused with any STM8 device.
The firmware package associated to this application note includes a pre-configured project
template which facilitates development of your own application to use an HD44780 LCD
display module.
The LCD interface offers many ways to easily create a user interface to display text
messages and other application related information, whilst using very few internal hardware
resources.
The HD44780 is a common LCD controller and is very widespread through various formats,
1 X 8, 2 x 16, 2 x 20, 4 x 20 and others. This example uses the very popular 2 x 16 format (2
lines of 16 characters) which is now offered at a very reasonable price. Other LCD display
modules based on compatible ST7066 or KS0066U controllers could also be used.
This application example intentionally uses the least possible microcontroller pins as the
interface bus to the LCD module is 4-bits wide.
This application example requires additional hardware:
■ LCD module HD44780
■ Potentiometer to adjust LCD contrast.
Reference documents
■ STM8SVLDISCOVERY STM8S value line discovery (UM1482).
■ STM8SVLDISCOVERY quick start guide (UM1480)
All these documents are available at http://www.st.com.
Contents
1 Application description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1 Hardware required . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 STM8SVLDISCOVERY hardware settings . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 HD44780 LCD display module description . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Hardware interface application schematics . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 Application principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5.2 LCD interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5.3 Custom characters definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2 Software description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1 STM8S peripherals used by the application . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 Configuring STM8S/A standard peripherals library . . . . . . . . . . . . . . . . . 12
2.2.1 For use with this application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.2.2 For other uses of the HD44780 LCD display driver . . . . . . . . . . . . . . . . 12
2.3 Application software description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3.1 Main application flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3.2 HD44780 LCD display module library description . . . . . . . . . . . . . . . . . 15
3 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
List of tables
List of figures
1 Application description
/&''LVSOD\SLQV
FKDUDFWHUV
/LQH
/LQH
-36
VEE
GND VCC RS R/W E DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7 NC NC
or V0
Register
LCD Read/Write Enable
Select 4-bit low order data 4-bit high order data
0V +5V power 0: Write LCD - -
0: Command bus bus
supply 1: Read module
1: Data
*1'
&1*1' 9&&
&13( 9((
&13& 56
&13& 5:
&13% (
&13% '%1&
&13% '%1&
&13% '%1&
'%1&
'%
'%
'%
'%
67069/',6&29(5< +'/LQH/&''LVSOD\
Note: After power ON, the LCD display must be initialized once only. During a debug session, user
can be led to reset the MCU and then to force a second initialization while the LCD has not
been powered OFF. To prevent the LCD display from an unexpected initialization, the LCD
pin VCC is connected to the MCU pin PE5 to manage by software the LCD power. This
ensures that an LCD power OFF is performed before any LCD initialization attempt.
Once your code has been completely developed and debugged, it is possible to fix the LCD
pin VCC by connecting it permanently to the +5V power supply and by removing PE5 pin
management in software. This will free-up one more pin on the microcontroller side.
1.5.1 Overview
This application note demonstrates some of the LCD functions provided in the library and
explains how they can be combined to create your own menus for the final application. Once
the hardware has been connected to the STM8SVLDISCOVERY and the software example
loaded in the STM8S device memory, a welcome message “STM8SVLDISCOVERY” is
displayed on the LCD screen. The user is then invited to press the on-board push-button to
continue the demonstration. This brief LCD demonstration simultaneously refreshes 4
changing items such as shown in Figure 3:
● an 8-character bar graph using custom characters defined in the LCD’s memory
● a time counter that counts minutes, seconds and hundredths of seconds
● the square value of an incrementing variable
● a rolling text message that is updated every second
MS30017V1
This firmware example uses some of the basic functions available in the HD44780 driver
(initializing the LCD, printing characters strings to the LCD, using LCD display ON/OFF,
setting cursor position, clearing the LCD display, etc…).
Up to 8 user characters (5 x 8 dots each) can be managed to create special characters or
even limited graphics by combining them, as for instance, on a 4 x 2 characters matrix
displayed on 2 lines. Section 1.5.3 describes how to create your own characters.
managed by 4 lines connected to PB0 to PB3 microcontroller pins. In addition, there are 3
control pins RS, R/W and E.
● R/W is set to write mode (0) as data is always sent from the microcontroller to the LCD
module and not in the opposite direction.
● RS is the register select pin. This pin allows the LCD module to decide if data coming
from the microcontroller must be interpreted as an LCD command, or if it must be
treated as text data to be displayed on the LCD screen.
● E enables LCD access for each MCU to LCD transaction. As soon as a communication
starts from the microcontroller side, this pin must be put high to enable the LCD. It
returns to low level once the procedure is completed.
XE
X
X
X
XF
X
X
X
-36
To define the application custom characters related to the bar graph used in our example,
we created the following 6 characters stored in a single tab.
These characters are then written into the HD44780 LCD display module at the start
address of CGRAM @0x40. These characters are defined from number 0 to 5 and can be
called directly by printing their hexadecimal code.
For instance, the C instruction LCD_printstring(“\3\n”) displays Character 3 at the
current cursor position. Displaying the bar graph prints a string containing the characters’
numbers (progressively changing from Character 0 to Character 5) at the same LCD display
location.
2 Software description
a. If the user wants to use other pins than the proposed ones, they must be configurable as output push-pull.
b. If another power pin is used instead, it must have a high sink capability.
34!24
#LOCK CONFIGURATION
'0)/ CONFIGURATION
4)- %NABLE MS INTERRUPT 4IME
BASE
($ ,#$
0OWER /.
($ ,#$ )NIT
($ ,#$ #LEAR
DISPLAY
($ ,#$ ,OAD
#USTOM #HARACTERS
7ELCOME -ENU
. 9
" PRESSED
($ ,#$ $EMO
-36
Cursor is set at location defined by row and u8 row: ROW number (must
column. Eg: LCD_LOCATE(1,4) sets cursor be between 1 to 2)
void LCD_LOCATE
on first line in 4th position. If position is out of u8 column: COLUMN None
(u8 row, u8 column)
range, then no action. Physical address of number (must be included
1st line is 0x00; 2nd line is 0x40 between 1 to 16)
Print characters strings with specified format
const char *fmt, ...:
void LCD_printf (does not support all formats depending
characters string with None
(const char *fmt, ...) on compiler limitations). Needs to include
specified format
stdarg.h library in header file.
3 Conclusion
LCD character display modules are very common in many 8-bit microcontroller based
applications. With a very affordable price and a quick implementation time, they offer the
easiest way to realize a simple and efficient interface for various applications in industry,
consumer, metering, telecommunications and other domains.
This short example includes all the basic functions to drive an HD44780 LCD controller and
any other compatible LCD display modules. This brief demo provides
STM8SVLDISCOVERY users with many possibilities to quickly create interfaces and
menus.
This application note can also be tailored for any microcontroller of the STM8 family with
slight firmware modifications.
4 Revision history
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY TWO AUTHORIZED ST REPRESENTATIVES, ST PRODUCTS ARE NOT
RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING
APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,
DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE
GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.