As I promised, here it is the library for ST7565R controller!
![Smile :-)](./images/smilies/icon_smile.gif)
It uses 6800 interface mode, and supports all graphics shape commands, as well as SetImage, Write Text , etc.
Add ST7565R controller into device select option in GLCD.bas:
Code: Select all
// validate model...
#option GLCD_MODEL = ST7565R
#IF Not (GLCD_MODEL in (KS0108, S1D10605, S1D15G00, SED1520, S1D13700, KS0713, SSD1306,ST7565R))
#ERROR GLCD_MODEL, "Invalid option. GLCD model not recognized."
#ENDIF
Then add the ST7565R definition in GLCD.bas:
Code: Select all
// import ST7565R driver...
#IF GLCD_MODEL = ST7565R
Include "ST7565R.bas"
Public Dim
Pos As ST7565R.Pos,
Cls As ST7565R.Cls,
SetPixel As ST7565R.SetPixel,
WriteByte As ST7565R.WriteByte,
ReadByte As ST7565R.ReadByte,
GetPixel As ST7565R.GetPixel,
InitLCD As ST7565R.InitLCD
#ENDIF
Write program named "ST7565R.bas" and store it into your swordfish user library folder:
Code: Select all
Module ST7565R
// NOTE: This driver uses 6800 interface mode, therefore the signals must be read as follow:
// RD -> E
// WR -> RW
// import the graphics module...
#define GLCD_PIXEL_01
#define GLCD_COLOR_01
#define GLCD_XY_08
Include "Graphics.bas"
Include "system.bas"
// default module options - user options can override these values...
#option GLCD_DATA = PORTD // data port
#option GLCD_RS = PORTA.1 // RESET pin
#option GLCD_WR = PORTA.3 //
#option GLCD_RD = PORTA.4 //
#option GLCD_CS = PORTA.0 // chip select
#option GLCD_DC = PORTA.2
#option GLCD_ASPECT_RATIO = 80 // aspect ratio, smaller number will squeeze y for GLCD circles and box
#option GLCD_INIT_DELAY = 100 // initialisation delay (ms)
// 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
#if IsOption(GLCD_RS) And Not IsValidPortPin(GLCD_RS)
#error GLCD_RS, "Invalid option. RS must be a valid port pin."
#endif
#if IsOption(GLCD_WR) And Not IsValidPortPin(GLCD_WR)
#error GLCD_WR, "Invalid option. WR must be a valid port pin."
#endif
#if IsOption(GLCD_RD) And Not IsValidPortPin(GLCD_RD)
#error GLCD_RD, "Invalid option. RD must be a valid port pin."
#endif
#if IsOption(GLCD_CS) And Not IsValidPortPin(GLCD_CS)
#error GLCD_CS, "Invalid option. CS must be a valid port pin."
#endif
#if IsOption(GLCD_DC) And Not IsValidPortPin(GLCD_DC)
#error GLCD_DC, "Invalid option. DC 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
// now create Data TRIS...
#option _GLCD_DATA_TRIS = GetTRIS(GLCD_DATA)
// GLCD width and height...
Public Const
GLCDWidth = 128,
GLCDHeight = 64
// x, y position...
Public Dim
Pos As TPosition
Const
cmdPage = $B0,
GLCDDelay = GLCD_INIT_DELAY
// port and pin settings, these are brought into
// the program by using the above options...
Dim
DATA As GLCD_DATA,
TRISData As _GLCD_DATA_TRIS,
RS As GLCD_RS.GLCD_RS@,
RD As GLCD_RD.GLCD_RD@,
WR As GLCD_WR.GLCD_WR@,
CS As GLCD_CS.GLCD_CS@,
DC As GLCD_DC.GLCD_DC@
{
****************************************************************************
* Name : WaitForIdle (PRIVATE) *
* Purpose : Block further GLCD access until signalled ready. *
****************************************************************************
}
Sub WaitForIdle()
TRISData = $FF
DC=0
WR=1
CS=0
RD=1
While DATA.7=1
Wend
RD=0
'CS=1
End Sub
{
****************************************************************************
* Name : SetData (PRIVATE) *
* Purpose : Write a data byte to GLCD *
****************************************************************************
}
Sub SetData(pData As Byte)
WaitForIdle
TRISData=$00
DATA = pData
DC=1
WR=0
'CS=0
RD=1
DelayUS(1)
RD=0
CS=1
End Sub
{
****************************************************************************
* Name : GetData (PRIVATE) *
* Purpose : Read byte from GLCD *
****************************************************************************
}
Function GetData() As Byte
WaitForIdle
TRISData = $FF
DC=1
WR=1
'CS=0
RD=1
DelayUS(1)
Result = DATA // get the data
RD=0
CS=1
End Function
{
****************************************************************************
* Name : Command (PRIVATE) *
* Purpose : Write a command byte to GLCD *
****************************************************************************
}
Sub Command(pCommand As Byte)
WaitForIdle
TRISData=$00
DATA = pCommand
DC=0
WR=0
'CS=0
RD=1
DelayUS(1)
RD=0
CS=1
End Sub
{
****************************************************************************
* Name : SetPosition (PRIVATE) *
* Purpose : Set GLCD x and y positions *
****************************************************************************
}
Sub SetPosition()
Command(cmdPage Or Pos.y) // set y position
Command($00 + (Pos.x And $0F))
Command($10+((Pos.x>>4)And $0F))
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 As Byte
For y=$b0 To $b7
Command(y)
Command($00)
Command($10)
For x=0 To 127
SetData(0)
Next
Next
End Sub
{
****************************************************************************
* Name : Initialize *
* Purpose : Configure the OLED before use *
****************************************************************************
}
Public Sub InitLCD()
Pos.x = 0
Pos.y = 0
DelayMS(GLCDDelay)
Output(RD)
Output(RS)
Output(WR)
Output(CS)
Output(DC)
Low(RS) DelayMS(10) High(RS)
DelayMS(50)
RD=0 WR=0 DC=1 CS=1
Command($ae)
Command($a1) //Bias set $a3
Command($a0) //ADC Select -> Normal
Command($c8) //Common Output Mode Select -> Normal
Command($25) // Vo Voltage Reg VALUE
Command($81) // Electronic Volume Control
Command($24) // Value $15 (0-64)
Command($2f) // Power Controller ALL ON internals
Command($40) //Start address 00
Command($B0) // Page 0
Command($af)
End Sub
In your program enter:
and then in main code start with:
And you are ready to have fun!
(Use #option to declare the ST7565R pinouts in your setup)
![Image](http://mtech-bg.com/st7565r.jpg)