Go Down

Topic: Confused by SoftI2C (Read 780 times) previous topic - next topic

JT007

Im wanting to use SoftI2C to connect 2 arduino Unos together.
I need all analogue pins for other use in program so i need to change to different pins from analogue 4&5 (the hardware SCL/SDA lines on the uno)

the code i am using is the wire master/slave examples.
where wire allows the setting of an address to listen to and master writes to it
im not 100% sure how this is done using SoftI2C.

Ie.    Wire.begin(4);                // join i2c bus with address #4

Receiver Code

Code: [Select]
#include <Wire.h>

void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}


And  Master
Code: [Select]
#include <Wire.h>

void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
}

byte x = 0;

void loop()
{
  Wire.beginTransmission(4); // transmit to device #4
  Wire.write("x is ");        // sends five bytes
  Wire.write(x);              // sends one byte 
  Wire.endTransmission();    // stop transmitting

  x++;
  delay(500);
}


A simple straight forward example code for a SoftI2C approach to bothe the master and slave above would be very much appreciated.


JT007

I knew i was a genius,
i have obviously asked something way beyond the intelligence of the forum.

spycatcher2k

I would of helped, but I don't see a link to the library you used, or the code you tried, or a note of what you have tried to resolve it.
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

JT007

Hi Drew,

The only example i have of softI2CMaster is this for reading a realtime clock

Code: [Select]
#include <I2cMaster.h>

// select software or hardware i2c
#define USE_SOFT_I2C 0

#if USE_SOFT_I2C
#if defined(__AVR_ATmega1280__)\
|| defined(__AVR_ATmega2560__)
// Mega analog pins 4 and 5
// pins for 168/328 shield on Mega
#define SDA_PIN 58
#define SCL_PIN 59

#elif defined(__AVR_ATmega168__)\
||defined(__AVR_ATmega168P__)\
||defined(__AVR_ATmega328P__)
// 168 and 328 Arduinos analog pin 4 and 5
#define SDA_PIN 18
#define SCL_PIN 19

#else  // CPU type
#error unknown CPU
#endif  // CPU type
// An instance of class for software master
SoftI2cMaster rtc(SDA_PIN, SCL_PIN);
#else // USE_SOFT_I2C
// hardware master with pullups enabled
TwiMaster rtc(true);
#endif  // USE_SOFT_I2C

//------------------------------------------------------------------------------
void setup(void) {
  Serial.begin(9600);

  uint8_t add = 1;

  // try read
  do {
    if (rtc.start(add | I2C_READ)) {
      Serial.print("Add read: ");
      Serial.println(add, HEX);
      rtc.read(true);
    }
    rtc.stop();
    //add += 2;
  } while (add);

  // try write
  //add = 0;
  do {
    if (rtc.start(add | I2C_WRITE)) {
      Serial.print("Add write: ");
      Serial.println(add, HEX);
    }
    rtc.stop();
    //add += 2;
  } while (add);

  Serial.println("Done");
}
void loop(void){}


As you can see from my earlier post it dosent follow the same format as the wire I2C
the library is posted here
http://rweather.github.io/arduinolibs/classSoftI2C.html 

a kindergarten idiot guide would be much appreciated.
i would like to be able to send from the master 1 byte to any slave by its address
and for a slave being poled by the master to receive the byte of data.

i would really appreciate some assistance

spycatcher2k

Ah, I see, the library does not have a slave version, I can't find one, why not use the serial pins?
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

JT007

I want to use the Ardunio to its max.
i need 6 digital pins for relay control, 2 digital pins for soft serial/RS485 and 6 analogue inputs , 2 pins for I2C bus to connect motor servos (MD03s) and an IMU6050. leaving the 2 hardware serial pins for reprogramming if reqd.

I build my own Arduino boards using a boot loaded ATMEGA328 so i can fit as much circuitry as i can to a control board.
i could use the Arduino mega instead but i would have to use the mega board instead of my own build- limited expertise and all that

Im still messing around with submersible vehicles and this configuration will save me at least £400 as i wont need a PC104 single board PC on board my vehicle.

i was hoping that someone out on the ether had connected 2 arduinos with softI2C,  searched for ages and found nothing.
you mentioned the code was just the Master.

is there a way i can have the master join the bus as channel 0 and for eg transmit to address  4 with a value of 200.
if i can get this far i can change the slave to use the hardware wire library instead.
cheers
Jason

spycatcher2k

These commands will do it in the library you linked to :

void    startWrite (unsigned int address)
   Starts a write operation by sending a start condition and the I2C control byte.
void    write (uint8_t value)
   Writes a single byte value on the I2C bus.
bool    endWrite ()
   Ends the current write operation.
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy