rs485 avr communication with master and slave

Go To Last Post
55 posts / 0 new

Pages

Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello its shahid ,

currently iam working on rs485 in atmega8 with one master and multiple slaves , i want to communicate with diffrent slaves with one master simultaneously .

i want to know how can i initialise slave address in atmega 8.

Last Edited: Thu. Aug 16, 2018 - 01:19 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Suggest you probably want to start reading somewhere around this area of the mega8 datasheet:

 

This is effectively "9 bit mode" where the extra bit says whether the 8 that follow are "address" or "data". You start by sending out an address frame to say "unit 5 I'm talking to you now". Then you send out data packets and all but unit 5 will ignore them until a new address packet is sent to switch the unit that you are intending the following data to go to and so on.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

shahid.kvmdam wrote:
i want to communicate with diffrent slaves with one master simultaneously

If you have only 1 RS485 link, then you can only be talking to one slave at a time - unless you're talking about "broadcasting" to all slaves ?

 

shahid.kvmdam wrote:
i want to know how can i initialise slave address in atmega 8

Not sure what you mean by that?

Do you mean:

  • How does the Master know the address of each slave?
  • or, how do you assign addresses to each slave?
  • or what?

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks for reply,

i want to know how can i assign address to each slave for rs485

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks for reply,

i want to know how can i assign address to each slave for rs485

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
or, how do you assign addresses to each slave?

datasheet wrote:
Each Slave MCU reads the UDR Register and determines if it has been selected.

So it's by agreement. Slave 37 will read UDR when the address byte arrives and only switch to "listening" mode if UDR=37 and so on.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

shahid.kvmdam wrote:
i want to know how can i assign address to each slave for rs485

You have to assign the addresses.

 

eg,

  • have some external switches to set the address
  • have some sort of "configuration" mode, where you put the addresses into EEPROM
  • If the slaves have different code in them anyhow, define an address for each type of code
  • etc, etc, ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sorry iam new in this so can you please send me function defination like

this

void RS485Slave_Init(char Slave_address);

 

i searched it from internet but i cant find it program

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
This is effectively "9 bit mode"

Do any AVRs have automatic address detection - as the 8052 Extended UART did ?

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

shahid.kvmdam wrote:
iam new in this

What, exactly, do you mean by "this" ?

 

  • Programming in general?
  • Programming microcontrollers in particular?
  • Other?
  • All of the above?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i want to program atmega8 and i want its coding in c++,

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That doesn't answer the question at all.

 

Do you have any prior experience in using C++ ?

 

Do you have any prior experience in using small microcontrollers ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Have you simply tried googling "atmega8 MPCM RS485". I would be utterly astonished if that did not locate some prior code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i have programed atmega8 as master and slave 

	#include <avr/io.h>
	#define F_CPU 1000000UL
	#include <util/delay.h>

	#define master 1
	#define slave 0

	void IniControllerAs(int x)
	{
		if(x==1)
		PORTD |= (1 << PIND2);
		else
		PORTD &=~ (1 << PIND2);
	}
	void indicate()
	{
		PORTB |= (1 << PINB1);
		_delay_ms(100);
		PORTB &=~ (1 << PINB1);
		_delay_ms(100);
		PORTB |= (1 << PINB1);
		_delay_ms(100);
		PORTB &=~ (1 << PINB1);
		_delay_ms(100);
		PORTB |= (1 << PINB1);
		_delay_ms(100);
		PORTB &=~ (1 << PINB1);
		_delay_ms(100);
		PORTB |= (1 << PINB1);
		_delay_ms(100);
		PORTB &=~ (1 << PINB1);
		_delay_ms(100);
		PORTB |= (1 << PINB1);
		_delay_ms(100);
		PORTB &=~ (1 << PINB1);
		_delay_ms(100);
	}
	int main(void)
	{
		DDRD |= 1 << PIND1;//pin1 of portD as OUTPUT
		DDRD &= ~(1 << PIND0);//pin0 of portD as INPUT
		DDRD |= 1 << PIND2;
		
		DDRC &= ~(1 << PINC5);                                                                                                                                                                   
		DDRB =0xff;
		
		unsigned char receiveData;
		int UBRR_Value = 25; // 2400 baud rate
		UBRRH = (unsigned char) (UBRR_Value >> 8);
		UBRRL = (unsigned char) UBRR_Value;
		//UCSRB = (1 << RXEN) | (1 << TXEN);
		UCSRC = (1 << USBS) | (3 << UCSZ0);
		IniControllerAs(slave);
		PORTB |= (1 << PINB1);
		_delay_ms(1000);
		PORTB &=~ (1 << PINB1);
		while (1)
		{
 			if(UCSRA &(1 << RXC))
			{
				receiveData = UDR;
				if (receiveData == 2)
				{
					PORTB |= (1 << PINB1);
					_delay_ms(1000);
					PORTB &=~ (1 << PINB1);
					_delay_ms(1000);
				}
				receiveData=0;
			}
			if (bit_is_set(PINC,5))//once button is pressed
			{
				IniControllerAs(master);
				//indicate();
				while (! (UCSRA & (1 << UDRE)) );
				UDR =1 ;//once transmitter is ready sent eight bit data
				_delay_ms(1000);
				// Get that data outa here!
				IniControllerAs(slave);
			}
			
		}
	}	

as slave

#include <avr/io.h>
//header to enable data flow control over pins
#define F_CPU 1000000UL
//telling controller crystal frequency attached
#include <util/delay.h>
//header to enable delay function in program
#define master 1
#define slave 0

void IniControllerAs(int x)
{
	if(x==1)
	PORTD |= (1 << PIND2);
	else
	PORTD &=~ (1 << PIND2);
}
void indicate()
{
	PORTB |= (1 << PINB1);
	_delay_ms(100);
	PORTB &=~ (1 << PINB1);
	_delay_ms(100);
	PORTB |= (1 << PINB1);
	_delay_ms(100);
	PORTB &=~ (1 << PINB1);
	_delay_ms(100);
	PORTB |= (1 << PINB1);
	_delay_ms(100);
	PORTB &=~ (1 << PINB1);
	_delay_ms(100);
	PORTB |= (1 << PINB1);
	_delay_ms(100);
	PORTB &=~ (1 << PINB1);
	_delay_ms(100);
	PORTB |= (1 << PINB1);
	_delay_ms(100);
	PORTB &=~ (1 << PINB1);
	_delay_ms(100);
}
int main(void)
{
	DDRD &=~(1 << PIND0);//pin0 of portD as INPUT	
	DDRD |= 1 << PIND1;//pin1 of portD as OUTPUT
	DDRD |= 1 << PIND2;
	DDRC &= ~(1 << PINC5);
	DDRB =0xff;
	
	
	int UBBRValue = 25;//AS described before setting baud rate
	unsigned char receiveData;
	//Put the upper part of the baud number here (bits 8 to 11)
	UBRRH = (unsigned char) (UBBRValue >> 8);
	//Put the remaining part of the baud number here
	UBRRL = (unsigned char) UBBRValue;
	//Enable the receiver and transmitter
	UCSRB = (1 << RXEN) | (1 << TXEN);
	//Set 2 stop bits and data bit length is 8-bit
	UCSRC = (1 << USBS) | (3 << UCSZ0);
	PORTB |= (1 << PINB1);
	_delay_ms(1000);
	PORTB &=~ (1 << PINB1);
	IniControllerAs(slave);
	while (1)
	{
		
		//while(!(UCSRA &(1 << RXC)));
		if(UCSRA &(1 << RXC))
		{
			receiveData = UDR;
			if (receiveData == 1)
			{
				PORTB |= (1 << PINB1);
				_delay_ms(1000);
				PORTB &=~ (1 << PINB1);
				_delay_ms(1000);
			}
			receiveData=0;
		}
		
		if (bit_is_set(PINC,5))//once button is pressed
		{
			IniControllerAs(master);
			//indicate();
			while (! (UCSRA & (1 << UDRE)) );
			UDR =2 ;//once transmitter is ready sent eight bit data
			_delay_ms(1000);
			// Get that data outa here!
			IniControllerAs(slave);
		}
		                                                                     
	}
}

 

in this programs ihave used dir pin to controll messages ,

for example 

in master DIR pin is HIGH and in slave DIR pin LOW , so the communication is don but whaen i used more then one slave this is not wotking , after some research on internet i discover that slave can be inistialise as a slave by initialising there slave address , so i want to know the that programming in c  as example

void RS485Slave_Init(char Slave_address);

but the whole program of this function is not given

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You seem to be missing the point!

 

See #7 - it is up to you to define how addresses are assigned to individual slaves.

In #7, I wrote:

You have to assign the addresses.

 

eg,

  • have some external switches to set the address
  • have some sort of "configuration" mode, where you put the addresses into EEPROM
  • If the slaves have different code in them anyhow, define an address for each type of code
  • etc, etc, ...

 

This is a design issue - you have to have this defined before you can think about coding!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Aug 10, 2018 - 11:05 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

shahid.kvmdam wrote:
i have programed atmega8 as master and slave 

and where in that code does it do anything to handle addressing - as described in the datasheet section posted in #2 ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

can you help me with function 

void RS485Slave_Init(char Slave_address);

i want this assign address of slave using code .

 

i want a function in which i send address as a parameters and it will assign tha address as slave address ,

just in I2C we save address in ADDRESS REGISTER ,

is ther an register in UART also

, waiting for your help

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

shahid.kvmdam wrote:
is ther an register in UART also

Did you read the datasheet section posted for you in #2 ?

 

 waiting for your help

Don't just sit there waiting - make some effort!

 

As clawson suggested in #13, there must be plenty of information on this already in the interwebs.

 

Again, read the datasheet section.

 

The MPCM - aka "multiprocessor" or "9-bit" mode - is nothing new. It has been widely used for decades

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The datasheet says:

1. All Slave MCUs are in Multi-processor Communication mode (MPCM in UCSRA is set)

 

I don't see any setting of the MPCM bit for the slaves on your function:

void IniControllerAs(int x)
{
	if(x==1)
	PORTD |= (1 << PIND2);
	else
	PORTD &=~ (1 << PIND2);
}

 

edit: by the way, don't you want to make your code more readable by using upper case for defines and actually use the defines, since you already have them?

#define MASTER 1
#define SLAVE 0

void IniControllerAs(int x)
{
	if(x == MASTER)
	PORTD |= (1 << PIND2);
	else
	PORTD &=~ (1 << PIND2);
}

 

 

Last Edited: Fri. Aug 10, 2018 - 11:59 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

El Tangas wrote:
I don't see any setting of the MPCM bit for the slaves

Indeed. See #16.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I mean, the steps are spelled out in the datasheet, they even have a numbered list...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, so the datasheet text is a bit unhelpful in making a clear distinction between what happens in the AVR hardware, and what you have to do in your code:

 

http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2486-8-bit-AVR-microcontroller-ATmega8_L_datasheet.pdf

 

ATmega8_L_datasheet wrote:
Setting the Multi-processor Communication mode (MPCM) bit in UCSRA enables a filtering function of incoming frames received by the USART Receiver. Frames that do not contain address information will be ignored and not put into the receive buffer. This effectively reduces the number of incoming frames that has to be handled by the CPU, in a system with multiple MCUs that communicate via the same serial bus. The Transmitter is unaffected by the MPCM setting, but has to be used differently when it is a part of a system utilizing the Multi-processor Communication mode.

This is all hardware stuff: your code just sets the MPCM bit, and then the Slave hardware does the filtering - all non-address frames will be ignored by the hardware.

 

Quote:
The Multi-processor Communication mode enables several Slave MCUs to receive data from a Master MCU. This is done by first decoding an address frame to find out which MCU has been addressed.

This is stuff that your code needs to do:

The hardware will tell you when an Address Frame - any address frame - has been received;

Your code must examine that frame, and determine whether or not it is the address for "this" slave.

 

If a particular Slave MCU has been addressed, it will receive the following data frames as normal, while the other Slave MCUs will ignore the received frames until another address frame is received.

This is misleading:  it will only happen when your code takes the necessary steps, as described in the following section:

 

 

2. The Master MCU sends an address frame, and all slaves receive and read this frame.

In the Slave MCUs, the RXC Flag in UCSRA will be set as normal

This is done by the hardware

 

3. Each Slave MCU reads the UDR Register and determines if it has been selected.
If so, it clears the MPCM bit in UCSRA, otherwise it waits for the next address byte and keeps the MPCM setting 

This must be done by your software.

 

 

4. The addressed MCU will receive all data frames until a new address frame is received.

This only happens because your software in the addressed slave cleared the MPCM bit.

 

The other Slave MCUs, which still have the MPCM bit set, will ignore the data frames

5. When the last data frame is received by the addressed MCU, the addressed MCU sets the MPC

Your software needs to set the MPCM bit.

Clearly, this means that your software needs some way to know which is the "last" data frame - this is something that you will have to build into your communication protocol.

 

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Aug 10, 2018 - 12:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Have you simply tried googling "atmega8 MPCM RS485". I would be utterly astonished if that did not locate some prior code.

Top hit is right here: https://www.avrfreaks.net/forum/mpcm-slave-sample-code

 

Which leads to: https://electronics.stackexchange.com/questions/290601/atmel-avr-xmega-a-usart-mpcm-multiprocessor-communication-mode-configuration-w

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
Which leads to:
Go on. Exactly how useful (or the word I would use is "misleading") will Xmega example code be to a mega8 user?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Actually, it does show the procedure - that is exactly the same.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Take a look at this: S.N.A.P.  http://www.hth.com/snap/

For C++ code look at one of the arduino librarys.

 

Jim

Edit, added reference to arduino c++ code

Click Link: Get Free Stock: Retire early!

share.robinhood.com/jamesc3274

 

 

 

Last Edited: Fri. Aug 10, 2018 - 12:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

shahid.kvmdam wrote:
i want to know how can i assign address to each slave for rs485

awneil wrote:
You have to assign the addresses.

 

Welllll.................

 

I had to check my NDA with a customer before putting my name in this hat. 

 

It sounds like the OP wants to Dynamically address the slave devices, as opposed to the old standard Static addressing.

 

Whats the difference?  In Dynamic addressing, the host does address the slave, and the host keeps a lookup table of these addresses.  In this one system I have worked with the slave devices each have a serial number in them that contains a code that identifies what kind of device they are, and some other data. These devices come with a default address of 128, when the new slave device is placed on the bus the master will 'scan' for it by sending out an inquiry with the address of 128.  This new slave will see the address and respond back with the default address and its serial number.  the master then will transmit to the slave a new address, which the slave writes into it's on board EEPROM and then replies with the new address confirming. The host then stores this in its own EEPROM address lookup table for the main app. The kit I work with has other stuff in the protocol I cannot divulge, but in the simplest of explanations I think I have given enough information to get you started.  One other thing....all information is done in ASCII.  While this can increase the amount of packets to complete a transaction it was found to be far more efficient to decode, and the possibility of corruption (determining if its address 0x0f, or data 0x0f for example). The MPCM is not used. 

 

the downside of this, if used in its simplest rendition is that you can only add one slave at a time. Otherwise when the inquiry is sent, there will be data collisions between multiple, unaddressed slaves unless there is something in the code to handle this.  I can say that this company that uses this scheme is very successful with it, and has a very large customer base using their kits.

 

Static addressing is done by setting DIP switches, a rotary switch, or jumpers on the slave device to physically set it's address.  These switches or jumpers are read at startup and the value is usually stored in RAM somewhere and compared to the data coming in on the bus.  The host then needs to be configured to know that this device is on the bus, and what it's address is.  This method has been in use for decades and is a very simple, and effective addressing method.

 

What are the pluses and minuses of these two methods?

The dynamic scheme remembers the addresses and as such simply assigns the next address in the sequence.  In a large network with many slaves this can reduce the chance of duplicate addresses.  There is also the reduced chance of tampering with addresses by some unsavory employee. The downside(s) to this method is that it places extra work on the host, and depending on how well the slave device is designed to handle power line conditions, the slave devices could corrupt their EEPROM in a brown out, or other power fluctuation rendering the device, or devices mute and in need of re-addressing.

 

The static scheme is SIMPLE...its a switch, or jumpers....End of story.  No need to worry about power issues affecting EEPROM.  The downside(s) are that jumpers and DIP switches take up PCB real estate, which costs money, and jumpers and DIP switches cost money so for a mass produced product, the costs can add up compared to a software based solution.  Another downside is that because the Switch, or jumper is a mechanical device, they can be affected by the environment they are in.  Meaning that there is the possibility that the address could change because a contact n the switch has corrosion on it which in turn makes a 0 a 1 or a 1 a zero.  Mechanical switches and jumpers can also be easily changed which can bring a network to it's knees.

 

In the end it's up to you the designer/programmer to decide which way you want to go, and it's up to you to use/design a protocol to handle the communications between your devices.

 

Hope this helps.

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
Static addressing is done by setting DIP switches, a rotary switch, or jumpers on the slave device to physically set it's address.
or, presumably, a manually preprogrammed EEPROM entry?

 

However can't help thinking you may be reading a little too much into the OPs posts. It seems to me he just doesn't know how to do "slave addressing" full stop however the slave addresses themselves might be derived. He clearly did not know about MPCM.  In fact, so far, it looks like he still does not understand MPCM as nothing he's shown so far shows any signs of using it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nice idea for the automatic address assignment.

 

jgmdesign wrote:
Static addressing is done by setting DIP switches, a rotary switch, or jumpers on the slave device to physically set it's address. 

That was my first suggestion in #7

Other possibilities:

I wrote:

  • have some sort of "configuration" mode, where you put the addresses into EEPROM
  • If the slaves have different code in them anyhow, define an address for each type of code

 

depending upon the nature of the network, it might be possible for the slave address to be related to its "position" - eg, slot number in a rack...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
can't help thinking you may be reading a little too much into the OPs posts.

I think so.

 

It seems to me he just doesn't know how to do "slave addressing" full stop

Indeed - neither how to manage the addresses at the "system" level, nor how to handle them in the code.

 

He clearly did not know about MPCM.

Indeed - although it could all be done without it...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
However can't help thinking you may be reading a little too much into the OPs posts.

I have no doubt that I probably am.  At the same time we have gone down this road before with an OP confusing things and other noobs start asking questions they think 'relevant', only making things complicated.  While I do not have Dr. Wagners, or Dr. Carters gifts for detailed, well written explanations, hopefully my post simply explaining two methods might help someone, and lower some confusion.

 

Then again, I might be reading into yours and Andy's posts too much too wink

 

 

FWIW I am not a fan of MPCM as using it would require every device connecting would have to support it.  Either in Hardware(like the AVR) or software.  But thats personal taste mind you.

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
FWIW I am not a fan of MPCM as using it would require every device connecting would have to support it. 

It is really quite common in microcontroller UARTs. 

 

As noted, the good ol' 8052 can even do the address matching in hardware!

 

But the big problem comes if you want to connect it to a PC ...

 

frown

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Aug 10, 2018 - 02:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
It sounds like the OP wants to Dynamically address the slave devices,
shahid.kvmdam wrote:
i want to communicate with diffrent slaves with one master simultaneously .

So y'all think that OP meant "on the same line" instead of "at the same time" for 'simultaneously'?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A little extension to gjmdesign's code:

Add a central point to manage addresses. Much like DHCP.

An AVR can determine the difference between a power up reset and a reset with the reset button.

The AVR can be programmed to send it's current adress and request a new adress from the "DHCP" server if it is reset by the reset button.

The "DHCP" server will then determine whether it may retains it's old address, or it may give it a new address.

If you have a spare I/O pin that can also be used, but with reset, no extra hardware is needed.

Actually, a node should send a power up message to the "DHCP" server, after each reset.

Then the DHCP server, or an intermediate router node can determine if the current address of the device is still valid for the physical location of the device.

Another very usefull feature is that you can use the reset of a node to send a "ping".

 

RS485 are limited to not very many transeivers on the same bus. 32 used to be the maximum, but nowadays it goes upto 128.

128 devices on the bus can be very troublesome while trouble shooting though.

It seems more logical to give each node a name, and use dynamic addresses to devide big networks in small segments, and the segment is part of the address and can therefore be used in routing.

Nothing new really, just as has been done in IPv4 for 40 years or so.

I believe the Tanenbaum book is one of the best about networks and their lower workings. I still might buy and read that book someday.

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

jgmdesign wrote:
It sounds like the OP wants to Dynamically address the slave devices,
shahid.kvmdam wrote:
i want to communicate with diffrent slaves with one master simultaneously .

So y'all think that OP meant "on the same line" instead of "at the same time" for 'simultaneously'?

 

Yeah, Good question.

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
So y'all think that OP meant "on the same line" 

See my first point in #3.

 

Yes, to do it simultaneously - ie, at the same time - would take multiple links.

 

But that would then obviate the need for addressing ...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:
a node should send a power up message to the "DHCP" server, after each reset.

then you'd need to add some sort of collision detection ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

OP has gone quiet - I think it's beyond the end of the working day where he's located ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If I were a betting man, I'd say India.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In some devices I have the address stored into EEPROM that can be changed at power up via a monitor program and a terminal.

;EEPROM Area
.eseg
nothing:
.db	0

;This module's address for RS485
my_adr:
.db	0x1C

 

in others I have a dip switch or a rotary switch

 

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

after all these discussion i think in rs485 , first i have to store each slave address in its own EEPROM , and when master send any address to all slaves to communicate , all slaves will check that the given address is there address or not , if it is same the the will react and send confirmation to master otherwise the will keep quit ,

 

am i right or not guys.

 

but thanks for help

Last Edited: Sat. Aug 11, 2018 - 06:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a setup, where you can always broadcast to all units, or specific groups of units, or a single unit.  If you expect a data reply (query), you can only send to one unit.  A setup mode command will set all units to start blinking an led, pushing a button on each unit will automatically assign an address then stored in EEPROM for that unit, or a specific desired (non-automatic) address can be assigned.   A unit generally only has one unique address, but it can have multiple addresses, some of them possibly shared.  So unit A might have address1, unit B might have address2 & 7, unit C might have address3, unit D might have address 4&7&9, unit E might have address 5& 7....so sending to address7 actually commands both units B & E.  For the most part commands are sent to one unit or all; don't use the "grouping"  much, but its available.   A certain broadcast command selects which units are active listeners.  Certain commands are inherently broadcast to all units, other commands require a specific address (which could be a group address), others go to whatever units are currently "listening".

 

One command broadcasts to all & sets them in a reporting mode...pushing any unit's button reports that unit's address(s) back to the master.

      

When in the dark remember-the future looks brighter than ever.

Last Edited: Sat. Aug 11, 2018 - 07:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

shahid.kvmdam wrote:
first i have to store each slave address in its own EEPROM

No, they do not necessarily have to be in EEPROM - there are many other options.

 

See #7, #15, #27, and #29

 

The important things is that each slave must have a unique address - where/how it's stored doesn't really matter, and will depends on the nature of your particular system.

 

when master send any address to all slaves to communicate , all slaves will check that the given address is their address or not , if it is same then the addressed slave will react and send confirmation to master; otherwise the slaves will keep quiet

Yes, that's basically it.

But the master will need to send something more than just an address - there will, in general, need to be some "payload" following the address. Hopefully this is obvious?

 

As noted by avrcandies, you may want to have a "broadcast" address - which will be accepted by all slaves.

eg, a 'Reset All Slaves' command.

As noted, slaves must not respond to such a command.

 

This is illustrated in the code I linked earlier - the "broadcast" address there is zero.

(As clawson noted, that code is actually for XMega - but the way it works is identical)

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Aug 11, 2018 - 08:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
Paulvdh wrote: a node should send a power up message to the "DHCP" server, after each reset. then you'd need to add some sort of collision detection ...
I have been using collision avoidance, and combine that with a 16-bit CRC, and sometimes automatic resends if no response is detected.

It do this in a project called "Mumarnet" which stands for: "MUlti MAsteR NETwork". and it works pretty well.

In a test I did once with about 7 nodes I transmitted > a Million messages, with a rate of about 800 messages/s @115k2baud.

There were about 10 messages missing, (did not get a response) and there were 3 actual collisions where data was lost.

(one of the) tricks with collision avoidance is to have as short a time between detecting the network is free and start of a transmission.

I have less than a handfull of opcodes in between, which translates to a < 1us window in which 2 nodes could decide to start sending at the same time.

If the difference is bigger, then the 2nd node detects the first is sending and will return an error message to a higher level function to try again later.

 

@OP:

Tell us more about what you already have.

Common is to have some kind of packet format with adresses, data length, payload, and probably a checksum and all nodes should adhere to the same format.

In Mumarnet I use the MPCM where the 9th bit indicates the start of a packet, so the ISR to receive data is only triggered if this bit has the correct value.

Then in the ISR I compare the received byte in software with the node address, and possibly a second broadcast adress.

If the adress is wrong, the ISR simply exits. If the adress byte matches, then the node turns of the MPCM and is ready to receive the rest of the packet.

 

Alternatively you can use something similar to CAN. CAN does not really have "addresses", but packet ID's which indicate what sort of data is in a particular packet.

 

I evaluated "SNAP" a long time ago, but discarded it because I really wanted the "Multi Master" part for my own home network, but it should be OK if you only have one master.

Another very common protocol for which you can find loads of code examples is modbus.

 

 

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:
@OP:

Tell us more about what you already have.

From the original question & replies, and the code shown in #14, that would appear to be "very little"

 

Common is to have some kind of packet format with adresses, data length, payload, and probably a checksum and all nodes should adhere to the same format.

None of that seems to have been considered yet?

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
None of that seems to have been considered yet?

I can extract very little info from OP's posts. OP's #14 indeed seems to be all that he has untill now.

I see nothing what indicates any kind of packet format.

OP seems to be a beginner with programming and with very minimal to no knowlege of network stacks ( yet ).

I should not have rambled about Mumarnet here. Too distractive, sorry for that.

So I'll try to amend to make a step by step "tutorial" on how to get from "nothing" to "something that works".

 

To OP:

In the function " IniControllerAs ( ) " you Set or reset PD2, and I assume this is connected to the Transmit / Receive bit of an RS485 transeiver chip.

Your code is a bit hard to read. This does not mean anything to a programmer without looking at a schematic (which is not included):

	PORTD |= (1 << PIND2);

"PIND2" is also probably meant for the PIN (input) register, but because both PORTD2 and PIND2 are just defined as being " 2 ", it does not matter much, and most find these names a bit silly.

Get into the habit of using names for pins that reflect the thing they are used for.

In my own code I use (Actual copy):

/* PORT and BIT for the rs485 Transmitter enable signal
PD4 is the default for the MumarBase pcb. */
#define NETWORK_RS485_DDR		DDRD
#define NETWORK_RS485_PORT		PORTD
#define NETWORK_RS485_PIN		PIND
#define NETWORK_RS485_ENABLE_BIT	(1<<4)

Seems like a lot of typing, but I've gotten into the habit of doing this for all I/O pins in all projects.

After these #define's you can code like:


// Initialisation: Set port to output push pull:
NETWORK_RS485_DDR  |= NETWORK_RS485_ENABLE_BIT;


// Set this bit.
NETWORK_RS485_PORT  |= NETWORK_RS485_ENABLE_BIT;

// Clear the bit again.
NETWORK_RS485_PORT  &= ~NETWORK_RS485_ENABLE_BIT;


The main advantages for me is the standardisation, it works the same in all my projects.

There is also no opportunity to use different port pins for the same thing if you use the same #define'd macro's in your whole project.

The code also becomes self documenting:

- The code itself is the comment. So Code and Comment can not get out of sync.

- No need to type extra text as comments, which saves text.

- No need to read the non existing comment (You will have forgotton how it works after you haven't looked at your code for a few years...)

- Text like " NETWORK_RS485_ENABLE_BIT is very unambiguous, and you can do text searches over multiple source code files to find all instances (in a decent IDE) where it is used.

 

In my projects the RS485 enable also tends to move around a bit, also because I use different AVR's.

All I have to do then is to change the pin and port #define's to move the function to another AVR I/O pin.

The code also reflects what it's intention is. You can easily see that the "rs485 enable bit is beig set or cleared, regardless of it's location.

Also, a good IDE will analyse the #define'd macro's, and give you a list of options after you type the first few letters of a macro name.

 

-------------------

But to move on from here you have a few options.

You can either study and use existing protocols such as SNAP or MODBUS, which have both been mentioned before.

If you want to go this way, Find some libs and study them. Then try to compile and see what happens.

Lots of documentation and code examples on the net.

For code it is always a good idea to search directly on: https://github.com/

Hundreds of thousands of projects with example code to learn from over there.

For general info about RS485 and RS485 related networks such as modbus, wikipedia is always a good start.

 

-------------------

Or you can write your own code, which is also a good learning experience.

For this scheme, Try to build your first programs from the ground up.

Start with a direct UsART connection. First between an AVR and a PC, later between 2 AVR's.

Write something to get some data through. Combination AVR & PC is handy, because you can use a "terminal emulator" on a PC to show text and type text back to your AVR.

If you can send & receive text reliable between 2 AVR's it is time to add some kind of packet format.

A very simple format would be:

1). " > " character to mark the start of a packet.

2). "A"  A single character as address byte.

3). "....." A string of text, which represents your data.

4). [CR] Carriage return, which signals the end of a packet.

 

This format is convenient, because the packets can be sent from a standard terminal emulator.

Beware though that all kind of different combinations of "carriage return" and "line feed" characters are used on different PC platforms.

I believe ModBus also uses something similar, I think each packat starts with a single " : " character.

 

In the receive routine you should also adhere strictly to your defined packet format.

So start with discarding all data that is not equal to the start of packet byte, " > " in this case.

If you found this character in the input, then the byte after that will be the address byte.

A slave should check in software if this 2nd byte received is equal to it's own address, or a defined broadcast address, or something else.

If the address is valid, It should receive and store all data coming after it untill the "carriage return" is received.

The slave knows then that a complete packet of data is received and could do something with it.

 

If the address is not valid, the slave should discard all data untill the same "carriage return" is received.

The slave knows that all data, including the "carriage return" is beloning to a packet that is for another node.

After it received the [CR] character, it should go back and listen for " > " for the start of the next packet.

This scheme ensures that if a " > " character is embedded in the data, it will not be mis interpreded as a start of packet.

 

This is about the absolute minimum for some kind of network scheme.

And all this must be done in software. The AVR has no internal registers for slave addresses or other fancy stuff.

The UsART hardware is just the basic stuff for sending and receiving bytes and generating interrupts.

 

AFTER you are able to send the above mentioned packet "strings" between 2 node's, it is time to add a RS485 driver and receivers in between.

First just 2 simple nodes. One AVR sends, the other receives. RS485 Enable can be continuously tied to a proper DC level. GND or Vcc.

Then add a 3rd node. A RS485 receiver, which only listens to your network, and logs all data to a terminal emulator program on your PC.

 

Next step is to make the RS485 line switchable, so a master can listen to a response from a slave.

A master could for example send a message each second, and the slave should respond to it, but only if the address matches.

(So make the master also send packets to some dummy address, which should be ignored by the slave).

 

All the good, bad & dummy packets should be visible on the terminal emulator on your pc. Each message on a separate line. It should look like:

>A line of text
>C message to a dummy address
>F packet to the right slave address
>X response from the slave to address "X"

(For clarity, I used CAPITALS for the addresses and added a space as the first byte of the data.)

(The [CR] is not shown explicitly, because the terminal emulator interprets it as a carriage return / newline. (Different settings for terminal emulators)).

All this is quite involved, and it is still just the bare minimum to get anything going.

For a reliable RS485 network a lot more code is needed.

 

You should also find a copy of "10 ways to bulletproof RS485" which is an old but valuable application note in pdf format.

https://duckduckgo.com/html?q=10+ways+to+bulletproof+RS485

 

And if you do not have any yet:

Give yourself a present and buy some USD 5 Logic Analysers from Ali / Ebay / China / Whatever.

Search for "24m 8ch" and use it with the free and open source Sigrok / Pulseview combination.

Such a box transforms a lot of guesswork into knowing and you will very soon wonder why you did not buy one a long time ago.

I find these small boxes often more usefull than my EUR 300 Rigol Oscilloscope.

Such a Logic Analyser should be a part of each Arduino starter kit, especially because lot's of starters do not even know what a Logic Analyser is, and how usefull it is for uC software development.

In your case, you can easily use a logic analyser to check if your AVR actually sends the data correctly, and sends it at the right baudrate, and also if the RS485_ENABLE lines of the AVR's are switched at the correct times if you send a message from master to slave and receive a response.

 

There are also 2 different common way's to set up an RS485 network.

In a Full Duplex setup, the master has a dedicated send line to RS485 receivers in all slaves, and slaves send messages back over a second twisted wire pair.

In a Half Duplex setup, the master sends a packet, and then quickly switches the RS485 transceiver to receive to receive a response from a slave.

Which one are you using?

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:
OP seems to be a beginner with programming and with very minimal to no knowlege of network stacks

Indeed - hence #10.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello everyone , Can i use RS485 in full duplex Mode using MAX485, can anyone explain in details with its programming in atmega8.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That seems to be a separate question?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you plan on putting more than one device on the full duplex bus you might as well stick with two wire 485. In fact programming wise it's the same thing for the most part. You still need to disable the transmit driver when device is idle

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

shahid.kvmdam wrote:
Can i use RS485 in full duplex Mode 

Not in conjunction with the slave addressing that's been discussed here - because that is inherently half-duplex !

 

So it definitely is a separate topic - should be split to a separate thread.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...

Pages

Topic locked