# Example codes for serial communication in 8051 using RS485 protocol

Status
Not open for further replies.

#### gigifazio

##### Junior Member level 2
Hi,

I must connect three united with protocol master\slave
have any example code function, for the serial comunication in 8051 whit the rs485 protocol?

#### glenjoy

##### Banned
Re: RS485 & 8051

what language do you want? in C or assembly?

KULDEEP07

Points: 2

### KULDEEP07

Points: 2

RS485 & 8051

I prefer C

Thx

#### IanP

Re: RS485 & 8051

Language is not that much important as the approach on how to do it ..
I will concntrate here on 2-wire half-duplex transmission:
From Master point of view the RS-485 communication is not different to normal serial transmission except thet it has to dedicate additional pin to switch from transmission to reception. Slaves will also need to have this additional pin from RS-485 drivers from their side.
So if you use RS-485 driver, such as for example MAX485, you will connect both control pins to one microcontroller pin and for transmission you will set this pin to L, for reception you will set this pin to high.
Example of assembly code (Pin P1.0 is the control pin):
#asm
SETB P1.0
JNB TI, $CLR TI MOV SBUF, A JNB TI,$
CLR P1.0
#endasm
Usually request from Master to a Slave to pass data will have to use unique address so a slave can respond to master's call.
In other words, a command from master may look like this:
Start Bit, Unique Address, Data ..Data, CheckSum, End Bit
and the response from a slave will have very simalar format..
Have you got it so far?

DrWhoF

Points: 2

### DrWhoF

Points: 2

#### gigifazio

##### Junior Member level 2
Re: RS485 & 8051

Ok but i do not have clearly like managing this "protocol" ...

#### IanP

Re: RS485 & 8051

If you would like to go any further from here, you should describe in more detailas what these devices (master and slaves) will do: will the slaves measure something, if yes then what, and what do you expect from the Master controller.
Theorizing about RS-485 "protocol" without knowing what it should do is like talking with blind about colours. In fact that will be you who will design this "protocol"and we are here to give you a hand ..

Points: 2

Points: 2

### DrWhoF

Points: 2

#### gigifazio

##### Junior Member level 2
Re: RS485 & 8051

I would create a netwarok with 1 master and more slave, the slave measuring 4 o 5 parameters (Example:voltage,temperature, exc) and send at the master this data only when it comes to it asked, but if are an allarm (example temperature > 30 °C) it send onli the name of alarm sensor.. the data line is an rs482.

1000 Tx

#### mrcube_ns

Re: RS485 & 8051

What is distance (max) between mcu and your sensors?

Maybe you dont need 485?

Maybe i2c is good enough?

If you dont have long distance you can use just one serial port of mcu and via digital outputs selecting devices to send data (something like r/w pins ...). Alarms -> you can read all devices in one second (maybe 2), so you have "on line" system.

Just think about maybe you dont need to inplement 485 protocol.

Best regards,

Mr.Cube

DrWhoF

### DrWhoF

Points: 2

#### IanP

Re: RS485 & 8051

Mr Cube suggested I2C bus. Here are some explenations as far as distance is concerned:
Question: What is the maximum distance of the I2C bus?
This depends on the load of the bus and the speed you run at. In typical applications, the length is a few meters (9-12ft). The maximum capacitive load has been specified (see also the electrical Spec's in the I2C FAQ). Another thing to be taken into account is the amount of noise picked up by long cabling. This noise can disturb the signal transmitted over the bus so badly that it becomes unreadable.
There is a method of increasing the distance on I2C bus but it is heavily paid for with the transmission speed (clock frequecny as low as 100Hz ..).

What is rs482? Device like MAX1482 is the RS-485 half-duplex transceiver. Do you have this one in mind?

The attached drawing shows how your hardware (around RS-485 communication) may look like .. (no protection included)

regards ..

#### Attachments

• rs-485_hardware1_204.pdf
64 KB · Views: 108
DrWhoF

### DrWhoF

Points: 2

#### tobb

##### Member level 5
RS485 & 8051

HI Can any one tell me how to working on RS485 Full Duplex ? Which chip i could use ? Thanx

#### IanP

Re: RS485 & 8051

RS-485 full duplex can by only realized in 4-wire configuration and you can use any of 485 drivers, but in pairs: one for transmission (twisted pair) and one for reception (another twisted pair).

DrWhoF

Points: 2

### DrWhoF

Points: 2

#### gigifazio

##### Junior Member level 2
Re: RS485 & 8051

mrcube_ns said:
What is distance (max) between mcu and your sensors?

Maybe you dont need 485?

Maybe i2c is good enough?

If you dont have long distance you can use just one serial port of mcu and via digital outputs selecting devices to send data (something like r/w pins ...). Alarms -> you can read all devices in one second (maybe 2), so you have "on line" system.

Just think about maybe you dont need to inplement 485 protocol.

Best regards,

Mr.Cube

the distance not is a problem because i have as specific the implementation about rs485... and I do not have to think at the hw but at the sw function..

IanP said:
RS-485 full duplex can by only realized in 4-wire configuration and you can use any of 485 drivers, but in pairs: one for transmission (twisted pair) and one for reception (another twisted pair).

this wants to say who i can:
1) configure the 8051 register for uart comunication?
2) to use any function for serial comunication?
3) not to think to a protocol of comunication for example "ModBus"?

I do not have cleary the n° 2 point..

Added after 1 hours 55 minutes:

What is rs482? Device like MAX1482 is the RS-485 half-duplex transceiver. Do you have this one in mind?

regards ..

sorry is an error ... I intend .. "rs485"

DrWhoF

### DrWhoF

Points: 2

#### IanP

Re: RS485 & 8051

I will describe here the simplest procedure of sending single byte through serial port. For the sake of simplicity, at this stage we will not use serial port interrupt, which I would like you to implement at the very end ..

Assuming that pin P1.0 is responsible for control of data flow direction and A contains byte to be sent, the sub may look like this:

#asm
SETB P1.0
JNB TI, $CLR TI MOV SBUF, A JNB TI,$
CLR P1.0
#endasm

and after this, because P1.0 is cleared, MAX485 driver is set for reception.

Lets make some further assumption:
- if you send number 01h, you will ask the Slave to send back temperature
- if the number is 02h, this will call for voltage .. etc.

- lets treat "<" (left delimiter) as the start character, and ">" (right delimiter) as end character.
- address of first Slave = 01h, second slave = 02h , ...

Example: command to call Slave nr.2 to send voltage reading will look as follows:
< 02h 02h >

How do you like it so far and any comments to the last drawing?

regards ..

DrWhoF

Points: 2

### DrWhoF

Points: 2

#### mrcube_ns

Re: RS485 & 8051

I must connect three united with protocol master\slave
have any example code function, for the serial comunication in 8051 whit the rs485 protocol?

Just to be clear:
RS485 is hardware (current signals ...), protocol is one you write.

Mr.Cube

To IanP:

Is there anyone with more help points then you?
BTW. nice picture you draw!

DrWhoF

### DrWhoF

Points: 2

#### mrcube_ns

Re: RS485 & 8051

gigifazio

This is complimet to IanP, on his drawing of 485 hardware connection (it wasn't hard to him to draw pic, to scan drawing and to post it).

BTW = between

You never say what 485 equipment you want to connect to mcu?
Maybe there are some finished code for those equipment.

Or you want to connect 3 8051 mcu?

Regards,

Mr.Cube

DrWhoF

Points: 2

### DrWhoF

Points: 2

#### gigifazio

##### Junior Member level 2
Re: RS485 & 8051

Ok i understand!!

however... yes i must connect 3 or 4 ... micro (core 51)

Status
Not open for further replies.