51 MCU modbus protocol program

Publisher:lidong4069Latest update time:2014-12-17 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include
#define uint8 unsigned char
#define uint16 unsigned int
#define FOSC 16000000
uint16 BAUD=9600;
uint16 TEMP_Alert=1000;
//Word address 0 - 255 (only the lower 8 bits)
//Bit address 0 - 255 (only the lower 8 bits)

uint16 TempRegister; //Used to test word address 16

uint8 localAddr = 0x01; //The address of the MCU control board
uint8 sendCount; //Number of bytes sent
uint8 receCount; //Number of bytes received
//uint8 sendPosi; //Sending position

uint8 xdata receiveBuf[1];
uint8 xdata sendBuf[1];

void checkComm0Modbus(void);
uint16 getRegisterVal(uint16 addr,uint16 *tempData);
uint16 setRegisterVal(uint16 addr,uint16 tempData);
void switch_BAUD(uint16 value);


/*****************************Baud rate adjustment function************************************/
////Function: adjust the baud rate of serial communication
////The serial port works in working mode 1, that is, 8-bit variable baud rate mode
/********************************************************/
void switch_BAUD(uint16 value)
{
 switch(value)
 {
  case 0x0001: { BAUD=9600;break; }
  case 0x0002: { BAUD=14400;break; }
  case 0x0003: { BAUD=19200;break; }
 }
 TR1=0; //Stop timer 1
 ES=0; //Disable serial port interrupt
 TH1=TL1=-(FOSC/12/32/BAUD); //Set baud rate
 TR1=1; //Turn on timer 1
 ES=1; //Enable serial port interrupt
}
/***************************CRC check code generation function************************************/
////Function: Generate CRC check code
////This code uses the table lookup method to improve the operation speed
/********************************************************/
uint16 crc16(uint8 *puchMsg, uint16 usDataLen)
{
 uint8 uchCRCHi = 0xFF ; /* High CRC byte initialization*/
 uint8 uchCRCLo = 0xFF ; /* Low CRC byte initialization*/
 uint16 uIndex ; /* Index in CRC loop*/
 while (usDataLen--) /* Transmit message buffer*/
 {
  uIndex = uchCRCHi ^ *puchMsg++ ; /* Calculate CRC */
  uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
  uchCRCLo = auchCRCLo[uIndex] ;
 }
 return (uchCRCLo << 8 | uchCRCHi) ;
}//uint16 crc16(uint8 *puchMsg, uint16 usDataLen)
/***********************************Serial port sending function************************************/
////Function: Send the data packet to the host through the serial port
////To be modified......
/*************************************************************/
//Start sending
void Begin_send(void)
{
 uint16 i=0;
 while(sendCount--)
 {
  SBUF = sendBuf[i++];
  while(!TI);
 }
}

/********Slave responds to host inquiry function, function code: 03, read multiple register values********/
////Function: The slave
reads the corresponding register value according to the content of the data packet receBuf[] received by the serial port, the starting address of the register to be read and the number of registers to be read, and packages the read data in the standard format of MODBUS
////response data and sends it to the host through the serial port. The data packet format is the same as above.
/****************************************************************/
////////////// Query data packet format:
/////////////////////////// receBuf[0] receBuf[1] receBuf[2] receBuf[3] receBuf[4] receBuf[5] receBuf[6] receBuf[7]
//Query data format: receBuf[]={slave address, function code, start address high bit, start address low bit, register number high bit, register number low bit, check code low bit, check code high bit}

////////////// Response data packet format:
////////////////////////// sendBuf[0] sendBuf[1] sendBuf[[2] sendBuf[3] sendBuf[4] sendBuf[5]... receBuf[6] receBuf[7]
//Response data format: receBuf[]={slave address, function code, byte count, data 1, data 2, data 3,... checksum low bit, checksum high bit}

void readRegisters(void)
{
 uint8 addr;
 uint8 tempAddr;
 uint16 crcData;
 uint8 readCount;
 uint8 byteCount;
 uint16 i;
 uint16 tempData = 0;

 //addr = (receBuf[2]<<8) + receBuf[3];
 //tempAddr = addr & 0xfff;
 addr = receBuf[3];
 tempAddr = addr;

 //readCount = (receBuf[4]<<8) + receBuf[5]; //Number of data to be readreadCount
 = receBuf[5];

 byteCount = readCount * 2; //Each register content occupies high and low two bytes

 for(i=0;i {
  getRegisterVal(tempAddr,&tempData);   
  sendBuf[i+3] = tempData >> 8;       
  sendBuf[i+4] = tempData & 0xff; 
 }

 sendBuf[0] = localAddr;
 sendBuf[1] = 3; //function code : 03
 sendBuf[2] = byteCount;
 byteCount += 3; //Add the previous address, function code, address total 3+byteCount bytes
 crcData = crc16(sendBuf,byteCount);
 sendBuf[byteCount] = crcData & 0xff; // CRC code low bit first
 byteCount++;
 sendBuf[byteCount] = crcData >> 8 ; //high bit at the end

 sendCount = byteCount + 1; //For example, if byteCount=49, there are actually 49+1 elements to be sent in sendBuf[]
 Begin_send();
}//void readRegisters(void)
/********Slave responds to host inquiry function, function code: 16, sets multiple register values********/
////Function: The slave sets the value of the corresponding register according to the content of the data packet receBuf[] received by the serial port and
the starting address of the forced register ////. The response data packet has
the same content as the inquiry data packet //// and is sent to the host through the serial port.
/****************************************************************/
//////////////Inquiry data packet format:
/////////////////////////// receBuf[0] receBuf[1] receBuf[2] receBuf[3] receBuf[4] receBuf[5] receBuf[6] receBuf[7] receBuf[8] ... receBuf[9] receBuf[10]
//Inquiry data format: receBuf[]={slave address, function code, start address high bit, start address low bit, register number high bit, register number low bit, byte count, data high bit, data low bit, ... check code low bit, check code high bit}
//The response data packet content is the other content except the byte count and data high and low bits in the inquiry data packet [page]
void presetMultipleRegisters(void)
{
 uint8 addr;
 uint8 tempAddr;
 uint8 byteCount;
 uint8 setCount;
 uint16 crcData;
 uint16 tempData;
 uint8 i;

 //addr = (receBuf[2]<<8) + receBuf[3];
 //tempAddr = addr & 0xfff;
 addr = receBuf[3];
 tempAddr = addr & 0xff;

 //setCount = (receBuf[4]<<8) + receBuf[5];
 setCount = receBuf[5];
 byteCount = receBuf[6];

 for(i=0;i {
  tempData = (receBuf[i*2+7]<<8) + receBuf[i*2+8]; // register value to be set
  setRegisterVal(tempAddr,tempData); 
 }

 sendBuf[0] = localAddr;
 sendBuf[1] = 16; //function code : 16
 sendBuf[2] = addr >> 8; //register address high bit
 sendBuf[3] = addr & 0xff; //register address low bit
 sendBuf[4] = setCount >> 8; //high bit of the number of registers to be set
 sendBuf[5] = setCount & 0xff; //low bit of the number of registers to be set
 crcData = crc16(sendBuf,6); //generate CRC checksum
 sendBuf[6] = crcData & 0xff; //CRC code low bit first
 sendBuf[7] = crcData >> 8; //high bit last
 sendCount = 8;
 Begin_send();
}//void presetMultipleRegisters(void)
/*****************************Query the content of the data packet received by uart**************************/ ////Function: The machine executes the corresponding command
according to the content of the data packet receBuf[1] received by the serial port, that is, the function code ///// **************************************************************/ void checkComm0Modbus(void) {  uint16 crcData;  uint16 tempData;





 if(receCount > 4)
 {
  switch(receBuf[1])
  {
   case 3://Read holding registers (one or more)
   {
    if(receCount >= 8) //From the query data packet format, we know that receCount should be equal to 8
    {//Receiving a set of data //The receive interrupt should be turned off
     if(receBuf[0]==localAddr) //Check address
     {
      crcData = crc16(receBuf,6); //Check check code
      if(crcData == receBuf[7]+(receBuf[6]<<8))
      if(receBuf[1] == 3)
      { //Read holding registers (one or more)
       readRegisters();
      }
     }
    }     
    receCount = 0;
    break;
   }

   case 16://Set multiple registers
   {
    tempData = (receBuf[4]<<8) + receBuf[5];
    tempData = tempData * 2; //Number of data
    tempData += 9; //From the query data packet format, we know that receCount should be equal to 9+byteCount
    if(receCount >= tempData)
    {
     if(receBuf[0]==localAddr )
     {
      crcData = crc16(receBuf,tempData-2);
      if(crcData == (receBuf[tempData-2]<<8)+ receBuf[tempData-1])
      {
       presetMultipleRegisters(); 
      }
     }
     receCount = 0;
    }
    break;
   }  
   default: break; 
  }
 }
}//void checkComm0(void)
/**********************************Function to read register contents**************************/
////Function: Read the corresponding register content according to the register address
/********************************************************/
//Get the register value and return 0 to indicate success
uint16 getRegisterVal(uint16 addr,uint16 *tempData)
{
 uint16 result = 0;
 uint16 tempAddr;

 tempAddr = addr & 0xfff;

 switch(tempAddr & 0xff)
 {
  case 0x00:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 01
  case 0x01:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 01
  case 0x02:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 01
  case 0x03:{ *tempData = TempRegister; break; }//Read the surrounding temperature of switch A of 01
  case 0x04:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 02
  case 0x05:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 02
  case 0x06:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 02
  case 0x07:{ *tempData = TempRegister; break; }//Read the surrounding temperature of switch A of 02
  case 0x08:{ *tempData = TempRegister; break; }//Read 03 switch A temperature
  case 0x09:{ *tempData = TempRegister; break; }//Read 03 switch A temperature
  case 0x0a:{ *tempData = TempRegister; break; }//Read 03 switch A temperature
  case 0x0b:{ *tempData = TempRegister; break; }//Read 03 switch surrounding temperature
  case 0x0c:{ *tempData = TempRegister; break; }//Read 04 switch A temperature
  case 0x0d:{ *tempData = TempRegister; break; }//Read 04 switch A temperature
  case 0x0e:{ *tempData = TempRegister; break; }//Read 04 switch A temperature
  case 0x0f:{ *tempData = TempRegister; break; }//Read 04 switch surrounding temperature
  case 0x10:{ *tempData = TempRegister; break; }//Read 05 switch A temperature
  case 0x11:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 05
  case 0x12:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 05
  case 0x13:{ *tempData = TempRegister; break; }//Read the temperature around switch A of 05
  case 0x14:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 06
  case 0x15:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 06
  case 0x16:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 06
  case 0x17:{ *tempData = TempRegister; break; }//Read the temperature around switch A of 06
  case 0x18:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 07
  case 0x19:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 07
  case 0x1a:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 07
  case 0x1b:{ *tempData = TempRegister; break; }//Read the temperature around switch A of 07
  case 0x1c:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 08
  case 0x1d:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 08
  case 0x1e:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 08
  case 0x1f:{ *tempData = TempRegister; break; }//Read the temperature around switch A of 08
  case 0x20:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 09
  case 0x21:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 09
  case 0x22:{ *tempData = TempRegister; break; }//Read 09 switch A temperature
  case 0x23:{ *tempData = TempRegister; break; }//Read 09 switch surrounding temperature
  case 0x24:{ *tempData = TempRegister; break; }//Read 10 switch A temperature
  case 0x25:{ *tempData = TempRegister; break; }//Read 10 switch A temperature
  case 0x26:{ *tempData = TempRegister; break; }//Read 10 switch A temperature
  case 0x27:{ *tempData = TempRegister; break; }//Read 10 switch surrounding temperature
  case 0x28:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 11
  case 0x29:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 11
  case 0x2a:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 11
  case 0x2b:{ *tempData = TempRegister; break; }//Read the surrounding temperature of switch 11
  case 0x2c:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 12
  case 0x2d:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 12
  case 0x2e:{ *tempData = TempRegister; break; }//Read the temperature of switch A of 12
  case 0x2f:{ *tempData = TempRegister; break; }//Read the surrounding temperature of switch 12
  case 0x30:{ *tempData = TempRegister; break; }//Read 13 switch A temperature
  case 0x31:{ *tempData = TempRegister; break; }//Read 13 switch A temperature
  case 0x32:{ *tempData = TempRegister; break; }//Read 13 switch A temperature
  case 0x33:{ *tempData = TempRegister; break; }//Read 13 switch peripheral temperature
  case 0x34:{ *tempData = TempRegister; break; }//Read 14 switch A temperature
  case 0x35:{ *tempData = TempRegister; break; }//Read 14 switch A temperature
  case 0x36:{ *tempData = TempRegister; break; }//Read 14 switch A temperature
  case 0x37:{ *tempData = TempRegister; break; }//Read 14 switch peripheral temperature
  case 0x38:{ *tempData = TempRegister; break; }//Read 15 switch A temperature
  case 0x39:{ *tempData = TempRegister; break; }//Read 15 switch A temperature
  case 0x3a:{ *tempData = TempRegister; break; }//Read 15 switch A temperature
  case 0x3b:{ *tempData = TempRegister; break; }//Read 15 switch peripheral temperature
  case 0x3c:{ *tempData = TempRegister; break; }//Read 16 switch A temperature
  case 0x3d:{ *tempData = TempRegister; break; }//Read 16 switch A temperature
  case 0x3e:{ *tempData = TempRegister; break; }//Read 16 switch A temperature
  case 0x3f:{ *tempData = TempRegister; break; }//Read 16 switch peripheral temperature
  case 0x40:{ *tempData = TempRegister; break; }//Read 17 switch A temperature
  case 0x41:{ *tempData = TempRegister; break; }//Read 17 switch A temperature
  case 0x42:{ *tempData = TempRegister; break; }//Read 17 switch A temperature
  case 0x43:{ *tempData = TempRegister; break; }//Read 17 switch peripheral temperature
  case 0x44:{ *tempData = TempRegister; break; }//Read 18 switch A temperature
  case 0x45:{ *tempData = TempRegister; break; }//Read 18 switch A temperature
  case 0x46:{ *tempData = TempRegister; break; }//Read 18 switch A temperature
  case 0x47:{ *tempData = TempRegister; break; }//Read 18 switch peripheral temperature
  case 0x48:{ *tempData = TempRegister; break; }//Read 19 switch A temperature
  case 0x49:{ *tempData = TempRegister; break; }//Read 19 switch A temperature
  case 0x4a:{ *tempData = TempRegister; break; }//Read 19 switch A temperature
  case 0x4b:{ *tempData = TempRegister; break; }//Read 19 switch peripheral temperature
  case 0x4c:{ *tempData = TempRegister; break; }//Read 20 switch A temperature
  case 0x4d:{ *tempData = TempRegister; break; }//Read 20 switch A temperature
  case 0x4e:{ *tempData = TempRegister; break; }//Read 20 switch A temperature
  case 0x4f:{ *tempData = TempRegister; break; }//Read 20 switch peripheral temperature
  case 0x50:{ *tempData = TempRegister; break; }//Read 21 switch A temperature
  case 0x51:{ *tempData = TempRegister; break; }//Read 21 switch A temperature
  case 0x52:{ *tempData = TempRegister; break; }//Read 21 switch A temperature
  case 0x53:{ *tempData = TempRegister; break; }//Read 21 switch peripheral temperature
  case 0x54:{ *tempData = TempRegister; break; }//Read 22 switch A temperature
  case 0x55:{ *tempData = TempRegister; break; }//Read 22 switch A temperature
  case 0x56:{ *tempData = TempRegister; break; }//Read 22 switch A temperature
  case 0x57:{ *tempData = TempRegister; break; }//Read 22 switch peripheral temperature
  case 0x58:{ *tempData = TempRegister; break; }//Read 23 switch A temperature
  case 0x59:{ *tempData = TempRegister; break; }//Read 23 switch A temperature
  case 0x5a:{ *tempData = TempRegister; break; }//Read 23 switch A temperature
  case 0x5b:{ *tempData = TempRegister; break; }//Read 23 switch peripheral temperature
  case 0x5c:{ *tempData = TempRegister; break; }//Read 24 switch A temperature
  case 0x5d:{ *tempData = TempRegister; break; }//Read 24 switch A temperature
  case 0x5e:{ *tempData = TempRegister; break; }//Read 24 switch A temperature
  case 0x5f:{ *tempData = TempRegister; break; }//Read 24 switch peripheral temperature
  case 0x60:{ *tempData = TempRegister; break; }//Read 25 switch A temperature
  case 0x61:{ *tempData = TempRegister; break; }//Read 25 switch A temperature
  case 0x62:{ *tempData = TempRegister; break; }//Read 25 switch A temperature
  case 0x63:{ *tempData = TempRegister; break; }//Read 25 switch peripheral temperature
  case 0x64:{ *tempData = TempRegister; break; }//Read 26 switch A temperature
  case 0x65:{ *tempData = TempRegister; break; }//Read 26 switch A temperature
  case 0x66:{ *tempData = TempRegister; break; }//Read 26 switch A temperature
  case 0x67:{ *tempData = TempRegister; break; }//Read 26 switch peripheral temperature
  case 0x68:{ *tempData = TempRegister; break; }//Read 27 switch A temperature
  case 0x69:{ *tempData = TempRegister; break; }//Read 27 switch A temperature case
  0x6a:{ *tempData = TempRegister; break; }//Read 27 switch A temperature
  case 0x6b:{ *tempData = TempRegister; break; }//Read 27 switch ambient temperature
  case 0x6c:{ *tempData = TempRegister; break; }//Read 28 switch A temperature
  case 0x6d:{ *tempData = TempRegister; break; }//Read 28 switch A temperature
  case 0x6e:{ *tempData = TempRegister; break; }//Read 28 switch A temperature
  case 0x6f:{ *tempData = TempRegister; break; }//Read 28 switch ambient temperature
  case 0x70:{ *tempData = TempRegister; break; }//Read 29 switch A temperature
  case 0x71:{ *tempData = TempRegister; break; }//Read 29 switch A temperature
  case 0x72:{ *tempData = TempRegister; break; }//Read 29 switch A temperature
  case 0x73:{ *tempData = TempRegister; break; }//Read 29 switch ambient temperature
  case 0x74:{ *tempData = TempRegister; break; }//Read 30 switch A temperature
  case 0x75:{ *tempData = TempRegister; break; }//Read 30 switch A temperature
  case 0x76:{ *tempData = TempRegister; break; }//Read 30 switch A temperature
  case 0x77:{ *tempData = TempRegister; break; }//Read 30 switch ambient temperature

  case 0x78:{ *tempData = localAddr; break; }//Read device address
  case 0x79:{ *tempData = BAUD; break; }//Read serial communication baud rate   
  case 0x7a:{ *tempData = TEMP_Alert; break; }//Read alarm temperature upper limit

  case 0x7b:{ *tempData = TempRegister; break; }//Read the year register 
  case 0x7c:{ *tempData = TempRegister; break; }//Read the month register 
  case 0x7d:{ *tempData = TempRegister; break; }//Read the day register 
  case 0x7e:{ *tempData = TempRegister; break; }//Read the hour register 
  case 0x7f:{ *tempData = TempRegister; break; }//Read the minute register 
  case 0x80:{ *tempData = TempRegister; break; }//Read the second register 
  default: break; 
 }
 return result;
}//uint16 getRegisterVal(uint16 addr,uint16 &data)[page]
/***********************************Set register content function******************************/
////Function function: Set the corresponding register content according to the register address
/************************************************************/
//Set register value and return 0 to indicate success
uint16 setRegisterVal(uint16 addr,uint16 tempData)
{
 uint16 result = 0;
 uint16 tempAddr;

 tempAddr = addr & 0xfff;

 switch(tempAddr & 0xff)
 {
  case 0x78:{ localAddr = tempData; break;}//Set the device address, the initial value of the device address is 0x01
  case 0x79:{ BAUD = tempData; switch_BAUD(BAUD); break;}//Set the serial communication baud rate, the serial communication baud rate register value is 1:9600, 2:14400, 3:19200, the initial value is 1 
  case 0x7a:{ TEMP_Alert = tempData; break;}//Set the alarm temperature upper limit, the alarm temperature value is stored in the register with an accuracy of 0.1°C and a 10-fold increase in value. For example, if the temperature value is 100.1, the read/write value is 1001 
  case 0x7b:{ TempRegister = tempData; break;}//Set the year register
  case 0x7c:{ TempRegister = tempData; break;}//Set the month register 
  case 0x7d:{ TempRegister = tempData; break;}//Set the day register 
  case 0x7e:{ TempRegister = tempData; break;}//Set the hour register 
  case 0x7f:{ TempRegister = tempData; break;}//Set the minute register 
  case 0x80:{ TempRegister = tempData; break;}//Set the second register             
  default: break; 
 }

 return result;
}

/* CRC 高位字节值表 */
const uint8 code auchCRCHi[] = {
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0/**/,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
    0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
    0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
  } ;
/* CRC低位字节值表*/
const uint8 code auchCRCLo[] = {
    0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06/**/,
    0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
    0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,    
    0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
    0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
    0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
    0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
    0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
    0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
    0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
    0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
    0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
    0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
    0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
    0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
    0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
    0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
    0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
    0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
    0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
    0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
    0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
    0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
    0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
    0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
    0x43, 0x83, 0x41, 0x81, 0x80, 0x40
  } ;

Reference address:51 MCU modbus protocol program

Previous article:51 Communication Protocol—and Verification
Next article:Design of multi-point temperature measurement system based on 51 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号