go_away

Author Topic: Communicating between two or more microcontrollers  (Read 3508 times)

0 Members and 1 Guest are viewing this topic.

Offline praveen_khmTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 1
Communicating between two or more microcontrollers
« on: August 07, 2010, 02:28:20 PM »
Hi guys,

I need some help in interfacing two micro controllers. Suppose I have two atmega8 uc and I need to send a signal to another atmega 8, how do I proceed. I know the UART one, but it works for one to one I suppose. (Also read the uart tutorial). However, if I have 10 different atmega uc's and I need to make all of them slave's and controlled by one master, how do I proceed?

A step by step tutorial would be more helpful, or any links which can help me with it.

Thanks,
Praveen
Robot building is all about sharing and learning
-------------------------------------------
www.robotplatform.com : Beginners guide to Electronics & Robotics

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: Communicating between two or more microcontrollers
« Reply #1 on: August 07, 2010, 02:32:26 PM »
Atmegas have built-in I2C modules that enable communication of 127 or more I2C devices. Google "avr i2c" and there should be a TON of examples, I2C is very very popular form of communication.

Offline madsci1016

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: Communicating between two or more microcontrollers
« Reply #2 on: August 07, 2010, 03:21:16 PM »
If the slaves never report back to the master, just take commands, then you can use the Uarts. Simple have all the slaves RX lines tied to the masters TX line. When you define the communication protocol, you can start out each packet with an address of a slave, and program your slaves to only listen to packets address to it.

Offline Pratheek

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 3
Re: Communicating between two or more microcontrollers
« Reply #3 on: August 08, 2010, 09:53:11 AM »
You can use both USART and I2C for your requirement.

I would go with I2C, if I were you because it is better suited for your requirement.
« Last Edit: August 09, 2010, 11:56:20 PM by Pratheek »

Offline praveen_khmTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 1
Re: Communicating between two or more microcontrollers
« Reply #4 on: August 09, 2010, 12:58:20 AM »
Thanks guys...

I am reading I2C. However the idea of madsci1016 sounded easy and interesting, using UART with one Tx connected to multiple Rx's of slaves.
Can someone post a sample code of the communication protocol(Using UART), or a link which can help me?

Cheers,
Praveen
« Last Edit: August 09, 2010, 02:15:50 AM by praveen_khm »
Robot building is all about sharing and learning
-------------------------------------------
www.robotplatform.com : Beginners guide to Electronics & Robotics

Offline Pratheek

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 3
Re: Communicating between two or more microcontrollers
« Reply #5 on: August 09, 2010, 06:24:55 AM »
There is a generic tutorial on USART on this site and another specifically for AVR.
Go through it and you should be able to write the code yourself.

Offline Loligo

  • Jr. Member
  • **
  • Posts: 10
  • Helpful? 1
Re: Communicating between two or more microcontrollers
« Reply #6 on: August 09, 2010, 10:42:18 AM »

Do you mean this uart tutorial?

Offline madsci1016

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: Communicating between two or more microcontrollers
« Reply #7 on: August 09, 2010, 11:45:39 AM »

Can someone post a sample code of the communication protocol(Using UART), or a link which can help me?



The nice thing about serial communications using UARTS is that there is no set communication protocol. You can define your own.

Microcontroller libraries usually have some function similar to UartSendByte() and UArtGetByte(). Thats what you use to send and receive a single byte over serial.

So for you, you can send multiple bytes for each 'packet'. And you define what the bytes in your packet mean. You can have a start and stop byte to show were the packet begins and ends, and a address byte to show which slave the packet is for.

For example:

$ 12 [data] *   

could be your protocol. $ signifies the start of a packet, 12 is the slave number, data is the data to send, and * is the end of packet.

Offline HyperNerd

  • Robot Overlord
  • ****
  • Posts: 139
  • Helpful? 5
  • N3rd1n80r
Re: Communicating between two or more microcontrollers
« Reply #8 on: August 09, 2010, 12:38:07 PM »
The only problem with using a method such as that suggested by madsci is that if your data contains the same byte value of the start or end byte (for example if you are sending raw sensor readings as single bytes rather than as an ASCII number), your programs could get confused as to where the end of the data packet is.

As may already be clear by my post, the solution to this problem is to send all data as ASCII strings and numbers, rather then raw byte format, and revert them to byte format at the receiving end.

 -HyperNerd
There are 10 types of people in the world - those who understand binary, and those who don't.

Offline madsci1016

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43

Offline praveen_khmTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 1
Re: Communicating between two or more microcontrollers
« Reply #10 on: August 09, 2010, 01:36:47 PM »
Thanks guys... will try getting in more details and then maybe experiment with it.  :)
Robot building is all about sharing and learning
-------------------------------------------
www.robotplatform.com : Beginners guide to Electronics & Robotics

Offline madsci1016

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: Communicating between two or more microcontrollers
« Reply #11 on: August 09, 2010, 01:45:02 PM »
The problem Hyper mentions can also be beaten by having your third byte represent the length of the packet, so it won't confuse digital data for end/start flags.

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: Communicating between two or more microcontrollers
« Reply #12 on: August 09, 2010, 02:46:29 PM »
I have a feeling I2C, or even SPI with chip selects would be much easier  ;D

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design

 


Get Your Ad Here