Go Down

Topic: Single wire interface (Read 3421 times) previous topic - next topic

gareth10

Hi guys,
I'm new to Audrino and programming. I wanted to start by looking simply at passing data between 2 Audrino Mega2560 boards.
I purchased the starter kit and an additional board to do this.
I want to pass the data over a single wire protocol.
Does anyone have any advice/starting points?
Many Thanks  :)

johnwasser

You could start with the OneWire library (although technically you need a common ground so you need two wires).
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

gareth10

I understand its a data wire and a ground. I understand the protocol, I am just struggling to code the devices to pass and read data between one another. All the libraries are aimed at using sensors as oppose to passing/checking data between 2 micro-controllers...Thanks

johnwasser

Sounds like you want to implement a multi-master one-wire bus.  Maybe you can use one of the external interrupt pins with a pull-up.  The device that wants to send will change that pin to an output and squirt out a message of fixed size and timing with a checksum.  The others will get a "falling edge" interrupt on the start bit and then clock in the data and checksum.  After each message you release the bus by setting it back to an input.

Good luck.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

ntruchsess

#4
Oct 30, 2013, 04:10 pm Last Edit: Oct 30, 2013, 04:23 pm by ntruchsess Reason: 1
The slave-side of the 1-Wire protocoll as it's defined by maxim is tricky to implement reliable in software as timing is very critical and controlled to 100% by the master. The slave has to respond to the masters reset by pulling down the bus within 6µs maximum (standard) or 1µs (overdrive). While this generaly speaking is quite easy to archive using interrupts it implies that you cannot run other code that is sensitive to interruptions and/or disables interrupts temporary in parallel.
Of course there is preexisting code for that: http://www.tm3d.de/index.php/1-wire-device-mit-avr, google will find more for you. If both sides of your communication are controlled by your own software I'd recommend not to stick with maxims definitions but to loosen the timing-requirenments and modify the OneWire-library you use on the master, or maybe even better - roll your own protocoll as 1-Wire is not designed to be truely bidirectional (while signaling of an interrupt-condition by the slave is defined by maxim, there are no implementations in software that support it and only a single chip (slave) that I know of that make use of it).

So if you are new to programming you better use a 2-wire protocol like serial as the mega has 3 additional serial-ports that are well supported by the Serial-library

Norbert

gareth10

Thanks guys, I'll have a play over the weekend and report back.
As I said, I'm fairly new to this, so it's a steep learning curve, but very interesting.
Cheers

gareth10

Hi guys, I've had a play over the weekend and have come up with the following code for the master and slave respectively.

Slave code
#include <OneWire.h>

void setup()
{
   OneWire.begin(2);                // join one wire bus with address #2
   OneWire.onReceive(receiveEvent); // register event
   OneWire.onRequest(sendInfo); //send infor to master on request
   Serial.begin(9600);           // start serial for output
}

void loop()
{

  delay = 100
}

// This function executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
   testByte1 = OneWire.read() //stores the data to the next available memory
   testByte2 = OneWire.read() //stores the data to the next available memory
   testByte3 = OneWire.read() //stores the data to the next available memory
   testByte4 = OneWire.read() //stores the data to the next available memory
   testByte5 = OneWire.read() //stores the data to the next available memory
   testByte6 = OneWire.read() //stores the data to the next available memory
}

void sendInfo(){
  OneWire.write(outgoingByte,2);
}


Master Code
#include <OneWire.h>

// Data wire is plugged into pin 1 on the Arduino
#define ONE_WIRE_BUS 1

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

int numberOfDevices;

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("1-Wire practice code");

// Start up the library
//  Devices.begin();

  delay(5000);  //serial port can lock up otherwise
  numberOfDevices = discoverOneWireDevices();
  Serial.println();
}

//void loop(void)
//{
//  printDataToSerial();
//  delay(10000); //wait 10 sec
//}

void loop()
{
   //OneWire.begin();// join the 1 wire bus
   OneWire.beginTransmission(2); // transmit to device #2
   OneWire.write(testByte1);        // sends byte1
   OneWire.write(testByte2);        // sends byte2
   OneWire.write(testByte3);        // sends byte3
   OneWire.write(testByte4);        // sends byte4
   OneWire.write(testByte5);        // sends byte5
   OneWire.write(testByte6);        // sends byte6
   OneWire.endTransmission();    // stop transmitting

   x++;
   delay(500); // delay added in
 
   //Recevie data from the slave controller
   OneWire.requestFrom(2,2)// requests 2 bytes of data from de vice on address 2
   incomingByte[0] = OneWire.read() //stores the data to the nexat available memory
   incomingByte[1] = OneWire.read() //stores the data to the nexat available memory
}


int discoverOneWireDevices(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];
  int count = 0;

  Serial.println("Looking for 1-Wire devices..."); // code to search for all 1-wire devices on the bus.
// This can be up to 127
  while(oneWire.search(addr)) {
    Serial.print("Found \'1-Wire\' device with address: ");
    for( i = 0; i < 8; i++) {
      Serial.print("0x");
      if (addr < 16) {
        Serial.print('0');
      }
      Serial.print(addr, HEX);
      if (i < 7) {
        Serial.print(", ");
      }
    }
    if ( OneWire::crc8( addr, 7) != addr[7]) { // a simple CRC to confirm the validity of the 1 wire address
        Serial.println("CRC is not valid!");
        return 0;
    }
    Serial.println();
    count++;
  }
  Serial.println("No more devices available");
  oneWire.reset_search();
  return count;
}

I'm still having some problems, can anyone help?
Thanks
Gareth

ntruchsess

What OneWire-library are you using that comes with an onReceive-method? I have seen this method in the Wire-library (for he I2C-bus) before, but not with OneWire.

- Norbert

gareth10

I took that from the I2C library, does the one wire protocol have an equivalent function?
I can't see why it does not work, so any further help would be great.
Cheers

ntruchsess

From what I know there's no OneWire-library for Arduino that does not have an equivalent function. So your code would not even compile, does it?
And from what I know there's no OneWire-library for Arduino that is capable to run in Slave/Device-mode. I'd appreciate being wrong ;-)

- Norbert

Go Up