Академический Документы
Профессиональный Документы
Культура Документы
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.
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 */
/**************************************************************************/
/* 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)*/
/**************************************************************************/
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.
/**************************************************************************/
/* 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;
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.
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
Description : Function sets whole display to required behavior and clears display.
For display setup it is necessary to use following definitions:
4.1 Introduction
4.2 Definitions
5.1 Definitions
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
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
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
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.1 Definitions
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
7.1 Definitions
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
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.
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.
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.
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
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
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
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
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
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
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.1 Definitions
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