$regfile = "xm32e5def.dat"
$crystal = 32000000 '32MHz
$hwstack = 180
$swstack = 190
$framesize = 190
Config Osc =enabled , 32mhzosc = Enabled
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1
dim twi_start as byte
Config Twic=400000
Config Scl = Portc.1
Config Sda = Portc.0
Open "twic" For Binary As #2
Config I2cdelay = 10
config TCC4=NORMAL,PRESCALE=1024
Tcc4_per=31250
const TWI_ADR = TWIC_CTRL
const TWI_CH = 1
i2Cinit #2
Declare Sub Lcd_init()
Declare Sub Lcd_comm_out(byval Comm As Byte)
Declare Sub Lcd_clear(byval Colo As Byte)
Declare Sub Lcd_show()
Declare Sub Lcd_show_bgf(byval Xs As Byte , Byval Ys As Byte)
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(1024) As Byte 'Display Data Buffer
dim i as integer: i=0
dim j as integer: j=0
'*******************************************************************************
'Init Display -- SSD1306 oder Treiber IC SH1106 wird häufig als Ersatz geliefert.
Const Driver_typ = 0 'SSD1306 =1 SH1106 =0
Call Lcd_init() 'Init Display
'*******************************************************************************
Call Lcd_text( "B2222" , 32 , 9 , 2) 'Draw Text to Buffer
Call Lcd_text( "B2222" , 32 , 29 , 1)
do
incr i
if i=100 then
Call Lcd_clear(black)
Call Lcd_text( "-0000-" , 32 , 29 , 1)
end if
If Tcc4_intflags.0 = 1 Then
incr j
Tcc4_intflags.0 = 1
if j=5 then
Call Lcd_clear(black)
Call Lcd_text( "-0110-" , 32 , 29 , 1)
end if
if j=10 then
Call Lcd_clear(black)
waitms 5
Restore Pic 'first restore Pic
Call Lcd_show_bgf(8 , 20) 'write BGF to Buffer
endif
End If
Call Lcd_show()
Loop
End
Tc4a_isr:
Return
'*******************************************************************************
'include used fonts
$include "My6_8.font"
$include "My12_16.font"
$include "Digital20x32.font" 'Font nur Zahlen Punkt und Komma
'include used BGF
$inc Pic , Nosize , "BGF\abc.bgf" '32x32 pixel
$inc Pic1 , Nosize , "BGF\time.bgf" '32x32 Pixel
$inc Pic2 , Nosize , "BGF\music.bgf" '32x32 Pixel
'Routines
'*******************************************************************************
' Show BASCOM Graphic Files (BGF)
' use the Graphic converter in Uncompressed Mode
' The Sub do not support RLE compression
' Set Xs=Start Xpoint Ys=Start Ypoint
'*******************************************************************************
Sub Lcd_show_bgf(byval Xs As Byte , Byval Ys As Byte)
Local Xz As Byte , Yz As Byte , Col As Byte
Local Bnr As Byte , Xdum As Byte , Xend As Byte , Yend As Byte
Read Yend 'Read Height
Read Xend 'Read Width
Yend = Yend + Ys 'Set end point
Xend = Xend + Xs 'Set end point
Decr Xend
Decr Yend
For Yz = Ys To Yend 'Ystart to Yend
For Xz = Xs To Xend Step 8 'Step 8 Pixel for one Byte
Read Col 'Read BGF file 1Byte = 8 Pixel
Xdum = Xz 'X Start Point
For Bnr = 7 To 0 Step -1 'MSB first Set 8Bit
If Col.bnr = 1 Then 'Read pixel
Call Lcd_set_pixel(xdum , Yz , White) 'Set Pixel
Else
Call Lcd_set_pixel(xdum , Yz , Black) 'Clear Pixel
End If
Incr Xdum 'Incr X pointer
Next
Next
Next
End Sub
'*******************************************************************************
'*******************************************************************************
' Draw Box Xs-Ys to Xe-Ye fill=1 Fill -- Fill=0 no fill
' Color=1 Set Pixel Color=0 clear Pixel
'*******************************************************************************
' 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 Page As Byte , Zab1 As Byte , Zab2 As Byte
Local Point As Word
Point = 1
Page = &HB0 'Page Address + 0xB0
Call Lcd_comm_out(&H40) 'Display start address + 0x40
For Zab1 = 0 To 7
Call Lcd_comm_out(page) 'send page address
Call Lcd_comm_out(&H10) 'column address upper 4 bits + 0x10
#if Driver_typ = 1
Call Lcd_comm_out(&H00) 'column address lower 4 bits + 0x00 H02 for SH1106
#else
Call Lcd_comm_out(&H02)
#endif
I2cstart #2 'start condition
I2cwbyte &H78,#2 'slave address
I2cwbyte &H40,#2
For Zab2 = 1 To 128 '128 columns wide
I2cwbyte Ddata(point),#2
Incr Point
Next
I2cstop #2
Incr Page 'after 128 columns, go to next page
Next
End Sub
'*******************************************************************************
' Clear Display and Display-Array
'*******************************************************************************
Sub Lcd_clear(byval Colo As Byte)
Local Page As Byte , Zab1 As Byte , Zab2 As Word
Page = &HB0 'Page Address + 0xB0
Call Lcd_comm_out(&H40) 'Display start address + 0x40
For Zab1 = 0 To 7
Call Lcd_comm_out(page) 'send page address
Call Lcd_comm_out(&H10) 'column address upper 4 bits + 0x10
#if Driver_typ = 1
Call Lcd_comm_out(&H00) 'column address lower 4 bits + 0x00 H02 for SH1106
#else
call Lcd_comm_out(&H02)
#endif
I2cstart #2 'start condition
I2cwbyte &H78,#2 'slave address
I2cwbyte &H40,#2
For Zab2 = 1 To 128 '128 columns wide
I2cwbyte Colo,#2
Next
I2cstop #2
Incr Page 'after 128 columns, go to next page
Next
For Zab2 = 1 To 1024
Ddata(zab2) = 0 'Clear Display Data Buffer
Next
End Sub
'*******************************************************************************
' Send Command to SSD1306
'*******************************************************************************
Sub Lcd_comm_out(byval Comm As Byte)
I2cstart #2 'start condition
I2cwbyte &H78,#2 'slave address
I2cwbyte &H00,#2
I2cwbyte Comm,#2
I2cstop #2
End Sub
'*******************************************************************************
' Init the Driver SSD1306
'*******************************************************************************
Sub Lcd_init()
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(&H3f) ' 1f 128x32
Call Lcd_comm_out(&Hd3) 'SETDISPLAYOFFSET
Call Lcd_comm_out(&H00)
Call Lcd_comm_out(&H40) 'SETSTARTLINE
Call Lcd_comm_out(&Had) 'CHARGEPUMP
Call Lcd_comm_out(&H8b) '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(&H12) ' 02 128x32 12
Call Lcd_comm_out(&H81) 'SETCONTRAST
Call Lcd_comm_out(200) 'value 1-->256
Call Lcd_comm_out(&Hd9) 'SETPRECHARGE
Call Lcd_comm_out(&H22) 'vccstate f1, 22
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 10
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 Pixels As Byte , Count As Byte , Carcount As Byte
Local Row As Byte , Block As Byte , Byteseach As Byte , Blocksize As Byte
Local Colums As Byte , Columcount As Byte , Rowcount As Byte , Stringsize As Byte
Local Xpos As Byte , Ypos As Byte , Pixel As Byte , 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)
Pixel = Lookup(3 , My6_8)
Case 2 :
Block = Lookup(0 , My12_16)
Byteseach = Lookup(1 , My12_16)
Blocksize = Lookup(2 , My12_16)
Pixel = Lookup(3 , My12_16)
Case 3 :
Block = Lookup(0 , Digital20x32)
Byteseach = Lookup(1 , Digital20x32)
Blocksize = Lookup(2 , Digital20x32)
Pixel = Lookup(3 , Digital20x32)
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
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 , Digital20x32)
End Select
Ypos = Rowcount + Yoffset
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