SPI Graphic Lcd Library
mikroPascal PRO for PIC32 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.
- When using this library with PIC32 family MCUs be aware of their voltage incompatibility with certain number of Samsung KS0108 based Glcd 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 Glcd 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/Glcd 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
Basic routines:
- SPI_Glcd_Init
- SPI_Glcd_Set_Side
- SPI_Glcd_Set_Page
- SPI_Glcd_Set_X
- SPI_Glcd_Read_Data
- SPI_Glcd_Write_Data
- SPI_Glcd_Set_Ext_Buffer
Advanced routines:
- SPI_Glcd_Fill
- SPI_Glcd_Dot
- SPI_Glcd_Line
- SPI_Glcd_V_Line
- SPI_Glcd_H_Line
- SPI_Glcd_Rectangle
- SPI_Glcd_Rectangle_Round_Edges
- SPI_Glcd_Rectangle_Round_Edges_Fill
- SPI_Glcd_Box
- SPI_Glcd_Circle
- SPI_Glcd_Circle_Fill
- SPI_Glcd_Set_Font
- SPI_Glcd_Set_Font_Adv
- SPI_Glcd_Set_Ext_Font_Adv
- SPI_Glcd_Write_Char
- SPI_Glcd_Write_Char_Adv
- SPI_Glcd_Write_Text
- SPI_Glcd_Write_Text_Adv
- SPI_Glcd_Image
- SPI_Glcd_Ext_Image
- SPI_Glcd_PartialImage
- SPI_Glcd_Ext_PartialImage
SPI_Glcd_Init
Prototype |
procedure SPI_Glcd_Init(DeviceAddress : byte); |
---|---|
Description |
Initializes the Glcd module via SPI interface. |
Parameters |
|
Returns |
Nothing. |
Requires |
Global variables :
|
Example |
// Port Expander module connections var SPExpanderRST : sbit at LATF0_bit; SPExpanderCS : sbit at LATF1_bit; SPExpanderRST_Direction : sbit at TRISF0_bit; SPExpanderCS_Direction : sbit at TRISF1_bit; // End Port Expander module connections ... // If Port Expander Library uses SPI module : SPI1_Init(); // Initialize SPI module used with PortExpander SPI_Glcd_Init(0); |
Notes |
None. |
SPI_Glcd_Set_Side
Prototype |
procedure SPI_Glcd_Set_Side(x_pos : byte); |
---|---|
Description |
Selects Glcd side. Refer to the Glcd datasheet for detail explanation. |
Parameters |
The parameter |
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
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); |
Notes |
For side, x axis and page layout explanation see schematic at the bottom of this page. |
SPI_Glcd_Set_Page
Prototype |
procedure SPI_Glcd_Set_Page(page : byte); |
---|---|
Description |
Selects page of Glcd. |
Returns |
|
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
SPI_Glcd_Set_Page(5); |
Notes |
For side, x axis and page layout explanation see schematic at the bottom of this page. |
SPI_Glcd_Set_X
Prototype |
procedure SPI_Glcd_Set_X(x_pos : byte); |
---|---|
Description |
Sets x-axis position to |
Parameters |
|
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
SPI_Glcd_Set_X(25); |
Notes |
For side, x axis and page layout explanation see schematic at the bottom of this page. |
SPI_Glcd_Read_Data
Prototype |
function SPI_Glcd_Read_Data() : byte; |
---|---|
Description |
Reads data from the current location of Glcd memory and moves to the next location. |
Returns |
One byte from Glcd memory. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. 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. |
Parameters |
None. |
Example |
var data_ : byte; ... data_ := SPI_Glcd_Read_Data(); |
Notes |
None. |
SPI_Glcd_Write_Data
Prototype |
procedure SPI_Glcd_Write_Data(data_ : byte); |
---|---|
Description |
Writes one byte to the current location in Glcd memory and moves to the next location. |
Parameters |
|
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. 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; ... SPI_Glcd_Write_Data(data_); |
Notes |
None. |
SPI_Glcd_Set_Ext_Buffer
Prototype |
procedure SPI_Glcd_Set_Ext_Buffer(getExtDataPtr : ^TSPI_Glcd_Get_Ext_Data_Ptr); |
---|---|
Returns |
Nothing. |
Description |
Function sets pointer to the user function which manipulates the external resource. Parameters :
User function prototype should be in the following format: Parameters used in the function have the following meaning :
|
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. 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 |
SPI_Glcd_Set_Ext_Buffer(@ReadExternalBuffer); |
SPI_Glcd_Fill
Prototype |
procedure SPI_Glcd_Fill(pattern : byte); |
---|---|
Description |
Fills Glcd memory with byte To clear the Glcd screen, use To fill the screen completely, use |
Parameters |
|
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
// Clear screen SPI_Glcd_Fill(0); |
Notes |
None. |
SPI_Glcd_Dot
Prototype |
procedure SPI_Glcd_Dot(x_pos, y_pos, color : byte); |
---|---|
Description |
Draws a dot on Glcd at coordinates ( |
Parameters |
The parameter |
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
// Invert the dot in the upper left corner SPI_Glcd_Dot(0, 0, 2); |
Notes |
For x and y axis layout explanation see schematic at the bottom of this page.. |
SPI_Glcd_Line
Prototype |
procedure SPI_Glcd_Line(x_start, y_start, x_end, y_end : integer; color : byte); |
---|---|
Description |
Draws a line on Glcd. Parameters : |
Parameters |
Parameter |
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
// Draw a line between dots (0,0) and (20,30) SPI_Glcd_Line(0, 0, 20, 30, 1); |
Notes |
None. |
SPI_Glcd_V_Line
Prototype |
procedure SPI_Glcd_V_Line(y_start, y_end, x_pos, color : byte); |
---|---|
Description |
Draws a vertical line on Glcd. |
Parameters |
Parameter |
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
// Draw a vertical line between dots (10,5) and (10,25) SPI_Glcd_V_Line(5, 25, 10, 1); |
Notes |
None. |
SPI_Glcd_H_Line
Prototype |
procedure SPI_Glcd_H_Line(x_start, x_end, y_pos, color : byte); |
---|---|
Description |
Draws a horizontal line on Glcd. |
Parameters |
The parameter |
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
// Draw a horizontal line between dots (10,20) and (50,20) SPI_Glcd_H_Line(10, 50, 20, 1); |
Notes |
None. |
SPI_Glcd_Rectangle
Prototype |
procedure SPI_Glcd_Rectangle(x_upper_left, y_upper_left, x_bottom_right, y_bottom_right, color : byte); |
---|---|
Description |
Draws a rectangle on Glcd. |
Parameters |
The parameter |
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
// Draw a rectangle between dots (5,5) and (40,40) SPI_Glcd_Rectangle(5, 5, 40, 40, 1); |
Notes |
None. |
SPI_Glcd_Rectangle_Round_Edges
Prototype |
procedure SPI_Glcd_Rectangle_Round_Edges(x_upper_left : byte; y_upper_left : byte; x_bottom_right : byte; y_bottom_right : byte; radius : byte; color : byte); |
---|---|
Description |
Draws a rounded edge rectangle on Glcd. |
Parameters |
The parameter |
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines. |
Example |
// Draw a rounded edge rectangle between dots (5,5) and (40,40) with the radius of 12 SPI_Glcd_Rectangle_Round_Edges(5, 5, 40, 40, 12, 1); |
Notes |
None. |
SPI_Glcd_Rectangle_Round_Edges_Fill
Prototype |
procedure SPI_Glcd_Rectangle_Round_Edges_Fill(x_upper_left : byte; y_upper_left : byte; x_bottom_right : byte; y_bottom_right : byte; radius : byte; color : byte); |
---|---|
Description |
Draws a filled rounded edge rectangle on Glcd. |
Parameters |
The parameter |
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routines. |
Example |
// Draws a filled rounded edge rectangle between dots (5,5) and (40,40) with the edge radius of 12 SPI_Glcd_Rectangle_Round_Edges(5, 5, 40, 40, 12, 1); |
Notes |
None. |
SPI_Glcd_Box
Prototype |
procedure SPI_Glcd_Box(x_upper_left, y_upper_left, x_bottom_right, y_bottom_right, color : byte); |
---|---|
Description |
Draws a box on Glcd. |
Parameters |
The parameter |
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
// Draw a box between dots (5,15) and (20,40) SPI_Glcd_Box(5, 15, 20, 40, 1); |
Notes |
None. |
SPI_Glcd_Circle
Prototype |
procedure SPI_Glcd_Circle(x_center, y_center, radius : integer; color : byte); |
---|---|
Description |
Draws a circle on Glcd. |
Parameters |
The parameter |
Returns |
Nothing. |
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); |
Notes |
None. |
SPI_Glcd_Circle_Fill
Prototype |
procedure SPI_Glcd_Circle_Fill(x_center : integer; y_center : integer; radius : integer; color : byte); |
---|---|
Description |
Draws a filled circle on Glcd. |
Parameters |
The parameter |
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
// Draw a filled circle with center in (50,50) and radius=10 SPI_Glcd_Circle_Fill(50, 50, 10, 1); |
Notes |
None. |
SPI_Glcd_Set_Font
Prototype |
procedure SPI_Glcd_Set_Font(activeFont: LongInt; aFontWidth, aFontHeight : byte; aFontOffs : word); |
---|---|
Description |
Sets font that will be used with SPI_Glcd_Write_Char and SPI_Glcd_Write_Text routines. |
Parameters |
The user can use fonts given in the file
For the sake of the backward compatibility, these fonts are supported also:
|
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
// Use the custom 5x7 font "myfont" which starts with space (32): SPI_Glcd_Set_Font(@myfont, 5, 7, 32); |
Notes |
None. |
SPI_Glcd_Set_Font_Adv
Prototype |
procedure Glcd_Set_Font_Adv(const activeFont : ^byte; font_color : word; font_orientation : byte); |
---|---|
Description |
Sets font that will be used with SPI_Glcd_Write_Char_Adv and SPI_Glcd_Write_Text_Adv routines. |
Parameters |
|
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
SPI_Glcd_Set_Font_Adv(@myfont, 0, 0); |
Notes |
None. |
SPI_Glcd_Set_Ext_Font_Adv
Prototype |
procedure SPI_Glcd_Set_Ext_Font_Adv(activeFont : dword; font_color : word; font_orientation : word); |
---|---|
Description |
Sets font that will be used with SPI_Glcd_Write_Char_Adv and SPI_Glcd_Write_Text_Adv routines. Font is located in an external resource. |
Parameters |
|
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
SPI_Glcd_Set_Ext_Font_Adv(173296, 5, 7, 32); |
Notes |
None. |
SPI_Glcd_Write_Char
Prototype |
procedure SPI_Glcd_Write_Char(chr1, x_pos, page_num, color : byte); |
---|---|
Description |
Prints character on the Glcd. |
Parameters |
The parameter |
Returns |
Nothing. |
Requires |
SPI_Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. Use the SPI_Glcd_Set_Font
to specify the font for display; if no font is specified, then the default |
Example |
// Write character 'C' on the position 10 inside the page 2: SPI_Glcd_Write_Char('C', 10, 2, 1); |
Notes |
For x axis and page layout explanation see schematic at the bottom of this page. |
SPI_Glcd_Write_Char_Adv
Prototype |
procedure SPI_Glcd_Write_Char_Adv(ch, x, y : word); |
---|---|
Returns |
Nothing. |
Description |
Writes a char on the glcd at coordinates (x, y).
|
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
SPI_Glcd_Write_Char_Adv('A',22,23); |
SPI_Glcd_Write_Text
Prototype |
procedure SPI_Glcd_Write_Text(var text: array[40] of char; x_pos, page_num, color : byte); |
---|---|
Description |
Prints text on Glcd. |
Parameters |
The parameter |
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. Use the SPI_Glcd_Set_Font
to specify the font for display; if no font is specified, then the default |
Example |
// Write text "Hello world!" on the position 10 inside the page 2: SPI_Glcd_Write_Text('Hello world!', 10, 2, 1); |
Notes |
For x axis and page layout explanation see schematic at the bottom of this page. |
SPI_Glcd_Write_Text_Adv
Prototype |
procedure SPI_Glcd_Write_Text_Adv(var text : string; x, y : word); |
---|---|
Returns |
Nothing. |
Description |
Writes text on the glcd at coordinates (x, y). Parameters :
|
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
SPI_Glcd_Write_Text_Adv("GLCD LIBRARY DEMO, WELCOME !", 0, 0); |
SPI_Glcd_Image
Prototype |
procedure SPI_Glcd_Image(const image: ^byte); |
---|---|
Description |
Displays bitmap on Glcd. |
Parameters |
|
Returns |
Nothing. |
Requires |
SPI_Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
// Draw image my_image on Glcd SPI_Glcd_Image(@my_image); |
Notes |
Use the mikroPascal PRO for PIC32 integrated Glcd Bitmap Editor, Tools > Glcd Bitmap Editor, to convert image to a constant array suitable for displaying on Glcd. |
SPI_Glcd_Ext_Image
Prototype |
procedure SPI_Glcd_Ext_Image(image : dword); |
---|---|
Description |
Displays a bitmap from an external resource. |
Parameters |
|
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
SPI_Glcd_Ext_Image(153608); |
Notes |
Use the mikroPascal PRO for PIC32 integrated Glcd Bitmap Editor, Tools > Glcd Bitmap Editor, to convert image to a constant array suitable for displaying on Glcd. |
SPI_Glcd_PartialImage
Prototype |
procedure SPI_Glcd_PartialImage(x_left, y_top, width, height, picture_width, picture_height : word; const image : ^byte); |
---|---|
Description |
Displays a partial area of the image on a desired location. |
Parameters |
|
Returns |
Nothing. |
Requires |
SPI_Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
// Draws a 10x15 part of the image starting from the upper left corner on the coordinate (10,12). Original image size is 16x32. SPI_Glcd_PartialImage(10, 12, 10, 15, 16, 32, @image); |
Notes |
Use the mikroPascal PRO for PIC32 integrated Glcd Bitmap Editor, Tools > Glcd Bitmap Editor, to convert image to a constant array suitable for displaying on Glcd. |
SPI_Glcd_Ext_PartialImage
Prototype |
procedure SPI_Glcd_Ext_PartialImage(x_left, y_top, width, height, picture_width, picture_height : word; image : dword); |
---|---|
Description |
Displays a partial area of the image, located on an external resource, on a desired location of the screen. |
Parameters |
|
Returns |
Nothing. |
Requires |
Glcd needs to be initialized for SPI communication, see SPI_Glcd_Init routine. |
Example |
SPI_Glcd_Ext_PartialImage(10, 12, 10, 15, 16, 32, 0); |
Notes |
Use the mikroPascal PRO for PIC32 integrated Glcd Bitmap Editor, Tools > Glcd Bitmap Editor, to convert image to a constant array suitable for displaying on Glcd. |
Library Example
The example demonstrates how to communicate to KS0108 Glcd via the SPI module, using serial to parallel convertor MCP23S17.
program SPI_Glcd;
// Port Expander module connections
var SPExpanderRST : sbit at LATD8_bit;
SPExpanderCS : sbit at LATD9_bit;
SPExpanderRST_Direction : sbit at TRISD8_bit;
SPExpanderCS_Direction : sbit at TRISD9_bit;
// End Port Expander module connections
var someText : array[20] of char;
counter : byte;
procedure Delay2S;
begin
Delay_ms(2000);
end;
begin
CHECON := 0x32;
AD1PCFG := 0xFFFF; // Configure AN pins as digital
// If Port Expander Library uses SPI2 module
// Initialize SPI module used with PortExpander
SPI2_Init_Advanced(_SPI_MASTER,_SPI_8_BIT, 4, _SPI_SS_DISABLE,_SPI_DATA_SAMPLE_MIDDLE,_SPI_CLK_IDLE_LOW,_SPI_ACTIVE_2_IDLE);
SPI_Glcd_Init(0); // Initialize Glcd via SPI
SPI_Glcd_Fill(0x00); // Clear Glcd
while TRUE do
begin
SPI_Glcd_Image(@truck_bmp); // Draw image
Delay2s(); Delay2s();
SPI_Glcd_fill(0x00); // Clear GLCD
SPI_Glcd_PartialImage(0,0,68,30,128,64,@truck_bmp); // Partial image
Delay_ms(500);
SPI_Glcd_PartialImage(24,16,68,30,128,64,@truck_bmp);
Delay_ms(500);
SPI_Glcd_PartialImage(56,34,68,30,128,64,@truck_bmp);
Delay2s(); Delay2s();
SPI_Glcd_Fill(0x00); // Clear GLCD
SPI_Glcd_Box(62,40,124,56,1); // Draw box
SPI_Glcd_Rectangle(5,5,84,35,1); // Draw rectangle
Delay_ms(1000);
SPI_Glcd_Rectangle_Round_Edges(2,2,87,38,7,1);
Delay_ms(1000);
SPI_Glcd_Rectangle_Round_Edges_Fill(8,8,81,32,12,1);
Delay_ms(1000);
SPI_Glcd_Line(0, 0, 127, 63, 1); // Draw line
Delay2s();
counter := 5;
while (counter <= 59) do // Draw horizontal and vertical lines
begin
Delay_ms(250);
SPI_Glcd_V_Line(2, 54, counter, 1);
SPI_Glcd_H_Line(2, 120, counter, 1);
Counter := counter + 5;
end;
Delay2S();
SPI_Glcd_Fill(0x00); // Clear GLCD
SPI_Glcd_Set_Font(@Font_Glcd_Character8x7, 8, 7, 32); // Choose font "Character8x7"
SPI_Glcd_Write_Text('mikroE', 1, 7, 2); // Write string
for counter := 1 to 10 do // Draw circles
SPI_Glcd_Circle(63,32, 3*counter, 1);
Delay2S();
SPI_Glcd_Circle_Fill(63,32, 30, 1); // Draw circles
Delay2S();
SPI_Glcd_Box(10,20, 70,63, 2); // Draw box
Delay2s();
SPI_Glcd_Fill(0xFF); // Fill Glcd
SPI_Glcd_Set_Font(@Font_Glcd_Character8x7, 8, 7, 32); // Change font
someText := '8x7 Font';
SPI_Glcd_Write_Text(someText, 5, 0, 2); // Write string
Delay2s();
SPI_Glcd_Set_Font(@Font_Glcd_System3x5, 3, 5, 32); // Change font
someText := '3X5 CAPITALS ONLY';
SPI_Glcd_Write_Text(someText, 60, 2, 2); // Write string
Delay2s();
SPI_Glcd_Set_Font(@Font_Glcd_System5x7, 5, 7, 32); // Change font
someText := '5x7 Font';
SPI_Glcd_Write_Text(someText, 5, 4, 2); // Write string
Delay2s();
SPI_Glcd_Set_Font(@Font_Glcd_5x7, 5, 7, 32); // Change font
someText := '5x7 Font (v2)';
SPI_Glcd_Write_Text(someText, 50, 6, 2); // Write string
Delay2s();
end;
end.
HW Connection
SPI Glcd HW connection
What do you think about this topic ? Send us feedback!