Proton BASIC Compiler - Using a KS0713 graphic LCD with an 18F device or the Amicus18 board


  • Pic® Basic


  • Using a KS0713 graphic LCD with an 18F device or the Amicus18 board

    The Samsung KS0713 graphic LCD chipset is becoming very popular, and in many cases is overtaking the Samsung KS0108 chipset as the LCD of choice. Its main merit is the fact that it operates from 3.3 volts, which makes it more suitable for low power, or battery driven applications.

    The compiler supports two LCD chipsets by default, these are the Toshiba T6963, and the aformentioned Samsung KS0108. The macro library adds support for the KS0713 graphic LCD, and also adds extra commands. These include:

    Glcd_Init Initialise the graphic LCD
    Glcd_Cls Clear the LCD
    Glcd_Plot Set or erase a pixel
    Glcd_Pixel Read a pixel’s condition
    Glcd_Line Draw or erase a line
    Glcd_LineTo Draw or erase a line from the previous ending location
    Glcd_Vline Draw or erase a vertical line
    Glcd_Hline Draw or erase a horizontal line
    Glcd_WriteByte Write a byte to the LCD
    Glcd_ReadByte Read a byte from the LCD
    Glcd_Circle Draw or erase a circle
    Glcd_Box Draw or erase a square
    Glcd_Rectangle Draw or erase a rectangle
    Glcd_Cursor Move the text cursor
    Glcd_Print Display ASCII text on the LCD at the current cursor location
    Glcd_PrintAt Display ASCII text at a pre-determined cursor location
    The library of macros allows interfacing to the KS0713 in parallel mode, and a suitable circuit is shown below:





    The datasheet for the Samsung KS0713 graphic LCD controller can be downloaded from here: KS0713 Datasheet.pdf

    KS0713 Graphic LCD library macro's overview

    Adding the functionality of the macro’s is as simple as including it into the program via the Include directive:

    Include "KS0713.inc" ' Load the KS0713 graphic LCD macros into the program

    Because the KS0713 chipset is used in a multitude of graphic LCD displays, there is not a pre-determined pin order, therefore a series of $defines allows a particular display to be tailored to meet the program’s requirements. These are:

    Connection to the LCD’s CS1 pin. This pin is sometimes named CS1B depending on the display manufacturer:

    $define GLCD_CS1 Port.Bit

    Connection to the LCD’s Reset pin. This pin is sometimes named RS1 or RST depending on the display manufacturer:

    $define GLCD_Reset Port.Bit

    Connection to the LCD’s A0 pin. This pin is sometimes named RS depending on the display manufacturer:

    $define GLCD_A0 Port.Bit

    Connection to the LCD’s RW pin:

    $define GLCD_RW Port.Bit

    Connection to the LCD’s EN pin. This pin is sometimes named RD depending on the display manufacturer:

    $define GLCD_EN_RD Port.Bit

    Connection to the LCD’s data port:

    $define GLCD_Data Port

    Note.
    The defines must be placed before the macros are included into the program:
    Code:
     $define GLCD_CS1   PORTC.2   ' LCD's CS1 pin
     $define GLCD_Reset PORTC.4   ' LCD's Reset pin
     $define GLCD_A0    PORTC.1   ' LCD's A0 pin
     $define GLCD_RW    PORTC.3   ' LCD's RW
     $define GLCD_EN_RD PORTC.5   ' LCD's En or RD pin
     $define GLCD_Data  PORTB     ' LCD's data port
    
    Include "KS0713.inc"         ' Load the KS0713 graphic LCD macros into the program
    Glcd_Init()

    Initialise the KS0713 graphic LCD.

    Note.
    The LCD is automatically initialised when the KS0713.inc file is included into the program.

    Glcd_Cls

    Glcd_Cls()

    Clear the graphic LCD’s screen.

    Example.
    Code:
    
      Include "Amicus18.inc"       ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
      Include "KS0713.inc"         ' Load the KS0713 graphic LCD macros into the program
    
      Glcd_Cls()                   ' Clear the LCD’s screen 
    
      Glcd_Cursor(0, 0)            ' Move to the start of the first line 
      Glcd_Print("Hello World")    ' Display some text
    Note.
    This should be issued before any of the following macros are used, as the KS0713 has a tendancy to power up with noise being displayed on the LCD’s screen.

    Glcd_Plot

    Glcd_Plot
    (pXpos , pYpos, pColour)

    Alter a pixel on the graphic LCD.

    Operators.
    • pXpos: X position of the pixel (0 to 127)
    • pYpos: Y position of the pixel (0 to 63)
    • pColour: The colour of the pixel (0 = Off, 1 = On, 2 = Xor with the underlying pixel)
    Example.
    Code:
    
      Include "Amicus18.inc"       ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
      Include "KS0713.inc"         ' Load the KS0713 graphic LCD macros into the program
    
      Dim bXpos as Byte            ' Create a byte sized variable for the X position 
      Dim bYpos as Byte            ' Create a byte sized variable for the Y position
    
      Glcd_Cls()                   ' Clear the LCD’s screen
    
      For bYpos = 0 to 63           ' Form a loop for the Y position
        For bXpos = 0 to 127       ' Form a loop for the X position
          Glcd_Plot(bXpos, bYpos, 1)' Set a pixel at the current Xpos and Ypos
          DelayMs 5                ' Slow things down so we can see the pixel being drawn
        Next                       ' Close the X position loop
      Next                         ' Close the Y position loop
    Glcd_Pixel

    Var = Glcd_Pixel(pXpos, pYpos)

    Read a pixel from the LCD’s screen.

    Operators.
    • pXpos: X position of the pixel (0 to 127)
    • pYpos: Y position of the pixel (0 to 63)
    • Var: Can be a variable of type; Bit, Byte, Word, Dword, or an array.
    Example.
    Code:
    
      Include "Amicus18.inc"           ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
      Include "KS0713.inc"             ' Load the KS0713 graphic LCD macros into the program
    
      Dim bXpos as Byte                ' Create a byte sized variable for the X position 
      Dim bColour as Byte              ' Create a byte sized variable to hold the pixel colour
    
      Glcd_Cls()                       ' Clear the LCD’s screen
    
      For bXpos = 0 to 127 Step 8      ' Form a loop for the X pos with an increment of 8
        Glcd_Plot(bXpos, 0, 1)         ' Set a pixel at the current Xpos
      Next                             ' Close the X position loop
    '
    ' Read the pixels and re-display
    '
      For bXpos = 0 to 127             ' Form a loop for the X position
        bColour = Glcd_Pixel(bXpos, 0) ' Read a pixel from the LCD
        Glcd_Plot(bXpos, 8, bColour)   ' Copy the pixel back to the LCD
        DelayMs 50                     ' Slow things down so we can see the pixel being copied
      Next                             ' Close the X position loop
    Glcd_WriteByte

    Glcd_WriteByte(pXpos , pYpos, pVar)

    Alter a pixel on the graphic LCD.

    Operators.
    • pXpos: X position of the pixel (0 to 127)
    • pYpos: Y position of the pixel (0 to 7)
    • pVar: The byte to write to the LCD’s screen
    Example.
    Code:
    
      Include "Amicus18.inc"                ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
      Include "KS0713.inc"                  ' Load the KS0713 graphic LCD macros into the program
    
      Dim bXpos as Byte                     ' Create a byte sized variable for the X position 
      Dim bYpos as Byte                     ' Create a byte sized variable for the Y position
      Dim bData as Byte                     ' Create a byte sized variable for the byte to write
      Glcd_Cls()                            ' Clear the LCD’s screen
    
      bData = 0
      For bYpos = 0 to 7                    ' Form a loop for the Y position
        For bXpos = 0 to 127                ' Form a loop for the X position
          Glcd_WriteByte(bXpos, bYpos, bData) ' Write a byte at current Xpos and Ypos
          Inc bData                         ' Increment the byte to be written
        Next                                ' Close the X position loop
      Next                                  ' Close the Y position loop
    Glcd_ReadByte

    Var = Glcd_ReadByte(pXpos, pYpos)

    Read a byte from the LCD’s screen.

    Operators.
    • pXpos: X position of the byte (0 to 127)
    • pYpos: Y position of the byte (0 to 7)
    • Var: Can be a variable of type; Bit, Byte, Word, Dword, or an array.
    Example.
    Code:
    
      Include "Amicus18.inc"                ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
      Include "KS0713.inc"                  ' Load the KS0713 graphic LCD macros into the program
    
      Dim bXpos as Byte                     ' Create a byte sized variable for the X position 
      Dim bData as Byte                     ' Create a byte sized variable to hold the value
      
      Glcd_Cls()                            ' Clear the LCD’s screen
    
      For bXpos = 0 to 127 Step 2           ' Form a loop for the X pos with an increment of 2
        Glcd_WriteByte(bXpos, 0, $AA)       ' Write a byte at the current Xpos
      Next                                  ' Close the X position loop
    '
    ' Read the bytes and re-display
    '
      For bXpos = 0 to 127                  ' Form a loop for the X position
        bData = Glcd_ReadByte(bXpos, 0)     ' Read a byte from the LCD
        Glcd_WriteByte(bXpos, 2, bData)     ' Copy the byte back to the LCD
        DelayMs 50                          ' Slow things down so we can see the byte being copied
      Next                                  ' Close the X position loop
    Glcd_Line

    Glcd_Line(pStartXpos, pStartYpos, pEndXpos, pEndYpos, pColour)

    Draw a line on the LCD’s screen.

    Operators.
    • pStartXpos: Starting X position of the line (0 to 127)
    • pStartYpos: Starting Y position of the line (0 to 63)
    • pEndXpos: Ending X position of the line (0 to 127)
    • pEndYpos: Ending Y position of the line (0 to 63)
    • pColour: The colour of the line (0 = Off, 1 = On, 2 = Xor with the underlying pixel)
    Example.
    Code:
    
      Include "Amicus18.inc"              ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
      Include "KS0713.inc"                ' Load the KS0713 graphic LCD macros into the program
    
      Dim bEndYpos as Byte                ' Create a byte sized variable for ending Y position
    
      Glcd_Cls()                          ' Clear the LCD’s screen
    
      For bEndYpos = 0 to 63              ' Form a loop for the ending Y position
        Glcd_Line(0, 0, 127, bEndYpos, 1) ' Draw a line
      Next                                ' Close the ending Y position loop
    Glcd_LineTo

    Glcd_LineTo(pEndXpos, pEndYpos, pColour)

    Draw a line on the LCD’s screen starting from the end of a previous line.

    Operators.
    • pEndXpos: Ending X position of the line (0 to 127)
    • pEndYpos: Ending Y position of the line (0 to 63)
    • pColour: The colour of the line (0 = Off, 1 = On, 2 = Xor with the underlying pixel)
    Example.
    Code:
    
      Include "Amicus18.inc"       ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
      Include "KS0713.inc"         ' Load the KS0713 graphic LCD macros into the program
    
      Glcd_Cls()                   ' Clear the LCD’s screen
    
      Glcd_Line(0, 0, 127, 0, 1)   ' Draw a line at the top of the LCD’s screen
      Glcd_LineTo(127, 63, 1)      ' Draw a line at the right of the LCD’s screen
      Glcd_LineTo(0, 63, 1)        ' Draw a line at the bottom of the LCD’s screen
      Glcd_LineTo(0, 0, 1)         ' Draw a line at the left of the LCD’s screen
    Note.
    The Glcd_LineTo macro must follow immediately after a Glcd_Line macro, in order for its end coordinates to be still valid.

    Glcd_Vline

    Glcd_Vline(pXpos, pYpos, pSize, pColour)

    Draw a vertical line on the LCD’s screen.

    Operators.
    • pXpos: Starting X position of the line (0 to 127)
    • pYpos: Starting Y position of the line (0 to 63)
    • pSize: Length of the line (0 to 63)
    • pColour: The colour of the line (0 = Off, 1 = On, 2 = Xor with the underlying pixel)
    Example.
    Code:
    
      Include "Amicus18.inc"             ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
      Include "KS0713.inc"               ' Load the KS0713 graphic LCD macros into the program
    
      Dim bXpos as Byte                  ' Create a byte sized variable for X position 
      Dim bLength as Byte                ' Create a byte sized variable for length of the line
    
      Glcd_Cls()                         ' Clear the LCD’s screen
    
      bLength = 0
      For bXpos = 0 to 127               ' Form a loop for the X position
        Glcd_Vline(bXpos, 0, bLength, 1) ' Draw a vertical line
        Inc bLength                      ' Increment the length of the line
        If bLength = 63 Then             ' Has the line length reached 63 pixels?
          bLength = 0                    ' Yes. So reset it
        EndIf
      Next                               ' Close the X position loop
    Glcd_Hline

    Glcd_Hline(pXpos, pYpos, pSize, pColour)

    Draw a horizontal line on the LCD’s screen.

    Operators.
    • pXpos: Starting X position of the line (0 to 127)
    • pYpos: Starting Y position of the line (0 to 63)
    • pSize: Length of the line (0 to 63)
    • pColour: The colour of the line (0 = Off, 1 = On, 2 = Xor with the underlying pixel)
    Example.
    Code:
    
      Include "Amicus18.inc"             ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
      Include "KS0713.inc"               ' Load the KS0713 graphic LCD macros into the program
    
      Dim bYpos as Byte                  ' Create a byte sized variable for Y position
      Dim bLength as Byte                ' Create a byte sized variable for length of the line
    
      Glcd_Cls()                         ' Clear the LCD’s screen
    
      bLength = 0
      For bYpos = 0 to 63                ' Form a loop for the Y position
        Glcd_Hline(0, bYpos, bLength, 1) ' Draw a horizontal line
        Inc bLength                      ' Increment the length of the line
      Next                               ' Close the Y position loop
    Glcd_Box

    Glcd_Box(pXpos, pYpos, pSize, pColour)

    Draw a box on the LCD’s screen.

    Operators.
    • pXpos: Starting X position of the box (0 to 127)
    • pYpos: Starting Y position of the box (0 to 63)
    • pSize: Size of the box in pixels (0 to 63)
    • pColour: The colour of the box (0 = Off, 1 = On, 2 = Xor with the underlying pixel)
    Example.
    Code:
    
       Include "Amicus18.inc"       ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
      Include "KS0713.inc"         ' Load the KS0713 graphic LCD macros into the program
    
      Dim bSize as Byte            ' Create a byte sized variable for size of the box 
    
      Glcd_Cls()                   ' Clear the LCD’s screen
    
      For bSize = 0 to 127 Step 4  ' Form a loop for the size of the box
        Glcd_Box(0, 0, bSize, 1)   ' Draw a box
        DelayMs 100                ' Slow things down so we can see the box being drawn
      Next                         ' Close the size loop
    Glcd_Rectangle

    Glcd_Rectangle(pXpos, pYpos, pLength, pHeight, pColour)

    Draw a rectangle on the LCD’s screen.

    Operators.
    • pXpos: Starting X position of the rectangle (0 to 127)
    • pYpos: Starting Y position of the rectangle (0 to 63)
    • pLength: Length of the rectangle in pixels (0 to 127)
    • pHeight: Height of the rectangle in pixels (0 to 63)
    • pColour: The colour of the rectangle (0 = Off, 1 = On, 2 = Xor with the underlying pixel)
    Example.
    Code:
    
      Include "Amicus18.inc"                      ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
      Include "KS0713.inc"                        ' Load the KS0713 graphic LCD macros into the program
    
      Dim bHeight as Byte                         ' Create a byte sized variable for height
      Dim bLength as Byte                         ' Create a byte sized variable for length
    
      Glcd_Cls()                                  ' Clear the LCD’s screen
    
      For bLength = 0 to 127 Step 4               ' Form a loop for the length
        bHeight = bLength / 2
        Glcd_Rectangle(0, 0, bLength, bHeight, 1) ' Draw a rectangle
        DelayMs 100                               ' Slow things down so we can see it being drawn
      Next                                        ' Close the loop
    Glcd_Circle

    Glcd_Circle(pXpos, pYpos, pRadius, pColour)

    Draw a circle on the LCD’s screen.

    Operators.
    • pXpos: Centre X position of the circle (0 to 127)
    • pYpos: Centre Y position of the circle (0 to 63)
    • pRadius: Radius of the circle in pixels (0 to 63)
    • pColour: The colour of the circle (0 = Off, 1 = On, 2 = Xor with the underlying pixel)
    Example.
    Code:
    
      Include "Amicus18.inc"            ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
      Include "KS0713.inc"              ' Load the KS0713 graphic LCD macros into the program
    
      Dim bRadius as Byte               ' Create a byte sized variable for radius of the circle
    
      Glcd_Cls()                        ' Clear the LCD’s screen
    
      For bRadius = 0 to 127 Step 4     ' Form a loop for the radius of the circle
        Glcd_Circle(63, 32, bRadius, 1) ' Draw a circle
        DelayMs 100                     ' Slow things down so we can see the circle being drawn
      Next                              ' Close the radius loop
    Glcd_Cursor

    Glcd_Cursor(pXpos, pYpos)

    Move the text position on the LCD’s screen, prior to displaying ASCII characters.

    Operators.
    • pXpos: X position of the cursor (0 to 21)
    • pYpos: Y position (line) of the cursor (0 to 7)
    Example.
    Code:
    
      Include "Amicus18.inc"       ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
      Include "KS0713.inc"         ' Load the KS0713 graphic LCD macros into the program
    
      Glcd_Cls()                   ' Clear the LCD’s screen
      
      Glcd_Cursor(0, 0)            ' Move to the start of the first line
      Glcd_Print("Hello World")    ' Display some text
    Glcd_Print

    Glcd_Print(i)

    Display an ASCII string on the LCD at the current cursor position.

    Operator.
    • pString: The string to display on the LCD
    Example.
    Code:
    
      Include "Amicus18.inc"       ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
      Include "KS0713.inc"         ' Load the KS0713 graphic LCD macros into the program
    
      Dim bTemp as Byte            ' Create a byte sized variable
      Dim wTemp as Word            ' Create a word sized variable
      Dim fTemp as Float           ' Create a floating point sized variable
    
      Glcd_Cls()                   ' Clear the LCD’s screen
    
      Glcd_Cursor(0, 0)            ' Move to the start of the first line
      Glcd_Print("Hello World")    ' Display some text
    
      bTemp = 0                    ' Reset bTemp
      wTemp = 0                    ' Reset wTemp
      fTemp = 0                    ' Reset fTemp
    
      While                        ' Form an infinite loop
        Glcd_Cursor(0, 1)          ' Move to the start of the second line
        Glcd_Print("Byte: ")
        Glcd_Print(Dec bTemp)      ' Convert an integer to ASCII and display it
        Glcd_Print(" ")
    
        Glcd_Cursor(0, 2)          ' Move to the start of the third line
        Glcd_Print("Word: ")
        Glcd_Print(Dec wTemp)      ' Convert an integer to ASCII and display it
        Glcd_Print(" ")
    
        Glcd_Cursor(0, 3)          ' Move to the start of the fourth line
        Glcd_Print("Float: ")
        Glcd_Print(Dec1 fTemp)     ' Convert a float to ASCII and display it
    
        Inc bTemp                  ' Increment bTemp
        Inc wTemp                  ' Increment wTemp
        fTemp = fTemp + 0.2        ' Increment fTemp
    
        DelayMs 100                ' Slow things down so we can see the text change
      Wend                         ' Do it forever
    Note.
    The string to display may contain any single modifier as used in the compiler’s Print command. i.e. Dec, Hex, Bin, Str, Cstr, Estr etc.. However, it may not contain the At modifier, and multiple modifiers seprated by commas will be ignored.

    Glcd_PrintAt

    Glcd_PrintAt(pXpos, pYpos, pString)

    Display an ASCII string on the LCD at a given cursor position.

    Operators.
    • pXpos: X position of the cursor (0 to 21)
    • pYpos: Y position (line) of the cursor (0 to 7)
    • pString: The ASCII string to display on the LCD
    Example.
    Code:
    
      Include "Amicus18.inc"            ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board.
       Include "KS0713.inc"              ' Load the KS0713 graphic LCD macros into the program
    
      Dim bTemp as Byte                 ' Create a byte sized variable
      Dim wTemp as Word                 ' Create a word sized variable
      Dim fTemp as Float                ' Create a floating point sized variable
    
      Glcd_Cls()                        ' Clear the LCD’s screen
    
      Glcd_PrintAt(0, 0, "Hello World") ' Display text at the start of the first line 
    
      bTemp = 0                         ' Reset bTemp
      wTemp = 0                          ' Reset wTemp
      fTemp = 0                         ' Reset fTemp
    
      While                              ' Form an infinite loop
        Glcd_PrintAt(0, 1, "Byte: ")    ' Display text at the start of the second line
        Glcd_Print(Dec bTemp)           ' Convert an integer to ASCII and display it
        Glcd_Print(" ")
    
        Glcd_PrintAt(0, 2, "Word: ")    ' Display text at the start of the third line 
        Glcd_Print(Dec wTemp)           ' Convert an integer to ASCII and display it
        Glcd_Print(" ")
    
        Glcd_PrintAt(0, 3, "Float: ")   ' Display text at the start of the fourth line 
        Glcd_Print(Dec1 fTemp)          ' Convert a float to ASCII and display it
    
        Inc bTemp                       ' Increment bTemp
        Inc wTemp                       ' Increment wTemp
        fTemp = fTemp + 0.2             ' Increment fTemp
    
        DelayMs 100                     ' Slow things down so we can see the text change
      Wend                              ' Do it forever
    Note.
    The string to display may contain any single modifier as used in the compiler’s Print command. i.e. Dec, Hex, Bin, Str, Cstr, Estr etc.. However, it may not contain the At modifier, and multiple modifiers seperated by commas will be ignored.

    The KS0713 graphic LCD library source code and a demonstration program can be downloaded from here: KS0713-Source Code.zip
  • Recent Activity

    Stephen Moss-711

    error in lcd 16x2

    Thread Starter: José

    best regards friends Well, I designed a code with pic 18f2550, this code is for measuring resistors and capacitors, but when simulating in...

    Stephen Moss Today, 14:05 Go to last post
    Picprogrammer-103518

    Empty if..endif bug

    Thread Starter: Picprogrammer

    Hi, Noticed couple of bugs with 3.6.0.2 (not updating further as i need to keep this version stable). loop: If dummy = 1 then 'Nothing...

    Picprogrammer Yesterday, 19:11 Go to last post