Rs-485 usando o compilador mikroc for pic

429 views
Skip to first unread message

Fabio de Oliveira Nascimento

unread,
Mar 22, 2007, 11:25:33 PM3/22/07
to processo...@googlegroups.com, PEDRO PERES

RS-485 Library

RS-485 is a multipoint communication which allows multiple devices to be connected to a single signal cable. mikroC provides a set of library routines to provide you comfortable work with RS485 system using Master/Slave architecture. Master and Slave devices interchange packets of information, each of these packets containing synchronization bytes, CRC byte, address byte, and the data. Each Slave has its unique address and receives only the packets addressed to it. Slave can never initiate communication.

It is programmer’s responsibility to ensure that only one device transmits via 485 bus at a time.

RS-485 routines require USART module on PORTC. Pins of USART need to be attached to RS-485 interface transceiver, such as LTC485 or similar. Pins of transceiver (Receiver Output Enable and Driver Outputs Enable) should be connected to PORTC, pin 2 (check the figure at end of the chapter).

Note: Address 50 is the common address for all Slaves (packets containing address 50 will be received by all Slaves). The only exceptions are Slaves with addresses 150 and 169, which require their particular address to be specified in the packet.

Library Routines

RS485Master_Init

Prototype

void Rs485Master_Init(unsigned short * port, unsigned short pin);

Returns

Nothing.

Description

Initializes PIC MCU as Master in RS-485 communication.

Requires

USART HW module needs to be initialized. See Usart_Init.

Example
RS485Master_Init(PORTC, 2);

RS485Master_Receive

Prototype

void RS485Master_Receive(unsigned short *data);

Returns

Nothing.

Description

Receives any message sent by Slaves. Messages are multi-byte, so this routine must be called for each byte received (see the example at the end of the chapter). Upon receiving a message, buffer is filled with the following values:

  • data[0..2] is the message
  • data[3] is number of message bytes received, 1–3
  • data[4] is set to 255 when message is received
  • data[5] is set to 255 if error has occurred
  • data[6] is the address of the Slave which sent the message

Function automatically adjusts data[4] and data[5] upon every received message. These flags need to be cleared from the program.

Requires

MCU must be initialized as Master in RS-485 communication in order to be assigned an address. See RS485Master_Init.

Example
unsigned short msg[8];
...
RS485Master_Receive(msg);

RS485Master_Send

Prototype

void RS485Master_Send(unsigned short *data, unsigned short datalen, unsigned short address);

Returns

Nothing.

Description

Sends message data from buffer to Slave(s) specified by address via RS-485; datalen is a number of bytes in message (1 <= datalen <= 3).

Requires

MCU must be initialized as Master in RS-485 communication in order to be assigned an address. See RS485Master_Init.

It is programmer’s responsibility to ensure (by protocol) that only one device sends data via 485 bus at a time.

Example
unsigned short msg[8];
...
RS485Master_Send(msg, 3, 0x12);

RS485Slave_Init

Prototype

void Rs485slave_Init(unsigned short * port, unsigned short pin, char address);

Returns

Nothing.

Description

Initializes MCU as Slave with a specified address in RS-485 communication. Slave address can take any value between 0 and 255, except 50, which is common address for all slaves.

Requires

USART HW module needs to be initialized. See Usart_Init.

Example

Initialize MCU as Slave with address 160:

RS485Slave_Init(PORTC, 2, 160);

RS485Slave_Receive

Prototype

void RS485Slave_Receive(unsigned short *data);

Returns

Nothing.

Description

Receives message addressed to it. Messages are multi-byte, so this routine must be called for each byte received (see the example at the end of the chapter). Upon receiving a message, buffer is filled with the following values:

  • data[0..2] is the message
  • data[3] is number of message bytes received, 1–3
  • data[4] is set to 255 when message is received
  • data[5] is set to 255 if error has occurred
  • rest of the buffer is undefined

Function automatically adjusts data[4] and data[5] upon every received message. These flags need to be cleared from the program.

Requires

MCU must be initialized as Slave in RS-485 communication in order to be assigned an address. See RS485Slave_Init.

Example
unsigned short msg[8];
...
RS485Slave_Read(msg);

RS485Slave_Send

Prototype

void RS485Slave_Send(unsigned short *data, unsigned short datalen);

Returns

Nothing.

Description

Sends message data from buffer to Master via RS-485; datalen is a number of bytes in message (1 <= datalen <= 3).

Requires

MCU must be initialized as Slave in RS-485 communication in order to be assigned an address. See RS485Slave_Init. It is programmer’s responsibility to ensure (by protocol) that only one device sends data via 485 bus at a time.

Example
unsigned short msg[8];
...
RS485Slave_Send(msg, 2);

Library Example

The example demonstrates working with PIC as Slave nod in RS-485 communication. PIC receives only packets addressed to it (address 160 in our example), and general messsages with target address 50. The received data is forwarded to PORTB, and sent back to Master.

unsigned short dat[8];   // buffer for receiving/sending messages
unsigned short i = 0, j = 0;

void interrupt(void) {
/* Every byte is received by RS485Slave_Read(dat);
   If message is received without errors,
   data[4] is set to 255 */

 if (RCSTA.OERR) PORTD = 0x81;
 RS485Slave_Read(dat);
}

main() {

  TRISB = 0;
  TRISD = 0;
  Usart_Init(9600);                // Initialize usart module
  RS485Slave_Init(PORTC, 2, 160);            // Initialize MCU as Slave with address 160
  PIE1.RCIE   = 1;                 // Enable interrupt
  INTCON.PEIE = 1;                 //    on byte received
  PIE2.TXIE   = 0;                 //    via USART (RS485)
  INTCON.GIE  = 1;
  PORTB  = 0;
  PORTD  = 0;
  dat[4] = 0;                      // Ensure that msg received flag is 0
  dat[5] = 0;                      // Ensure that error flag is 0

  do {
    if (dat[5]) PORTD = 0xAA;      // If there is error, set PORTD to $AA
    if (dat[4]) {                  // If message received:
      dat[4] = 0;                  //     Clear message received flag
      j = dat[3];                  //     Number of data bytes received
      for (i = 1; i < j; i++)
        PORTB = dat[--i];          //     Output received data bytes
      dat[0]++;                    //     Increment received dat[0]
      RS485Slave_Write(dat, 1);    //     Send it back to Master
    }
  } while (1);
} //~!

HW Connection

Example of interfacing PC to PIC MCU via RS485 bus

Example of interfacing PC to PIC MCU via RS485 bus

scheme_rs485.gif

pedro....@uninove.br

unread,
Mar 24, 2007, 1:55:18 PM3/24/07
to processo...@googlegroups.com
Fabio,


Acredito que esse seja o caminho, vamos nessa???

Será que podemos adaptar essa biblioteca para a linha 16x8xx???


Aguardo resposta

Prof. Pedro


Citando Fabio de Oliveira Nascimento <fab...@gmail.com>:

> RS-485 Library
> RS-485 is a multipoint communication which allows multiple devices to be
> connected to a single signal cable. mikroC provides a set of library routines
> to provide you comfortable work with RS485 system using Master/Slave
> architecture. Master and Slave devices interchange packets of information,
> each of these packets containing synchronization bytes, CRC byte, address
> byte, and the data. Each Slave has its unique address and receives only the
> packets addressed to it. Slave can never initiate communication.
>
> It is programmer's responsibility to ensure that only one device transmits
> via 485 bus at a time.
>
> RS-485 routines require USART module on PORTC. Pins of USART need to be
> attached to RS-485 interface transceiver, such as LTC485 or similar. Pins of
> transceiver (Receiver Output Enable and Driver Outputs Enable) should be
> connected to PORTC, pin 2 (check the figure at end of the chapter).
>
> Note: Address 50 is the common address for all Slaves (packets containing
> address 50 will be received by all Slaves). The only exceptions are Slaves
> with addresses 150 and 169, which require their particular address to be
> specified in the packet.
>
> Library Routines

> a.. RS485Master_Init
> b.. RS485Master_Receive
> c.. RS485Master_Send
> d.. RS485Slave_Init
> e.. RS485Slave_Receive
> f.. RS485Slave_Send

> RS485Master_Init
> Prototype void Rs485Master_Init(unsigned short * port, unsigned short
> pin);
>
> Returns Nothing.
>
> Description Initializes PIC MCU as Master in RS-485 communication.
>
> Requires USART HW module needs to be initialized. See Usart_Init.
>
> Example RS485Master_Init(PORTC, 2);
>
> RS485Master_Receive
> Prototype void RS485Master_Receive(unsigned short *data);
>
> Returns Nothing.
>
> Description Receives any message sent by Slaves. Messages are
> multi-byte, so this routine must be called for each byte received (see the
> example at the end of the chapter). Upon receiving a message, buffer is
> filled with the following values:
>

> a.. data[0..2] is the message
> b.. data[3] is number of message bytes received, 1-3
> c.. data[4] is set to 255 when message is received
> d.. data[5] is set to 255 if error has occurred
> e.. data[6] is the address of the Slave which sent the message

> a.. data[0..2] is the message
> b.. data[3] is number of message bytes received, 1-3
> c.. data[4] is set to 255 when message is received
> d.. data[5] is set to 255 if error has occurred
> e.. rest of the buffer is undefined

> Example of interfacing PC to PIC MCU via RS485 bus
>
> >
>


Pedro Augusto Peres
pedro....@uninove.br
eng.pedr...@gmail.com
_______________________________________________________________________________

Esta mensagem, incluindo os seus anexos, contém informações confidenciais
destinadas a indivíduo e propósito específicos, e é protegida por lei. Caso
você não seja o citado indivíduo, deve apagar esta mensagem.
É terminantemente proibida a utilização, acesso, cópia ou divulgação não
autorizada das informações presentes nesta mensagem.
As informações contidas nesta mensagem e em seus anexos são de
responsabilidade de seu autor, não representando idéias, opiniões,
pensamentos
ou qualquer forma de posicionamento por parte do Centro Universitário Nove de
Julho (UNINOVE).

The information contained in this communication is confidential, is law
protected, and is intended only for business use of the addressee. If you
have
received this communication in error, please immediately delete it.
It's forbidden the unathorized use, access, copy or disclose of the
information contained in this communication.
The content of this message is responsability of the author, and does not
represent ideas, opinions, thoughts or any kind of statement of Centro
Universitário Nove de Julho (UNINOVE) Corporation.

________________________________________________________
http://www.uninove.br
UNINOVE - 1º Centro Universitário da Cidade de São Paulo

Reply all
Reply to author
Forward
0 new messages