Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

M328PB Can't get second hardware TWI to drive SSD1306 Displa
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR old unsupported versions
View previous topic :: View next topic  
Author Message
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6321
Location: Holland

blank.gif
PostPosted: Tue Feb 03, 2026 11:27 am    Post subject: Reply with quote

ok i will post a similar version for the v2x2 version so you can use both SPI interfaces.
_________________
Mark
Back to top
View user's profile Visit poster's website
guyadams

Bascom Member



Joined: 22 Nov 2006
Posts: 44

uk.gif
PostPosted: Tue Feb 03, 2026 11:38 am    Post subject: Reply with quote

albertsm wrote:
ok i will post a similar version for the v2x2 version so you can use both SPI interfaces.


Great - not that it matters but it was a case of being able to use either TWI interface. This design does not actually use both but I needed to use the second TWI for the display as the ADC inputs on PortC were already used...

Best r
Guy
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6321
Location: Holland

blank.gif
PostPosted: Tue Feb 03, 2026 3:01 pm    Post subject: Reply with quote

Ok this version is the same as the previous one except it supports dynamic LCD address.
In order to use that create a variable named I2C_Addr and assign it the value of your LCD. That is typical &H78
For the rest use the I2C_TWI-MULTI.lib with its instructions.

You use 1 LCD so you only set the _I2CCHANNEL to the second TWI.
Now i think of it : the LCD lib you have now should work with the second TWI too ( I2C_TWI-MULTI.lib)

So please try that first if that is right.
glcdSSD1306-I2C-TWI-option.lib
I2C_TWI-MULTI.lib

And use the lib you tested last. It should work.

Then you can download and test the attached lib.
Same test. NO variable named I2C_ADDR and see if it still works.
Then the last test would be to create I2C_ADDR and assign it with &H78 BEFORE you use config graphlcd.
It should work too.

For users that want to use 2 LCD with different address : the multi lib is needed and since the lib initialize 1 LCD you have to call initlcd twice (one time with the address loaded in I2C_addr)

_________________
Mark
Back to top
View user's profile Visit poster's website
guyadams

Bascom Member



Joined: 22 Nov 2006
Posts: 44

uk.gif
PostPosted: Tue Feb 03, 2026 3:27 pm    Post subject: Reply with quote

Quote:
So please try that first if that is right.
glcdSSD1306-I2C-TWI-option.lib
I2C_TWI-MULTI.lib


This works like a charm - toggling _i2cchannel redirects the display correctly. I did notice that as long as one TWI is configured , then both ports work.

I will do the other test in a moment.....

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

Config Scl1 = PortE.1                                        ' used i2c pins
Config Sda1 = PortE.0
Config Twi1 = 400000                                         ' i2c speed


I2cinit

Const cTWI = 1

'$lib "i2c_twi.lbx"                                           ' we do not use software emulated I2C but the TWI
$lib "I2c_twi-multi.lib"                                     ' 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"                        ' override the default lib with this special one

Dim _i2cchannel as byte
_i2cchannel = 1                                                ' 0 for TWI0, 1 for TWI1


#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

Do

cls

Lcdat 1 , 1 , "BASCOM-AVR"
Lcdat 2 , 10 , "1995-2020"
Lcdat 8 , 5 , "MCS Electronics" , 1
Waitms 1000

Showpic 0 , 0 , Plaatje

Waitms 1000

Loop

End


$include "font8x8TT.font"                                   ' this is a true type font with variable spacing


Plaatje:
   $bgf "ks108.bgf"                                         ' include the picture data
Back to top
View user's profile
guyadams

Bascom Member



Joined: 22 Nov 2006
Posts: 44

uk.gif
PostPosted: Tue Feb 03, 2026 3:41 pm    Post subject: Reply with quote

And

Quote:
Then you can download and test the attached lib.
Same test. NO variable named I2C_ADDR and see if it still works.


This still works....

Finally

Quote:
Then the last test would be to create I2C_ADDR and assign it with &H78 BEFORE you use config graphlcd.
It should work too.


And this works too!

Thank you to all who contributed to fixing this!

Best r
Guy


This is how simple it now is to swap the SSD1306 from TWI port 0 where it might conflict with high ADC channel usage on Port C, to the second hardware TWI on Port E.
Code:
Config Scl1 = PortE.1                                        ' used i2c pins
Config Sda1 = PortE.0
Config Twi1 = 400000                                         ' i2c speed


I2cinit

Const cTWI = 1


$lib "I2c_twi-multi.lib"                                     ' we do not use software emulated I2C but the TWI
$lib "glcdSSD1306-I2C-TWI-option.lib"                        ' override the default lib with this special one

Dim _i2cchannel as byte
_i2cchannel = 1                                                ' 0 for TWI0, 1 for TWI1

Dim I2c_addr as byte
I2c_addr = &H78
 
Back to top
View user's profile
guyadams

Bascom Member



Joined: 22 Nov 2006
Posts: 44

uk.gif
PostPosted: Tue Feb 03, 2026 3:49 pm    Post subject: Reply with quote

Here is an image showing there has been and currently is the correct display activity after changing TWI channel. Obviously they are not running concurrently which is where the 2x2 library is used..

[/img]
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR old unsupported versions All times are GMT + 1 Hour
Goto page Previous  1, 2, 3
Page 3 of 3

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum