Single chip multi-machine communication with correction

Publisher:花开堂前Latest update time:2015-12-24 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Principle of multi-machine communication

   In multi-machine communication, the host must be able to identify each slave, which can be achieved in the 51 series microcontrollers through the SM2 bit of the SCON register. When the serial port sends data in mode 2 or mode 3, each frame of information is 11 bits, and the 9th bit is a data programmable bit. The address frame and the data frame are distinguished by setting TB8 to 1 or 0. When this bit is 1, the address frame is sent; when this bit is 0, the data frame is sent.

   During multi-machine communication, the host first sends the address of a slave and waits for the slave's response. After receiving the address frame, all slaves compare it with the local address. If they are the same, SM2 is set to 0 to prepare to receive data; if they are different, the current data is discarded and the SM2 bit remains unchanged.

Two-to-many machine communication circuit diagram

Here, U1 is the master, U2 is slave 1, and U3 is slave 2.

3. C language program

(1) Host program

#include
#include

#define _SUCC_   0x0f //Data transfer successful
#define _ERR_    0xf0 //Data transfer failed
unsigned char Table[9]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
unsigned char Buff[20];  //Data buffer
unsigned char temp=0xff;
sbit KEY1=P1^6;
sbit KEY2=P1^7;
//unsigned char addr;

//1ms delay function
void delay_1ms(unsigned int t)
{
 unsigned int x,y;
 for(x=t;x>0;x--)
  for(y=110;y>0;y--);
}
//Buffer initialization
void Buff_init()
{
 unsigned char i;    //Put the data in Table into the buffer
 for(i=0;i<9;i++)   
 {
  Buff[i]= Table[i];
  delay_1ms(100);
 
}
//Serial port initialization function
void serial_init()
{
 TMOD=0x20;  //Timer 1 works in mode 2
 TH1=0xfd;   
 TL1=0xfd;  //Baud rate is 9600
 PCON=0;
 SCON=0xd0;  //Serial port works in mode 3
 TR1=1;   //Start timer
 TI=0;
 RI=0;
}
//Send data function
void SEND_data(unsigned char *Buff)
{
 unsigned char i;
 unsigned char lenth;
 unsigned char check;
 lenth=strlen(Buff);      //Calculate data length
 check=lenth;

 TI=0;         //Send data length
 TB8=0;      //Send data frame
 SBUF=lenth;
 while(!TI);
 TI=0;
         
 for(i=0;i//Send data
 {
  check=check^Buff[i];
  TB8=0;
  SBUF=Buff[i];    
  while(!TI);
  TI=0;
 }

 TB8=0;      //Send check byte
 SBUF=check;     
 while(!TI);
 TI=0;      
}
//Send data to the specified slave address
void ADDR_data(unsigned addr)
{
 while(temp!=addr)  //The host waits for the slave to return its address as a response signal
 {
  TI=0;     //Send slave address
  TB8=1;     //Send address frame
  SBUF=addr;
  while(!TI);
  TI=0; 
  
  RI=0;
  while(!RI);
  temp=SBUF;
  RI=0;
 }

 temp=_ERR_;    //The host waits for the slave data to receive the successful signal
 while(temp!=_SUCC_)
 {
  SEND_data(Buff);
  RI=0;
  while(!RI);
  temp=SBUF;
  RI=0;
 }
      
void main()
{
 Buff_init();
 serial_init();
 while(1)
 {
  if(KEY1==0)
  {
   delay_1ms(5);
   if(KEY1==0)
   {
    while(!KEY1);
    ADDR_data(0x01);
   }
  }
  if(KEY2==0)
  {
   delay_1ms(5);
   if(KEY2==0)
   {
    while(!KEY2);
    ADDR_data(0x02);
   }
  }

 }
}[page]

(2) Slave 1 program

#include
#include

#define addr     0x01 // address of slave 1
#define _SUCC_   0x0f // data transmission successful
#define _ERR_    0xf0 // data transmission failed
unsigned char aa=0xff; // communication flag between host and slave
unsigned char Buff[20]; // data buffer

//Serial port initialization function
void serial_init()
{
 TMOD=0x20;  //Timer 1 works in mode 2
 TH1=0xfd;   
 TL1=0xfd;  //Baud rate is 9600
 PCON=0;
 SCON=0xd0;  //Serial port works in mode 3
 TR1=1;   //Start timer
 TI=0;
 RI=0;
}
//Receive data function
unsigned char RECE_data(unsigned char *Buff)
{
 unsigned char i,temp;
 unsigned char lenth;
 unsigned char check;

 RI=0;      //Receive data lengthwhile
 (!RI);
 if(RB8==1)     //If the address frame is received, return 0xfe
  return 0xfe;
 lenth=SBUF;
 RI=0;      
 
 check=lenth;
 for(i=0;i//Receive data
 {
  while(!RI);
  if(RB8==1)    //If the address frame is received, return 0xfe
   return 0xfe;
  Buff[i]=SBUF;    
  check=check^(Buff[i]);
  RI=0;
 }

 while(!RI);     //Receive checksum byteif
 (RB8==1)     //If the address frame is received, return 0xfe
  return 0xfe;
 temp=SBUF;
 RI=0;
      
 check=temp^check;   //Compare the checksum received from the host with the checksum calculated by itselfif
 (check!=0)    //The checksums are inconsistent, indicating that the data is received incorrectly. An error signal is sent to the host, and the function returns 0xff
 {
  TI=0;
  TB8=0;
  SBUF=_ERR_;
  while(!TI);
  TI=0;
  return 0xff;
 }
 TI=0;           //The checksums are consistent, indicating that the data is received correctly. A success signal is sent to the host, and the function returns 0x00
 TB8=0;
 SBUF=_SUCC_;
 while(!TI);
 TI=0;
 return 0;

void main()
{
 serial_init();
 while(1)
 {
  SM2=1;              //Receive address framewhile
  (aa!=addr)   //The slave waits for the host to request its own address
  {
   RI=0;
   while(!RI);
   aa=SBUF;
   RI=0;
  }

  TI=0;      //Once requested, the slave returns its own address as a response and waits for receiving data
  TB8=0;
  SBUF=addr;
  while(!TI);
  TI=0;

  SM2=0;                  //Receive data frame
  aa=0xff;     //The slave receives data and saves it to the data buffer
  while(aa==0xff)
  {
   aa=RECE_data(Buff);
  }
  if(aa==0xfe)
   continue; 
  P1=Buff[1];      //Check the received data
 }
}

(3) Slave 2 program

#include
#include

#define addr     0x02 // address of slave 2
#define _SUCC_   0x0f // data transmission successful
#define _ERR_    0xf0 // data transmission failed
unsigned char aa=0xff; // communication flag between host and slave
unsigned char Buff[20]; // data buffer

//Serial port initialization function
void serial_init()
{
 TMOD=0x20;  //Timer 1 works in mode 2
 TH1=0xfd;   
 TL1=0xfd;  //Baud rate is 9600
 PCON=0;
 SCON=0xd0;  //Serial port works in mode 3
 TR1=1;   //Start timer
 TI=0;
 RI=0;
}
//Receive data function
unsigned char RECE_data(unsigned char *Buff)
{
 unsigned char i,temp;
 unsigned char lenth;
 unsigned char check;

 RI=0;      //Receive data lengthwhile
 (!RI);
 if(RB8==1)     //If the address frame is received, return 0xfe
  return 0xfe;
 lenth=SBUF;
 RI=0;      
 
 check=lenth;
 for(i=0;i//Receive data
 {
  while(!RI);
  if(RB8==1)    //If the address frame is received, return 0xfe
   return 0xfe;
  Buff[i]=SBUF;    
  check=check^(Buff[i]);
  RI=0;
 }

 while(!RI);     //Receive checksum byteif
 (RB8==1)     //If the address frame is received, return 0xfe
  return 0xfe;
 temp=SBUF;
 RI=0;
      
 check=temp^check;   //Compare the checksum received from the host with the checksum calculated by itselfif
 (check!=0)    //The checksums are inconsistent, indicating that the data is received incorrectly. An error signal is sent to the host, and the function returns 0xff
 {
  TI=0;
  TB8=0;
  SBUF=_ERR_;
  while(!TI);
  TI=0;
  return 0xff;
 }
 TI=0;           //The checksums are consistent, indicating that the data is received correctly. A success signal is sent to the host, and the function returns 0x00
 TB8=0;
 SBUF=_SUCC_;
 while(!TI);
 TI=0;
 return 0;

void main()
{
 serial_init();
 while(1)
 {
  SM2=1;              //Receive address framewhile
  (aa!=addr)   //The slave waits for the host to request its own address
  {
   RI=0;
   while(!RI);
   aa=SBUF;
   RI=0;
  }

  TI=0;      //Once requested, the slave returns its own address as a response and waits for receiving data
  TB8=0;
  SBUF=addr;
  while(!TI);
  TI=0;

  SM2=0;                  //Receive data frame
  aa=0xff;     //The slave receives data and saves it to the data buffer
  while(aa==0xff)
  {
   aa=RECE_data(Buff);
  }
  if(aa==0xfe)
   continue; 
  P1=Buff[2];      //Check the received data
 }
}

Reference address:Single chip multi-machine communication with correction

Previous article:The importance of variable initialization in multi-machine communication of single-chip microcomputers...
Next article:MRC multiple encryption technology of single chip microcomputer

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号