Code:
'This code is for driving an OLED screen
'My screen was 0.96" 128x64 B/W SSD1306 chip with I2C protocol
'I used a 16f877a, you must change definitions and register settings due to
'your hardware.
'This code have a large char library in it, so it takes some memory, you may
'delete some of chars you won't use if you need some space for your program
'My codes don't include bitmap drawing I only intended to write texts on screen
'So if you want to draw bitmap, you must alter printing codes.
'Now
'Screen contains 8 lines (which are 8 pixel height) and 128 columns
'there are 1 printout subroutine for each Line, Simply change the X value on
'that line's subroutine and your text will start from that column.
'There also 8 text sobroutines which are contains text for each line, you can
'write your text on these subroutines, but !!!REMEMBER!!! to change the first
'value of lookup table to number of chars u used in your text.
'==========================MCU SETUP============================================
DEFINE OSC 16
OPTION_REG=0
ADCON0=0
ADCON1=7
TRISA=%00111111
TRISB=%00000001
TRISC=0
PORTC=$FF
'==============================BAUD SETUP=======================================
I2CDevice var byte
SCL var PortC.3 ' I2C Clock PortC.3
SDA var PortC.4 ' I2C Data PortC.4
DC VAR byte' "DATA OR COMMAND", $40=DATA; $80=COMMAND
LCD_DATA VAR BYTE
COM VAR BYTE'COMMAND
X VAR BYTE' LCD Column POSITION (0 TO 127)
Y VAR BYTE'LCD Line POSITION FOR PAGE MODE(0 TO 7)
I var byte
charset var byte 'Char codes index
say var byte 'counter
text_reg var byte 'index for text
letter_reg var byte 'data carrier for text chars
letter_count var byte 'count of letters in your text
CLEAR
'=============================================================================
I2CDevice = $78 ' Display = $78,
PAUSE 20
'=========================lcd initialization====================================
INIT:
I2CWrite SDA,SCL,I2CDevice,[DC,$AE]'Display Off
pause 10
I2CWrite SDA,SCL,I2CDevice,[DC,$20,$10]' Page adressing mode
I2CWrite SDA,SCL,I2CDevice,[DC,$D3,$00]' Set offset to 0
I2CWrite SDA,SCL,I2CDevice,[DC,$40]' Set display start line 0
I2CWrite SDA,SCL,I2CDevice,[DC,$81,$FF]' Set contrast to full
I2CWrite SDA,SCL,I2CDevice,[DC,$A4]' display on continue
I2CWrite SDA,SCL,I2CDevice,[DC,$A6]' $A6=NORMAL MODE;$A7=INVERSE MODE
I2CWrite SDA,SCL,I2CDevice,[DC,$A1]' set column 127 as start
i2cwrite sda,scl,I2CDevice,[DC,$C8]'Flip display vertically ****
'above 2 lines for arranging screen direction, deleting them will
'turn the display direction 180 degree
I2CWrite SDA,SCL,I2CDevice,[DC,$AF]'Display On
pause 10
'===============================================================================
GOSUB CLEAR_LCD
pause 500
MAIN:
gosub Print_Line_0
gosub Print_Line_1
gosub Print_Line_2
gosub Print_Line_3
gosub Print_Line_4
'gosub Print_Line_5 'You can comment out the lines you don't want to use
'gosub Print_Line_6
'gosub Print_Line_7
sleep 5
GOTO MAIN
'=============================Text to Write=====================================
Line_0:
lookup text_reg,[4,"Test"],letter_reg' first data is number of chars in your
'text
return
Line_1:
lookup text_reg,[9,"Test teST"],letter_reg' first data is number of chars in your
'text
return
Line_2:
lookup text_reg,[7,"T e s t"],letter_reg' first data is number of chars in your
'text
return
Line_3:
lookup text_reg,[8,"%]Test{-"],letter_reg' first data is number of chars in your
'text
return
Line_4:
lookup text_reg,[7,"Hop Hop"],letter_reg' first data is number of chars in your
'text
return
Line_5:
lookup text_reg,[4,"Test"],letter_reg' first data is number of chars in your
'text
return
Line_6:
lookup text_reg,[4,"Test"],letter_reg' first data is number of chars in your
'text
return
Line_7:
lookup text_reg,[4,"Test"],letter_reg' first data is number of chars in your
'text
return
'======================Print Lines==============================================
Print_Line_0:
X=0 'You can change this value to set starting column
Y=0 'Don't change this line
gosub SET_XY
gosub Line_0
letter_count=letter_reg
text_reg=text_reg+1
for say=1 to letter_count
gosub Line_0
gosub Find_Char
text_reg=text_reg+1
next say
text_reg=0
return
Print_Line_1:
X=10 'You can change this value to set starting column
Y=1 'Don't change this line
gosub SET_XY
gosub Line_1
letter_count=letter_reg
text_reg=text_reg+1
for say=1 to letter_count
gosub Line_1
gosub Find_Char
text_reg=text_reg+1
next say
text_reg=0
return
Print_Line_2:
X=20 'You can change this value to set starting column
Y=2 'Don't change this line
gosub SET_XY
gosub Line_2
letter_count=letter_reg
text_reg=text_reg+1
for say=1 to letter_count
gosub Line_2
gosub Find_Char
text_reg=text_reg+1
next say
text_reg=0
return
Print_Line_3:
X=30 'You can change this value to set starting column
Y=3 'Don't change this line
gosub SET_XY
gosub Line_3
letter_count=letter_reg
text_reg=text_reg+1
for say=1 to letter_count
gosub Line_3
gosub Find_Char
text_reg=text_reg+1
next say
text_reg=0
return
Print_Line_4:
X=40 'You can change this value to set starting column
Y=4 'Don't change this line
gosub SET_XY
gosub Line_4
letter_count=letter_reg
text_reg=text_reg+1
for say=1 to letter_count
gosub Line_4
gosub Find_Char
text_reg=text_reg+1
next say
text_reg=0
return
Print_Line_5:
X=50 'You can change this value to set starting column
Y=5 'Don't change this line
gosub SET_XY
gosub Line_5
letter_count=letter_reg
text_reg=text_reg+1
for say=1 to letter_count
gosub Line_5
gosub Find_Char
text_reg=text_reg+1
next say
text_reg=0
return
Print_Line_6:
X=60 'You can change this value to set starting column
Y=6 'Don't change this line
gosub SET_XY
gosub Line_6
letter_count=letter_reg
text_reg=text_reg+1
for say=1 to letter_count
gosub Line_6
gosub Find_Char
text_reg=text_reg+1
next say
text_reg=0
return
Print_Line_7:
X=55 'You can change this value to set starting column
Y=7 'Don't change this line
gosub SET_XY
gosub Line_7
letter_count=letter_reg
text_reg=text_reg+1
for say=1 to letter_count
gosub Line_7
gosub Find_Char
text_reg=text_reg+1
next say
text_reg=0
return
'===============================================================================
'=======================Send data===============================================
SEND_DATA:
DC=$40
I2CWrite SDA,SCL,I2CDevice,[DC,LCD_DATA]
RETURN
'===============================================================================
'======================Send Command=============================================
SEND_COMMAND:
DC=$80
I2CWrite SDA,SCL,I2CDevice,[DC,COM]
RETURN
'===============================================================================
'==============================clear lcd========================================
CLEAR_LCD:
X=0
Y=0
LCD_DATA=$00
'Send 0 to every column in every line
FOR Y=0 TO 7
gosub SET_XY
FOR I=0 TO 127
GOSUB SEND_DATA
NEXT I
NEXT Y
X=0
Y=0
gosub SET_XY
RETURN
'=============================Find Char=========================================
Find_Char:
select case letter_reg
case " "
for charset=0 to 5
lookup charset,[$00,$00,$00,$00,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "!"
for charset=0 to 5
lookup charset,[$00,$00,$5F,$00,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "#"
for charset=0 to 5
lookup charset,[$14,$7F,$14,$7F,$14,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "$"
for charset=0 to 5
lookup charset,[$24,$2A,$7F,$2A,$12,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "%"
for charset=0 to 5
lookup charset,[$23,$13,$08,$64,$62,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "&"
for charset=0 to 5
lookup charset,[$36,$49,$56,$20,$50,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "'"
for charset=0 to 5
lookup charset,[$00,$08,$07,$03,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "("
for charset=0 to 5
lookup charset,[$00,$1C,$22,$41,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case ")"
for charset=0 to 5
lookup charset,[$00,$41,$22,$1C,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "*"
for charset=0 to 5
lookup charset,[$2A,$1C,$7F,$1C,$2A,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "+"
for charset=0 to 5
lookup charset,[$08,$08,$3E,$08,$08,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case ","
for charset=0 to 5
lookup charset,[$00,$00,$70,$30,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "-"
for charset=0 to 5
lookup charset,[$08,$08,$08,$08,$08,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "."
for charset=0 to 5
lookup charset,[$00,$00,$60,$60,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "/"
for charset=0 to 5
lookup charset,[$20,$10,$08,$04,$02,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "0"
for charset=0 to 5
lookup charset,[$3E,$51,$49,$45,$3E,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "1"
for charset=0 to 5
lookup charset,[$00,$42,$7F,$40,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "2"
for charset=0 to 5
lookup charset,[$72,$49,$49,$49,$46,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "3"
for charset=0 to 5
lookup charset,[$21,$41,$49,$4D,$33,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "4"
for charset=0 to 5
lookup charset,[$18,$14,$12,$7F,$10,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "5"
for charset=0 to 5
lookup charset,[$27,$45,$45,$45,$39,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "6"
for charset=0 to 5
lookup charset,[$3C,$4A,$49,$49,$31,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "7"
for charset=0 to 5
lookup charset,[$41,$21,$11,$09,$07,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "8"
for charset=0 to 5
lookup charset,[$36,$49,$49,$49,$36,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "9"
for charset=0 to 5
lookup charset,[$46,$49,$49,$29,$1E,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case ":"
for charset=0 to 5
lookup charset,[$00,$00,$14,$00,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case ";"
for charset=0 to 5
lookup charset,[$00,$40,$34,$00,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "<"
for charset=0 to 5
lookup charset,[$00,$08,$14,$22,$41,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "="
for charset=0 to 5
lookup charset,[$14,$14,$14,$14,$14,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case ">"
for charset=0 to 5
lookup charset,[$00,$41,$22,$14,$08,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "?"
for charset=0 to 5
lookup charset,[$02,$01,$59,$09,$06,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "@"
for charset=0 to 5
lookup charset,[$3E,$41,$5D,$59,$4E,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "A"
for charset=0 to 5
lookup charset,[$7C,$12,$11,$12,$7C,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "B"
for charset=0 to 5
lookup charset,[$7F,$49,$49,$49,$36,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "C"
for charset=0 to 5
lookup charset,[$3E,$41,$41,$41,$22,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "D"
for charset=0 to 5
lookup charset,[$7F,$41,$41,$41,$3E,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "E"
for charset=0 to 5
lookup charset,[$7F,$49,$49,$49,$41,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "F"
for charset=0 to 5
lookup charset,[$7F,$09,$09,$09,$01,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "G"
for charset=0 to 5
lookup charset,[$3E,$41,$41,$51,$73,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "H"
for charset=0 to 5
lookup charset,[$7F,$08,$08,$08,$7F,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "I"
for charset=0 to 5
lookup charset,[$00,$41,$7F,$41,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "J"
for charset=0 to 5
lookup charset,[$20,$40,$41,$3F,$01,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "K"
for charset=0 to 5
lookup charset,[$7F,$08,$14,$22,$41,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "L"
for charset=0 to 5
lookup charset,[$7F,$40,$40,$40,$40,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "M"
for charset=0 to 5
lookup charset,[$7F,$02,$1C,$02,$7F,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "N"
for charset=0 to 5
lookup charset,[$7F,$04,$08,$10,$7F,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "O"
for charset=0 to 5
lookup charset,[$3E,$41,$41,$41,$3E,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "P"
for charset=0 to 5
lookup charset,[$7F,$09,$09,$09,$06,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "Q"
for charset=0 to 5
lookup charset,[$3E,$41,$51,$21,$5E,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "R"
for charset=0 to 5
lookup charset,[$7F,$09,$19,$29,$46,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "S"
for charset=0 to 5
lookup charset,[$26,$49,$49,$49,$32,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "T"
for charset=0 to 5
lookup charset,[$03,$01,$7F,$01,$03,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "U"
for charset=0 to 5
lookup charset,[$3F,$40,$40,$40,$3F,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "V"
for charset=0 to 5
lookup charset,[$1F,$20,$40,$20,$1F,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "W"
for charset=0 to 5
lookup charset,[$3F,$40,$38,$40,$3F,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "X"
for charset=0 to 5
lookup charset,[$63,$14,$08,$14,$63,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "Y"
for charset=0 to 5
lookup charset,[$03,$04,$78,$04,$03,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "Z"
for charset=0 to 5
lookup charset,[$61,$59,$49,$4D,$43,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "["
for charset=0 to 5
lookup charset,[$00,$7F,$41,$41,$41,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "\"
for charset=0 to 5
lookup charset,[$02,$04,$08,$10,$20,$00 ],LCD_DATA
gosub SEND_DATA
next charset
return
case "]"
for charset=0 to 5
lookup charset,[$00,$41,$41,$41,$7F,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "^"
for charset=0 to 5
lookup charset,[$04,$02,$01,$02,$04,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "_"
for charset=0 to 5
lookup charset,[$40,$40,$40,$40,$40,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "`"
for charset=0 to 5
lookup charset,[$00,$03,$07,$08,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "a"
for charset=0 to 5
lookup charset,[$20,$54,$54,$38,$40,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "b"
for charset=0 to 5
lookup charset,[$7F,$28,$44,$44,$38,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "c"
for charset=0 to 5
lookup charset,[$38,$44,$44,$44,$28,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "d"
for charset=0 to 5
lookup charset,[$38,$44,$44,$28,$7F,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "e"
for charset=0 to 5
lookup charset,[$38,$54,$54,$54,$18,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "f"
for charset=0 to 5
lookup charset,[$00,$08,$7E,$09,$02,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "g"
for charset=0 to 5
lookup charset,[$0C,$52,$52,$4A,$3C,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "h"
for charset=0 to 5
lookup charset,[$7F,$08,$04,$04,$78,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "i"
for charset=0 to 5
lookup charset,[$00,$44,$7D,$40,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "j"
for charset=0 to 5
lookup charset,[$20,$40,$40,$3D,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "k"
for charset=0 to 5
lookup charset,[$7F,$10,$28,$44,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "l"
for charset=0 to 5
lookup charset,[$00,$41,$7F,$40,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "m"
for charset=0 to 5
lookup charset,[$7C,$04,$78,$04,$78,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "n"
for charset=0 to 5
lookup charset,[$7C,$08,$04,$04,$78,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "o"
for charset=0 to 5
lookup charset,[$38,$44,$44,$44,$38,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "p"
for charset=0 to 5
lookup charset,[$7C,$18,$24,$24,$18,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "q"
for charset=0 to 5
lookup charset,[$18,$24,$24,$18,$7C,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "r"
for charset=0 to 5
lookup charset,[$7C,$08,$04,$04,$08,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "s"
for charset=0 to 5
lookup charset,[$48,$54,$54,$54,$24,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "t"
for charset=0 to 5
lookup charset,[$04,$04,$3F,$44,$24,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "u"
for charset=0 to 5
lookup charset,[$3C,$40,$40,$20,$7C,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "v"
for charset=0 to 5
lookup charset,[$1C,$20,$40,$20,$1C,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "w"
for charset=0 to 5
lookup charset,[$3C,$40,$30,$40,$3C,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "x"
for charset=0 to 5
lookup charset,[$44,$28,$10,$28,$44,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "y"
for charset=0 to 5
lookup charset,[$4C,$50,$50,$50,$3C,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "z"
for charset=0 to 5
lookup charset,[$44,$64,$54,$4C,$44,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "{"
for charset=0 to 5
lookup charset,[$00,$08,$36,$41,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "|"
for charset=0 to 5
lookup charset,[$00,$00,$77,$00,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "}"
for charset=0 to 5
lookup charset,[$00,$41,$36,$08,$00,$00],LCD_DATA
gosub SEND_DATA
next charset
return
case "~"
for charset=0 to 5
lookup charset,[$02,$01,$02,$04,$02,$00],LCD_DATA
gosub SEND_DATA
next charset
return
end select
return
'===============================================================================
'=========================CHAR_SET==============================================
'===========================================SET X AND Y=========================
SET_XY:
COM=$21:GOSUB SEND_COMMAND
COM=X:GOSUB SEND_COMMAND
COM=127:GOSUB SEND_COMMAND
'Above 3 lines means; Column starts at X and End at 127
COM=$22:GOSUB SEND_COMMAND
COM=Y:GOSUB SEND_COMMAND
COM=Y:GOSUB SEND_COMMAND
'Above 3 lines means; Line starts at Y and end at Y,
'which means ; Only work on specified line!
RETURN
'===============================================================================
END
Bookmarks