multiple i2c devices

Discuss the Firewing language

multiple i2c devices

Postby paulbrohan » Fri Nov 23, 2018 3:28 pm

I hoped to read multiple i2c devices in turn (the clock and several BME280's) each few minutes. i have r2 board and sd plus but im new to firewing.
if its possible please i would really appreciate sample code to close one i2c bus down and reinitialise a new bus on a different pair of pins.
paulbrohan
 
Posts: 5
Joined: Thu Nov 01, 2018 10:07 am

Re: multiple i2c devices

Postby Jerry Messina » Mon Nov 26, 2018 11:30 am

The BME280 uses a fixed I2C address so you can't have more than one on a bus.

If you look at the I2C.bas module you'll see that the pins used for SCL/SDA are fixed at compile time using '#option'
statements... you can't change them at runtime.
Code: Select all
module I2C

' default pin settings
#if defined(_A5) and defined(_A4)
#warning "system A5 A4 definitions used"
#option I2C_SCL = _A5 
#option I2C_SDA = _A4
#else
#option I2C_SCL = PORTB.8
#option I2C_SDA = PORTB.9
#endif


Probably the easiest way to do this is to use multiple copies of the I2C.bas module, one for each of the
BME280's. That way each BME will have its own bus and you can assign different pins to each device.

The steps to do this are as follows:
1) copy I2C.bas to I2C1.bas
2) edit I2C1.bas and replace all occurences of "I2C" with "I2C1"
3) save the file
4) repeat 1-3 for each new bus, incrementing the number... I2C2, I2C3, I2C4, etc

In your main program add 'imports' for each of the I2Cx.bas modules you've created.
Be sure to set the two "#option I2Cx_SCL = PORTx.0" and "#option I2Cx_SDA = PORTx.0" settings with the
appropriate bus number 'x' and port pin definitions before including the files.

To use them, reference the module name and the function:
Code: Select all
#option I2C1_SCL = PORTB.0
#option I2C1_SDA = PORTB.1
imports I2C1

#option I2C2_SCL = PORTB.2
#option I2C2_SDA = PORTB.3
imports I2C2

#option I2C3_SCL = PORTB.4
#option I2C3_SDA = PORTB.5
imports I2C3

sub main()

I2C1.Start()
I2C1.Stop()

I2C2.Start()
I2C2.Stop()

I2C3.Start()
I2C3.Stop()

end sub
Jerry Messina
 
Posts: 278
Joined: Thu Feb 14, 2013 10:16 am


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x