Lcd Library
mikroBasic PRO for PIC32 provides a library for communication with Lcds (with HD44780 compliant controllers) through the 4-bit interface. An example of Lcd connections is given on the schematic at the bottom of this page.
For creating a set of custom Lcd characters use Lcd Custom Character Tool.
Library Dependency Tree

External dependencies of Lcd Library
The following variables must be defined in all projects using Lcd Library : | Description : | Example : |
---|---|---|
dim LCD_RS as sbit sfr atomic external |
Register Select line. | dim LCD_RS as sbit at LATD0_bit |
dim LCD_EN as sbit sfr atomic external |
Enable line. | dim LCD_EN as sbit at LATD1_bit |
dim LCD_D4 as sbit sfr atomic external |
Data 4 line. | dim LCD_D4 as sbit at LATB0_bit |
dim LCD_D5 as sbit sfr atomic external |
Data 5 line. | dim LCD_D5 as sbit at LATB1_bit |
dim LCD_D6 as sbit sfr atomic external |
Data 6 line. | dim LCD_D6 as sbit at LATB2_bit |
dim LCD_D7 as sbit sfr atomic external |
Data 7 line. | dim LCD_D7 as sbit at LATB3_bit |
dim LCD_RS_Direction as sbit sfr atomic external |
Register select direction pin. | dim LCD_RS_Direction as sbit at TRISD0_bit |
dim LCD_EN_Direction as sbit sfr atomic external |
Enable direction pin. | dim LCD_EN_Direction as sbit at TRISD1_bit |
dim LCD_D4_Direction as sbit sfr atomic external |
Data 4 direction pin. | dim LCD_D4_Direction as sbit at TRISB0_bit |
dim LCD_D5_Direction as sbit sfr atomic external |
Data 5 direction pin. | dim LCD_D5_Direction as sbit at TRISB1_bit |
dim LCD_D6_Direction as sbit sfr atomic external |
Data 6 direction pin. | dim LCD_D6_Direction as sbit at TRISB2_bit |
dim LCD_D7_Direction as sbit sfr atomic external |
Data 7 direction pin. | dim LCD_D7_Direction as sbit at TRISB3_bit |
Library Routines
Lcd_Init
Prototype |
sub procedure Lcd_Init() |
---|---|
Description |
Initializes Lcd module. |
Parameters |
None. |
Returns |
Nothing. |
Requires |
Global variables:
|
Example |
' LCD module connections dim LCD_RS as sbit at LATD0_bit dim LCD_EN as sbit at LATD1_bit dim LCD_D4 as sbit at LATB0_bit dim LCD_D5 as sbit at LATB1_bit dim LCD_D6 as sbit at LATB2_bit dim LCD_D7 as sbit at LATB3_bit dim LCD_RS_Direction as sbit at TRISD0_bit dim LCD_EN_Direction as sbit at TRISD1_bit dim LCD_D4_Direction as sbit at TRISB0_bit dim LCD_D5_Direction as sbit at TRISB1_bit dim LCD_D6_Direction as sbit at TRISB2_bit dim LCD_D7_Direction as sbit at TRISB3_bit ' End LCD module connections ... Lcd_Init() |
Notes |
None. |
Lcd_Out
Prototype |
sub procedure Lcd_Out(dim row, column as word, dim byref text as string) |
---|---|
Description |
Prints text on Lcd starting from specified position. Both string variables and literals can be passed as a text. |
Parameters |
|
Returns |
Nothing. |
Requires |
The Lcd module needs to be initialized. See Lcd_Init routine. |
Example |
' Write text "Hello!" on Lcd starting from row 1, column 3: Lcd_Out(1, 3, "Hello!") |
Notes |
None. |
Lcd_Out_Cp
Prototype |
sub procedure Lcd_Out_Cp(dim byref text as string) |
---|---|
Returns |
Nothing. |
Description |
Prints text on Lcd at current cursor position. Both string variables and literals can be passed as a text. |
Parameters |
|
Requires |
The Lcd module needs to be initialized. See Lcd_Init routine. |
Example |
' Write text "Here!" at current cursor position: Lcd_Out_Cp("Here!") |
Notes |
None. |
Lcd_Chr
Prototype |
sub procedure Lcd_Chr(dim row, column as word, dim out_char as byte) |
---|---|
Description |
Prints character on Lcd at specified position. Both variables and literals can be passed as a character. |
Parameters |
|
Returns |
Nothing. |
Requires |
The Lcd module needs to be initialized. See Lcd_Init routine. |
Example |
' Write character "i" at row 2, column 3: Lcd_Chr(2, 3, "i") |
Notes |
None. |
Lcd_Chr_Cp
Prototype |
sub procedure 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 a character. |
Parameters |
|
Returns |
Nothing. |
Requires |
The Lcd module needs to be initialized. See Lcd_Init routine. |
Example |
' Write character "e" at current cursor position: Lcd_Chr_Cp("e") |
Notes |
None. |
Lcd_Cmd
Prototype |
sub procedure Lcd_Cmd(dim out_char as byte) |
---|---|
Description |
Sends command to Lcd. |
Parameters |
|
Returns |
Nothing. |
Requires |
The Lcd module needs to be initialized. See Lcd_Init table. |
Example |
' Clear Lcd display: Lcd_Cmd(_LCD_CLEAR) |
Notes |
Predefined constants can be passed to the function, see Available Lcd Commands. |
Available Lcd Commands
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
The following code demonstrates usage of the Lcd Library routines:
program Lcd_Test
' LCD module connections
dim LCD_RS as sbit at LATB2_bit
dim LCD_EN as sbit at LATB3_bit
dim LCD_D4 as sbit at LATB4_bit
dim LCD_D5 as sbit at LATB5_bit
dim LCD_D6 as sbit at LATB6_bit
dim LCD_D7 as sbit at LATB7_bit
dim LCD_RS_Direction as sbit at TRISB2_bit
dim LCD_EN_Direction as sbit at TRISB3_bit
dim LCD_D4_Direction as sbit at TRISB4_bit
dim LCD_D5_Direction as sbit at TRISB5_bit
dim LCD_D6_Direction as sbit at TRISB6_bit
dim LCD_D7_Direction as sbit at TRISB7_bit
' End LCD module connections
dim txt1 as char[16]
txt2 as char[12]
txt3 as char[8]
txt4 as char[7]
i as byte ' Loop variable
sub procedure Move_Delay() ' Function used for text moving
Delay_ms(500) ' You can change the moving speed here
end sub
main:
CHECON = 0x32
AD1PCFG = 0xFFFF ' Configure AN pins as digital I/O
txt1 = "mikroElektronika"
txt2 = "LV32MX v6"
txt3 = "Lcd4bit"
txt4 = "example"
Lcd_Init() ' Initialize LCD
Lcd_Cmd(_LCD_CLEAR) ' Clear display
Lcd_Cmd(_LCD_CURSOR_OFF) ' Cursor off
LCD_Out(1,6,txt3) ' Write text in first row
LCD_Out(2,6,txt4) ' Write text in second row
Delay_ms(2000)
Lcd_Cmd(_LCD_CLEAR) ' Clear display
LCD_Out(1,1,txt1) ' Write text in first row
Lcd_Out(2,4,txt2) ' Write text in second row
Delay_ms(500)
' Moving text
for i = 0 to 3 ' Move text to the right 4 times
Lcd_Cmd(_LCD_SHIFT_RIGHT)
Move_Delay()
next i
while TRUE ' Endless loop
for i = 0 to 7 ' Move text to the left 7 times
Lcd_Cmd(_LCD_SHIFT_LEFT)
Move_Delay()
next i
for i = 0 to 7 ' Move text to the right 7 times
Lcd_Cmd(_LCD_SHIFT_RIGHT)
Move_Delay()
next i
wend
end.
Lcd HW connection
What do you think about this topic ? Send us feedback!