SPI Lcd Library
The mikroBasic PRO for ARM 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 :
- When using this library with ARM family MCUs be aware of their voltage incompatibility with certain number of Lcd modules.
So, additional external power supply for these modules may be required. - Library uses the SPI module for communication. The user must initialize the appropriate SPI module before using the SPI Lcd Library.
- For MCUs with multiple SPI modules it is possible to initialize all of them and then switch by using the
SPI_Set_Active()routine. See the SPI Library functions. - 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 Lcd Library
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 |
sub procedure SPI_Lcd_Config(dim DeviceAddress as byte) |
|---|---|
| Description |
Initializes the Lcd module via SPI interface. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
External dependencies are the same as Port Expander Library external dependencies.
|
| Example |
Stellaris' Port Expander module connections dim SPExpanderRST as sbit at GPIO_PORTA_DATA0_bit dim SPExpanderCS as sbit at GPIO_PORTA_DATA1_bit dim SPExpanderRST_Direction as sbit at GPIO_PORTA_DIR0_bit dim SPExpanderCS_Direction as sbit at GPIO_PORTA_DIR1_bit ' End Port Expander module connections ... ' If Port Expander Library uses SPI1 module SPI1_Init() ' Initialize SPI module used with PortExpander SPI_Lcd_Config(0) ' initialize lcd over spi interface STM32
' Port Expander module connections
dim SPExpanderRST as sbit at GPIOB_ODR.B0
SPExpanderCS as sbit at GPIOB_ODR.B1
' End Port Expander module connections
...
' If Port Expander Library uses SPI1 module
SPI1_Init() ' Initialize SPI module used with PortExpander
SPI_Lcd_Config(0) ' initialize lcd over spi interface
|
| Notes |
None. |
SPI_Lcd_Out
| Prototype |
sub procedure SPI_Lcd_Out(dim row, column as byte, dim byref text as string) |
|---|---|
| Description |
Prints text on the Lcd starting from specified position. Both string variables and literals can be passed as a text. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Lcd needs to be initialized for SPI communication, see SPI_Lcd_Config routine. |
| Example |
' Write text "Hello!" on Lcd starting from row 1, column 3: SPI_Lcd_Out(1, 3, "Hello!") |
| Notes |
None. |
SPI_Lcd_Out_Cp
| Prototype |
sub procedure SPI_Lcd_Out_CP(dim byref text as string) |
|---|---|
| Description |
Prints text on the Lcd at current cursor position. Both string variables and literals can be passed as a text. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Lcd needs to be initialized for SPI communication, see SPI_Lcd_Config routine. |
| Example |
' Write text "Here!" at current cursor position:
SPI_Lcd_Out_CP("Here!")
|
| Notes |
None. |
SPI_Lcd_Chr
| Prototype |
sub procedure SPI_Lcd_Chr(dim Row, Column, Out_Char as byte) |
|---|---|
| Description |
Prints character on Lcd at specified position. Both variables and literals can be passed as character. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Lcd needs to be initialized for SPI communication, see SPI_Lcd_Config routine. |
| Example |
' Write character "i" at row 2, column 3:
SPI_Lcd_Chr(2, 3, "i")
|
| Notes |
None. |
SPI_Lcd_Chr_Cp
| Prototype |
sub procedure SPI_Lcd_Chr_CP(dim Out_Char as byte) |
|---|---|
| Description |
Prints character on Lcd at current cursor position. Both variables and literals can be passed as character. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Lcd needs to be initialized for SPI communication, see SPI_Lcd_Config routine. |
| Example |
' Write character "e" at current cursor position:
SPI_Lcd_Chr_Cp("e")
|
| Notes |
None. |
SPI_Lcd_Cmd
| Prototype |
sub procedure SPI_Lcd_Cmd(dim out_char as byte) |
|---|---|
| Description |
Sends command to Lcd. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Lcd needs to be initialized for SPI communication, see SPI_Lcd_Config routine. |
| Example |
' Clear Lcd display: SPI_Lcd_Cmd(_LCD_CLEAR) |
| Notes |
Predefined constants can be passed to the routine, see Available SPI Lcd Commands. |
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
Default Pin Configuration
Use SPI_Lcd_Init for default pin settings (see the first figure below).
Stellaris
program Spi_Lcd
dim text as char[16]
dim counter as byte
' Port Expander module connections
dim SPExpanderRST as sbit at GPIO_PORTA_DATA0_bit
dim SPExpanderCS as sbit at GPIO_PORTA_DATA1_bit
dim SPExpanderRST_Direction as sbit at GPIO_PORTA_DIR0_bit
dim SPExpanderCS_Direction as sbit at GPIO_PORTA_DIR1_bit
' End Port Expander module connections
sub procedure Move_Delay() ' Function used for text moving
Delay_ms(500) ' You can change the moving speed here
end sub
main:
text = "mikroElektronika"
' Initialize SPI module used with PortExpander
SPI0_Init()
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
Delay_ms(2000)
' 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
' Moving text
for counter = 0 to 3 ' Move text to the right 4 times
Spi_Lcd_Cmd(_LCD_SHIFT_RIGHT)
Move_Delay()
next counter
while TRUE ' Endless loop
for counter = 0 to 6 ' Move text to the left 7 times
Spi_Lcd_Cmd(_LCD_SHIFT_LEFT)
Move_Delay()
next counter
for counter = 0 to 6 ' Move text to the right 7 times
Spi_Lcd_Cmd(_LCD_SHIFT_RIGHT)
Move_Delay()
next counter
wend
end.
STM32
program Spi_Lcd
dim text as char[16]
dim counter as byte
' Port Expander module connections
dim SPExpanderRST as sbit at GPIOB_ODR.B0
dim SPExpanderCS as sbit at GPIOB_ODR.B1
' End Port Expander module connections
sub procedure Move_Delay() ' Function used for text moving
Delay_ms(500) ' You can change the moving speed here
end sub
main:
text = "mikroElektronika"
' Initialize SPI module used with PortExpander
SPI1_Init_Advanced(_SPI_FPCLK_DIV4, _SPI_MASTER or _SPI_8_BIT or
_SPI_CLK_IDLE_LOW or _SPI_FIRST_CLK_EDGE_TRANSITION or
_SPI_MSB_FIRST or _SPI_SS_DISABLE or _SPI_SSM_ENABLE or _SPI_SSI_1,
@_GPIO_MODULE_SPI1_PB345)
GPIO_Alternate_Function_Enable(@_GPIO_MODULE_SWJ_JTAGDISABLE)
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
Delay_ms(2000)
' 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
' Moving text
for counter = 0 to 3 ' Move text to the right 4 times
Spi_Lcd_Cmd(_LCD_SHIFT_RIGHT)
Move_Delay()
next counter
while TRUE ' Endless loop
for counter = 0 to 6 ' Move text to the left 7 times
Spi_Lcd_Cmd(_LCD_SHIFT_LEFT)
Move_Delay()
next counter
for counter = 0 to 6 ' Move text to the right 7 times
Spi_Lcd_Cmd(_LCD_SHIFT_RIGHT)
Move_Delay()
next counter
wend
end.
What do you think about this topic ? Send us feedback!




