Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Using both hardware and software TWI/I2C at the same time?

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
JC

Bascom Member



Joined: 15 Dec 2007
Posts: 586
Location: Cleveland, OH

usa.gif
PostPosted: Sat Mar 07, 2020 7:48 pm    Post subject: Using both hardware and software TWI/I2C at the same time? Reply with quote

Still designing a project, so no hardware to test this on at the moment.

I see Bascom supports both the hardware I2C, (on chips with that module), and a software I2C.

Is it possible to use both the hardware I2C on its pins, and also use the software I2C on different pins, within the same program?

Will this work?

Will this confuse the compiler?

Any simple examples of setting this up?

Thank you!

JC


(BASCOM-AVR version : 2.0.8.2 )
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sat Mar 07, 2020 8:08 pm    Post subject: Reply with quote

Hard and soft channels together? Afaik, no. Multiple soft channels, yes. Multiple hard channels on XMega, yes.
Back to top
View user's profile
JC

Bascom Member



Joined: 15 Dec 2007
Posts: 586
Location: Cleveland, OH

usa.gif
PostPosted: Sat Mar 07, 2020 11:38 pm    Post subject: Reply with quote

Thank you for your insight!

I love the Xmegas, but this project will be on a Nano (M328P).

I'll have a look at using multiple software I2C channels.
Too bad I can't let the hardware do the work!

Thank you,

JC
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun Mar 08, 2020 7:51 am    Post subject: Reply with quote

JC wrote:
Too bad I can't let the hardware do the work!

Well, the TWI hardware unit inside the controller is still available, even you use the soft routines.
If you write your own routines for controlling the hardware, you can have both.
I did guess you don't want that.
Back to top
View user's profile
hgrueneis

Bascom Member



Joined: 04 Apr 2009
Posts: 902
Location: A-4786 Brunnenthal

austria.gif
PostPosted: Sun Mar 08, 2020 10:18 am    Post subject: Reply with quote

JC,

how about multiple slaves with different addresses on one TWI line?

Best regards
Hubert
Back to top
View user's profile
jenalcom

Bascom Member



Joined: 10 Apr 2004
Posts: 365
Location: Perth, Western Australia

australia.gif
PostPosted: Sun Mar 08, 2020 10:39 am    Post subject: Reply with quote

Quote:
Too bad I can't let the hardware do the work!


If all your slaves have the same address then how about using some logic IC to select the one you need?

I haven't tried it myself but I have thought before of using a CMOS 4066 switch IC. Enable two switches at a time (for SCL and SDA) for each slave. One side of each pair goes to the common master SCL and SDA while the other side of each pair go to the individual slave chips SCL and SDA. You will probably need to put pull-ups on the master side and each of the slave sides of twice your normal pull up value (ie 10k or whatever).
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1257
Location: SYDNEY

australia.gif
PostPosted: Sun Mar 08, 2020 12:38 pm    Post subject: Reply with quote

I use the PCA9544A this is an I2C mux have code in the forum for it
as I am using an 12 BIT ADC that only has one address and wanted to use more than one
would this work for you.
Regards Paul
Back to top
View user's profile
JC

Bascom Member



Joined: 15 Dec 2007
Posts: 586
Location: Cleveland, OH

usa.gif
PostPosted: Sun Mar 08, 2020 7:35 pm    Post subject: Reply with quote

I appreciate everyone's interest!

Unfortunately both sensors have the same address, and they are both on a very, very small breakout board.
I don't mind hacking boards, but this one is small enough that it would be very challenging to change the default address on one of the two sensors.

I like the idea of multiplexing the I2C bus, either with low level logic, the 4066 switch, or with a formal I2C multiplexer chip.
I was hoping to avoid the complexity of adding that for this "simple" project.

As MWS noted, the HW I2C hardware module is still there, so I will likely get the software I2C up and running on two general I/O pins, and then see what it would take to manage the hardware I2C module with my own code. That is certainly likely a bit easier than writing my own low level bit-banged I2C routines.

I appreciate everyone's help.

JC
Back to top
View user's profile Visit poster's website
hgrueneis

Bascom Member



Joined: 04 Apr 2009
Posts: 902
Location: A-4786 Brunnenthal

austria.gif
PostPosted: Sun Mar 08, 2020 7:54 pm    Post subject: Reply with quote

JC'

all that is needed, is to switch the SDA line from one to the other....easily done.
Not much of an effort (chip or mosfet or...), and it requires only one line.

Best regards
Hubert
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Sun Mar 08, 2020 9:02 pm    Post subject: Reply with quote

just use the code from i2c_TWI.lib
it should not be hard to figure it out.
either include the code in some basic subs, or rename the lib, and tweak it.
especial the labels must probably be changed.
for example : _i2c_init
would become : _hw_i2c_init

I2CINIT would then be a call to this routine.

but you could also create the bascom basic versions by including the code:

declare sub twi_i2c_init()

sub twi_i2c_init()
$asm
; set i2c pins to right state , open collector , pull up activated
Cbi _sdaDDR,_sda ; init SDA
Cbi _sclDDR,_scl ; init SCL

sbi _sdaPORT,_sda ; activate pull up
sbi _sclPORT,_scl ; activate pull up
$end asm
end sub

of course this also requires that you define _sdaPORT etc,
best to use a new name too.

it should not be much work.
bascom also has multi bus support but it is software only. i understand that using TWI is desirable, but on the other hand the multi bus uses the same routines once.

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

Bascom Member



Joined: 28 Jul 2006
Posts: 1257
Location: SYDNEY

australia.gif
PostPosted: Mon Mar 09, 2020 8:16 am    Post subject: Reply with quote

I have the the PCA9544A on a small adapter board about 15mm square its very small
the PCA9544A also buffers the buss and can work between 5v and 3v sensors
switches with I2C commands so you only need to run one buss to it.
Regards Paul
Back to top
View user's profile
JC

Bascom Member



Joined: 15 Dec 2007
Posts: 586
Location: Cleveland, OH

usa.gif
PostPosted: Tue Mar 10, 2020 2:10 am    Post subject: Reply with quote

Lots of great suggestions, I appreciate it!

Full disclosure:
I don't get to spend as much time on microcontroller projects as I'd like to, as my main job is in another field.
I have a week's vacation coming up in two weeks and I usually take a software focused project with me to work on while I am away from home, (No O'scope, no soldering iron, no parts bins, etc.).
I've been getting a couple sensors set up on a spare PCB to take with me, the project being to write some drivers for them while I am on vacation.
Hence my reluctance to do much of a hardware approach for this, as I won't be at my workbench in case I need to debug the hardware.

It appears, however, that I've still got several good software approaches to investigate, so I'll have a good project to keep me busy.

I certainly appreciate everyone's input on the Forum.
One gets to benefit from everyone else's knowledge and experience!

Thank you,

JC
Back to top
View user's profile Visit poster's website
Pzx

Bascom Member



Joined: 05 May 2015
Posts: 39

poland.gif
PostPosted: Tue May 05, 2020 10:37 am    Post subject: Reply with quote

Hello

I have the same problem. Two sensors with the same I2C address. The project is based on ATMEGA2560.
Can I use I2C multibus or this is is dedicated to XMEGA only? Bascom I2C multibus help has ATMEGA88 chip. Is this a mistake?

The pcb board is already done, so I can not use I2C multiplexer.
One sensor is connected to standard hardware I2C (Scl = Portd.0 Sda = Portd.1) and works fine.
I have not used pins Portc.6 and Portc.7 ideal for connecting the second I2C sensor.
Using hardware I2C for one sensor and software I2C for the second sensor would be great solution for me.
Does anyone have any Bascom code experience with common hardware and software I2C or two independent software I2C in one ATMEGA chip?

Thanks
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Mon May 11, 2020 9:06 am    Post subject: Reply with quote

you can use the multibus lib. it is intended for normal AVR like mega.
from the help :

While XMEGA supports multiple TWI busses, the normal AVR only supports on TWI or no I2C bus. The I2C_MULTIBUS library supports up to 16 I2C busses.
See CONFIG I2CBUS

so because xmega supports multiple buses, this lib was made for normal AVR.

_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot 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