SPI Lcd8 (8-bit interface) Library

The mikroPascal PRO for dsPIC30/33 and PIC24 provides a library for communication with Lcd (with HD44780 compliant controllers) in 8-bit mode via SPI interface.

For creating a custom set of Lcd characters use Lcd Custom Character Tool.

  Important :

Library Dependency Tree

SPI Lcd8 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_Lcd8_Config

Prototype

procedure SPI_Lcd8_Config(DeviceAddress : byte);

Description

Initializes the Lcd module via SPI interface.

Parameters
  • DeviceAddress: SPI expander hardware address, see schematic at the bottom of this page
Returns

Nothing.

Requires Global variables :
  • SPExpanderCS: Chip Select line
  • SPExpanderRST: Reset line
  • SPExpanderCS_Direction: Direction of the Chip Select pin
  • SPExpanderRST_Direction: Direction of the Reset pin
must be defined before using this function.


The SPI module needs to be initialized. See SPIx_Init and SPIx_Init_Advanced routines.

Example
// Port Expander module connections
var SPExpanderRST : sbit at LATF0_bit;
var SPExpanderCS  : sbit at LATF1_bit;
var SPExpanderRST_Direction : sbit at TRISF0_bit;
var SPExpanderCS_Direction  : sbit at TRISF1_bit;
// End Port Expander module connections
...
// If Port Expander Library uses SPI1 module
SPI1_Init();                                // Initialize SPI module used with PortExpander
SPI_Lcd8_Config(0);                         // intialize Lcd in 8bit mode via SPI
Notes

None.

SPI_Lcd8_Out

Prototype

procedure SPI_Lcd8_Out(row, column: byte; var text: string);

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

Lcd needs to be initialized for SPI communication, see SPI_Lcd8_Config routine.

Example
// Write text "Hello!" on Lcd starting from row 1, column 3:
SPI_Lcd8_Out(1, 3, 'Hello!');
Notes

None.

SPI_Lcd8_Out_Cp

Prototype

procedure SPI_Lcd8_Out_CP(var text: string);

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
Returns

Nothing.

Requires

Lcd needs to be initialized for SPI communication, see SPI_Lcd8_Config routine.

Example
// Write text "Here!" at current cursor position:
SPI_Lcd8_Out_Cp('Here!');
Notes

None.

SPI_Lcd8_Chr

Prototype

procedure SPI_Lcd8_Chr(row, column, out_char: byte);

Description

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

Parameters
  • row: writing position row number
  • column: writing position column number
  • out_char: character to be written
Returns

Nothing.

Requires

Lcd needs to be initialized for SPI communication, see SPI_Lcd8_Config routine.

Example
// Write character "i" at row 2, column 3:
SPI_Lcd8_Chr(2, 3, 'i');
Notes

None.

SPI_Lcd8_Chr_Cp

Prototype

procedure SPI_Lcd8_Chr_CP(out_char: byte);

Description

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

Parameters
  • out_char: character to be written
Returns

Nothing.

Requires

Lcd needs to be initialized for SPI communication, see SPI_Lcd8_Config routine.

Example

Print “e” at current cursor position:

// Write character "e" at current cursor position:
SPI_Lcd8_Chr_Cp('e');
Notes

None.

SPI_Lcd8_Cmd

Prototype

procedure SPI_Lcd8_Cmd(out_char: byte);

Description

Sends command to Lcd.

Parameters
  • out_char: command to be sent
Returns

Nothing.

Requires

Lcd needs to be initialized for SPI communication, see SPI_Lcd8_Config routine.

Example
// Clear Lcd display:
SPI_Lcd8_Cmd(_LCD_CLEAR);
Notes

Predefined constants can be passed to the routine, see Available SPI Lcd8 Commands.

Available SPI Lcd8 Commands

SPI Lcd8 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 in 8-bit mode via the SPI module, using serial to parallel convertor MCP23S17.

Copy Code To ClipboardCopy Code To Clipboard
program Spi_Lcd8;

var text : array[16] of char;
var counter : byte;

// Port Expander module connections
var SPExpanderRST : sbit at LATF0_bit;
var SPExpanderCS  : sbit at LATF1_bit;
var SPExpanderRST_Direction : sbit at TRISF0_bit;
var SPExpanderCS_Direction  : sbit at TRISF1_bit;
// End Port Expander module connections

procedure Move_Delay();                  // Function used for text moving
  begin
    Delay_ms(500);                       // You can change the moving speed here
  end;

  begin
    text := 'mikroE';

    SPI1_Init();                               // Initialize SPI interface

    //  If Port Expander Library uses SPI2 module
    //  SPI2_Init();                           // Initialize SPI module used with PortExpander
    Spi_Lcd8_Config(0);                        // Intialize LCD in 8bit mode via SPI
    Spi_Lcd8_Cmd(_LCD_CLEAR);                  // Clear display
    Spi_Lcd8_Cmd(_LCD_CURSOR_OFF);             // Turn cursor off
    Spi_Lcd8_Out(1,6, text);                   // Print text to LCD, 1st row, 6th column...
    Spi_Lcd8_Chr_CP('!');                      // Append '!'
    Spi_Lcd8_Out(2,1, 'mikroelektronika');     // Print text to LCD, 2nd row, 1st column...

    //    Spi_Lcd8_Out(3,1, text);             // For LCD modules with more than two rows
    //    Spi_Lcd8_Out(4,15, text);            // For LCD modules with more than two rows

    Delay_ms(2000);

    // Moving text
    for counter := 0 to 3 do                   // Move text to the right 4 times
      begin
        Spi_Lcd8_Cmd(_LCD_SHIFT_RIGHT);
        Move_Delay();
      end;

    while TRUE do                              // Endless loop
      begin
        for counter := 0 to 6 do               // Move text to the left 7 times
          begin
            Spi_Lcd8_Cmd(_LCD_SHIFT_LEFT);
            Move_Delay();
          end;

        for counter := 0 to 6 do               // Move text to the right 7 times
          begin
            Spi_Lcd8_Cmd(_LCD_SHIFT_RIGHT);
            Move_Delay();
          end;

      end;
  end.

SPI Lcd8 HW connection

SPI Lcd8 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