-2

Here is my sensor topology.. enter image description here

Here, the atmega128 is the main controller and all other atmega8 are connected to the main controller using rs485 cable.

What is the best communication protocol for this to detect and avoid collision.?

I have 20 atmega8 connected in a row..(i.e all the tx pins of atmega8 are connected together and finally connected to the Rx of the main controller) and distance between the any atmega8 or atmega128 is 3 meters....

///EDITED

I have 20 atmega8 connected in a row.. Here..Atmega8 cannot talk with each other..Instead they will be listening for commands from the Atmega128 controller.. When a broadcast message comes from the Atmega128, all the atmega8 should respond with their status.. Here what the collision comes into the picture.. The baudrate is 9600.. Basically Atmega8 are my sensors and Atmega128 is a main comtroller asking for the sattus of the sensors and commanding them to change their state.

T Obulesu
  • 1
  • 1
  • 2
    You'll have to define what "best" means in the context of your application. Do you want it to be simple or cheap? Are you worried about performance, such as bandwidth or latency? As it stands, this question is far too vague. – Dave Tweed Dec 02 '17 at 17:16
  • How many pairs in your cable do you have to play with here? – ThreePhaseEel Dec 02 '17 at 17:18
  • 1
    TIA-485 has collision detection with active and inactive lines, so that's for free already. Use a proper interface chip, and it will give you line status. – MrGerber Dec 02 '17 at 18:39
  • Apart from that, I'd stick with some easy addressing, and a simple implementation of a datagram protocol with request reply. If it's not a critical communication, stick with a simple data validation scheme like xor or CRC, just to sort out most bad transmissions. If you make sure that server nodes only speak when spoken to, you also avoid a lot of the problems. If you want something proven, go for modbus – MrGerber Dec 02 '17 at 18:44
  • I'd throw old UART junk away, throw old 8 bit junk away and get some microcontrollers with CAN. It is CSMA/CA, problem solved. – Lundin Dec 04 '17 at 15:36

2 Answers2

0

Define best... But since you definitely don't want to develop something very serious from scratch, go to master/slave topology, meaning only master is allowed to start communication.

You should come up with a way to enumerate slaves. Best would be to hard wire bumbers by pull ups/downs. Maybe with external connectors wired some way.

0

This will be a half answer, since it's dirty and not super efficient. Well you never stated the baudrate you were going to use. So who knows, maybe it's efficient enough.

Use a ground wire and a signal wire. This signal wire will be connected to the ADC of every Atmega (input) and to some digital pin of every Atmega (output).

It won't go straight to the digital output pin, it will have a 1 kΩ resistor connected between itself and the digital output pin.

So in ascii art it will look something like this:

 _______A_______
|               |
|     PF0(ADC0) |<--------|
|               |         |
|   Atmega128   |         |
|               |         |
|           PD0 |->-1kΩ---|
|_______________|         |
                          |
                          |
 _______B_______          |
|               |         |
|     PC0(ADC0) |<--------|
|               |         |
|   Atmega8   B |         |
|               |         |
|           PD0 |->-1kΩ---|
|_______________|         |
                          |
                          |
 _______C_______          |
|               |         |
|     PC0(ADC0) |<--------|
|               |         |
|   Atmega8     |         |
|               |         |
|           PD0 |->-1kΩ---|
|_______________|         .
                          .
                          .

And then you assign particular frequencies to every Atmega. So if, say A want to output a logical "1" then A will output a square wave on PD0 at... say 10 kHz for an example. And if A wants to output a logical "0" then A will be quiet.

If B wants to talk then it will make a square wave signal of say 20 kHz or something else, not 10 kHz. Same thing with C, it gets its own frequency to talk on.

Then internally in every Atmega you will perform FFT. It doesn't have to be 8192 points or some other unnecessarily high number you can easily perform a 32 point FFT. The fewer points you have, the more your sine wave will smear and overlap. Life is not ideal.

It's not pure sine waves that you will put out through PD0, so you will get some other harmonics. In A's case you will see some small sine waves at 30 kHz, 50 kHz, 70 kHz and etc, because they are a part of the square wave at 10 kHz.

Also, since everything is just connected with resistors, the longer your chain is, the less each output will mean. So if you have 4 Atmega's connected. Then there's 4 resistors connecting to the signal wire. If one of them is outputting a high value and the rest low value. Then you will have \$5×0.25 = 1.25\$ V. That means that when you read ADC0 (10 bit ADC), you will read \$\frac{1.25×1024}{5}= 256\$. That's pretty okay. But if you got more, say 20 Atmega's, then you will read \$5×\frac{1}{20}×\frac{1024}{5}=51\$... Hmmm, that's not too bad either... But this means that you will need 20 different frequencies as well, far enough between so they don't overlap when you use your FFT.

So the more frequencies you use the more points of the FFT you have to calculate.


In other words what you will be doing is like having each Atmega pluck a guitar string, and then you say:
- "Oh, that's an A tone, okay so Atmega #2 is sending me a '1' right now"
- "Hmm, that's a C tone I believe, then that has to be Atmega #1 that's sending me a '1' right now"
- "There's an absence of the tone A, so Atmega #2 is sending me a '0', got ya!"


Regarding your Atmega8's, if they are never going to talk to each other. Then you can just make it so they listen to the Atmega128 by only calculating the DFT of one frequency. That frequency would be of the square wave your Atmega128 would broadcast at. Or you can make it so each Atmega8 listens to a different frequency, this way you can easily talk to each Atmega8 simultaneously. Though you would need some DAC at the Atmega128, not a square wave.

I can continue, but I believe you get the gist of it. Remember, it's not the most efficient solution.

Harry Svensson
  • 8,139
  • 3
  • 33
  • 53
  • 1
    Jesus that's seems over engineering. Interesting idea, though. Have you used it in a real application? What is the benefits of using this method? – MrGerber Dec 02 '17 at 18:46
  • I was going to use it in one of my projects... but I never got that far with that idea and ended up using wireless communication instead. - For my project I needed some µC to connect and disconnect at specific connections / terminals, using a specific frequency for its specific connection would help a lot if I implemented it. The connections were like north / E / S / W, so whenever some data was going to W it had some frequency, whenever some data was going to E it had some other frequency. And there were copies of this connection.. It made sense in my case, maybe not *so much* in this case. – Harry Svensson Dec 02 '17 at 20:21
  • Well.. the problem comes when all the atmega8 sends message at once.. – T Obulesu Dec 03 '17 at 11:13
  • The baudrate is 9600 – T Obulesu Dec 03 '17 at 11:14
  • @TObulesu Also, I've written in my answer that **that** problem can easily be solved by giving each Atmega8 a channel for each of them to broadcast at. In other words, my answer solves that. But it's up to you to understand and/or/not debunk my answer. – Harry Svensson Dec 03 '17 at 14:39