| View previous topic :: View next topic |
| Author |
Message |
guyadams
Joined: 22 Nov 2006 Posts: 44

|
Posted: Sun Feb 01, 2026 12:05 pm Post subject: |
|
|
| MWS wrote: | Then ofamily's hint likely applies, i.e that twi-multi uses additional processor registers, which your v2 graphic lib uses too.
If these additional registers are not saved while the v2 calls the multi lib's functions, these registers return corrupted.
Using the flyswatter, every processor registers can be saved before calling multi-lib functions, a more surgical approach is to walk through both libs an save double used registers selectively. |
Thanks - I wonder why it works on TWI 0 from the basis that the register usage will be similar but with a 1 instead of a 0 in the register definitions to direct to the other TWI port. As I am sure you can tell my programming skills are not good, so the step of saving multiple registers or identifying them is new (I have got some $asm code that Pushes and Pops an individual register but that is it). My default is to unfortunately continue using the software TWI or if there is a revision of the board move the display to TWI 0/remap the ADC inputs....
Cheers
Guy |
|
| Back to top |
|
 |
O-Family
Joined: 23 May 2010 Posts: 352 Location: Japan

|
Posted: Sun Feb 01, 2026 1:49 pm Post subject: |
|
|
"glcdSSD1306-I2C_V2x2.lib" is the library for connecting two identical displays simultaneously.
If you're using only one display, "glcdSSD1306-I2C_V2.lib" is fine.
"I2Cstart" is not required; write it as shown below.
I2cinit
I2cinit Twi1
As described in the "I2C_TWI-MULTI" help, why not also include the first channel setting? |
|
| Back to top |
|
 |
MWS
Joined: 22 Aug 2009 Posts: 2377

|
Posted: Sun Feb 01, 2026 3:37 pm Post subject: |
|
|
| guyadams wrote: | | Thanks - I wonder why it works on TWI 0 from the basis that the register usage will be similar but with a 1 instead of a 0 in the register definitions to direct to the other TWI port. |
Ofamily"s hint was (did not check it myself) that twi-multi uses more processor registers than the basic one channel hard twi.
On 8bit AVRs these registers are called R0 - R31.
If for example twi-multi uses additional R23, which the basic twi did not, then the v2 graphic's which we assume uses R23 too, will work with the basic twi, but not with twi-multi, as the latter would corrupt R23 while being called. |
|
| Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6321 Location: Holland

|
Posted: Sun Feb 01, 2026 3:54 pm Post subject: |
|
|
It appears that R20 is used in the graphic lib but also but i2c multi. That means that in one of the libs this reg should be saved/restored. Or that a different register should be used.
The graphic libs use a lot of regs.
The multi lib is not written with graphic LCD in mind. So it is not good practice to alter this lib.
On the other hand it is the simplest thing.
You could always fork the lib to a custom one where you save r20 for usage with the graphic i2c lib. _________________ Mark |
|
| Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6321 Location: Holland

|
Posted: Sun Feb 01, 2026 4:01 pm Post subject: |
|
|
| Quote: | | I wonder why it works on TWI 0 |
That is unlikely. Because all is the same code, except pointer Z is loaded with either TWI0 or TWI1. You can see this in the lib at label _i2c_chan_setup
So when it works for TWI0 without problem it must work for TWI1 too.
When i look at the sample that i tested the normal I2c_TWI was used. But that lib uses R20 as well.
So maybe i did not check properly. Or the graphic lib was altered later.
I can check it at a later moment. _________________ Mark |
|
| Back to top |
|
 |
MWS
Joined: 22 Aug 2009 Posts: 2377

|
Posted: Sun Feb 01, 2026 7:58 pm Post subject: |
|
|
| albertsm wrote: | | Quote: | | I wonder why it works on TWI 0 |
That is unlikely. Because all is the same code, except pointer Z is loaded with either TWI0 or TWI1.You can see this in the lib at label _i2c_chan_setup |
The TO did already confirm within this thread that TWI0 with twi-multi does NOT work.
You've left out in your quote the words "from the basis" which translates to twi-non-multi lib, i.e. the standard hard-twi lib. |
|
| Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6321 Location: Holland

|
Posted: Sun Feb 01, 2026 9:17 pm Post subject: |
|
|
@MWS. Yeah got confused. I should have re-read the thread from the beginning.
| Quote: | | $lib "i2c_twi.lib" 'Hardware TWI - have used successfully on TWI #0 half a dozen times |
Do you mean with this LCD ? Or in general?
The way O-Family solves it in his version of the multibus lib is ok: instead of calling the i2c lib directly, create a local version that saves/restores and calls the code.
Only regs saved are R20,R30 and R31.
If you test the lcd with the $lib "i2c_twi.lbx" and let me know if it works i will mod the glcdSSD1306-I2C_V2x2.lib so it works with both TWI's. _________________ Mark |
|
| Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6321 Location: Holland

|
Posted: Sun Feb 01, 2026 9:58 pm Post subject: |
|
|
When the i2c_twi does not work use the attached one.
This has an option for using twi with normal AVR. (non xmega/xtiny).
Just add a constant to your code : const cTWI= 1 'value does not actual matter
It is intended for TWI/ TWI0. I expect the default i2c_twi will not work but with this modified lib it should. _________________ Mark
Last edited by albertsm on Tue Feb 03, 2026 10:48 am; edited 1 time in total |
|
| Back to top |
|
 |
guyadams
Joined: 22 Nov 2006 Posts: 44

|
Posted: Mon Feb 02, 2026 12:28 am Post subject: |
|
|
| albertsm wrote: | When the i2c_twi does not work use the attached one.
This has an option for using twi with normal AVR. (non xmega/xtiny).
Just add a constant to your code : const cTWI= 1 'value does not actual matter
It is intended for TWI/ TWI0. I expect the default i2c_twi will not work but with this modified lib it should. |
Thanks - The code below runs the display on TWI_0. However when I try to initialize TWI_1 I get a compile error Error : 211 Line : 83 External routine not found [_I2C1] , in File : C:\Data Guys Personal Archive\Personal\TT Motor\Code\DAC80004 DDS V1\M328PB_DAC80004_Micro2House_LCD_V1Test.bas. Have I called the correct libraries?
I have wired up displays on both ports for debugging... The image at the bottom shows TWI_0 on the left.
| Code: | '--------------------------------------------------------------
'M328PB + SSD1306 on TWI #1 Test File - Hardware TWI
'--------------------------------------------------------------
$regfile = "m328PBdef.dat"
$crystal = 8000000
'
$hwstack = 68
$swstack = 64
$framesize = 64
'$lib "i2c_twi.lbx" 'Hardware TWI - have used successfully SSD1306 on TWI #0 half a dozen times
'$lib "I2C_TWI-MULTI.lib" 'Important for using 2 Hardware TWI interfaces
$lib "glcdSSD1306-I2C_V2.lib" 'Override the default lib with this special one
'$lib "glcdSSD1306-I2C_V2_multibus.lib" 'Override the default lib with this special one
$lib "glcdSSD1306-I2C-TWI-option.lib"
Config Clockdiv = 1 'make sure we get 8Mhz
Config Scl1 = PortE.1 'TWI 1 PortE.1 Port E.0
Config Sda1 = PortE.0 'use this for the second TWI
Config Twi1 = 100000 'speed 100 KHz
Config Scl = PortC.5 'TWI 0 PortC.5 and PortC.4
Config Sda = Portc.4 'use this for the first TWI
Config Twi = 100000 'speed 100 KHz
Config Graphlcd = Custom , Cols = 128 , Rows = 64 , Lcdname = "SSD1306"
Dim _TWI_STOP_TIMEOUT As Byte
Dim _contrast As Byte ' Contrast value of OLED. [0-255]
Dim I2c_addr As Byte ' I2C slave address selection. [&H78 or &H7A]
Dim _i2cchannel As Byte ' you MUST dim this variable yourself when using the I2C_TWI-MULTI lib
Dim cTWI as byte
cTWI = 1
'Dim cTWI0 as byte '? correct interpretation
'cTWI0 = 1
'_i2cchannel = 1
'_i2cchannel = 0
_TWI_STOP_TIMEOUT = 1
_contrast = 255
I2c_addr = &H78
Ddrd = &B00010000 'Use PortD.4 as prog execution indicator
'I2cinit Twi1
I2cinit
I2Cstart 'fix for compiler
Do
Cls
waitms 1000
Setfont Font16x16
Lcdat 1 , 1 , " BASCOM"
Setfont Font8x8tt ' select font
Lcdat 3 , 5 , "MCS Electronics" , 1 ' Optional invert
Lcdat 5 , 1 , " Device [1] " , 2 ' Optional solid underline
Lcdat 7 , 1 , " Device [1] " , 3 ' Optional dotted underline
reset portd.4 'Debug - prog execution
waitms 1000
set portd.4 'Debug - prog execution
loop
End
$include "font8x8TT.font" ' this is a true type font with variable spacing
$include "font16x16.font" |
[/img] |
|
| Back to top |
|
 |
MWS
Joined: 22 Aug 2009 Posts: 2377

|
Posted: Mon Feb 02, 2026 9:22 am Post subject: |
|
|
| guyadams wrote: | | albertsm wrote: | | Just add a constant to your code : const cTWI= 1 'value does not actual matter |
Thanks - The code below runs the display on TWI_0. |
You seem not to read exactly.
A constant is different from the variable you've set up.
| Code: | im cTWI as byte
cTWI = 1 |
You override the override:
| Code: | $lib "glcdSSD1306-I2C_V2.lib" 'Override the default lib with this special one
$lib "glcdSSD1306-I2C-TWI-option.lib" |
| Quote: | | However when I try to initialize TWI_1 I get a compile error Error : 211 Line : 83 External routine not found [_I2C1] , in File : C:\Data Guys Personal Archive\Personal\TT Motor\Code\DAC80004 DDS |
No wonder, due to not including any hard-twi lib you use the soft-I2C/TWI lib
| Code: | '$lib "i2c_twi.lbx" 'Hardware TWI - have used successfully SSD1306 on TWI #0 half a dozen times
'$lib "I2C_TWI-MULTI.lib" 'Important for using 2 Hardware TWI interfaces
$lib "glcdSSD1306-I2C_V2.lib" 'Override the default lib with this special one
'$lib "glcdSSD1306-I2C_V2_multibus.lib" 'Override the default lib with this special one
$lib "glcdSSD1306-I2C-TWI-option.lib" |
|
|
| Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6321 Location: Holland

|
Posted: Mon Feb 02, 2026 10:02 am Post subject: |
|
|
To be clear : i like you test the sample ssd1306-i2c.bas from the samples folder.
It has this configuration :
$lib "i2c_twi.lbx" ' we do NOT use software emulated I2C but the TWI
$lib "glcdSSD1306-I2C.lib" ' override the default lib with this special one
So that is all existing code that comes with bascom. I like to know if it works. (because i do not have the lcd hooked up)
Then the next test is with :
$lib "i2c_twi.lbx" 'Hardware TWI - have used successfully SSD1306 on TWI #0 half a dozen times
$lib "glcdSSD1306-I2C-TWI-option.lib"
Using a constant in your code : CONST cTWI=1
And both test must be done on TWI0
So code referencing TWI1 should be remarked.
Now let me know the results of these tests.
This will not work for TWI1 and the LCD address is hard coded in this old lib.
When i know the results i will change the SSD1306 lib in a similar way so you can use both TWI0 and TWI1 _________________ Mark |
|
| Back to top |
|
 |
guyadams
Joined: 22 Nov 2006 Posts: 44

|
Posted: Tue Feb 03, 2026 1:03 am Post subject: |
|
|
Thanks for the input again. Sorry for the silly errors.
The sample code works, however the revised library ($lib "glcdSSD1306-I2C-TWI-option.lib") does not - it gives a full white screen with underlines on each line if programmed after the working version or from a cold start it is blank..
Slowing the I2C bus to 100KHz makes no difference
This is the code
| Code: | '-------------------------------------------------------------------------------
' SSD1306-I2C.BAS
' (c) MCS Electronics 1995-2020
' Sample to demo the 128x64 I2C OLED display
'
'-------------------------------------------------------------------------------
$regfile = "m328PBdef.dat"
$hwstack = 32
$swstack = 32
$framesize = 32
$crystal = 8000000
Config Clockdiv = 1 ' make sure the chip runs at 8 MHz
Config Scl = Portc.5 ' used i2c pins
Config Sda = Portc.4
Config Twi = 400000 ' i2c speed
I2cinit
Const cTWI = 1
$lib "i2c_twi.lbx" ' we do not use software emulated I2C but the TWI
'$lib "glcdSSD1306-I2C.lib" ' override the default lib with this special one
$lib "glcdSSD1306-I2C-TWI-option.lib"
#if _build < 20784
Dim ___lcdrow As Byte , ___lcdcol As Byte ' dim these for older compiler versions
#endif
Config Graphlcd = Custom , Cols = 128 , Rows = 64 , Lcdname = "SSD1306"
Cls
Setfont Font8x8tt ' select font
Lcdat 1 , 1 , "BASCOM-AVR"
Lcdat 2 , 10 , "1995-2020"
Lcdat 8 , 5 , "MCS Electronics" , 1
Waitms 3000
Showpic 0 , 0 , Plaatje
End
$include "font8x8TT.font" ' this is a true type font with variable spacing
Plaatje:
$bgf "ks108.bgf" ' include the picture data |
|
|
| Back to top |
|
 |
guyadams
Joined: 22 Nov 2006 Posts: 44

|
Posted: Tue Feb 03, 2026 1:22 am Post subject: |
|
|
| Oh I should add - the Const cTWI = 1 declaration is greyed out/italics in Bascom before compiling even when the new lib is being called - the insert code function on the forum seems to bypass the compiler checks. |
|
| Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6321 Location: Holland

|
Posted: Tue Feb 03, 2026 10:50 am Post subject: |
|
|
Odd that the sample works since it uses R20 in the lib which the TWI will use too. I will check that.
But for the option lib i made a dumb error. I corrected it so you can download it again and try again. _________________ Mark |
|
| Back to top |
|
 |
guyadams
Joined: 22 Nov 2006 Posts: 44

|
Posted: Tue Feb 03, 2026 11:23 am Post subject: |
|
|
Thanks - I deleted the previous version from the lib folder and added the new one. Yes $lib "glcdSSD1306-I2C-TWI-option.lib" now works on TWI 0.
Best r
Guy |
|
| Back to top |
|
 |
|