Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: praveen_khm on August 07, 2010, 02:28:20 PM

Title: Communicating between two or more microcontrollers
Post by: praveen_khm 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
Title: Re: Communicating between two or more microcontrollers
Post by: Razor Concepts 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.
Title: Re: Communicating between two or more microcontrollers
Post by: madsci1016 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.
Title: Re: Communicating between two or more microcontrollers
Post by: Pratheek 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.
Title: Re: Communicating between two or more microcontrollers
Post by: praveen_khm 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
Title: Re: Communicating between two or more microcontrollers
Post by: Pratheek 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.
Title: Re: Communicating between two or more microcontrollers
Post by: Loligo on August 09, 2010, 10:42:18 AM

Do you mean this uart tutorial (http://www.societyofrobots.com/microcontroller_uart.shtml)?
Title: Re: Communicating between two or more microcontrollers
Post by: madsci1016 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.
Title: Re: Communicating between two or more microcontrollers
Post by: HyperNerd 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
Title: Re: Communicating between two or more microcontrollers
Post by: madsci1016 on August 09, 2010, 01:23:29 PM
http://www.billporter.info/?p=214 (http://www.billporter.info/?p=214)
Title: Re: Communicating between two or more microcontrollers
Post by: praveen_khm on August 09, 2010, 01:36:47 PM
Thanks guys... will try getting in more details and then maybe experiment with it.  :)
Title: Re: Communicating between two or more microcontrollers
Post by: madsci1016 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.
Title: Re: Communicating between two or more microcontrollers
Post by: Razor Concepts on August 09, 2010, 02:46:29 PM
I have a feeling I2C, or even SPI with chip selects would be much easier  ;D
Title: Re: Communicating between two or more microcontrollers
Post by: GearMotion on August 10, 2010, 04:46:10 PM
http://www.bdmicro.com/code/robin/ (http://www.bdmicro.com/code/robin/)