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
jer291113
 
Posts: 9
Joined: Fri Jul 23, 2010 9:56 am

Re: New i2c libraries with 'softi2c'

Post by jer291113 »

I really aprreciate the work you have done so no apology is necessary and surprised there isn't more interest.

I'm a retired EE and wanted to play with the Arduino environment, I don't quite understand why the pin-out defs don't match the schematic but that's the way it is.
I'll try your advice -> "If it is on the silkscreen try it. If it is an analog pin or there are problems read the code."

By the way I did find the pin_definitions...

Thanks!!!

schamp
 
Posts: 9
Joined: Mon Jul 05, 2010 7:41 pm

Re: New i2c libraries with 'softi2c'

Post by schamp »

I think this is a huge improvement over the provided "Wire" library. For one, it makes it easier to match an I2C interchange as described in a datasheet. What is the license? Have you considered contributing it to the Arduino core?

User avatar
stevekamerman
 
Posts: 1
Joined: Thu Dec 30, 2010 3:46 am

Re: New i2c libraries with 'softi2c'

Post by stevekamerman »

Hi guys, I'm not sure where this is all going at this point - I've seen lots of different libraries and other hacks for using i2c / TWI over the non-hardware ports. I looked around at quite a few libraries and decided to roll my own. This lib is specifically for the DS1307 and is class-compatible with the Arduino Time class, so you can simply specify the pins you want to use for SCL and SDA, and you're good to go!

Here's an example sketch that I'm testing with my new homebrew breadboard RTC:

Code: Select all


/*
 * TimeRTCSet.pde
 * example code illustrating Time library with Real Time Clock.
 *
 * RTC clock is set in response to serial port time message 
 * A Processing example sketch to set the time is inclided in the download
 */

#include <Time.h>
#include <TwoWireBase.h>
#include <SoftI2cMaster.h>
#include <SoftDS1307RTC.h>  // a basic DS1307 library that returns time as a time_t

#define SDA_PIN 10
#define SCL_PIN 11

void setup()  {
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= BANNED) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");      
}

void loop()
{
  if(Serial.available())
  {
     time_t t = processSyncMessage();
     if(t >0)
     {
        RTC.set(t);   // set the RTC and the system time to the received value
        setTime(t);          
     }
  }
   digitalClockDisplay();  
   delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

/*  code to process time sync messages from the serial port   */
#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message

time_t processSyncMessage() {
  // return the time if a valid sync message is received on the serial port.
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
    char c = Serial.read() ; 
    Serial.print(c);  
    if( c == TIME_HEADER ) {       
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c = Serial.read();          
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
        }
      }   
      return pctime; 
    }  
  }
  return 0;
}
Anyway, if you are interested in taking a further look into the code, check it out at my Github repo: https://github.com/kamermans/SoftDS1307RTC

User avatar
lemuroid
 
Posts: 18
Joined: Wed Feb 02, 2011 5:35 pm

Re: New i2c libraries with 'softi2c'

Post by lemuroid »

Woot! The software i2c works. if you are struggling to follow the above thread ( i got distracted by the discussion of pins 93 and schematics)

the simple answer is,

step 1: get the i2c libraries posted at the top of the thread and put them in the proper location for arduino to find them.
step 2: modify the softDS1307 code as follows:

#define USE_SOFT_I2C 1

#if USE_SOFT_I2C

// pins for DS1307 with software i2c
#define SCL_PIN 59
#define SDA_PIN 58

User avatar
jarcher
 
Posts: 64
Joined: Sun Mar 27, 2011 2:49 am

Re: New i2c libraries with 'softi2c'

Post by jarcher »

Can this library be used inside an ISR? In other words, does it use interrupts itself?

User avatar
lemuroid
 
Posts: 18
Joined: Wed Feb 02, 2011 5:35 pm

Re: New i2c libraries with 'softi2c'

Post by lemuroid »

I had this working fine with version 022 but now it throws error messasges in version 1.0. Any ideas how to update the i2c library to work in 1.0?

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

Re: New i2c libraries with 'softi2c'

Post by fat16lib »

I added a new version, i2cv2.zip, in the original post that should work with Arduino 1.0.

harvey11
 
Posts: 3
Joined: Tue Dec 20, 2011 7:59 am

Re: New i2c libraries with 'softi2c'

Post by harvey11 »

Hi everyone, just wanted to share a bit of code to make your libraries compatible with Arduino 1.0 as well as keep them working on Arduino 22. You can make them work easily by simply changing “WProgram.h” to “Arduino.h”, but if they are shared, anyone who does not upgrade to the Arduino 1.0 IDE will lose the function of that library.
You will need to change the header file that ends in .h as well as the source file that ends in .cpp. As you might already know, the file “WProgram.h” has been replaced with “Arduino.h” in version 1.0 of the Arduino IDE.
You can make the library backwards compatible by replacing the line #include “WProgram.h” with the following code:

Code: Select all #if defined(ARDUINO) ampamp ARDUINO gt= 100
#include “Arduino.h”
#else
#include “WProgram.h”
#endif

This simply checks for the version of Arduino and loads the correct file. When there is an upgrade to the Arduino IDE, the version number will be higher than 100, still keeping the function of the code listed above. I have tried this with some small libraries that I have made as well as the PCD8544 (Nokia 5110/3310 LCD from Adafruit) and it works well. Happy coding.

User avatar
thelandlord
 
Posts: 7
Joined: Fri Apr 15, 2011 8:37 pm

Re: New i2c libraries with 'softi2c'

Post by thelandlord »

I have downloaded this library and can run the scan demo successfully using an Arduino Uno board against the I2C port and digital pin (6,7) using TwiMaster and SoftI2CMaster respectively. I am trying to turn on/off an LED connected to a MCP23017 GPIO, the code runs successfully with TwiMaster, but fails when I run with it SoftI2cMaster using any pair of pins. Curiously enough I can scan the bus using the SoftI2cMaster and see the MCP23017.

The wiring, ports, etc are solid since it works with the TwiMaster. Scanning the bus with the SoftI2cMaster implementation identifies the MCP23017 and writing to the ports do not generate any errors. Any pointers on how to proceed to debug this problem?

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

Re: New i2c libraries with 'softi2c'

Post by fat16lib »

There must be some timing difference between TWI and my soft I2C that causes the failure. You can never exactly emulate hardware I2C with a bit-bang implementation.

I happen to have a MCP23017 that I never used for a project.

Could you post an example that demonstrates the problem and I will look at it. Please try to isolate the problem so the example is as simple as possible.

I will look at it with a scope and logic analyzer and try to find a fix.

Edit:
I found an MCP23017 library and modified it to use I2cMaster and verified your problem. I see a glitches on the SDA line for Soft I2C and am trying to find the reason.

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

Re: New i2c libraries with 'softi2c'

Post by fat16lib »

I have updated i2cv2.zip with corrections that should fix soft I2C problems with the MCP23017. Go to the original post at the beginning of this topic and download the new version.

I have attached the library and sketch that I used to debug the MCP23017 problem.
Attachments
mcp23017.zip
MCP23017 library with test sketch
(4.14 KiB) Downloaded 291 times

User avatar
thelandlord
 
Posts: 7
Joined: Fri Apr 15, 2011 8:37 pm

Re: New i2c libraries with 'softi2c'

Post by thelandlord »

Thanks for the prompt response!!!!
The addition of the extra line to the write method worked as you described in your post. Enclosed is an extract of the code i use for testing. Now I can use SoftI2cMaster or TwiMaster to perform the same function. For my own education, how did you go about troubleshooting this problem? Good Kung-Fu!

SoftI2cMaster rtc(18,19);
//TwiMaster rtc(false);
uint8_t address = 0x24 << 1;
void setup()
{
Serial.begin(19200);
Serial.println("Begin Demo");

for ( uint8_t i = 1; i < 126; ++i )
{
if ( rtc.start( (i << 1) | I2C_READ) )
{
Serial.print("Add read: ");
Serial.println(i, HEX);
rtc.read(true);
}
rtc.stop();
}
if (!rtc.start( address | I2C_WRITE) ) Serial.println("start failed");
if (!rtc.write(0x01) ) Serial.println("Write 1 failed");
if (!rtc.write(0x00) ) Serial.println("Write 2 failed");
rtc.stop();
if (!rtc.start( address | I2C_WRITE) ) Serial.println("start failed");
if (!rtc.write(0x13) ) Serial.println("Write 3 failed");
if (!rtc.write(0x01) ) Serial.println("Write 4 failed");
rtc.stop();
}

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

Re: New i2c libraries with 'softi2c'

Post by fat16lib »

I setup the MCP230177 with a scope on SCL (yellow) and SDA (blue) and looked at TWI I2C:
hardI2C.jpg
hardI2C.jpg (101.85 KiB) Viewed 8447 times
and software I2C:
softI2C.jpg
softI2C.jpg (104.64 KiB) Viewed 8447 times
And noticed SDA was LOW after sending a byte with TWI and HIGH with software I2C.

I added the line to write and it seemed to work so I did the post with the added line.

I thought more about it and realized it was really stop() that had a problem. I did more mods, deleted the post with the added line, and posted a new version of i2cv2.zip.

You should download the new version of i2cv2.zip which is posted in the beginning of this topic.

User avatar
stan12
 
Posts: 11
Joined: Fri Jul 13, 2012 12:54 am

Re: New i2c libraries with 'softi2c'

Post by stan12 »

I am using
Windows XP
Arduino IDE 1.0
Adafruit Data Logger shield on Arduino Mega 2560

I2cMaster library installed into 'libraries' folder from i2cv2.zip

When I run softDS1307 sketch (from i2cv2.zip) I have initially in Serial Monitor window:

Code: Select all

The current time is 07/13/2012 Thu 00:51:22
Control: 00

Options are:
(0) Display date and time
(1) Set time
(2) Set date
(3) Set Control
(4) Dump all
(5) Set NV RAM
Enter option: 
time seems correct, but no options are working.

When I enter any number and hit 'send', I am always getting:

Code: Select all

Invalid entry
99

Invalid option
I made no modifications to softDS1307 code.
Results are the same in IDE 1.0 and 1.0.1

Is this normal or am I doing anything wrong?

i2cScanAddress sketch, when I changed to

Code: Select all

#define USE_SOFT_I2C 1
produces this output:

Code: Select all

Add read: D0
Add write: D0
Done
with USE_SOFT_I2C 0
it only shows word 'done' which means it did not find anything.

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

Re: New i2c libraries with 'softi2c'

Post by fat16lib »

I suspect you have an option other than "No line ending" enabled in the serial monitor.

This is at the bottom of the serial monitor near the baud rate selector.

The input function needs to be fixed to treat any combination of CR and LF as end-of-line.

I keep meaning to post a new version of the I2C stuff but have not found time to finish the new version. Sorry!

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

Return to “Arduino”