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
You could start with the OneWire library (although technically you need a common ground so you need two wires).
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
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.
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: tm3d.de - geniale 1-Wire Sensoren und mehr, 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
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
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
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
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
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