Lcd Library

The mikroC 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

Lcd Library Dependency Tree

External dependencies of Lcd Library

The following variables must be defined in all projects using Lcd Library : Description : Example :
extern sfr atomic sbit LCD_RS: Register Select line. sbit LCD_RS at LATD0_bit;
extern sfr atomic sbit LCD_EN: Enable line. sbit LCD_EN at LATD1_bit;
extern sfr atomic sbit LCD_D4; Data 4 line. sbit LCD_D4 at LATB0_bit;
extern sfr atomic sbit LCD_D5; Data 5 line. sbit LCD_D5 at LATB1_bit;
extern sfr atomic sbit LCD_D6; Data 6 line. sbit LCD_D6 at LATB2_bit;
extern sfr atomic sbit LCD_D7; Data 7 line. sbit LCD_D7 at LATB3_bit
extern sfr atomic sbit LCD_RS_Direction; Register Select direction pin. sbit LCD_RS_Direction at TRISD0_bit;
extern sfr atomic sbit LCD_EN_Direction; Enable direction pin. sbit LCD_EN_Direction at TRISD1_bit;
extern sfr atomic sbit LCD_D4_Direction; Data 4 direction pin. sbit LCD_D4_Direction at TRISB0_bit;
extern sfr atomic sbit LCD_D5_Direction; Data 5 direction pin. sbit LCD_D5_Direction at TRISB1_bit;
extern sfr atomic sbit LCD_D6_Direction; Data 6 direction pin. sbit LCD_D6_Direction at TRISB2_bit;
extern sfr atomic sbit LCD_D7_Direction; Data 7 direction pin. sbit LCD_D7_Direction at TRISB3_bit;

Library Routines

Lcd_Init

Prototype

void Lcd_Init();

Description

Initializes Lcd module.

Parameters

None.

Returns

Nothing.

Requires

Global variables:

  • LCD_RS: Register Select (data/instruction) signal pin
  • LCD_EN: Enable signal pin
  • LCD_D4: Data bit 4
  • LCD_D5: Data bit 5
  • LCD_D6: Data bit 6
  • LCD_D7: Data bit 7

  • LCD_RS_Direction: Direction of the Register Select pin
  • LCD_EN_Direction: Direction of the Enable signal pin
  • LCD_D4_Direction: Direction of the Data 4 pin
  • LCD_D5_Direction: Direction of the Data 5 pin
  • LCD_D6_Direction: Direction of the Data 6 pin
  • LCD_D7_Direction: Direction of the Data 7 pin
must be defined before using this function.

Example
// Lcd module connections
sbit LCD_RS at LATD0_bit;
sbit LCD_EN at LATD1_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;

sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End Lcd module connections
...

Lcd_Init();
Notes

None.

Lcd_Out

Prototype

void Lcd_Out(unsigned int row, unsigned int column, char *text);

Description

Prints text on Lcd starting from specified position. Both string variables and literals can be passed as a text.

Parameters
  • row: starting position row number
  • column: starting position column number
  • text: text to be written
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

void Lcd_Out_Cp(char *text);

Returns

Nothing.

Description

Prints text on Lcd at current cursor position. Both string variables and literals can be passed as a text.

Parameters
  • text: text to be written
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

void Lcd_Chr(unsigned int row, unsigned int column, char out_char);

Description

Prints character on Lcd at specified position. Both variables and literals can be passed as a character.

Parameters
  • row: writing position row number
  • column: writing position column number
  • out_char: character to be written
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

void Lcd_Chr_Cp(char out_char);

Description

Prints character on Lcd at current cursor position. Both variables and literals can be passed as a character.

Parameters
  • out_char: character to be written
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

void Lcd_Cmd(char out_char);

Description

Sends command to Lcd.

Parameters
  • out_char: command to be sent
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:

Copy Code To ClipboardCopy Code To Clipboard
// LCD module connections
sbit LCD_RS at LATB2_bit;
sbit LCD_EN at LATB3_bit;
sbit LCD_D4 at LATB4_bit;
sbit LCD_D5 at LATB5_bit;
sbit LCD_D6 at LATB6_bit;
sbit LCD_D7 at LATB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

char txt1[] = "mikroElektronika";    
char txt2[] = "LV32MX v6";
char txt3[] = "Lcd4bit";
char txt4[] = "example";

char i;                              // Loop variable

void Move_Delay() {                  // Function used for text moving
  Delay_ms(750);                     // You can change the moving speed here
}

void main(){
  CHECON = 0x32;
  AD1PCFG  = 0xFFFF;                 // Configure AN pins as digital I/O

  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,5,txt2);                 // Write text in second row

  Delay_ms(2000);

  // Moving text
  for(i=0; i<4; i++) {               // Move text to the right 4 times
    Lcd_Cmd(_LCD_SHIFT_RIGHT);
    Move_Delay();
  }

  while(1) {                         // Endless loop
    for(i=0; i<8; i++) {             // Move text to the left 7 times
      Lcd_Cmd(_LCD_SHIFT_LEFT);
      Move_Delay();
    }

    for(i=0; i<8; i++) {             // Move text to the right 7 times
      Lcd_Cmd(_LCD_SHIFT_RIGHT);
      Move_Delay();
    }
  }
}

Lcd HW connection

Lcd HW connection

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code