Hello,
Module KS0108 with 3 CS and my TurnON and TurnOFF SUB
Code: Select all
{
*************************************************************************
* 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 : TurnOFF *
* Purpose : Clear the GLCD screen *
****************************************************************************
}
Public Sub TurnOFF()
// 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)
End Sub
{
****************************************************************************
* Name : TurnON *
* Purpose : Clear the GLCD screen *
****************************************************************************
}
Public Sub TurnON()
// 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(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
The GLCD.BAS module where I set aliases
Code: Select all
Module GLCD
// validate model...
#option GLCD_MODEL = KS0108
#if Not (GLCD_MODEL in (KS0108, S1D10605, S1D15G00, SED1520, S1D13700, KS0108_SPI))
#error GLCD_MODEL, "Invalid option. GLCD model not recognized."
#endif
// import KS0108 driver...
#if GLCD_MODEL = KS0108
Include "KS0108.bas"
Public Dim
Pos As KS0108.Pos,
Cls As KS0108.Cls,
SetPixel As KS0108.SetPixel,
WriteByte As KS0108.WriteByte,
ReadByte As KS0108.ReadByte,
GetPixel As KS0108.GetPixel,
TurnON As KS0108.TurnON,
TurnOFF As KS0108.TurnOFF
#endif
my calling test program
Code: Select all
Device = 18F46K22
Clock = 16
Include "intosc.bas"
#option GLCD_MODEL = KS0108
#option GLCD_DATA = PORTD // data port
#option GLCD_RS = PORTC.2 // RS pin
#option GLCD_EN = PORTE.0 // EN pin
#option GLCD_RW = PORTC.1 // RW pin
#option GLCD_CS1 = PORTE.1 // chip select
#option GLCD_CS2 = PORTE.2 // chip select
#option GLCD_INIT_DELAY = 100
#option GLCD_INVERT_CS = true // invert CS lines...
Include "glcd.bas"
Include "convert.bas"
Include "graphics.bas"
Include "FixedFont.bas"
DelayMS(1000)
GLCD.Cls
GLCD.SetFont(Fixed)
GLCD.WriteStr(10,2,"Chiamate Attive")