M8/M16 multi-host Communication Example: Use 485 if the interference is high

Source: Internet
Author: User

During multi-host communication, the RS232 interfaces of all devices are on the communication line. Only one device can be a host, and the others can be a slave device. The communication is initiated by the host. Generally, a data frame uses one start bit and nine data bits. Among them, 9th bits (rxb8) are used to indicate whether the frame is an address frame or a data frame. When the Frame Type characterization bit is "1", it indicates that the frame data is an address frame. When the Frame Type characterization bit is "0", it indicates that the frame is a data frame.

In AVR, The usart receiver can filter received data frames by setting the mpcm flag in the ucsra register of the slave machine. If the filter function is enabled, the data frames received by the receiver that are not address information frames will be filtered and not placed into the receiving buffer, this makes it easier for the slave MCU to process data frame programs in multi-host communication (compared with the standard 51 structure ). The sender is not affected by mpcm bit settings.

Multi-host communication mode allows multiple slave hosts to receive data from one host on the communication line. Decodes the address in the received address frame to determine which slave is addressed by the host. If a slave is addressable by the host, it will receive the next data frame sent by the host, while other slave will ignore the data frame until it receives another address frame. (The slave address is determined by the software of each slave ).

For the host MCU in the multi-host communication system, you can set a 9-bit data frame structure (ucsz = 7 ). When sending an address frame, set 9th bits to "1"; when sending a data frame, set 9th bits to "0 ". In this case, the slave must also be set to receive 9-bit data frames.

The data exchange process for multi-host communication is as follows:
1) set all slave nodes to work in multi-host communication mode (mpcm = 1 ).
2) The communication starts when the host sends an address frame. For example, if the 8-bit data is 0x01 (the first slave address), 9th bits = "1 ", call the 1 slave.
3) All slaves receive and read the address frames issued by the host. Among all slave MCU, the RXC flag is set to receive the address frame.
4) each slave MCU reads The UDR register and determines whether it is addressed by the host. If it is addressable, clear the mpcm bit in the ucsar register and wait for the received data; otherwise, keep the mpcm as "1" and wait for the receiving of the next address frame (this step should be implemented by the user software ):

A) After receiving the address frame, the MCU, which acts as the No. 1 slave, determines that the read UDR data 0x01 is its own address, and sets the mpcm position to "0 ", after receiving, all the data frames sent by the host until the next address frame.

B) processing of other slave MCU: After receiving the address frame, it is determined that reading The UDR data 0x01 is not its own address ", in this way, they will ignore the data frames subsequently sent by the host until the host sends the address frame again.

5) After receiving the last data frame from the addressable MCU, place the mpcm position and wait for the next address frame to appear (this step should also be implemented by the user's software ), repeat steps 2.

The following is an M16 multi-host Communication Example of M8. You only need to modify the header file.

Host
---------------------------------------------------------------------------------

# Include<12008.h>
# Include<Delay. h>
# Include<Usart. h>
# Include<Crc8.h>   // The CRC check function is in this file. 

# Define  Amount10  // Set the communication data volume (including 1 address frame, n data frames, and 1 Verification frame)

UnsignedChar Send [amount];          // Sender
UnsignedCharInbox [amount];          // Inbox
UnsignedCharN = 0;                    // Number of memory interruptions

//--------------------------------------------------------------------
Interrupt [12]Rxd_isr (void)           // Receive interrupted

  UnsignedCharError = 0;
  If (Ucsra & 4|Ucsra & 16)Error = 1;// A parity error or frame error is recorded.  
  Inbox [N] = UDR;                      // Save it to the inbox
  N ++;                                // Number of memory interruptions
  If (error)Inbox [0] = 0;               // If a communication error occurs, the inbox address frame is marked as Invalid Address 0.
}

//---------------------------------------------------------------------
VoidMain (void)
{
  Usart_init ();                        // Serial port Initialization
  Ucsra = 0;                           // Host address filtering disabled (multi-host communication)
  # ASM ("Sei ")       

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.