'*******************************************************************************
' Demo for SSD1306 128x32 Pixel
'
' Hardware: http://www.adafruit.com/category/63_98
'
' use 512 Byte Frame Buffer
' Spi Mode
'
'
'
' Date:22.05.2014
' Email: Hkipnik@aol.com
' Copyright (c) 2013 Hkipnik. All rights reserved.
'
' http://creativecommons.org/licenses/by-sa/3.0/de/
'
' Sie dürfen:
'
' * das Werk bzw. den Inhalt vervielfältigen, verbreiten und öffentlich zugänglich machen
'
' * Abwandlungen und Bearbeitungen des Werkes bzw. Inhaltes anfertigen
'
' Zu Den Folgenden Bedingungen:
'
' * Namensnennung.
' Sie müssen den Namen des Autors/Rechteinhabers in der von ihm festgelegten Weise nennen.
'
' * Keine kommerzielle Nutzung.
' Dieses Werk darf nicht für kommerzielle Zwecke verwendet werden.
'
' * Weitergabe unter gleichen Bedingungen.
' Wenn Sie das lizenzierte Werk bzw. den lizenzierten Inhalt bearbeiten
' oder in anderer Weise erkennbar als Grundlage für eigenes Schaffen verwenden,
' dürfen Sie die daraufhin neu entstandenen Werke bzw. Inhalte nur
' unter Verwendung von Lizenzbedingungen weitergeben, die mit denen
' dieses Lizenzvertrages identisch oder vergleichbar sind.
'
'*******************************************************************************
$regfile = "m64def.dat"
$hwstack = 150
$swstack = 150
$framesize = 250
$baud = 9600
'*******************************************************************************
'Hardware config
'$lib "i2c_twi.lbx" 'Hardware I2C
Config Scl = Portd.5 ' used i2c pins
Config Sda = Portd.4
Config Twi = 100000 ' i2c speed
I2cinit
'*******************************************************************************
'
'
'*******************************************************************************
'*******************************************************************************
'Declare Subs
Declare Sub Lcd_init()
Declare Sub Lcd_data_out(byval Data1 As Byte)
Declare Sub Lcd_comm_out(byval Comm As Byte)
Declare Sub Lcd_clear(byval Colo As Byte)
Declare Sub Lcd_show()
Declare Sub Lcd_text(byval S As String , Byval Xoffset As Byte , Byval Yoffset As Byte , Byval Fontset As Byte)
Declare Sub Lcd_set_pixel(byval Xp As Byte , Byval Yp As Byte , Byval Colo As Byte)
Const White = &HFF
Const Black = &H00
Dim Ddata(512) As Byte 'Display Data Buffer
'*******************************************************************************
Call Lcd_init() 'Init Display
'*******************************************************************************
'use for Demo
Dim Zab As Word
Dim Text11 As String * 20
'*******************************************************************************
Do
Call Lcd_clear(black) 'Clear black
Call Lcd_text( "Bascom" , 10 , 5 , 2) 'Draw Text to Buffer
Call Lcd_show() 'show Buffer
Wait 2
Call Lcd_clear(black)
Call Lcd_text( "Hallo" , 10 , 5 , 2)
Call Lcd_show()
Wait 2
Call Lcd_clear(black)
Wait 1
Loop
End
'*******************************************************************************
'include used fonts
$include "My6_8.font"
$include "My12_16.font"
'*******************************************************************************
'*******************************************************************************
'Routines
'*******************************************************************************
' Set or Clear a Pixel to X-Y Position White= Set Pixel Black= Clear Pixel
' and write Data to Display-Array
'*******************************************************************************
Sub Lcd_set_pixel(byval Xp As Byte , Byval Yp As Byte , Byval Colo As Byte)
Local B1 As Byte , Zeiger As Word , Bitnr As Byte
Decr Yp
B1 = Yp / 8
Zeiger = B1 * 128
Zeiger = Zeiger + Xp
Bitnr = Yp Mod 8
If Colo = Black Then
Ddata(zeiger).bitnr = 0
Else
Ddata(zeiger).bitnr = 1
End If
End Sub
'*******************************************************************************
' Updated the Display whith Display-Array
'*******************************************************************************
Sub Lcd_show()
Local Zab2 As Word , Point As Word
Point = 1
Call Lcd_comm_out(&H21)
Call Lcd_comm_out(0)
Call Lcd_comm_out(127)
Call Lcd_comm_out(&H22)
Call Lcd_comm_out(0)
Call Lcd_comm_out(3)
I2cstart 'start condition
I2cwbyte &H78 'slave address
I2cwbyte &H40
For Zab2 = 1 To 512
I2cwbyte Ddata(point)
Incr Point
Next
End Sub
'*******************************************************************************
' Clear Display and Display-Array
'*******************************************************************************
Sub Lcd_clear(byval Colo As Byte)
Local Zab2 As Word
Call Lcd_comm_out(&H21) 'Set Column
Call Lcd_comm_out(0) 'Column start address
Call Lcd_comm_out(127) 'Column end address
Call Lcd_comm_out(&H22) 'Set Page
Call Lcd_comm_out(0) 'Page start address
Call Lcd_comm_out(3) 'Page end address
I2cstart 'start condition
I2cwbyte &H78 'slave address
I2cwbyte &H40
For Zab2 = 1 To 512
I2cwbyte Colo
Next
For Zab2 = 1 To 512
Ddata(zab2) = 0 'Clear Display Data Buffer
Next
End Sub
'*******************************************************************************
' Send Data to SSD1306 not use
'*******************************************************************************
Sub Lcd_data_out(byval Data1 As Byte)
I2cstart 'start condition
I2cwbyte &H78 'slave address
I2cwbyte &H40
I2cwbyte Data1
I2cstop
End Sub
'*******************************************************************************
' Send Command to SSD1306
'*******************************************************************************
Sub Lcd_comm_out(byval Comm As Byte)
I2cstart 'start condition
I2cwbyte &H78 'slave address
I2cwbyte &H00
I2cwbyte Comm
I2cstop
End Sub
'*******************************************************************************
' Init the Driver SSD1306
'*******************************************************************************
Sub Lcd_init()
'Lcd_rst = 0
Waitms 100 'Reset Display
'Lcd_rst = 1
Waitms 100
Call Lcd_comm_out(&Hae) 'DISPLAYOFF
Call Lcd_comm_out(&Hd5) 'SETDISPLAYCLOCKDIV
Call Lcd_comm_out(&H80) 'ratio 0x80
Call Lcd_comm_out(&Ha8) 'SETMULTIPLEX
Call Lcd_comm_out(&H1f) ' 1f 128x32
Call Lcd_comm_out(&Hd3) 'SETDISPLAYOFFSET
Call Lcd_comm_out(&H00)
Call Lcd_comm_out(&H40) 'SETSTARTLINE
Call Lcd_comm_out(&H8d) 'CHARGEPUMP
Call Lcd_comm_out(&H14) 'vccstate 14
Call Lcd_comm_out(&H20) 'MEMORYMODE
Call Lcd_comm_out(&H00) '
Call Lcd_comm_out(&Ha1) 'SEGREMAP a0
Call Lcd_comm_out(&Hc8) 'COMSCANDEC
Call Lcd_comm_out(&Hda) 'SETCOMPINS
Call Lcd_comm_out(&H02) ' 02 128x32 12
Call Lcd_comm_out(&H81) 'SETCONTRAST
Call Lcd_comm_out(&H8f) ' 8f 128x32
Call Lcd_comm_out(&Hd9) 'SETPRECHARGE
Call Lcd_comm_out(&Hf1) 'vccstate f1
Call Lcd_comm_out(&Hdb) 'SETVCOMDETECT
' Call Lcd_comm_out(&H40) '
Call Lcd_comm_out(&Ha4) 'DISPLAYALLON_RESUME
Call Lcd_comm_out(&Ha6) 'NORMALDISPLAY
Call Lcd_comm_out(&Haf)
Waitms 100
End Sub
'*******************************************************************************
'LCD_Text String -- X -- Y Start -- Font
'*******************************************************************************
Sub Lcd_text(byval S As String , Xoffset As Byte , Yoffset As Byte , Fontset As Byte)
Local Tempstring As String * 1 , Temp As Word
Local A As Byte , Pixels As Byte , Count As Byte , Carcount As Byte , Lus As Byte
Local Row As Byte , Block As Byte , Byteseach As Byte , Blocksize As Byte , Dummy As Byte
Local Colums As Byte , Columcount As Byte , Rowcount As Byte , Stringsize As Byte
Local Xpos As Byte , Ypos As Byte , Pixel As Word , Pixelcount As Byte
Local Offset As Word
Stringsize = Len(s) - 1 'Size of the text string -1 because we must start with 0
Select Case Fontset
Case 1 :
Block = Lookup(0 , My6_8) 'Add or remove here fontset's that you need or not,
Byteseach = Lookup(1 , My6_8)
Blocksize = Lookup(2 , My6_8)
Dummy = Lookup(3 , My6_8)
Case 2 :
Block = Lookup(0 , My12_16)
Byteseach = Lookup(1 , My12_16)
Blocksize = Lookup(2 , My12_16)
Dummy = Lookup(3 , My12_16)
' Case 3 :
' Block = Lookup(0 , Tahoma20x24)
' Byteseach = Lookup(1 , Tahoma20x24)
' Blocksize = Lookup(2 , Tahoma20x24)
' Dummy = Lookup(3 , Tahoma20x24)
End Select
Colums = Blocksize / Block 'Calculate the numbers of colums
Row = Block * 8 'Row is always 8 pixels high = 1 byte, so working with row in steps of 8.
Row = Row - 1 'Want to start with row=0 instead of 1
Colums = Colums - 1 'Same for the colums
For Carcount = 0 To Stringsize 'Loop for the numbers of caracters that must be displayed
Temp = Carcount + 1 'Cut the text string in seperate caracters
Tempstring = Mid(s , Temp , 1)
Offset = Asc(tempstring) - 32 'Font files start with caracter 32
Offset = Offset * Blocksize
Offset = Offset + 4
Temp = Carcount * Byteseach
Temp = Temp + Xoffset
For Rowcount = 0 To Row Step 8 'Loop for numbers of rows
A = Rowcount + Yoffset
Xpos = Temp
For Columcount = 0 To Colums 'Loop for numbers of Colums
Select Case Fontset
Case 1 : Pixels = Lookup(offset , My6_8)
Case 2 : Pixels = Lookup(offset , My12_16)
' Case 3 : Pixels = Lookup(offset , Tahoma20x24)
End Select
Ypos = A
For Pixelcount = 0 To 7 'Loop for 8 pixels to be set or not
Pixel = Pixels.0 'Set the pixel (or not)
If Pixel = 1 Then
Call Lcd_set_pixel(xpos , Ypos , White)
Else
Call Lcd_set_pixel(xpos , Ypos , Black)
End If
Shift Pixels , Right 'Shift the byte 1 bit to the right so the next pixel comes availible
Incr Ypos 'Each pixel on his own spot
Next Pixelcount
Incr Offset
Incr Xpos 'Do some calculation to get the caracter on the correct Xposition
Next Columcount
Next Rowcount
Next Carcount
End Sub