Вы находитесь на странице: 1из 14

A Library of Functions for HD44780 Based LCD

Modules (no R/W)


LaMa-TIC-RpR
/*******************************************************************************
The Answer to the Ultimate Question of Life, the Universe, and Everything is: 42
(Douglas Adams - The Hitchhiker's Guide to the Galaxy)
*******************************************************************************/

1. Introduction .......................................................................................................................................................... 2
2. HW and SW setup in LCD_HD44780_iram.h ..................................................................................................... 2
2.1 HW setup of the MCU .................................................................................................................................. 2
2.2 HW setup of the Display .............................................................................................................................. 2
2.3 Timing functions .......................................................................................................................................... 4
3. Display Setup Functions ....................................................................................................................................... 5
3.1 Definitions .................................................................................................................................................... 5
3.2 void LcdReset(void) ..................................................................................................................................... 5
3.3 void LcdSetup(tUC setup) ............................................................................................................................ 5
3.4 void LcdSetCursorDirection(tUC direction) ................................................................................................ 6
3.5 void LcdSetDisplayShiftEnable(tUC onoff) ................................................................................................ 6
3.6 void LcdSetDisplayOnOff(tUC onoff) ......................................................................................................... 6
3.7 void LcdSetCursorOnOff(tUC onoff) .......................................................................................................... 6
3.8 void LcdSetBlinkOnOff(tUC onoff) ............................................................................................................ 6
3.9 void LcdSetDisplayFont(tUC font) .............................................................................................................. 6
3.10 void LcdShiftDisplay(tUC direction) ........................................................................................................... 7
3.11 void LcdSetAddressCGRAM(tUC address)................................................................................................. 7
4. Position Reading Functions .................................................................................................................................. 7
4.1 Introduction .................................................................................................................................................. 7
4.2 Definitions .................................................................................................................................................... 7
4.3 tUC LcdGetColPos(void) ............................................................................................................................. 7
4.4 tUC LcdGetLinePos(void)............................................................................................................................ 7
4.5 tUC LcdGetDDRAMPos(void) .................................................................................................................... 8
5. Section 3. Position Setup Functions ..................................................................................................................... 8
5.1 Definitions .................................................................................................................................................... 8
5.2 void LcdReturnHome(void) ......................................................................................................................... 8
5.3 void LcdSetLine(tUC line) ........................................................................................................................... 8
5.4 void LcdSetPos(tUC line, tUC col) .............................................................................................................. 8
5.5 void LcdSetAddressDDRAM(tUC address) ................................................................................................ 9
5.6 void LcdMoveCursor(tUC direction) ........................................................................................................... 9
5.7 void LcdRollUp(void) .................................................................................................................................. 9
5.8 void LcdRollDown(void) ............................................................................................................................. 9
6. Display Clearing Functions .................................................................................................................................. 9
6.1 Definitions .................................................................................................................................................... 9
6.2 void LcdClear(void) ................................................................................................................................... 10
6.3 void LcdClearLine(tUC line) ..................................................................................................................... 10
7. Display Reading Functions................................................................................................................................. 10
7.1 Definitions .................................................................................................................................................. 10
7.2 tUC LcdGetChar(void) ............................................................................................................................... 10
7.3 tUC LcdGetCharLC(tUC line, tUC col) ..................................................................................................... 10
7.4 void LcdGetLineL(tUC line, tSC *str) ....................................................................................................... 11
7.5 void LcdGetLineLC(tUC line,tUC col, tSC *str)....................................................................................... 11
7.6 void LcdGetLineLCn(tUC line,tUC col,tUC n, tSC *str) .......................................................................... 11
7.7 void LcdGetWordLC(tUC line,tUC col, tSC *str) ..................................................................................... 11
8. Display Writting Functions ................................................................................................................................ 12
8.1 Definitions .................................................................................................................................................. 12
8.2 void LcdWriteChar(tUC chr) ..................................................................................................................... 12
8.3 void LcdWriteCharLC(tUC line,tUC col,tUC chr) .................................................................................... 12
8.4 void LcdOutText(tSC *str) ......................................................................................................................... 12
8.5 void LcdOutTextL(tUC line, tSC *str) ....................................................................................................... 12
8.6 void LcdOutTextLC(tUC line,tUC col, tSC *str) ...................................................................................... 13
8.7 void LcdOutTextLCn(tUC line,tUC col,tUC n,tSC *str) ........................................................................... 13
9. Switching between more screens........................................................................................................................ 13
9.1 Definitions .................................................................................................................................................. 13
9.2 void LcdSwitchScreen(S_LcdContent *new_screen); ............................................................................... 13

1. Introduction
A project presents control of the HD44780 driven display where it is assumed that RW pin of the LCD
is permanently connected to “Write” level (GND). The SW contains mirror of the LCD display as array of
characters stored within RAM.
Both read and write function can be used because writing is performed to the display and also to the
array. Read functions are directed to the character array stored within RAM.

2. HW and SW setup in LCD_HD44780.h


The header file contains definition which reflects HW configuration and required communication protocol between
MCU and HD44780.

2.1 HW Setup of the MCU

Connections between an MCU and a display are defined in the part “LCD HARDWARE CONTROL PORTS”.
The control pins of the display can be connected to any port. Note that this driver does not require to connect R/W
pin of the display to the MU because the solution is based on the presence of mirror of the display in the memory
of MCU. The data lines must be connected to either entire port in the case of eight bit data communication or to
one of the port nibbles in case of four bit data communication. Data interface port selection is made by means of
definitions LCD_DPORT and LCD_DPORTDIR. The data interface can be 8bit or 4bit length. In the first case
entire port is assigned for data transfer. In the second case the user can select upper (LCD_DPORTH) or lower
(LCD_DPORTL) nibble of the selected port for data transfer. The SW does not affect the other nibble if 4bit
interface is selected.

The example below presents LCD control lines connection bits of the port S. The port P low nibble is used for 4 bit
data communication between MCU and LCD.
There are two more definitions presented. They are LCD_DIR_IN and LCD_DIR_OUT. They define port direction
setup. These definition do not have to be changed for entire S12(X) family devices.

The last definition which is important is definition of LCD_FBUS. It represent bus clock of the MCU.

/**************************************************************************/
/* LCD HARDWARE CONTROL PORTS */
/**************************************************************************/
#define LCD_E PTS_PTS0 /* disp ENABLE pin */
#define LCD_RS PTS_PTS2 /* disp RS pin (data/ctrl = 1/0) */
//The SW does not use RW pin. It uses mirror display in the memory of the MCU
//The R/W pin of the display must be connected to GND
//#define LCD_RW /* disp R/W pin (write/read = 1/0) */
#define LCD_DE DDRS_DDRS0 /* disp ENABLE pin direction bit */
#define LCD_DRS DDRS_DDRS2 /* disp RS pin direction bit */
//The SW does not use RW pin. It uses mirror display in the memory of the MCU
//The R/W pin of the display must be connected to GND
// #define LCD_DRW /* disp R/W pin direction bit */

#define LCD_DPORT PTP /* display data port */


#define LCD_DPORTDIR DDRP /* display data port direction re- */
/* gister address */
#define LCD_DATAWIDTH 4 /* width of data communication - 4,8 bit interface */

#if LCD_DATAWIDTH==4 /* if 4 bits communication (#define LCD_DATAWIDTH 4) */


/* is selected define data port nibble */
//#define LCD_DPORTH /* MSB port nibble */
#define LCD_DPORTL /* LSB port nibble */
#endif

#define LCD_FBUS 25000000UL /* uC bus frequency [Hz](necessary */

/**************************************************************************/
/* DATA PORT DIRECTIONS SETUP */
/**************************************************************************/
#define LCD_DIR_IN 0 /* def. input direction value for DDR reg.*/
/* to set port as input (see uP data sheet)*/
#define LCD_DIR_OUT 1 /* def. output direction value for DDR reg.*/
/* to set port as output(see uP data sheet)*/
/**************************************************************************/

2.2 HW Setup of the Display

In order to define HW of the LCD display for control SW it is necessary to define which type of display is
connected to the MCU.

An example presented below present setup for display 4x20.

/**************************************************************************/
/* DISPLAY SETUP */
/**************************************************************************/
#define LCD_LINES 4 /* number of physical display lines - 1,2,4 */
#define LCD_MLINES 2 /* number of display memory lines - 1,2 */
#define LCD_COLUMNS 20 /* number of physical display columns - 8,16,24,32,40 */
#define LCD_LINE0ADDR 0x00 /* display line addresses - line 0 */
#define LCD_LINE1ADDR 0x40 /* display line addresses - line 1 */
#define LCD_LINE2ADDR 0x14 /* display line addresses - line 2 */
#define LCD_LINE3ADDR 0x54 /* display line addresses - line 3 */

Physical look of the MCU is defined by means of LCD_LINES and LCD_COLUMNS. (For example displays
4x20, 2x16, …) These two definitions also create mirror of the display screen in the MCU memory. For this
purpose a variable extern tSC LcdArray[LCD_LINES][LCD_COLUMNS+1]; is defined in the file. The
variable is a part of the structure which provides us to switch between more screens.
The definition LCD_MLINES defines how the LCD is organized from internal memory addresses points of view.
(For example, a display 4x20 can contain 2 memory lines where the first memory line covers 1st and 3rd physical
LCD lines and 2nd memory line covers 2nd and 4th physical LCD lines)

The last definition defines used oscillator frequency connected to the HD44780 display driver.

Also, display driver setup can be found in a variable extern S_LCD Lcd. The type S_LCD is defined:

typedef struct
{
S_LcdPosition cursor; // cursor position structure
unsigned char movingSetup; // display moving setup
unsigned char onOffControl; // display on/off control
unsigned char interface ; // display interface
// 0 0 0 0 0 1 I/D S
// 0 0 0 0 1 D C B
// 0 0 1 DL N F . .
}
S_LCD;

Where:

typedef struct
{
unsigned char line; // cursor line
unsigned char col ; // cursor column
unsigned char address; // cursor address
}
S_LcdPosition;

2.3 Timing Functions

The SW has to ensure correct delays between consecutive actions since the control signal which reads busy of the
LCD is not used. For this purpose time delay functions are defined in the file LCD_HD44780.c.

static void LcdDelay10ms (void);


static void LcdDelay1us (void);

They are written in assembler and take into account time spent by instruction. Both of them use LCD_FBUS to
calculate their loop constants. LCD_FBUS must have the same value as bus clock of the application.
All delays necessary for display control are derived from this two functions and respect the worst case scenario of
display clock oscillator frequency.

Clock oscillation frequency fOSC min. 190 typ. 270 max. 350 kHz
3. Display Setup Functions

3.1 Definitions

#define tUL unsigned long


#define tUI unsigned int
#define tUC unsigned char
#define tSC signed char

3.2 void LcdReset(void)

Description : Standard display reset.


Function has to be called as a first instruction befor display is used. it initializes display and sets
communication parameters.
The function sets display parameters following way:

LCD_CURSORRIGHT | LCD_SHIFTOFF | LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF|


LCD_SMALLFONT

After initialization process the display is ready to be used.

Global Data : none


Static Global Data: none
Returns : none
Arguments : none
Special Issues : none

3.3 void LcdSetup(tUC setup)

Description : Function sets whole display to required behavior and clears display.
For display setup it is necessary to use following definitions:

LCD_CURSORLEFT or LCD_CURSORRIGHT => Sets cursor move direction


LCD_SHIFTON or LCD_SHIFTOFF => Enables accompanies display shift
LCD_DISPLAYOFF or LCD_DISPLAYON => Switches on/off display
LCD_CURSORON or LCD_CURSOROFF => Switches on/off cursor
LCD_BLINKON or LCD_BLINKOFF => Switches cursor blink on/off
LCD_LARGEFONT or LCD_SMALLFONT => Sets large 5x10 / small 5x8 font

Global Data : none


Static Global Data: none
Returns : none
Arguments :
tUC setup = ((LCD_CURSORLEFT or LCD_CURSORRIGHT) |
(LCD_SHIFTON or LCD_SHIFTOFF ) |
(LCD_DISPLAYOFF or LCD_DISPLAYON ) |
(LCD_CURSORON or LCD_CURSOROFF ) |
(LCD_BLINKON or LCD_BLINKOFF ) |
(LCD_LARGEFONT or LCD_SMALLFONT ))

Special Issues : none


3.4 void LcdSetCursorDirection(tUC direction)

Description : Set cursor moving direction to the right or left


Global Data : none
Static Global Data: Lcd.movingSetup
Returns : none
Arguments : tUC direction = {LCD_CURSORRIGHT, LCD_CURSORLEFT}
Special Issues : none

3.5 void LcdSetDisplayShiftEnable(tUC onoff)

Description : Switch on/off display shift


Global Data : none
Static Global Data: Lcd.movingSetup
Returns : none
Arguments : tUC onoff = {LCD_SHIFTON, LCD_SHIFTOFF}
Special Issues : none

3.6 void LcdSetDisplayOnOff(tUC onoff)

Description : Switch on/off display


Global Data : none
Static Global Data: Lcd.onOffControl
Returns : none
Arguments : tUC onoff = {LCD_DISPLAYON, LCD_DISPLAYOFF}
Special Issues : none

3.7 void LcdSetCursorOnOff(tUC onoff)

Description : Switch on/off cursor


Global Data : none
Static Global Data: Lcd.onOffControl
Returns : none
Arguments : tUC onoff = {LCD_CURSORON, LCD_CURSOROFF}
Special Issues : none
Display Setup Functions

3.8 void LcdSetBlinkOnOff(tUC onoff)

Description : Switch on/off cursor blink


Global Data : none
Static Global Data: Lcd.onOffControl
Returns : none
Arguments : tUC onoff = {LCD_BLINKON, LCD_BLINKOFF}
Special Issues : none

3.9 void LcdSetDisplayFont(tUC font)

Description : Set display font


Global Data : none
Static Global Data: Lcd.interface
Returns : none
Arguments : tUC font = {LCD_LARGEFONT, LCD_SMALLFONT}
Special Issues : none

3.10 void LcdShiftDisplay(tUC direction)

Description : shift display


Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC direction = {LCD_RIGHT, LCD_LEFT}
Special Issues : none

3.11 void LcdSetAddressCGRAM(tUC address)

Description : set CG RAM address


Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC address
Special Issues : none

4. Position Reading Functions

4.1 Introduction

4.2 Definitions

#define tUL unsigned long


#define tUI unsigned int
#define tUC unsigned char
#define tSC signed char

4.3 tUC LcdGetColPos(void)

Description : Get current column position of the cursor


Global Data : none
Static Global Data: Lcd.cursor.address, Lcd.cursor.line, Lcd.cursor.col
Returns : diplay column
Arguments : none
Special Issues : none

4.4 tUC LcdGetLinePos(void)

Description : Get current line position of the cursor


Global Data : none
Static Global Data: Lcd.cursor.address, Lcd.cursor.line, Lcd.cursor.col
Returns : diplay line
Arguments : none
Special Issues : none

4.5 tUC LcdGetDDRAMPos(void)

Description : Get current DDRAM position of the cursor


Global Data : none
Static Global Data: Lcd.cursor.address, Lcd.cursor.line, Lcd.cursor.col
Returns : DDRAM address
Arguments : none
Special Issues : none

5. Section 3. Position Setup Functions

5.1 Definitions

#define tUL unsigned long


#define tUI unsigned int
#define tUC unsigned char
#define tSC signed char

5.2 void LcdReturnHome(void)

Description : Return cursor to the home position and return shifted display to original position.
DDRAM contents remains unchanged. ((Set cursor at position (0,0))
Global Data : none
Static Global Data: none
Returns : none
Arguments : none
Special Issues : none

5.3 void LcdSetLine(tUC line)

Description : Set DDRAM address at the first char of line x (Set cursor at position (line,0) )
Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC line = <0,LCD_LINES)
Special Issues : none

5.4 void LcdSetPos(tUC line, tUC col)

Description : Set DDRAM address at position given by coordinates (line,column)


Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC line = <0,LCD_LINES), tUC column = <0,LCD_COLUMNS)
Special Issues : none

5.5 void LcdSetAddressDDRAM(tUC address)

Description : Set cursor position at DDRAM address.


Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC address
Special Issues : none

5.6 void LcdMoveCursor(tUC direction)

Description : Move cursor in the given direction.


Global Data : none
Static Global Data: Lcd.cursor.address, Lcd.cursor.line, Lcd.cursor.col
Returns : none
Arguments : tUC direction = {LCD_RIGHT, LCD_LEFT, LCD_DOWN, LCD_UP}
Special Issues : none

5.7 void LcdRollUp(void)

Description : Roll display up and set first position on the last line.
Global Data : none
Static Global Data: none
Returns : none
Arguments : none
Special Issues : none

5.8 void LcdRollDown(void)

Description : Roll display down and set first posit. on the first line.
Global Data : none
Static Global Data: none
Returns : none
Arguments : none
Special Issues : none
Position Setup Functions

6. Display Clearing Functions

6.1 Definitions

#define tUL unsigned long


#define tUI unsigned int
#define tUC unsigned char
#define tSC signed char
6.2 void LcdClear(void)

Description : Clear entire display and return the cursor to the home position (0,0)
Global Data : none
Static Global Data: Lcd.cursor.address, Lcd.cursor.line, Lcd.cursor.col
Returns : none
Arguments : none
Special Issues : none

6.3 void LcdClearLine(tUC line)

Description : Clear selected line


Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC line = <0,LCD_LINES)
Special Issues : none

7. Display Reading Functions


Display Reading Functions

7.1 Definitions

#define tUL unsigned long


#define tUI unsigned int
#define tUC unsigned char
#define tSC signed char

7.2 tUC LcdGetChar(void)

Description : Read character from display from actual DDRAM address (actual cursor position).
Global Data : none
Static Global Data: none
Returns : tUC read character
Arguments : none
Special Issues : none

7.3 tUC LcdGetCharLC(tUC line, tUC col)

Description : Read character from current cursor position (line,column).


Current cursor (DDRAM address) remains unchanged
Global Data : none
Static Global Data: none
Returns : tUC read character
Arguments : tUC line = <0,LCD_LINES)
tUC column = <0,LCD_COLUMNS)
Special Issues : none
7.4 void LcdGetLineL(tUC line, tSC *str)

Description : Read line x and put it into string. Current cursor position remains unchanged.
Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC line = <0,LCD_LINES)
tSC *string
Special Issues : String should have length 1 byte (for termination string character '\0') longer than
LCD_COLUMNS.

7.5 void LcdGetLineLC(tUC line,tUC col, tSC *str)

Description : Read text from position (line,column) till the end of the line and put it into the string.
Cursor position remains unchanged.
Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC line = <0,LCD_LINES),
tUC column = <0,LCD_COLUMNS),
tSC *string
Special Issues : String should have length at least 1 byte (for termination string character '\0') longer
than LCD_COLUMNS.

7.6 void LcdGetLineLCn(tUC line,tUC col,tUC n, tSC *str)

Description : Get n characters from starting position (line,column). Reading is finished also when the
end of line is reached. In this case less than required character is read. Cursor position remains
unchanged.
Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC line = <0,LCD_LINES),
tUC column = <0,LCD_COLUMNS),
tUC number of characters,
tSC *string
Special Issues : String should have length at least 1 byte (for termination string character '\0') longer
than LCD_COLUMNS.

7.7 void LcdGetWordLC(tUC line,tUC col, tSC *str)

Description : Read text but one word only from position (line,column). It gets all characters till either
character ‘ ‘ is reached or end of line is reached.
Cursor position remains unchanged.
Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC line = <0,LCD_LINES),
tUC column = <0,LCD_COLUMNS),
tSC *string
Special Issues : String should have length at least 1 byte (for termination string character '\0') longer
than LCD_COLUMNS.
Display Reading Functions
8. Display Writing Functions

8.1 Definitions

#define tUL unsigned long


#define tUI unsigned int
#define tUC unsigned char
#define tSC signed char

8.2 void LcdWriteChar(tUC chr)

Description : Write character to display on current position, cursor moves by autoincrement setup.
When character is written at the last/first position in the line then cursor remains at the current
position even autoincrement setup is set to LCD_CURSORRIGHT/LCD_CURSORLEFT.
Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC character
Special Issues : none

8.3 void LcdWriteCharLC(tUC line,tUC col,tUC chr)

Description : Write character to (line, column) position, cursor moves by autoincrement setup. When
character is written at the last/first position in the line then cursor remains at the current position
even autoincrement setup is set to LCD_CURSORRIGHT/LCD_CURSORLEFT.
Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC line = <0,LCD_LINES),
tUC column = <0,LCD_COLUMNS),
tUC character
Special Issues : none

8.4 void LcdOutText(tSC *str)

Description : Write string to current position. String must be finished by '\0'. Autoincrement setup has
no influence. Cursor will be placed behind the last written character. If last column in the line is
reached the cursor stays at this position.
Global Data : none
Static Global Data: none
Returns : none
Arguments : tSC *string
Special Issues : none
Display Writting Functions

8.5 void LcdOutTextL(tUC line, tSC *str)

Description : Write string to selected (line,0) from beginning. String must be finished by '\0'. Maximum
LCD_COLUMNS characters are written. Autoincrement setup has no influence. Cursor will be placed
behind the last written character. If last column in the line is reached the cursor stays at this position.
Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC line = <0,LCD_LINES)
tSC *string
Special Issues : none
Special Issues : none

8.6 void LcdOutTextLC(tUC line,tUC col, tSC *str)

Description : Write string from selected (line,column) position. String must be finished by '\0'.
Autoincrement setup has no influence. Cursor will be placed behind the last written character. If last
column in the line is reached the cursor stays at this position.
Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC line = <0,LCD_LINES),
tUC column = <0,LCD_COLUMNS),
tSC *string
Special Issues : none

8.7 void LcdOutTextLCn(tUC line,tUC col,tUC n,tSC *str)

Description : Write string from selected (line,column) position but maximally “n” characters. String
must be finished by '\0'. Autoincrement setup has no influence. Cursor will be placed behind the last
written character. If last column in the line is reached the cursor stays at this position.
Global Data : none
Static Global Data: none
Returns : none
Arguments : tUC line = <0,LCD_LINES),
tUC column = <0,LCD_COLUMNS),
tUC number of characters,
tSC *string
Special Issues : none

9. Switching Between More Screens

9.1 Definitions

#define tUL unsigned long


#define tUI unsigned int
#define tUC unsigned char
#define tSC signed char

9.2 void LcdSwitchScreen(S_LcdContent *new_screen);

Description : Select and display another screen. The SW contains a structure S_LcdContent defaultscreen
which is used as a storage structure of cursor position and current display content.
The type S_LcdContent is defined:
typedef struct
{
tSC LcdArray[LCD_LINES][LCD_COLUMNS+1]; /*display content*/
tUC curline, curcol; /*last cursor position*/
}
S_LcdContent;

There is also pointer volatile S_LcdContent *LcdContent which point to current screen which is currently used
and corresponds to displayed data on the LCD. Another screen can be prepared on the background. For example,
volatile S_LcdContent screen1.
The content of display can be prepared in easy way, for example:
// prepare content of alternative screens

(void) sprintf(screen1.LcdArray[0],"screen1 ");


for(i=1;i<4;i++) {for(j=0;j<LCD_COLUMNS;j++) { screen1.LcdArray[i][j]=k++;}}

When it is required the screen can be switched to another screen. LcdSwitchScreen(&screen1);


This command will store last cursor position, loads pointer to new screen to the pointer LcdContent and sets
cursor to the position stored in the screen1 structure.
Global Data : volatile S_LcdContent *LcdContent
Static Global Data: none
Returns : none
Arguments : S_LcdContent *new_screen

Вам также может понравиться