New i2c libraries with 'softi2c'

by fat16lib on Fri Dec 11, 2009 2:37 pm

Two new software I2C libraries are contained in DigitalIO20130221.zip http://code.google.com/p/rtoslibs/downloads/list. Future updates will be posted on the RTOS libs site.

Here are two i2c libraries i promised to post.

I wrote these libraries for use with simple slave devices like real time clocks, EEPROM, ADCs, and simple sensors.

The interrupt driven Arduino Wire library has a number of disadvantages for these simple devices where the Arduino is the master. The Wire library has limited transfer size due to buffer restrictions, is hard to debug since it is interrupt driven, and only supports the TWI hardware pins.

There are two libraries, SoftI2cMaster and TwiMaster. They are both derived from a TwoWireBase base class. This allows applications to use either library by only changing the init() call in setup().

SoftI2cMaster is a software i2c library that can use any two Arduino pins for SCL and SDA. It has an i2c clock rate of about 65 kHz.

TwiMaster uses the ATmega TWI hardware so you must use the hardware SCL and SDA pins. It is setup with a i2c clock rate of 400 kHz.

26 Dec 2011 new version as file i2cv2.zip
Attachments
i2cv2.zip
Updated 26 Dec 2011
(226.88 KiB) Downloaded 3109 times
i2c.zip
(12.34 KiB) Downloaded 2432 times
Last edited by fat16lib on Thu Feb 21, 2013 9:36 am, edited 3 times in total.
fat16lib
 
Posts: 591
Joined: Wed Dec 24, 2008 12:54 pm

Re: New i2c libraries with 'softi2c'

by adafruit on Fri Dec 11, 2009 4:22 pm

oh man oh man we're going to hook this up to our DS1307 ASAP :)
User avatar
adafruit
 
Posts: 11506
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: New i2c libraries with 'softi2c'

by gwenhastings on Wed Dec 16, 2009 4:18 pm

YAY!!!!.. I have a MaxM ,17xMCP23016-23018 coming in as well as a K22(i2c) and A relay 16 board.. this is going to be real handy!!
gwenhastings
 
Posts: 26
Joined: Sat Dec 05, 2009 2:14 pm

Success! Connecting 328p to DS1307

by wlewis on Thu Dec 24, 2009 10:06 am

To connect the DS1307 to a 328p make sure you provide 2.5 - 3v to the DS1307 battery pin.

"how sweet it is".. The DS1307 works like a charm :D
Last edited by wlewis on Sat Dec 26, 2009 9:40 pm, edited 1 time in total.
wlewis
 
Posts: 5
Joined: Sat Dec 12, 2009 7:34 pm

Re: New i2c libraries with 'softi2c'

by fat16lib on Thu Dec 24, 2009 11:22 am

The library is setup to use software i2c on pins 2 and 3. to use the hardware pins do the following.

Connect the DS1307 SCL to analog pin 5 and SDA to analog pin 4.

Run i2cScanAddress.pde with

Code: Select all
#define USE_SOFT_I2C 0


When power and all connections to the DS1307 are correct the sketch will print:

Add read: D0
Add write: D0
Done


Then run softD1307.pde with USE_SOFT_I2C set to zero.
fat16lib
 
Posts: 591
Joined: Wed Dec 24, 2008 12:54 pm

Re: New i2c libraries with 'softi2c'

by jer291113 on Fri Aug 06, 2010 9:00 am

I was able to get the SD card to read and write but I can not get the s/w I2C to access the DS1307 on the Data Logger Shield and the Arduino Mega.

Has anyone gotten the sketch softDS1307 using the SoftI2CMaster to work?

Any help would be appreciated...Thanks!
jer291113
 
Posts: 9
Joined: Fri Jul 23, 2010 8:56 am

Re: New i2c libraries with 'softi2c'

by fat16lib on Fri Aug 06, 2010 6:11 pm

Could you provide more details about what you have tried and what happened?

The Data Logger Shield is setup for hardware I2C so it's best to use hardware I2C by setting USE_SOFT_I2C to zero in the softDS1307 sketch. Sorry about the sketch name, it works for both hardware and software I2C.

To use software I2C on the Data Logger Shield you will need to redefine SDA_PIN and SCL_PIN.
fat16lib
 
Posts: 591
Joined: Wed Dec 24, 2008 12:54 pm

Re: New i2c libraries with 'softi2c'

by jer291113 on Sat Aug 07, 2010 8:16 am

Thank you for your response to my query.

I am using an Arduino Mega with the Adafruit Data Logging Shield.

I did get it working with the h/w version but when I tried the s/w I2C using Mega pins PF4 (pin 93) and PF5 (pin 92) for SDA and SDC respectively I would get errors, basically no communication with the DS1307.

Hope this is enough info...

Thanks again!
jer291113
 
Posts: 9
Joined: Fri Jul 23, 2010 8:56 am

Re: New i2c libraries with 'softi2c'

by fat16lib on Sat Aug 07, 2010 10:54 am

How did you get pins 93 and 92?

In the Arduino core file pins_arduino.c PF4 is digital pin 58 and PF5 is digital pin 59.

I tried softDS1307 on a Mega with the Data Logging Shield and it seemed to work with these definitions:
Code: Select all
// pins for DS1307 with software i2c
#define SCL_PIN 59
#define SDA_PIN 58
fat16lib
 
Posts: 591
Joined: Wed Dec 24, 2008 12:54 pm

Re: New i2c libraries with 'softi2c'

by jer291113 on Sat Aug 07, 2010 11:58 am

I am missing something here...
I followed the schematic of the Mega board not the pin_out file. The sda and sdc pins of the shield plug into the "analog in" pins labeled 4 and 5. The schematic shows those pins going to PF5 (92) and PF4 (93). The pins you you refer to are going to the Digital I/O header on the end of the board per the schematic which connect to the pins that you reference.
I thought I could use any pins (as long as they are defined in your sketch) using the s/w I2C without having to use any jumpers.
Again did I miss something?
jer291113
 
Posts: 9
Joined: Fri Jul 23, 2010 8:56 am

Re: New i2c libraries with 'softi2c'

by fat16lib on Sat Aug 07, 2010 12:13 pm

I did not use jumpers. These are the digital numbers for analog pins 4 and 5 on the Mega.

Some Mega docs are wrong. I only trust the code.

Try the above defs with a Mega, unmodified Data Logging Shield, and no jumpers.
fat16lib
 
Posts: 591
Joined: Wed Dec 24, 2008 12:54 pm

Re: New i2c libraries with 'softi2c'

by jer291113 on Sat Aug 07, 2010 12:17 pm

I looked at your sketch read.txt file and noticed you refer to the two pins that I tried to use however you reference the 168/328 chips. I'm using the Mega with the ATMega1280 would that make the difference?


Thanks for your assistance and sharing your sketch...
jer291113
 
Posts: 9
Joined: Fri Jul 23, 2010 8:56 am

Re: New i2c libraries with 'softi2c'

by fat16lib on Sat Aug 07, 2010 12:28 pm

Just try what I am telling you. The definitions above are for the Mega pins.

You are using the pin numbers for the 1280 part and this has nothing to do with the software Arduino pin number. You can't get the digital pin number for these pins from the schematic.
fat16lib
 
Posts: 591
Joined: Wed Dec 24, 2008 12:54 pm

Re: New i2c libraries with 'softi2c'

by jer291113 on Sat Aug 07, 2010 1:07 pm

Okay I got it working...yeah!

I misunderstood some comments in the sketch as well as the schematic doesn't jive with the board.

Where can I get the pin_defintions for the Arduino that is correct and complete that you referred to?

Again thank you for your help...it's greatly appreciated!
jer291113
 
Posts: 9
Joined: Fri Jul 23, 2010 8:56 am

Re: New i2c libraries with 'softi2c'

by fat16lib on Sat Aug 07, 2010 2:02 pm

My rule for Arduino pin numbers:

If it is on the silkscreen try it. If it is an analog pin or there are problems read the code.

Not a very satisfactory answer but as an old engineer taught me "The data-sheet is a hint."

Sorry about the unfinished nature of softDS1307. I intended to make a more formal library for software I2C but there was not much interest.
fat16lib
 
Posts: 591
Joined: Wed Dec 24, 2008 12:54 pm