KS0108CS3

Modified KS0108 Driver Module for GLCD with 2 or 3 CS lines

by Eric Couratier and Ahmed Lazreg

Modification Details

This is a modified version of the KS0108.bas module provided by the standard graphics library of Swordfish basic compiler. This driver has been modified namely for making it compatible with screens that have 192x64 pixels size. These screens have 3 CS lines instead of the usually 2 CS lines found on most 128x64 screens.

This version introduces two major options:

#option GLCD_CS_COUNT = 2 : this option can have two values: 2 or 3

#option GLCD_SIDE_SIZE = 64 : GLCS screens have one, two or more controllers. Each controller controls a part (side) of the screen. Usually, blocks have size of 64 pixels, but some screens have blocks with only 60 pixels. This option let you define your block size.

two other options have been added to override the default size of GLCD screen handled by the KS0108 driver.

#option GLCD_SCREEN_WIDTH = 128 : Screen Width in Pixels

#option GLCD_SCREEN_HEIGHT = 64 : Screen Width in Pixels

Details Of Use

The use of the driver is similar to the existing KS0108 driver. By defaults, all options are set for a screen of 128x64 and only 2 CS lines (CS1, CS2).

For 192x64 GLCD with 3 CS (CS1, CS2, CS3) lines, user has only to add otpions related to screen width and count of CS lines.

Device = 18F452
Clock = 8

#option GLCD_CS_COUNT = 3 '<<<< Three CS lines CS1, CS2, CS3
#option GLCD_SCREEN_WIDTH = 192  '<<<< The screen width to override the std 128 width

Include "GLCD.bas"
Include "FixedFont.bas"
Include "Arial.bas"
Include "Garamond.bas"
Include "Tahoma.bas"
Include "Verdana.bas"
Include "Times.bas"

ADCON1 = $07 // PORTE as digital (GLCD)

GLCD.Cls	
GLCD.SetFont(ArialBold)
GLCD.WriteAt(4,11,"Arial Font")
GLCD.SetFont(GaramondBold)
GLCD.WriteAt(4,20,"Garamond Font")
GLCD.SetFont(TahomaBold)
GLCD.WriteAt(4,30,"Tahoma Font")
GLCD.SetFont(VerdanaBold)
GLCD.WriteAt(4,40,"Verdana Font")
GLCD.SetFont(TimesNewRomanBold)
GLCD.WriteAt(4,50,"Times New Roman")

Thats all.

The screen that has been used for tests is referenced as HY-19264C--201. it's a GLCD 192x64 pixels with CS1, CS2, C3 lines, and an RST signal.

Device = 18F452
Clock = 8
{
Sample code that handles GLCD 192x64 with CS1, CS2, CS3 and RST Lines, 
ref : HY-19264C--201 
This code has been tested on an EasyPIC3 board. So do not forgot to change 
pins options if you use another hardware
}

#option GLCD_CS_COUNT = 3
#option GLCD_SCREEN_WIDTH = 192

#option GLCD_DATA = PORTD        // data port
#option GLCD_RS = PORTC.2        // RS pin
#option GLCD_EN = PORTC.0        // EN pin
#option GLCD_RW = PORTC.1        // RW pin
#option GLCD_CS1 = PORTB.3       // chip select
#option GLCD_CS2 = PORTB.1       // chip select
#option GLCD_CS3 = PORTB.0       // chip select
#option GLCD_ASPECT_RATIO = 75   // aspect ratio, smaller number will squeeze y for GLCD circles and box
#option GLCD_INIT_DELAY = 100    // initialisation delay (ms)
#option GLCD_INVERT_CS = false   // invert CS lines... 

#option GLCD_RST = PORTB.2       // RST select


Include "GLCD.bas"
Include "FixedFont.bas"
Include "Arial.bas"
Include "Garamond.bas"
Include "Tahoma.bas"
Include "Verdana.bas"
Include "Times.bas"

ADCON1 = $07 // PORTE as digital (GLCD)

GLCD.Cls	
GLCD.SetFont(ArialBold)
GLCD.WriteAt(4,11,"Arial Font")
GLCD.SetFont(GaramondBold)
GLCD.WriteAt(4,20,"Garamond Font")
GLCD.SetFont(TahomaBold)
GLCD.WriteAt(4,30,"Tahoma Font")
GLCD.SetFont(VerdanaBold)
GLCD.WriteAt(4,40,"Verdana Font")
GLCD.SetFont(TimesNewRomanBold)
GLCD.WriteAt(4,50,"Times New Roman")

How To Integrate With Swordfish

The Graphic LCD components of Swordfish are in Mecanique\Swordfish\Library\GLCD, the existing file KS0108.bas should NOT BE MODIFIED.

Instead, copy this modified driver in your Mecanique\Swordfish\UserLibrary. Swordfish compiler will use this version instead of the standard one, and this keeps this file safe if you update your Swordfish Basic libraries.


Please, feel free to contact us if you find any problem with this driver.


The modified KS0108.bas Module code:-

{
*************************************************************************
*  Name    : KS0108.BAS                                                 *
*  Author  : David John Barker                                          *
*  Notice  : Copyright (c) 2006 Mecanique                               *
*          : All Rights Reserved                                        *
*  Date    : 02/05/2006                                                 *
*  Version : 1.0                                                        *
*  Modified: Eric Couratier, Ahmed Lazreg, Sept 12th, 2007              *
*  Notes   : This is a modified version of KS0108 driver.               *
*            It handles screens with both 2 (default) or 3 CS lines.    *
*            added options:                                             *
*            GLCD_SIDE_SIZE (def. 64) Screen Block (side) size. For     *
*                           Some screens = 60 (check your datasheet).   *
*            GLCD_SCREEN_WIDTH (def. 128) : Screen Width in Pixels      *
*            GLCD_SCREEN_HEIGHT (def. 64) : Screen Height in Pixels     *
*            GLCD_CS_COUNT (def. 2)       : Count of CS Lines, 2 or 3   *
*                                                                       *    
*************************************************************************
}

Module KS0108

// import the graphics module...
#define GLCD_PIXEL_01
#define GLCD_COLOR_01
#define GLCD_XY_08
Include "Graphics.bas"   
Include "system.bas"   

#option GLCD_SIDE_SIZE = 64      // Screen Block (side) size. For Some screens = 60 (check your datasheet).
#option GLCD_SCREEN_WIDTH = 128  // Screen Width in Pixels
#option GLCD_SCREEN_HEIGHT = 64  // Screen Height in Pixels

#option GLCD_CS_COUNT = 2    // Count of CS Lines. Must be 2 or 3


// default module options - user options can override these values...
#option GLCD_DATA = PORTD        // data port
#option GLCD_RS = PORTC.1        // RS pin
#option GLCD_EN = PORTE.0        // EN pin
#option GLCD_RW = PORTC.0        // RW pin
#option GLCD_CS1 = PORTE.1       // chip select
#option GLCD_CS2 = PORTE.2       // chip select
#option GLCD_ASPECT_RATIO = 75   // aspect ratio, smaller number will squeeze y for GLCD circles and box
#option GLCD_INIT_DELAY = 100    // initialisation delay (ms)
#option GLCD_INVERT_CS = false   // invert CS lines... 

#if GLCD_CS_COUNT = 3
    #if Not (GLCD_CS_COUNT in (2,3))
        #error GLCD_CS_COUNT, "Invalid option. GLCD GLCD_CS_COUNT must be 2 or 3."
    #endif
    #option GLCD_CS3 = PORTC.2       // chip select
#endif


// validate data port...
#if IsOption(GLCD_DATA) 
   #if Not IsValidPort(GLCD_DATA)
      #error GLCD_DATA, "Invalid option. DATA must be a valid port name."
   #endif
   #ifdef GLCD_DATA@ Then
      #error GLCD_DATA@, "Invalid option. DATA port cannot be a single bit value."
   #endif
#endif

// validate RS pin...
#if IsOption(GLCD_RS) And Not IsValidPortPin(GLCD_RS) 
   #error GLCD_RS, "Invalid option. RS must be a valid port pin."
#endif

// validate EN pin...
#if IsOption(GLCD_EN) And Not IsValidPortPin(GLCD_EN) 
   #error GLCD_EN, "Invalid option. EN must be a valid port pin."
#endif

// validate RW pin...
#if IsOption(GLCD_RW) And Not IsValidPortPin(GLCD_RW) 
   #error GLCD_RW, "Invalid option. RW must be a valid port pin."
#endif

// validate CS1 pin...
#if IsOption(GLCD_CS1) And Not IsValidPortPin(GLCD_CS1) 
   #error GLCD_CS1, "Invalid option. CS1 must be a valid port pin."
#endif

// validate CS2 pin...
#if IsOption(GLCD_CS2) And Not IsValidPortPin(GLCD_CS2) 
   #error GLCD_CS2, "Invalid option. CS2 must be a valid port pin."
#endif

// validate CS3 pin...
#if IsOption(GLCD_CS3) And Not IsValidPortPin(GLCD_CS3) 
   #error GLCD_CS3, "Invalid option. CS3 must be a valid port pin."
#endif

// validate initialisation delay...
#if IsOption(GLCD_INIT_DELAY)
   #if Not (GLCD_INIT_DELAY in (0 to 1000))
      #error GLCD_INIT_DELAY, "Invalid option. GLCD initialize delay must be between 0 and 1000 (ms)."
   #endif
#endif 

// validate invert CS...
#if IsOption(GLCD_INVERT_CS)
   #if Not (GLCD_INVERT_CS in (true, false))
      #error GLCD_INVERT_CS, "Invalid option. GLCD invert CS must be true or false."
   #endif
#endif 

// validate RST pin...
#if IsOption(GLCD_RST) And Not IsValidPortPin(GLCD_RST) 
   #error GLCD_RST, "Invalid option. RST must be a valid port pin."
#endif

// now create Data TRIS...
#option _GLCD_DATA_TRIS = GetTRIS(GLCD_DATA)

// GLCD width and height...
Public Const
   GLCDWidth = GLCD_SCREEN_WIDTH,
   GLCDHeight = GLCD_SCREEN_HEIGHT,
   GLCDSideSize = GLCD_SIDE_SIZE, 
   GLCDSideSizex2 = GLCD_SIDE_SIZE*2
// x, y position...
Public Dim
   Pos As TPosition

// KS0108 commands...   
Const
   cmdOn = $3F,
   cmdOff = $3E,
   cmdPage = $B8,
   cmdColumn = $40,
   cmdRam = $C0,
   GLCDDelay = GLCD_INIT_DELAY     

// port and pin settings, these are brought into
// the program by using the above options...
Dim        
   DATA As GLCD_DATA,           // data in (PORT) 
   TRISData As _GLCD_DATA_TRIS, // data TRIS
   RS As GLCD_RS.GLCD_RS@,      // RS pin (instruction or data)
   EN As GLCD_EN.GLCD_EN@,      // EN pin
   RW As GLCD_RW.GLCD_RW@,      // RW pin (read or write)
   CS1 As GLCD_CS1.GLCD_CS1@,   // chip1 select
   CS2 As GLCD_CS2.GLCD_CS2@    // chip2 select
#if GLCD_CS_COUNT = 3
Dim CS3 As GLCD_CS3.GLCD_CS3@    // chip3 select
#endif

#if IsOption(GLCD_RST)
Dim
   RST As GLCD_RST.GLCD_RST@    // RST pin
#endif
{
****************************************************************************
* Name    : CheckPage (PRIVATE)                                            *
* Purpose : Toggles between left and right half of GLCD                    *
****************************************************************************
}     
#if GLCD_CS_COUNT = 2
Sub CheckPage()
   #if GLCD_INVERT_CS
   If Pos.x < GLCDSideSize Then
      CS1 = 1
      CS2 = 0	
   Else
      CS1 = 0
      CS2 = 1
   EndIf
   #else
   If Pos.x < GLCDSideSize Then
      CS1 = 0
      CS2 = 1	
   Else
      CS1 = 1
      CS2 = 0
   EndIf
   #endif
End Sub
#endif

#if GLCD_CS_COUNT = 3
Sub CheckPage()
   #if GLCD_INVERT_CS
   If Pos.x < GLCDSideSize Then
      CS1 = 1
      CS2 = 0	
      CS3 = 0
   ElseIf Pos.x < GLCDSideSizex2 Then
      CS1 = 0
      CS2 = 1
      CS3 = 0
   Else
      CS1 = 0
      CS2 = 0
      CS3 = 1
   EndIf
   #else
   If Pos.x < GLCDSideSize Then
      CS1 = 0
      CS2 = 1	
      CS3 = 1
   ElseIf Pos.x < GLCDSideSizex2 Then
      CS1 = 1
      CS2 = 0
      CS3 = 1
   Else
      CS1 = 1
      CS2 = 1
      CS3 = 0
   EndIf
   #endif
End Sub
#endif


{
****************************************************************************
* Name    : StrobeEN (PRIVATE)                                             *
* Purpose : Strobe the GLCD EN pin                                         *
****************************************************************************
}    
Inline Sub StrobeEN()
   EN = 1
   DelayUS(1)
   EN = 0 
End Sub
{
****************************************************************************
* Name    : GLCDData (PRIVATE)                                             *
* Purpose : Switch to GLCD data mode                                       *
****************************************************************************
}    
Inline Sub GLCDData()
   RS = 1
End Sub
{
****************************************************************************
* Name    : GLCDInst (PRIVATE)                                             *
* Purpose : Switch to GLCD instruction mode                                *
****************************************************************************
}    
Inline Sub GLCDInst()
   RS = 0	 
End Sub
{
****************************************************************************
* Name    : GLCDRead (PRIVATE)                                             *
* Purpose : Set GLCD to read mode                                          *
****************************************************************************
}    
Inline Sub GLCDRead()
   RW = 1
End Sub
{
****************************************************************************
* Name    : GLCDWrite (PRIVATE)                                            *
* Purpose : Set GLCD to write mode                                         *
****************************************************************************
}    
Inline Sub GLCDWrite()
   RW = 0	 
End Sub
{
****************************************************************************
* Name    : WaitForIdle (PRIVATE)                                          *
* Purpose : Block further GLCD access until signalled ready.               *
****************************************************************************
}     
Sub WaitForIdle()
   Dim Timeout As Byte
   Timeout = $FF 
   GLCDInst	          // instruction data
   GLCDRead           // read mode
   TRISData = $FF     // set data bus to input
   Repeat
      ClrWDT
      StrobeEN        
      Dec(Timeout)
   Until (DATA.7 = 0) Or (Timeout = 0)
End Sub 
{
****************************************************************************
* Name    : SetData (PRIVATE)                                              *
* Purpose : Write a data byte to GLCD                                      *
****************************************************************************
}     
Sub SetData(pData As Byte) 
   WaitForIdle      // block until not busy
   GLCDData         // access display RAM data
   GLCDWrite        // write mode
   TRISData = $00   // set data bus to output
   DATA = pData     // write to the bus
   StrobeEN         // write to GLCD
End Sub
{
****************************************************************************
* Name    : GetData (PRIVATE)                                              *
* Purpose : Read byte from GLCD                                            *
****************************************************************************
}     
Function GetData() As Byte
   WaitForIdle      // block until not busy
   TRISData = $FF   // set data bus to input
   GLCDData         // access display RAM data
   GLCDRead         // read mode
   StrobeEN         // latch data
   Result = DATA    // get the data
End Function  
{
****************************************************************************
* Name    : Command (PRIVATE)                                              *
* Purpose : Write a command byte to GLCD                                   *
****************************************************************************
}     
Sub Command(pCommand As Byte) 
   WaitForIdle          // block until not busy
   GLCDInst             // instruction mode
   GLCDWrite            // write mode
   TRISData = $00       // set data bus to output
   DATA = pCommand      // write to the bus
   StrobeEN             // write to GLCD
End Sub
{
****************************************************************************
* Name    : SetPosition (PRIVATE)                                          *
* Purpose : Set GLCD x and y positions                                     *
****************************************************************************
}     
Sub SetPosition()
   Dim xPos As Byte
   CheckPage                   // set correct page
   xPos = Pos.x Mod GLCDSideSize
   Command(cmdColumn Or xPos)  // set x position   
   Command(cmdPage Or Pos.y)   // set y position
End Sub 
{
****************************************************************************
* Name    : WriteByte                                                      *
* Purpose : Write a byte at x, page                                        *
****************************************************************************
} 
Public Sub WriteByte(pValue As Byte)
   SetPosition
   SetData(pValue)
End Sub 
{
****************************************************************************
* Name    : ReadByte                                                       *
* Purpose : Read a byte at x, page                                         *
****************************************************************************
} 
Public Function ReadByte() As Byte
   SetPosition
   GetData
   Result = GetData
End Function
{
****************************************************************************
* Name    : SetPixel                                                       *
* Purpose : Set pixel at pixel location x,y                                *
****************************************************************************
} 
Public Sub SetPixel(pX, pY As Byte)
   Dim YBit As Byte
   Dim Pixel As Byte

   If (pX < GLCDWidth) And (pY < GLCDHeight) Then

      YBit = 1 << (pY Mod 8)
      Pos.y = pY / 8
      Pos.x = pX

      SetPosition
      GetData
      Pixel = GetData  


      // pen is white...
      If Pen.Color = 0 Then
         If Pen.Mode = pmCopy Then
            yBit = Not yBit
            Pixel = Pixel And YBit
         EndIf

      // pen is black...
      Else

         // pen copy or merge...
         If Pen.Mode <> pmXOR Then
            Pixel = Pixel Or yBit

         // pen XOR
         Else
            If (Pixel And YBit) = 0 Then
               Pixel = Pixel Or yBit
            Else
               yBit = Not yBit
               Pixel = Pixel And YBit
            EndIf      
         EndIf
      EndIf

      SetPosition
      SetData(Pixel)
   EndIf
End Sub
{
****************************************************************************
* Name    : GetPixel                                                       *
* Purpose : Return pixel colour at pixel position x, y                     *
****************************************************************************
}
Public Function GetPixel(pX, pY As Byte) As TColor
   Dim Pixel As Byte
   Pos.y = pY / 8
   Pos.x = pX
   SetPosition
   GetData
   Pixel = GetData >> (pY Mod 8) 
   Result = Pixel.0  
End Function
{
****************************************************************************
* Name    : Cls                                                            *
* Purpose : Clear the GLCD screen                                          *
****************************************************************************
} 
Public Sub Cls()
   Dim x, y, PagesCount As Byte
   PagesCount = GLCDHeight / 8 
   // enable both displays...
   #if GLCD_INVERT_CS 
   CS1 = 1 
   CS2 = 1
   #if GLCD_CS_COUNT = 3
   CS3 = 1
   #endif
   #else
   CS1 = 0 
   CS2 = 0
   #if GLCD_CS_COUNT = 3
   CS3 = 0
   #endif
   #endif	
   Command(cmdOff) 
   y = 0
   Repeat	
	  Command(cmdPage Or y)
	  x = 0
      Repeat	
		 Command(cmdColumn Or x)	
         SetData($00)
         Inc(x)
      Until x > (GLCDSideSize-1)
	  Inc(y)
   Until y > (PagesCount-1)	
   Command(cmdRam)
   Command(cmdOn)           
End Sub 
{
****************************************************************************
* Name    : Initialize                                                     *
* Purpose : Configure the GLCD before use                                  *
****************************************************************************
} 
Sub Initialize()
#if IsOption(GLCD_RST) 
   Output(RST)
   RST = 1
#endif
   Pos.x = 0
   Pos.y = 0  
   DelayMS(GLCDDelay) // start up delay, allow GLCD to settle
   Output(EN)         // enable is low
   Output(RS)         // data or instruction is low (command mode)
   Output(RW)         // read/write is low
   Output(CS1)        // screen (1)
   Output(CS2)        // screen (2)
#if GLCD_CS_COUNT = 3
   Output(CS3)        // screen (3)
#endif
   Command(cmdRam)
   Command(cmdOn) 
End Sub 

// configure the module
Initialize