SPI Graphic Lcd Library

The mikroPascal PRO for 8051 provides a library for operating Graphic Lcd 128x64 (with commonly used Samsung KS108/KS107 controller) via SPI interface.

For creating a custom set of Glcd images use Glcd Bitmap Editor Tool.

Notes:

External dependencies of SPI Graphic LcdLibrary

The implementation of SPI Graphic Lcd Library routines is based on Port Expander Library routines.

External dependencies are the same as Port Expander Library external dependencies.

Library Routines

Basic routines:

Advanced routines:

SPI_Glcd_Init

Prototype

procedure SPI_Glcd_Init(DeviceAddress : byte);

Returns

Nothing.

Description

Initializes the Glcd module via SPI interface.

Parameters :

  • DeviceAddress: spi expander hardware address, see schematic at the bottom of this page

Requires

SPExpanderCS and SPExpanderRST variables 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 pinout definition
var SPExpanderRST : sbit at P1_0_bit;
    SPExpanderCS : sbit at P1_1_bit;
...

// If Port Expander Library uses SPI1 module :
SPI1_Init();                       // Initialize SPI module used with PortExpander
SPI_Glcd_Init(0);

SPI_Glcd_Set_Side

Prototype

procedure SPI_Glcd_Set_Side(x_pos : byte);

Returns

Nothing.

Description

Selects Glcd side. Refer to the Glcd datasheet for detail explanation.

Parameters :

  • x_pos: position on x-axis. Valid values: 0..127

The parameter x_pos specifies the Glcd side: values from 0 to 63 specify the left side, values from 64 to 127 specify the right side.

Note: For side, x axis and page layout explanation see schematic at the bottom of this page.

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Example

The following two lines are equivalent, and both of them select the left side of Glcd:

SPI_Glcd_Set_Side(0);
SPI_Glcd_Set_Side(10);

SPI_Glcd_Set_Page

Prototype

procedure SPI_Glcd_Set_Page(page : byte);

Returns

Nothing.

Description

Selects page of Glcd.

Parameters :

  • page: page number. Valid values: 0..7

Note: For side, x axis and page layout explanation see schematic at the bottom of this page.

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Example
SPI_Glcd_Set_Page(5);

SPI_Glcd_Set_X

Prototype

procedure SPI_Glcd_Set_X(x_pos : byte);

Returns

Nothing.

Description

Sets x-axis position to x_pos dots from the left border of Glcd within the selected side.

Parameters :

  • x_pos: position on x-axis. Valid values: 0..63

Note: For side, x axis and page layout explanation see schematic at the bottom of this page.

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Example
SPI_Glcd_Set_X(25);

SPI_Glcd_Read_Data

Prototype

function SPI_Glcd_Read_Data() : byte;

Returns

One byte from Glcd memory.

Description

Reads data from the current location of Glcd memory and moves to the next location.

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Glcd side, x-axis position and page should be set first. See the functions SPI_Glcd_Set_Side, SPI_Glcd_Set_X, and SPI_Glcd_Set_Page.

Example
var data : byte;
...
data := SPI_Glcd_Read_Data();

SPI_Glcd_Write_Data

Prototype

procedure SPI_Glcd_Write_Data(ddata : byte);

Returns

Nothing.

Description

Writes one byte to the current location in Glcd memory and moves to the next location.

Parameters :

  • Ddata: data to be written

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Glcd side, x-axis position and page should be set first. See the functions SPI_Glcd_Set_Side, SPI_Glcd_Set_X, and SPI_Glcd_Set_Page.

Example
var ddata : byte;
...
SPI_Glcd_Write_Data(ddata);

SPI_Glcd_Fill

Prototype

procedure SPI_Glcd_Fill(pattern: byte);

Returns

Nothing.

Description

Fills Glcd memory with byte pattern.

Parameters :

  • pattern: byte to fill Glcd memory with

To clear the Glcd screen, use SPI_Glcd_Fill(0).

To fill the screen completely, use SPI_Glcd_Fill(0xFF).

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Example
// Clear screen
SPI_Glcd_Fill(0);

SPI_Glcd_Dot

Prototype

procedure SPI_Glcd_Dot(x_pos : byte; y_pos : byte; color : byte);

Returns

Nothing.

Description

Draws a dot on Glcd at coordinates (x_pos, y_pos).

Parameters :

  • x_pos: x position. Valid values: 0..127
  • y_pos: y position. Valid values: 0..63
  • color: color parameter. Valid values: 0..2

The parameter color determines the dot state: 0 clears dot, 1 puts a dot, and 2 inverts dot state.

Note: For x and y axis layout explanation see schematic at the bottom of this page.

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Example
// Invert the dot in the upper left corner
SPI_Glcd_Dot(0, 0, 2);

SPI_Glcd_Line

Prototype

procedure SPI_Glcd_Line(x_start : integer; y_start : integer; x_end : integer; y_end : integer; color : byte);

Returns

Nothing.

Description

Draws a line on Glcd.

Parameters :

  • x_start: x coordinate of the line start. Valid values: 0..127
  • y_start: y coordinate of the line start. Valid values: 0..63
  • x_end: x coordinate of the line end. Valid values: 0..127
  • y_end: y coordinate of the line end. Valid values: 0..63
  • color: color parameter. Valid values: 0..2

Parameter color determines the line color: 0 white, 1 black, and 2 inverts each dot.

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Example
// Draw a line between dots (0,0) and (20,30)
SPI_Glcd_Line(0, 0, 20, 30, 1);

SPI_Glcd_V_Line

Prototype

procedure SPI_Glcd_V_Line(y_start: byte; y_end: byte; x_pos: byte; color: byte);

Returns

Nothing.

Description

Draws a vertical line on Glcd.

Parameters :

  • y_start: y coordinate of the line start. Valid values: 0..63
  • y_end: y coordinate of the line end. Valid values: 0..63
  • x_pos: x coordinate of vertical line. Valid values: 0..127
  • color: color parameter. Valid values: 0..2

Parameter color determines the line color: 0 white, 1 black, and 2 inverts each dot.

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Example
// Draw a vertical line between dots (10,5) and (10,25)
SPI_Glcd_V_Line(5, 25, 10, 1);

SPI_Glcd_H_Line

Prototype

procedure SPI_Glcd_V_Line(x_start : byte; x_end : byte; y_pos : byte; color : byte);

Returns

Nothing.

Description

Draws a horizontal line on Glcd.

Parameters :

  • x_start: x coordinate of the line start. Valid values: 0..127
  • x_end: x coordinate of the line end. Valid values: 0..127
  • y_pos: y coordinate of horizontal line. Valid values: 0..63
  • color: color parameter. Valid values: 0..2

The parameter color determines the line color: 0 white, 1 black, and 2 inverts each dot.

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Example
// Draw a horizontal line between dots (10,20) and (50,20)
SPI_Glcd_H_Line(10, 50, 20, 1);

SPI_Glcd_Rectangle

Prototype

procedure SPI_Glcd_Rectangle(x_upper_left : byte; y_upper_left : byte; x_bottom_right : byte; y_bottom_right : byte; color : byte);

Returns

Nothing.

Description

Draws a rectangle on Glcd.

Parameters :

  • x_upper_left: x coordinate of the upper left rectangle corner. Valid values: 0..127
  • y_upper_left: y coordinate of the upper left rectangle corner. Valid values: 0..63
  • x_bottom_right: x coordinate of the lower right rectangle corner. Valid values: 0..127
  • y_bottom_right: y coordinate of the lower right rectangle corner. Valid values: 0..63
  • color: color parameter. Valid values: 0..2

The parameter color determines the color of the rectangle border: 0 white, 1 black, and 2 inverts each dot.

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Example
// Draw a rectangle between dots (5,5) and (40,40)
SPI_Glcd_Rectangle(5, 5, 40, 40, 1);

SPI_Glcd_Box

Prototype

procedure SPI_Glcd_Box(x_upper_left : byte; y_upper_left : byte; x_bottom_right : byte; y_bottom_right : byte; color : byte);

Returns

Nothing.

Description

Draws a box on Glcd.

Parameters :

  • x_upper_left: x coordinate of the upper left box corner. Valid values: 0..127
  • y_upper_left: y coordinate of the upper left box corner. Valid values: 0..63
  • x_bottom_right: x coordinate of the lower right box corner. Valid values: 0..127
  • y_bottom_right: y coordinate of the lower right box corner. Valid values: 0..63
  • color: color parameter. Valid values: 0..2

The parameter color determines the color of the box fill: 0 white, 1 black, and 2 inverts each dot.

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Example
// Draw a box between dots (5,15) and (20,40)
SPI_Glcd_Box(5, 15, 20, 40, 1);

SPI_Glcd_Circle

Prototype

procedure SPI_Glcd_Circle(x_center : integer; y_center : integer; radius : integer; color : byte);

Returns

Nothing.

Description

Draws a circle on Glcd.

Parameters :

  • x_center: x coordinate of the circle center. Valid values: 0..127
  • y_center: y coordinate of the circle center. Valid values: 0..63
  • radius: radius size
  • color: color parameter. Valid values: 0..2

The parameter color determines the color of the circle line: 0 white, 1 black, and 2 inverts each dot.

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine.

Example
// Draw a circle with center in (50,50) and radius=10
SPI_Glcd_Circle(50, 50, 10, 1);

SPI_Glcd_Set_Font

Prototype

procedure SPI_Glcd_Set_Font(activeFont : longint; aFontWidth : byte; aFontHeight : byte; aFontOffs : word);

Returns

Nothing.

Description

Sets font that will be used with SPI_Glcd_Write_Char and SPI_Glcd_Write_Text routines.

Parameters :

  • activeFont: font to be set. Needs to be formatted as an array of char
  • aFontWidth: width of the font characters in dots.
  • aFontHeight: height of the font characters in dots.
  • aFontOffs: number that represents difference between the mikroPascal PRO for 8051 character set and regular ASCII set (eg. if 'A' is 65 in ASCII character, and 'A' is 45 in the mikroPascal PRO for 8051 character set, aFontOffs is 20). Demo fonts supplied with the library have an offset of 32, which means that they start with space.

The user can use fonts given in the file __Lib_GLCDFonts file located in the Uses folder or create his own fonts.

List of supported fonts:
  • Font_Glcd_System3x5
  • Font_Glcd_System5x7
  • Font_Glcd_5x7
  • Font_Glcd_Character8x7

For the sake of the backward compatibility, these fonts are supported also:

  • System3x5 (equivalent to Font_Glcd_System3x5)
  • FontSystem5x7_v2 (equivalent to Font_Glcd_System5x7)
  • font5x7 (equivalent to Font_Glcd_5x7)
  • Character8x7 (equivalent to Font_Glcd_Character8x7)
Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Example
// Use the custom 5x7 font "myfont" which starts with space (32):
SPI_Glcd_Set_Font(@myfont, 5, 7, 32);

SPI_Glcd_Write_Char

Prototype

procedure SPI_Glcd_Write_Char(chr1 : byte; x_pos : byte; page_num : byte; color : byte);

Returns

Nothing.

Description

Prints character on Glcd.

Parameters :

  • chr1: character to be written
  • x_pos: character starting position on x-axis. Valid values: 0..(127-FontWidth)
  • page_num: the number of the page on which character will be written. Valid values: 0..7
  • color: color parameter. Valid values: 0..2

The parameter color determines the color of the character: 0 white, 1 black, and 2 inverts each dot.

Note: For x axis and page layout explanation see schematic at the bottom of this page.

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Use the SPI_Glcd_Set_Font to specify the font for display; if no font is specified, then the default Font_Glcd_System5x7 font supplied with the library will be used.

Example
// Write character 'C' on the position 10 inside the page 2:
SPI_Glcd_Write_Char("C", 10, 2, 1);

SPI_Glcd_Write_Text

Prototype

procedure SPI_Glcd_Write_Text(var text : string; x_pos : byte; page_numb : byte; color : byte);

Returns

Nothing.

Description

Prints text on Glcd.

Parameters :

  • text: text to be written
  • x_pos: text starting position on x-axis.
  • page_num: the number of the page on which text will be written. Valid values: 0..7
  • color: color parameter. Valid values: 0..2

The parameter color determines the color of the text: 0 white, 1 black, and 2 inverts each dot.

Note: For x axis and page layout explanation see schematic at the bottom of this page.

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Use the SPI_Glcd_Set_Font to specify the font for display; if no font is specified, then the default Font_Glcd_System5x7 font supplied with the library will be used.

Example
// Write text "Hello world!" on the position 10 inside the page 2:
SPI_Glcd_Write_Text("Hello world!", 10, 2, 1);

SPI_Glcd_Image

Prototype

procedure SPI_Glcd_Image(const image : ^byte);

Returns

Nothing.

Description

Displays bitmap on Glcd.

Parameters :

  • image: image to be displayed. Bitmap array can be located in both code and RAM memory (due to the mikroPascal PRO for 8051 pointer to const and pointer to RAM equivalency).

Use the mikroPascal PRO for 8051 integrated Glcd Bitmap Editor (menu option Tools › Glcd Bitmap Editor) to convert image to a constant array suitable for displaying on Glcd.

Requires

Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines.

Example
// Draw image my_image on Glcd
SPI_Glcd_Image(my_image);

Library Example

The example demonstrates how to communicate to KS0108 Glcd via the SPI module, using serial to parallel convertor MCP23S17.

program SPI_Lcd;

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

// Port Expander module connections
var SPExpanderRST : sbit at P1_0_bit;
var SPExpanderCS  : sbit at P1_1_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 := 'mikroElektronika';
    SPI1_Init();                         // Initialize SPI
    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
    
    // Moving text
    for counter := 0 to 3 do             // Move text to the right 4 times
      begin
        SPI_Lcd_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_Lcd_Cmd(_LCD_SHIFT_LEFT);
            Move_Delay();
          end;

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

      end;
  end.

HW Connection

SPI Glcd HW connection

SPI Glcd HW connection

Copyright (c) 2002-2013 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