'*****************************************************************************
'* Name : IST3020 Library *
'* Author : Simeon Kartalov *
'* Date : 11/07/2014 *
'* Version : 1.0 *
'* Notes : SPI mode *
'*****************************************************************************
#define GLCD_PIXEL_01
#define GLCD_COLOR_01
#define GLCD_XY_08
Module IST3020SPI
imports GlcdGraphics
' default module options - user options can override these values...
#option GLCD_RS = PORTB.14 ' RS pin
#option GLCD_SCK = PORTB.6 ' SCK pin
#option GLCD_SDI = PORTB.7 ' SDI pin
#option GLCD_CS = PORTB.15 ' chip select
#option GLCD_ASPECT_RATIO = 100 ' 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...
' 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 SCK pin...
#if IsOption(GLCD_SCK) And Not IsValidPortPin(GLCD_SCK)
#error GLCD_SCK, "Invalid option. SCK must be a valid port pin."
#endif
' validate SDI pin...
#if IsOption(GLCD_SDI) And Not IsValidPortPin(GLCD_SDI)
#error GLCD_SDI, "Invalid option. SDI must be a valid port pin."
#endif
' validate CS pin...
#if IsOption(GLCD_CS) And Not IsValidPortPin(GLCD_CS)
#error GLCD_CS, "Invalid option. CS 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
' GLCD width and height...
public const Width = 192
public const Height = 64
' x, y position...
Public Pos As GraphicPosition
' IST3020 commands...
private Const _commandOn = &HAF
private Const _commandOff = &HAE
private Const _commandPage = &HB0
private Const _commandColumnMSB = &H10
private Const _commandColumnLSB = &H00
private Const _commandRam = &H40
private Const _glcdDelayMS = GLCD_INIT_DELAY
' port and pin settings, these are brought into
' the program by using the above options...
private _rs As GLCD_RS.GLCD_RS@ ' _rs pin (instruction or data)
private _sck As GLCD_SCK.GLCD_SCK@ ' _sck pin
private _sdi As GLCD_SDI.GLCD_SDI@ ' _sdi pin
private _cs As GLCD_CS.GLCD_CS@ ' chip select
#if IsOption(GLCD_RST)
private _rst As GLCD_RST.GLCD_RST@ ' _rst pin
#endif
private sub delay()
low(_sck)
Delayus(1)
high(_sck)
delayus(1)
end sub
'*****************************************************************************
'* Name : Strobe *
'* Purpose : output SPI data *
'*****************************************************************************
private Sub Strobe(data as byte)
_sdi=data.7:Delay()
_sdi=data.6:Delay()
_sdi=data.5:Delay()
_sdi=data.4:Delay()
_sdi=data.3:Delay()
_sdi=data.2:Delay()
_sdi=data.1:Delay()
_sdi=data.0:Delay()
End Sub
'*****************************************************************************
'* Name : WaitForIdle *
'* Purpose : Block further GLCD access until signalled ready. *
'*****************************************************************************
private Sub WaitForIdle()
Dim Timeout As Byte = &HFF
_rs=0 ' instruction data
input(_sdi)
do
Timeout = Timeout - 1
'ClearWDT
loop Until (_sdi = 0) Or (Timeout = 0)
output(_sdi)
End Sub
private sub waitforidle_alt()
delayms(10)
end sub
'*****************************************************************************
'* Name : SetData *
'* Purpose : Write a data byte to GLCD *
'*****************************************************************************
private Sub SetData(value As byte)
_cs=0
WaitForIdle ' block until not busy
_rs=1 ' access display RAM data
Strobe(value) ' write to GLCD
_cs=1
End Sub
'*****************************************************************************
'* Name : Command *
'* Purpose : Write a command byte to GLCD *
'*****************************************************************************
private Sub Command(command As byte)
_cs=0
WaitForIdle ' block until not busy
_rs=0 ' instruction mode
Strobe(command) ' write to GLCD
_cs=1
End Sub
'*****************************************************************************
'* Name : SetPosition *
'* Purpose : Set GLCD x and y positions *
'*****************************************************************************
private Sub SetPosition()
Command(_commandPage Or Pos.y) ' set y position
Command(_commandColumnMSB Or pos.x >> 4) ' set x position MSB
Command(_commandColumnLSB Or (Pos.x and &H0F)) ' set x position LSB
End Sub
'*****************************************************************************
'* Name : WriteByte *
'* Purpose : Write a byte at x, page *
'*****************************************************************************
public Sub WriteByte(value As Byte)
SetPosition
SetData(value)
End Sub
'*****************************************************************************
'* Name : SetPixel *
'* Purpose : Set pixel at pixel location x,y *
'*****************************************************************************
Public Sub SetPixel(x as byte, y As Byte)
Dim YBit As Byte
Dim Pixel As Byte
If (x < Width) And (y < Height) Then
YBit = 1 << (y Mod 8)
Pos.y = y / 8
Pos.x = x
' SetPosition
'
Pixel = 0
' pen is white...
If Pen.Color = 0 Then
If Pen.Mode = PenMode.IsCopy Then
yBit = Not yBit
Pixel = Pixel And YBit
end if
' pen is black...
Else
' pen copy or merge...
If Pen.Mode <> PenMode.IsXOR 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
end if
end if
end if
SetPosition
SetData(Pixel)
end if
End Sub
'*****************************************************************************
'* Name : GetPixel *
'* Purpose : Return pixel colour at pixel position x, y *
'*****************************************************************************
Public Function GetPixel(x as byte, y As Byte) As GraphicColor
'Dim Pixel As Byte
' Pos.y = y / 8
'Pos.x = x
'SetPosition
' GetData
'Pixel = GetData >> (y Mod 8)
'GetPixel = Pixel.0
GetPixel=0
End Function
'*****************************************************************************
'* Name : Clear *
'* Purpose : Clear the GLCD screen *
'*****************************************************************************
Public Sub Clear()
Dim y As Byte
dim xx as ushort
_cs=0
'Command(_commandOff)
for y=0 to 7
Command(_commandPage or y)
command(_commandColumnMSB)
command(_commandColumnLSB)
for xx=0 to width-1
setdata($00)
next
next
'Command(_commandOn)
_cs=1
End Sub
'*****************************************************************************
'* Name : Initialize *
'* Purpose : Configure the GLCD before use *
'*****************************************************************************
private Sub Main()
#if IsOption(GLCD_RST)
Output(_rst)
_rst = 1
#endif
Output(_rs)
Output(_cs)
output(_sdi):output(_sck)
_sck=1:_sdi=0:_cs=0
_rs=0
_rst=1
_rst=0
delayms(10)
_rst=1
delayms(100)
Pos.x = 0
Pos.y = 0
DelayMS(_glcdDelayMS) ' start up delay, allow GLCD to settle
Command($ab) 'Build in osscilator: ON
delayms(100)
Command($a0) 'ADC=0 (normal direction)
Command($c8) 'SHL=1 (reverse direction)
Command($a2) 'LCD BIAS: 1/9
Command($2c) 'VC
delayms(10)
Command($2e) 'VR
delayms(10)
Command($2f) 'VF
delayms(10)
Command($20) 'Regulator resistor: R1
Command($81) 'Set reference voltage mode
command(40) '=32 (defauls)
command($40) 'SET START LINE
Command($af) 'Display ON
command($91) 'contrast
command($0F)
command($92)
command($1F)
End Sub
end moduleReturn to Development Environment
Users browsing this forum: No registered users and 2 guests