New i2c libraries with 'softi2c'

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
fat16lib
 
Posts: 595
Joined: Wed Dec 24, 2008 1:54 pm

New i2c libraries with 'softi2c'

Post by fat16lib »

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 5741 times
i2c.zip
(12.34 KiB) Downloaded 3807 times
Last edited by fat16lib on Thu Feb 21, 2013 10:36 am, edited 3 times in total.

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: New i2c libraries with 'softi2c'

Post by adafruit »

oh man oh man we're going to hook this up to our DS1307 ASAP :)

User avatar
gwenhastings
 
Posts: 32
Joined: Sat Dec 05, 2009 3:14 pm

Re: New i2c libraries with 'softi2c'

Post by gwenhastings »

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!!

wlewis
 
Posts: 5
Joined: Sat Dec 12, 2009 8:34 pm

Success! Connecting 328p to DS1307

Post by wlewis »

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 10:40 pm, edited 1 time in total.

User avatar
fat16lib
 
Posts: 595
Joined: Wed Dec 24, 2008 1:54 pm

Re: New i2c libraries with 'softi2c'

Post by fat16lib »

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.

User avatar
jer291113
 
Posts: 9
Joined: Fri Jul 23, 2010 9:56 am

Re: New i2c libraries with 'softi2c'

Post by jer291113 »

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!

User avatar
fat16lib
 
Posts: 595
Joined: Wed Dec 24, 2008 1:54 pm

Re: New i2c libraries with 'softi2c'

Post by fat16lib »

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.

User avatar
jer291113
 
Posts: 9
Joined: Fri Jul 23, 2010 9:56 am

Re: New i2c libraries with 'softi2c'

Post by jer291113 »

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!

User avatar
fat16lib
 
Posts: 595
Joined: Wed Dec 24, 2008 1:54 pm

Re: New i2c libraries with 'softi2c'

Post by fat16lib »

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

User avatar
jer291113
 
Posts: 9
Joined: Fri Jul 23, 2010 9:56 am

Re: New i2c libraries with 'softi2c'

Post by jer291113 »

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?

User avatar
fat16lib
 
Posts: 595
Joined: Wed Dec 24, 2008 1:54 pm

Re: New i2c libraries with 'softi2c'

Post by fat16lib »

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.

User avatar
jer291113
 
Posts: 9
Joined: Fri Jul 23, 2010 9:56 am

Re: New i2c libraries with 'softi2c'

Post by jer291113 »

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...

User avatar
fat16lib
 
Posts: 595
Joined: Wed Dec 24, 2008 1:54 pm

Re: New i2c libraries with 'softi2c'

Post by fat16lib »

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.

User avatar
jer291113
 
Posts: 9
Joined: Fri Jul 23, 2010 9:56 am

Re: New i2c libraries with 'softi2c'

Post by jer291113 »

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!

User avatar
fat16lib
 
Posts: 595
Joined: Wed Dec 24, 2008 1:54 pm

Re: New i2c libraries with 'softi2c'

Post by fat16lib »

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.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”