SPI Lcd Library
The mikroPascal PRO for PIC provides a library for communication with Lcd (with HD44780 compliant controllers) in 4-bit mode via SPI interface.
For creating a custom set of Lcd characters use Lcd Custom Character Tool.
Important :
- The library uses the SPI module for communication. The user must initialize the SPI module before using the SPI Lcd Library.
- This Library is designed to work with the mikroElektronika's Serial Lcd Adapter Board pinout. See schematic at the bottom of this page for details.
Library Dependency Tree
External dependencies of SPI LcdLibrary
The implementation of SPI Lcd Library routines is based on Port Expander Library routines.
External dependencies are the same as Port Expander Library external dependencies.
Library Routines
SPI_Lcd_Config
| Prototype |
procedure SPI_Lcd_Config(DeviceAddress: byte); |
|---|---|
| Returns |
Nothing. |
| Description |
Initializes the Lcd module via SPI interface. Parameters :
|
| Requires |
Global variables :
|
| Example |
// port expander pinout definition
var SPExpanderCS : sbit at RC1_bit;
SPExpanderRST : sbit at RC0_bit;
SPExpanderCS_Direction : sbit at TRISC1_bit;
SPExpanderRST_Direction : sbit at TRISC_bit;
// If Port Expander Library uses SPI1 module
SPI1_Init(); // Initialize SPI module used with PortExpander
SPI_Lcd_Config(0); // initialize Lcd over SPI interface
|
SPI_Lcd_Out
| Prototype |
procedure SPI_Lcd_Out(row: byte; column: byte; var text: string); |
|---|---|
| Returns |
Nothing. |
| Description |
Prints text on the Lcd starting from specified position. Both string variables and literals can be passed as a text. Parameters :
|
| Requires |
Lcd needs to be initialized for SPI communication, see SPI_Lcd_Config routines. |
| Example |
// Write text "Hello!" on Lcd starting from row 1, column 3: SPI_Lcd_Out(1, 3, "Hello!"); |
SPI_Lcd_Out_CP
| Prototype |
procedure SPI_Lcd_Out_CP(var text : string); |
|---|---|
| Returns |
Nothing. |
| Description |
Prints text on the Lcd at current cursor position. Both string variables and literals can be passed as a text. Parameters :
|
| Requires |
Lcd needs to be initialized for SPI communication, see SPI_Lcd_Config routines. |
| Example |
// Write text "Here!" at current cursor position:
SPI_Lcd_Out_CP("Here!");
|
SPI_Lcd_Chr
| Prototype |
procedure SPI_Lcd_Chr(Row : byte; Column : byte; Out_Char : byte); |
|---|---|
| Returns |
Nothing. |
| Description |
Prints character on Lcd at specified position. Both variables and literals can be passed as character. Parameters :
|
| Requires |
Lcd needs to be initialized for SPI communication, see SPI_Lcd_Config routines. |
| Example |
// Write character "i" at row 2, column 3: SPI_Lcd_Chr(2, 3, 'i'); |
SPI_Lcd_Chr_CP
| Prototype |
procedure SPI_Lcd_Chr_CP(Out_Char : byte); |
|---|---|
| Returns |
Nothing. |
| Description |
Prints character on Lcd at current cursor position. Both variables and literals can be passed as character. Parameters :
|
| Requires |
Lcd needs to be initialized for SPI communication, see SPI_Lcd_Config routines. |
| Example |
// Write character "e" at current cursor position:
SPI_Lcd_Chr_CP('e');
|
SPI_Lcd_Cmd
| Prototype |
procedure SPI_Lcd_Cmd(out_char : byte); |
|---|---|
| Returns |
Nothing. |
| Description |
Sends command to Lcd. Parameters :
Note :
Predefined constants can be passed to the function, see Available SPI Lcd Commands.
|
| Requires |
Lcd needs to be initialized for SPI communication, see SPI_Lcd_Config routines. |
| Example |
// Clear Lcd display: SPI_Lcd_Cmd(_LCD_CLEAR); |
Available SPI Lcd Commands
| SPI Lcd Command | Purpose |
|---|---|
| _LCD_FIRST_ROW | Move cursor to the 1st row |
| _LCD_SECOND_ROW | Move cursor to the 2nd row |
| _LCD_THIRD_ROW | Move cursor to the 3rd row |
| _LCD_FOURTH_ROW | Move cursor to the 4th row |
| _LCD_CLEAR | Clear display |
| _LCD_RETURN_HOME | Return cursor to home position, returns a shifted display to its original position. Display data RAM is unaffected. |
| _LCD_CURSOR_OFF | Turn off cursor |
| _LCD_UNDERLINE_ON | Underline cursor on |
| _LCD_BLINK_CURSOR_ON | Blink cursor on |
| _LCD_MOVE_CURSOR_LEFT | Move cursor left without changing display data RAM |
| _LCD_MOVE_CURSOR_RIGHT | Move cursor right without changing display data RAM |
| _LCD_TURN_ON | Turn Lcd display on |
| _LCD_TURN_OFF | Turn Lcd display off |
| _LCD_SHIFT_LEFT | Shift display left without changing display data RAM |
| _LCD_SHIFT_RIGHT | Shift display right without changing display data RAM |
Library Example
This example demonstrates how to communicate Lcd via the SPI module, using serial to parallel convertor MCP23S17.
program Spi_Lcd;
var text : string[16];
// Port Expander module connections
var SPExpanderRST : sbit at RC0_bit;
SPExpanderCS : sbit at RC1_bit;
SPExpanderRST_Direction : sbit at TRISC0_bit;
SPExpanderCS_Direction : sbit at TRISC1_bit;
// End Port Expander module connections
begin
ANSEL := 0; // Configure AN pins as digital
ANSELH := 0;
C1ON_bit := 0; // Disable comparators
C2ON_bit := 0;
text := 'mikroElektronika';
// If Port Expander Library uses SPI1 module
SPI1_Init(); // Initialize SPI module used with PortExpander
// // If Port Expander Library uses SPI2 module
// SPI2_Init(); // Initialize SPI module used with PortExpander
SPI_Lcd_Config(0); // Initialize Lcd over SPI interface
SPI_Lcd_Cmd(_LCD_CLEAR); // Clear display
SPI_Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
SPI_Lcd_Out(1,6, 'mikroE'); // Print text to Lcd, 1st row, 6th column
SPI_Lcd_Chr_CP('!'); // Append '!'
SPI_Lcd_Out(2,1, text); // Print text to Lcd, 2nd row, 1st column
// SPI_Lcd_Out(3,1,"mikroE"); // For Lcd with more than two rows
// SPI_Lcd_Out(4,15,"mikroE"); // For Lcd with more than two rows
end.
HW Connection

SPI Lcd HW connection
What do you think about this topic ? Send us feedback!




