PDA

View Full Version : 128x64 GLCD ST7920 Example code



wellyboot
- 13th March 2015, 12:20
Hi

I bought 1 of these displays cheap on ebay, it uses ST7920 driver IC

I couldn't find any picbasicpro example code for this display so I have come up with this example code to draw basic graphics onto it
operating in 4Bit parallel mode using 16F877 @ 20Mhz

the module I bought has a built in reset circuit so have left RST not connected

PSB - pulled high for parallel mode
R/W - is not used in my code - so can be pulled to 0v or connected to PORTB 1
Vout - not connected
DB7 - PORTB 7
DB6 - PORTB 6
DB5 - PORTB 5
DB4 - PORTB 4
DB3 - DB0 - not connected
E - PORTB 3
RS - PORTB 2
BLA - PORTB 0 or +5v (LED BACKLIGHT ANODE - Module has built in current limit resistor)
BLK - 0v
Vo - contrast connect this to variable resistor



' ===================================
' = ST7920 128X64 GLCD DEMO CODE =
' = 4BIT MODE
' = 12/3/15 D.J.WELBURN =
' = 16F877 20MHz EXT XTAL =
' = [email protected] =
' = www.youtube.com/user/wellyb00t =
' = http://twitter.com/wellyb00t =
' ===================================
'




@ __config 0x3F76 'HS xtal
@ ERRORLEVEL -306
DEFINE OSC 20


TRISA = 0
TRISB = 0
TRISC = 0
TRISD = 0
TRISE = 0
'*** PORTB.7,6,5,4 CONNECT TO DB7,6,5,4 ON DISPLAY 4 BIT MODE ***
SYMBOL e = PORTB.3 'E PIN ON DISPLAY
SYMBOL rs = PORTB.2 'RS PIN ON DISPLAY
SYMBOL rw = PORTB.1 'RW PIN ON DISPLAY
SYMBOL led = PORTB.0 'LED BACKLIGHT + ON DISPLAY

instr var byte 'instruction to send
sdata var byte 'data to send
counter var byte 'gen purpose counter
horcount var byte
CLEAR


'graphic display area is actually bitmapped as 256 horizontal x 32 vertical
'horizontal address (X) 0 - 15
'0 - 7 (X) address for top half of display lines 0 - 31
'8 - 15(X) address for bottom half of display lines 32-63
'
'vertical address (Y) 0 - 31
'setting vertical address to 0 and horizontal address to 8 will display data on line 32
'
'to draw on graphic area send vertical address (Y) 0 - 31 then horizontal address (X) 0 - 15
'then send 2 data bytes (sdata)
'the horizontal address will automatically increase by 1 each time data is sent


MAIN:
led = 1
GOSUB glcd_basic_init
GOSUB glcd_ext_init
GOSUB clear_graphic

'THE FOLLOWING WILL DRAW A BOX ON THE BOTTOM HALF OF THE SCREEN
'GDRAM location Y = 0 & X = 8
'Y position on screen = 32 & X position on screen = 0
'
'--------------------TOP OF BOX--------------------------------
instr = %10000000 'set vertical address 'Y' = 0
GOSUB send_inst
instr = %10001000 'set horizontal start address 'X' = 8
GOSUB send_inst
sdata = %11111111
GOSUB send_data
sdata = %11111111
GOSUB send_data
sdata = %11111111
GOSUB send_data
sdata = %11111111
GOSUB send_data
sdata = %11111111
GOSUB send_data
sdata = %11111111
GOSUB send_data
'---------------------SIDES OF BOX-----------------------------
FOR counter = 1 TO 20
instr = counter + 128 'set vertical 1 TO 20
GOSUB send_inst
instr = %10001000 'set horizontal
GOSUB send_inst
sdata = %10000000
GOSUB send_data
sdata = 0
GOSUB send_data
sdata = 0
GOSUB send_data
sdata = 0
GOSUB send_data
sdata = 0
GOSUB send_data
sdata = %00000001
GOSUB send_data
NEXT counter
'----------------------BOTTOM OF BOX----------------------------
instr = %10010101 'set vertical 21
GOSUB send_inst
instr = %10001000 'set horizontal
GOSUB send_inst
sdata = %11111111
GOSUB send_data
sdata = %11111111
GOSUB send_data
sdata = %11111111
GOSUB send_data
sdata = %11111111
GOSUB send_data
sdata = %11111111
GOSUB send_data
sdata = %11111111
GOSUB send_data

MAIN1:



GOTO MAIN1


clear_graphic: '********CLEARS ALL GRAPHIC SCREEN AREA*******************

FOR counter = 0 TO 31 'to increase vertical counter
instr = counter + 128 'set vertical address
GOSUB send_inst
instr = %10000000 'set horizontal adress
GOSUB send_inst
FOR horcount = 0 TO 15
sdata = 0
GOSUB send_data
sdata = 0
GOSUB send_data
NEXT horcount
NEXT counter
RETURN

glcd_basic_init: '************* INITIALISATION OF GLCD *****************
PAUSE 100
instr = %00100000
GOSUB send_inst
instr = %00100000
GOSUB send_inst
instr = %00001100 'disp on,cursor off, blink off
GOSUB send_inst
instr = %00000001 'clear display
GOSUB send_inst
PAUSE 12
instr = %00000110 'entry mode set
GOSUB send_inst
RETURN
glcd_ext_init:
instr = %00100100 'ext function set
GOSUB send_inst
PAUSE 12
instr = %00100110 'graphic display on
GOSUB send_inst
PAUSE 12
RETURN

send_inst: '************** SEND INSTRUCTION TO GLCD ********************
rs = 0
rw = 0
PORTB.7 = instr.7
PORTB.6 = instr.6
PORTB.5 = instr.5
PORTB.4 = instr.4
e = 1
PAUSEUS 1
e = 0
PAUSEUS 1
PORTB.7 = instr.3
PORTB.6 = instr.2
PORTB.5 = instr.1
PORTB.4 = instr.0
e = 1
PAUSEUS 1
e = 0
PAUSEUS 100
RETURN

send_data: '************** SEND DATA TO GLCD ************************
rs = 1
rw = 0
PORTB.7 = sdata.7
PORTB.6 = sdata.6
PORTB.5 = sdata.5
PORTB.4 = sdata.4
e = 1
PAUSEUS 1
e = 0
PAUSEUS 1
PORTB.7 = sdata.3
PORTB.6 = sdata.2
PORTB.5 = sdata.1
PORTB.4 = sdata.0
e = 1
PAUSEUS 1
e = 0
PAUSEUS 100
RETURN

END