Graphic Lcd Library
The mikroBasic PRO for 8051 provides a library for operating Graphic Lcd 128x64 (with commonly used Samsung KS108/KS107 controller).
For creating a custom set of Glcd images use Glcd Bitmap Editor Tool.
External dependencies of Graphic Lcd Library
| The following variables must be defined in all projects using Graphic Lcd Library: | Description : | Example : |
|---|---|---|
dim GLCD_DataPort as byte external volatile sfr |
Lcd Data Port. | dim GLCD_DataPort as sbit at P0 |
dim GLCD_CS1 as sbit bdata sfr external |
Chip Select 1 line. | dim GLCD_CS1 as sbit at P2_0_bit |
dim GLCD_CS2 as sbit bdata sfr external |
Chip Select 2 line. | dim GLCD_CS2 as sbit at P2_1_bit |
dim GLCD_RS as sbit bdata sfr external |
Register select line. | dim GLCD_RS as sbit at P2_2_bit |
dim GLCD_RW as sbit bdata sfr external |
Read/Write line. | dim GLCD_RW as sbit at P2_3_bit |
dim GLCD_RST as sbit bdata sfr external |
Reset line. | dim GLCD_RST as sbit at P2_5_bit |
dim GLCD_EN as sbit bdata sfr external |
Enable line. | dim GLCD_EN as sbit at P2_4_bit |
Library Routines
Basic routines:
Advanced routines:
- Glcd_Fill
- Glcd_Dot
- Glcd_Line
- Glcd_V_Line
- Glcd_H_Line
- Glcd_Rectangle
- Glcd_Box
- Glcd_Circle
- Glcd_Set_Font
- Glcd_Write_Char
- Glcd_Write_Text
- Glcd_Image
Glcd_Init
| Prototype |
sub procedure Glcd_Init() |
|---|---|
| Returns |
Nothing. |
| Description |
Initializes the Glcd module. Each of the control lines is both port and pin configurable, while data lines must be on a single port (pins <0:7>). |
| Requires |
Global variables :
|
| Example |
' glcd pinout settings dim GLCD_DataPort as byte at P0 dim GLCD_CS1 as sbit at P2_0_bit dim GLCD_CS2 as sbit at P2_1_bit dim GLCD_RS as sbit at P2_2_bit dim GLCD_RW as sbit at P2_3_bit dim GLCD_RST as sbit at P2_5_bit dim GLCD_EN as sbit at P2_4_bit ... Glcd_Init() |
Glcd_Set_Side
| Prototype |
|
|---|---|
| Returns |
Nothing. |
| Description |
Selects Glcd side. Refer to the Glcd datasheet for detailed explanation. Parameters :
The parameter Note: For side, x axis and page layout explanation see schematic at the bottom of this page. |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. |
| Example |
The following two lines are equivalent, and both of them select the left side of Glcd: Glcd_Select_Side(0) Glcd_Select_Side(10) |
Glcd_Set_X
| Prototype |
sub procedure Glcd_Set_X(dim x_pos as byte) |
|---|---|
| Returns |
Nothing. |
| Description |
Sets x-axis position to Parameters :
Note: For side, x axis and page layout explanation see schematic at the bottom of this page. |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. |
| Example |
Glcd_Set_X(25) |
Glcd_Set_Page
| Prototype |
sub procedure Glcd_Set_Page(dim page as byte) |
|---|---|
| Returns |
Nothing. |
| Description |
Selects page of the Glcd. Parameters :
Note: For side, x axis and page layout explanation see schematic at the bottom of this page. |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. |
| Example |
Glcd_Set_Page(5) |
Glcd_Read_Data
| Prototype |
sub function Glcd_Read_Data() as byte |
|---|---|
| Returns |
One byte from Glcd memory. |
| Description |
Reads data from from the current location of Glcd memory and moves to the next location. |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. Glcd side, x-axis position and page should be set first. See functions Glcd_Set_Side, Glcd_Set_X, and Glcd_Set_Page. |
| Example |
dim data as byte ... data = Glcd_Read_Data() |
Glcd_Write_Data
| Prototype |
sub procedure Glcd_Write_Data(dim ddata as byte) |
|---|---|
| Returns |
Nothing. |
| Description |
Writes one byte to the current location in Glcd memory and moves to the next location. Parameters :
|
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. Glcd side, x-axis position and page should be set first. See functions Glcd_Set_Side, Glcd_Set_X, and Glcd_Set_Page. |
| Example |
dim data as byte ... Glcd_Write_Data(data) |
Glcd_Fill
| Prototype |
sub procedure Glcd_Fill(dim pattern as byte) |
|---|---|
| Returns |
Nothing. |
| Description |
Fills Glcd memory with the byte Parameters :
To clear the Glcd screen, use To fill the screen completely, use |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. |
| Example |
' Clear screen Glcd_Fill(0) |
Glcd_Dot
| Prototype |
sub procedure Glcd_Dot(dim x_pos as byte, dim y_pos as byte, dim color as byte) |
|---|---|
| Returns |
Nothing. |
| Description |
Draws a dot on Glcd at coordinates ( Parameters :
The parameter Note: For x and y axis layout explanation see schematic at the bottom of this page. |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. |
| Example |
' Invert the dot in the upper left corner Glcd_Dot(0, 0, 2) |
Glcd_Line
| Prototype |
sub procedure Glcd_Line(dim x_start as integer, dim y_start as integer, dim x_end as integer, dim y_end as integer, dim color as byte) |
|---|---|
| Returns |
Nothing. |
| Description |
Draws a line on Glcd. Parameters :
The parameter |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. |
| Example |
' Draw a line between dots (0,0) and (20,30) Glcd_Line(0, 0, 20, 30, 1) |
Glcd_V_Line
| Prototype |
sub procedure Glcd_V_Line(dim y_start as byte, dim y_end as byte, dim x_pos as byte, dim color as byte) |
|---|---|
| Returns |
Nothing. |
| Description |
Draws a vertical line on Glcd. Parameters :
The parameter |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. |
| Example |
' Draw a vertical line between dots (10,5) and (10,25) Glcd_V_Line(5, 25, 10, 1) |
Glcd_H_Line
| Prototype |
sub procedure Glcd_V_Line(dim x_start as byte, dim x_end as byte, dim y_pos as byte, dim color as byte) |
|---|---|
| Returns |
Nothing. |
| Description |
Draws a horizontal line on Glcd. Parameters :
The parameter |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. |
| Example |
' Draw a horizontal line between dots (10,20) and (50,20) Glcd_H_Line(10, 50, 20, 1) |
Glcd_Rectangle
| Prototype |
sub procedure Glcd_Rectangle(dim x_upper_left as byte, dim y_upper_left as byte, dim x_bottom_right as byte, dim y_bottom_right as byte, dim color as byte) |
|---|---|
| Returns |
Nothing. |
| Description |
Draws a rectangle on Glcd. Parameters :
The parameter |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. |
| Example |
' Draw a rectangle between dots (5,5) and (40,40) Glcd_Rectangle(5, 5, 40, 40, 1) |
Glcd_Box
| Prototype |
sub procedure Glcd_Box(dim x_upper_left as byte, dim y_upper_left as byte, dim x_bottom_right as byte, dim y_bottom_right as byte, dim color as byte) |
|---|---|
| Returns |
Nothing. |
| Description |
Draws a box on Glcd. Parameters :
The parameter |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. |
| Example |
' Draw a box between dots (5,15) and (20,40) Glcd_Box(5, 15, 20, 40, 1) |
Glcd_Circle
| Prototype |
sub procedure Glcd_Circle(dim x_center as integer, dim y_center as integer, dim radius as integer, dim color as byte) |
|---|---|
| Returns |
Nothing. |
| Description |
Draws a circle on Glcd. Parameters :
The parameter |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. |
| Example |
' Draw a circle with center in (50,50) and radius=10 Glcd_Circle(50, 50, 10, 1) |
Glcd_Set_Font
| Prototype |
sub procedure Glcd_Set_Font(dim byref const ActiveFont as ^byte, dim FontWidth as byte, dim FontHeight as byte, dim FontOffs as word) |
|---|---|
| Returns |
Nothing. |
| Description |
Sets font that will be used with Glcd_Write_Char and 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:
|
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. |
| Example |
' Use the custom 5x7 font "myfont" which starts with space (32): Glcd_Set_Font(myfont, 5, 7, 32) |
Glcd_Write_Char
| Prototype |
sub procedure Glcd_Write_Char(dim chr as byte, dim x_pos as byte, dim page_num as byte, dim color as byte) |
|---|---|
| Returns |
Nothing. |
| Description |
Prints character on the Glcd. Parameters :
The parameter Note: For x axis and page layout explanation see schematic at the bottom of this page. |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. Use Glcd_Set_Font to specify the font for display; if no font is specified, then default |
| Example |
' Write character 'C' on the position 10 inside the page 2:
Glcd_Write_Char('C', 10, 2, 1)
|
Glcd_Write_Text
| Prototype |
sub procedure Glcd_Write_Text(dim byref text as string[20], dim x_pos as byte, dim page_num as byte, dim color as byte) |
|---|---|
| Returns |
Nothing. |
| Description |
Prints text on Glcd. Parameters :
The parameter Note: For x axis and page layout explanation see schematic at the bottom of this page. |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. Use Glcd_Set_Font to specify the font for display; if no font is specified, then default |
| Example |
' Write text "Hello world!" on the position 10 inside the page 2:
Glcd_Write_Text("Hello world!", 10, 2, 1)
|
Glcd_Image
| Prototype |
sub procedure Glcd_Image(dim byref const image as ^byte) |
|---|---|
| Returns |
Nothing. |
| Description |
Displays bitmap on Glcd. Parameters :
Use the mikroBasic PRO for 8051 integrated Glcd Bitmap Editor to convert image to a constant array suitable for displaying on Glcd. |
| Requires |
Glcd needs to be initialized, see Glcd_Init routine. |
| Example |
' Draw image my_image on Glcd Glcd_Image(my_image) |
Library Example
The following example demonstrates routines of the Glcd library: initialization, clear(pattern fill), image displaying, drawing lines, circles, boxes and rectangles, text displaying and handling.
program GLCD_Test
' Glcd module connections
dim GLCD_DataPort as byte at P0 ' GLCD data port
dim GLCD_CS1 as sbit at P2_0_bit ' GLCD chip select 1 signal
dim GLCD_CS2 as sbit at P2_1_bit ' GLCD chip select 2 signal
dim GLCD_RS as sbit at P2_2_bit ' GLCD register select signal
dim GLCD_RW as sbit at P2_3_bit ' GLCD read/write signal
dim GLCD_EN as sbit at P2_4_bit ' GLCD enable signal
dim GLCD_RST as sbit at P2_5_bit ' GLCD reset signal
' End Glcd module connections
dim counter as byte
someText as char[18]
sub procedure Delay2S() ' 2 seconds delay function
Delay_ms(2000)
end sub
main:
'#DEFINE COMPLETE_EXAMPLE
Glcd_Init() ' Initialize GLCD
Glcd_Fill(0x00) ' Clear GLCD
while TRUE
'#IFDEF COMPLETE_EXAMPLE
Glcd_Image(@truck_bmp) ' Draw image
Delay2S() Delay2S()
'#ENDIF
Glcd_Fill(0x00) ' Clear GLCD
Glcd_Box(62,40,124,63,1) ' Draw box
Glcd_Rectangle(5,5,84,35,1) ' Draw rectangle
Glcd_Line(0, 0, 127, 63, 1) ' Draw line
Delay2S()
counter = 5
while (counter <= 59) ' Draw horizontal and vertical lines
Delay_ms(250)
Glcd_V_Line(2, 54, counter, 1)
Glcd_H_Line(2, 120, counter, 1)
Counter = counter + 5
wend
Delay2S()
Glcd_Fill(0x00) ' Clear GLCD
'#IFDEF COMPLETE_EXAMPLE
someText = "mikroE"
Glcd_Set_Font(@Character8x7, 8, 7, 32) ' Choose font "Character8x7"
Glcd_Write_Text(someText, 1, 7, 2) ' Write string
for counter = 1 to 10 ' Draw circles
Glcd_Circle(63,32, 3*counter, 1)
next counter
Delay2S()
Glcd_Box(10,20, 70,63, 2) ' Draw box
Delay2S()
Glcd_Fill(0xFF) ' Fill GLCD
Glcd_Set_Font(@Character8x7, 8, 7, 32) ' Change font
someText = "8x7 Font"
Glcd_Write_Text(someText, 5, 0, 2) ' Write string
delay2S()
Glcd_Set_Font(@System3x5, 3, 5, 32) ' Change font
someText = "3X5 CAPITALS ONLY"
Glcd_Write_Text(someText, 60, 2, 2) ' Write string
delay2S()
Glcd_Set_Font(@font5x7, 5, 7, 32) ' Change font
someText = "5x7 Font"
Glcd_Write_Text(someText, 5, 4, 2) ' Write string
delay2S()
Glcd_Set_Font(@FontSystem5x7_v2, 5, 7, 32) ' Change font
someText = "5x7 Font (v2)"
Glcd_Write_Text(someText, 5, 6, 2) ' Write string
delay2S()
'#ENDIF
wend
end.
HW Connection

Glcd HW connection
What do you think about this topic ? Send us feedback!



