Page 1 of 2
GLCD Graphic LCD - Using Swordfish Basic
Posted: Fri May 13, 2011 5:08 am
by bitfogav
Hi all, Ive been interested in GLCD for awhile now and always thought they was some complicated devices. After receiving my GLCD 128x64 display recently (with a ks0108 driver) I got down to it and started making a demo programme using swordfish.
The displays seem quite easy to use, you can easily draw lines, circles, rectangles, ellipse, images, and even text.
Below is a demo picture of my GLCD using all of the above.. also my demo code below which ive commented to show what is going on, any questions please ask!.

- bitfogavsGLCD1.JPG (33.34 KiB) Viewed 27043 times
Code: Select all
Device = 18F2620 // select our device
Clock = 32 // internal 8Mhz * 4 PLL 32Mhz Clock
Config OSC = INTIO67 // configure OSC for internal
// User options
#Option GLCD_MODEL = KS0108 // GLCD driver
#option GLCD_SCREEN_WIDTH = 128 // Screen Width in Pixels
#option GLCD_SCREEN_HEIGHT = 64 // Screen Height in Pixels
#Option GLCD_DATA = PORTB // data port
#Option GLCD_RS = PORTC.0 // RS pin
#Option GLCD_RW = PORTC.1 // RW pin
#Option GLCD_EN = PORTC.2 // EN pin
#Option GLCD_CS1 = PORTC.3 // chip select
#Option GLCD_CS2 = PORTC.4 // chip select
#Option GLCD_RST = PORTC.5 // reset pin
#Option GLCD_ASPECT_RATIO = 100 // aspect ratio, smaller number will squeeze y for GLCD circles and box
#Option GLCD_INIT_DELAY = 50 // initialisation delay (ms)
#Option GLCD_INVERT_CS = true // invert CS lines...
// Modules
Include "glcd.bas" // main GLCD module
Include "graphics.bas" // support for GLCD
Include "Bitmaps.bas" // support for GLCD
Include "utils.bas" // support to make all MCU pins Digital
Include "Arial.bas" // ArialFont for GLCD
// PORT setup and PLL Multiplier (set internal Osc to 32Mhz)
TRISA = %00000000 //
TRISB = %00000000 // Set all Ports output
TRISC = %00000000 //
OSCCON = %01111100 // Sets up the internal oscillator for 8Mhz
OSCTUNE.6 = 1 // Frequency Multiplier PLL for INTOSC Enable bit (Enabled)
SetAllDigital // set MCU pins as digital
// Demo pattern on GLCD
GLCD.Cls // clear GLCD display
GLCD.Rectangle(0,0,127,63) // draws a rectangle on all corners of display (X,Y,X,Y)
GLCD.Line(6,6,20,60) // draws line (left of screen)
GLCD.Line(125,2,105,2) // draws line (top right of screen)
GLCD.Ellipse(40,32,20,30) // draws Ellipse
GLCD.Circle(94,25,20) // draws Circle
GLCD.SetFont(ArialBold) // set font BOLD
GLCD.WriteAt(60,50,"Bitfogav") // write String to GLCD at x=60 y=50
GLCD.SetImage(65,4,BatteryImage, cmXOR) // draws image on GLCD (battery Image from Bitmap module)
// Main Programme
While true
// Flash LED
Output(PORTA.0)
High(PORTA.0)
DelayMS(1000)
Low(PORTA.0)
DelayMS(1000)
Wend
Re: GLCD Graphic LCD - Using Swordfish Basic
Posted: Fri May 13, 2011 8:26 pm
by brad
I'm so glad that you posted this! I didn't realize that swordfish had built-in libraries for these displays. I certainly have ideas for them and you've got me excited to use them.
I was thinking, what if you code a simple loop that increments through the various values that determine the size and position of a circle. You could make it move around the screen and change size. That would be cool!
What plans have you got for it?
Re: GLCD Graphic LCD - Using Swordfish Basic
Posted: Sat May 14, 2011 12:08 am
by bitfogav
Hehe! Brad you must be on the same wavelength as me?!
Ive actually been working on this piece of code that does exacly what you just said, but ive actually used the random number Gen to give me random positions on the screen with random size circles.
Your need to download this user module for the random number gen used in my code if you want to test it out? and put the random number gen2 (.bas) file into your swordfish UserLibrary directory.
brad wrote:What plans have you got for it?
Im definitely thinking about making a game of some sort with this display..
https://youtu.be/qgkEDm6B_8c
Code: Select all
{
*****************************************************************************
* Name : DemoCircleGraphics.BAS *
* Author : Gavin Wiggett *
* Notice : Copyright (c) 2011 *
* : All Rights Reserved *
* Date : 12/05/2011 *
* Version : 1.0 *
* Notes : Draws random Circles on display in random positions. *
* : GLCD 128x64 with KS0108 driver *
*****************************************************************************
}
Device = 18F2620 // select our device
Clock = 32 // internal 8Mhz * 4 PLL 32Mhz Clock
Config OSC = INTIO67 // configure OSC for internal
// User options
#Option GLCD_MODEL = KS0108 // GLCD driver
#option GLCD_SCREEN_WIDTH = 128 // Screen Width in Pixels
#option GLCD_SCREEN_HEIGHT = 64 // Screen Height in Pixels
#Option GLCD_DATA = PORTB // data port
#Option GLCD_RS = PORTC.0 // RS pin
#Option GLCD_RW = PORTC.1 // RW pin
#Option GLCD_EN = PORTC.2 // EN pin
#Option GLCD_CS1 = PORTC.3 // chip select
#Option GLCD_CS2 = PORTC.4 // chip select
#Option GLCD_RST = PORTC.5 // reset pin
#Option GLCD_ASPECT_RATIO = 100 // aspect ratio, smaller number will squeeze y for GLCD circles and box
#Option GLCD_INIT_DELAY = 50 // initialisation delay (ms)
#Option GLCD_INVERT_CS = true // invert CS lines...
// Modules
Include "glcd.bas" // main GLCD module
Include "graphics.bas" // support for GLCD
Include "Bitmaps.bas" // support for GLCD
Include "utils.bas" // support to make all MCU pins Digital
Include "RandGen2.bas" // random number Gen
// Variables
Dim circleX, //
circleY, // variables for GLCD Circle data
circleRadius As Word //
Dim range,
randNumber,
clearTimer As Byte // range for random number Gen
// Subroutins
// PORT setup and PLL Multiplier (set internal Osc to 32Mhz)
TRISA = %00000000 //
TRISB = %00000000 // Set all Ports output
TRISC = %00000000 //
OSCCON = %01111100 // Sets up the internal oscillator for 8Mhz
OSCTUNE.6 = 1 // Frequency Multiplier PLL for INTOSC Enable bit (Enabled)
SetAllDigital // set MCU pins as digital
// Random Gen
range = 167
RandGen2.Initialize(2) // Initialize the Random Number generator.
RandGen2.SetRndMax(range) // Set random MAX number
circleX = 0 //
circleY = 0 // Initialise our variables
circleRadius = 20 //
clearTimer = 25 //
GLCD.Cls // clear display before we start main prog
// Main Programme
While true
range = 127 // max range of display grid X
RandGen2.SetRndMax(range) // set MAX random gen range
circleX = RandGen2.rand () // Get a random number
range = 63 // max range of display grid Y
RandGen2.SetRndMax(range) // set MAX random gen range
circleY = RandGen2.rand () // Get a random number
RandGen2.SetRndMax(8) // set MAX random gen value
randNumber = RandGen2.rand () // Get a random number between 0 - 8
Select randNumber
Case 0 //
circleRadius = 2 //
Case 1 //
circleRadius = 3 //
Case 2 //
circleRadius = 5 //
Case 3 //
circleRadius = 6 //
Case 4 // Select which size circle to display
circleRadius = 7 //
Case 5 //
circleRadius = 10 //
Case 6 //
circleRadius = 12 //
Case 7 //
circleRadius = 14 //
Case 8 //
circleRadius = 15
End Select
If clearTimer <> 0 Then
Dec(clearTimer) // decrease our clear display timer
If clearTimer = 0 Then
GLCD.Cls // clear display
clearTimer = 25 // reset our timer
EndIf
EndIf
GLCD.Circle(circleX,circleY,circleRadius) // draw data to display
DelayMS(1000)
Wend
Re: GLCD Graphic LCD - Using Swordfish Basic
Posted: Sat May 14, 2011 9:10 am
by brad
Very nice!
My first thoughts are that you could make a game of pong perhaps. You could start off by having one fixed size circle and you could make it move down / right. When it reached the bottom or right extremes of the display, you would simply reverse the count thus reversing the direction.
Once that's up and working, you would see about drawing to rectangular paddles on either side and make sure that the only time a ball would bounce back was if it had hit the very left or right edge of the screen AND there was actually part of the paddle there
Keep us posted with whatever it is you do next!
Re: GLCD Graphic LCD - Using Swordfish Basic
Posted: Sat May 14, 2011 8:16 pm
by odessa
Hi Gav,
I've been playing with those in Isis. I got one from Sure electronics a few months ago and tried to set it up to an 18f4550.
One thing is confusing me though ... I presume that pin 3 Vo is used like a standard LCD ie through a 10k to earth/postive then adjusted for contrast. What is pin 18 used for Vee ? I've read its to do with contrast too ?
Can you shed any light please ( literally as I got no display

)
Thanks
Jay
Re: GLCD Graphic LCD - Using Swordfish Basic
Posted: Sat May 14, 2011 9:32 pm
by bitfogav
Yes Brad i will keep you posted
Hi Jay, Good question, I had a few problems with this myself and took a bit of searching to find out how to get my display working, I was at a point of giving up when I looked at the datasheet again hehe!.
I assumed that you just connect up pin3 V0 to your POT as you would with a LCD 16x2, but you need to connect your POT directly to pin18 Vout/Vee aswell. Heres a diagram I just made up which might help explain how to connect up V0 + VEE connections.

- GLCD Pot connections.jpg (27.54 KiB) Viewed 27025 times
Re: GLCD Graphic LCD - Using Swordfish Basic
Posted: Sat May 14, 2011 10:04 pm
by odessa
Excellent Gav, works fine now thx very much
jay
Re: GLCD Graphic LCD - Using Swordfish Basic
Posted: Sat May 14, 2011 10:50 pm
by odessa
Thanks Gav
https://youtu.be/oy7P04greWQ
I used to program the display Sinclair Spectrums in WH Smiths in the eighties with a bit of code similar to this
Code: Select all
Device = 18F4550
Clock = 20
// GLCD options
#Option GLCD_MODEL = KS0108 // GLCD driver
#option GLCD_SCREEN_WIDTH = 128 // Screen Width in Pixels
#option GLCD_SCREEN_HEIGHT = 64 // Screen Height in Pixels
#Option GLCD_DATA = PORTD // data port
#Option GLCD_RS = PORTB.2 // RS pin
#Option GLCD_RW = PORTB.3 // RW pin
#Option GLCD_EN = PORTB.4 // EN pin
#Option GLCD_CS1 = PORTB.0 // chip select
#Option GLCD_CS2 = PORTB.1 // chip select
#Option GLCD_RST = PORTB.5 // reset pin
#Option GLCD_ASPECT_RATIO = 100 // aspect ratio, smaller number will squeeze y for GLCD circles and box
#Option GLCD_INIT_DELAY = 50 // initialisation delay (ms)
#Option GLCD_INVERT_CS = true // invert CS lines...
// Modules
Include "glcd.bas" // main GLCD module
Include "graphics.bas" // support for GLCD
Include "utils.bas" // support to make all MCU pins Digital
Dim x As Word
Dim y As Word
Sub incx()
x=x+2
delayms (20)
If x>=128 Then delayms(1000)
x=0
GLCD.Cls
End If
End Sub
Sub decy()
y=y-2
delayms (20)
If y<=0 Then delayms (1000)
y=128
GLCD.Cls
End If
End Sub
Sub drwscr()
GLCD.Line(0,0,x,64)
GLCD.Line(128,64,y,0)
End Sub
TRISB = %00000000
TRISD = %00000000
x=0
y=128
// Main Program
While true
incx
decy
drwscr
Wend
Re: GLCD Graphic LCD - Using Swordfish Basic
Posted: Sat May 14, 2011 11:12 pm
by bitfogav
Very cool Jay
Im enjoying playing around with these displays

Re: GLCD Graphic LCD - Using Swordfish Basic
Posted: Sun May 15, 2011 11:52 pm
by bitfogav
Wouldn't say that this is very clear but this is my first attempt at a bouncing ball around the screen of the GLCD 168x64. you might have to look closely as the ball appears as a shaded line, as it goes around the screen.
https://youtu.be/xvAX6WoYnw0
Another version of it can be seen here:
https://youtu.be/gMMkN6ejdfI
Re: GLCD Graphic LCD - Using Swordfish Basic
Posted: Mon May 16, 2011 12:08 am
by odessa
Thats really cool
You could make a really good breakout game with one of these... so many possibilties
I'm playing with adding bitmaps at the moment, will post some up when I get time
Re: GLCD Graphic LCD - Using Swordfish Basic
Posted: Mon May 16, 2011 12:30 am
by bitfogav
Thanks Jay
Well the idea came from Brad and making a pong game but yes your right I could make a breakout game too

I guess its just finding the best technique to drawing the ball on the display to make it more obvious on the screen..
Looking forward to seeing what you come up with on Bitmaps, Ive tried a few GLCD font creators but didnt have much luck importing the data to Swordfish format..
As far as I can work out looking at the bitmap Module, the first four bits of the Image array are for the display settings.
So for an example the code below is of the Battery Image from the Bitmap module. The first four bits of the array are - screen settings (KS0108), width of image, height of image, and not sure what the fourth one is for.
Code: Select all
public const BatteryImage(20) as byte =
($01,8,10,0,
254,226,227,227,227,227,226,254,
3, 2, 2, 2, 2, 2, 2, 3)
Re: GLCD Graphic LCD - Using Swordfish Basic
Posted: Mon May 16, 2011 2:40 am
by odessa
https://youtu.be/fMtcdnFjsKk
Thats a little video of bitmaps made using photoshop then converted to 1 bit mode. Works quite well

Re: GLCD Graphic LCD - Using Swordfish Basic
Posted: Mon May 16, 2011 2:48 am
by bitfogav
Works very well

your have to share how you got the bitmap format working

Re: GLCD Graphic LCD - Using Swordfish Basic
Posted: Mon May 16, 2011 3:06 am
by odessa

- gav.gif (380.42 KiB) Viewed 27003 times
Thats for you
