资源描述
#include #define uint8 unsigned char#define uint16 unsigned int#define FOSC 16000000uint16 BAUD=9600;uint16 TEMP_Alert=1000;/字地址 0 - 255 (只取低8位)/位地址 0 - 255 (只取低8位)/* 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 ;uint16 TempRegister; /用于测试 字址址16uint8 localAddr = 0x01; /单片机控制板的地址uint8 sendCount; /发送字节个数uint8 receCount; /接收到的字节个数/uint8 sendPosi; /发送位置uint8 xdata receBuf1;uint8 xdata sendBuf1;void checkComm0Modbus(void);uint16 getRegisterVal(uint16 addr,uint16 *tempData);uint16 setRegisterVal(uint16 addr,uint16 tempData);void switch_BAUD(uint16 value);/*波特率调整函数 */函数功能:调整串口通信波特率/串口工作在工作方式1,即8位波特率可变模式/*/void switch_BAUD(uint16 value) switch(value) case 0x0001: BAUD=9600;break; case 0x0002: BAUD=14400;break; case 0x0003: BAUD=19200;break; TR1=0; /停止定时器1 ES=0; /关闭串口中断 TH1=TL1=-(FOSC/12/32/BAUD); /设置波特率 TR1=1; /开启定时器1 ES=1; /使能串口中断/*end*/*CRC校验码生成函数 */函数功能:生成CRC校验码/本代码中使用查表法,以提高运算速度/*/uint16 crc16(uint8 *puchMsg, uint16 usDataLen) uint8 uchCRCHi = 0xFF ; /* 高CRC字节初始化 */ uint8 uchCRCLo = 0xFF ; /* 低CRC 字节初始化 */ uint16 uIndex ; /* CRC循环中的索引 */ while (usDataLen-) /* 传输消息缓冲区 */ uIndex = uchCRCHi *puchMsg+ ; /* 计算CRC */ uchCRCHi = uchCRCLo auchCRCHiuIndex ; uchCRCLo = auchCRCLouIndex ; return (uchCRCLo 8 | uchCRCHi) ; /uint16 crc16(uint8 *puchMsg, uint16 usDataLen)/*end*/*串口发送函数 */函数功能:将数据包通过串口发送至主机/有待修改./*/开始发送void Begin_send(void) uint16 i=0; while(sendCount-) SBUF = sendBufi+; while(!TI); /*end*/*从机响应主机问询函数,function code : 03,读取多个寄存器值 */函数功能:丛机根据串口接收到的数据包receBuf里面的内容,根据被读取寄存器/的起始地址和读取的寄存器个数,去读相应的寄存器的值,并将读取的数据以MODBUS/响应数据的标准格式打包,经过串口发送到主机。数据包格式同上。/*/ 询问数据包格式:/ receBuf0 receBuf1 receBuf2 receBuf3 receBuf4 receBuf5 receBuf6 receBuf7 /询问数据格式:receBuf=从站地址, 功能码, 起始地址高位,起始地址低位,寄存器数高位, 寄存器数低位, 校验码低位, 校验码高位/ 响应数据包格式:/ sendBuf0 sendBuf1 sendBuf2 sendBuf3 sendBuf4 sendBuf5. receBuf6 receBuf7 /响应数据格式:receBuf=从站地址, 功能码, 字节计数, 数据1, 数据2, 数据3,. 校验码低位, 校验码高位void readRegisters(void) uint8 addr; uint8 tempAddr; uint16 crcData; uint8 readCount; uint8 byteCount; uint16 i; uint16 tempData = 0; /addr = (receBuf28) + receBuf3; /tempAddr = addr & 0xfff; addr = receBuf3; tempAddr = addr; /readCount = (receBuf48) + receBuf5; /要读的个数 readCount = receBuf5; byteCount = readCount * 2; /每个寄存器内容占高,低两个字节 for(i=0;i 8; sendBufi+4 = tempData & 0xff; sendBuf0 = localAddr; sendBuf1 = 3; /function code : 03 sendBuf2 = byteCount; byteCount += 3; /加上前面的地址,功能码,地址 共3+byteCount个字节 crcData = crc16(sendBuf,byteCount); sendBufbyteCount = crcData & 0xff; / CRC代码低位在前 byteCount+; sendBufbyteCount = crcData 8 ; /高位在后 sendCount = byteCount + 1;/例如byteCount=49,则sendBuf中实际上有49+1个元素待发 Begin_send();/void readRegisters(void)/*end*/ /*从机响应主机问询函数,function code : 16,设置多个寄存器值 */函数功能:丛机根据串口接收到的数据包receBuf里面的内容,根据被强制寄存器/的起始地址,去设置相应寄存器的值,响应数据包同询问数据包/的内容相同,经过串口发送到主机。/*/询问数据包格式:/ receBuf0 receBuf1 receBuf2 receBuf3 receBuf4 receBuf5 receBuf6 receBuf7 receBuf8 . receBuf9 receBuf10 /询问数据格式:receBuf=从站地址, 功能码, 起始地址高位,起始地址低位,寄存器数高位,寄存器数低位, 字节计数, 数据高位, 数据低位,. 校验码低位, 校验码高位/响应数据包内容为除去询问数据包中的字节计数和数据高低位的其他内容void presetMultipleRegisters(void)uint8 addr;uint8 tempAddr;uint8 byteCount;uint8 setCount;uint16 crcData;uint16 tempData;uint8 i;/addr = (receBuf28) + receBuf3;/tempAddr = addr & 0xfff;addr = receBuf3;tempAddr = addr & 0xff;/setCount = (receBuf48) + receBuf5;setCount = receBuf5;byteCount = receBuf6; for(i=0;isetCount;i+,tempAddr+) tempData = (receBufi*2+7 8; /寄存器地址高位sendBuf3 = addr & 0xff;/寄存器地址低位sendBuf4 = setCount 8;/待设置寄存器数量高位sendBuf5 = setCount & 0xff;/待设置寄存器数量低位crcData = crc16(sendBuf,6);/生成CRC校验码sendBuf6 = crcData & 0xff; /CRC代码低位在前sendBuf7 = crcData 8; /高位在后sendCount = 8;Begin_send(); /void presetMultipleRegisters(void)/*end*/*查询uart接收的数据包内容函数 */函数功能:丛机根据串口接收到的数据包receBuf1里面的内容,即function code执行/相应的命令/*/void checkComm0Modbus(void)uint16 crcData;uint16 tempData;if(receCount 4) switch(receBuf1) case 3:/读取保持寄存器(一个或多个) if(receCount = 8) /从询问数据包格式可知,receCount应该等于8 /接收完成一组数据 /应该关闭接收中断 if(receBuf0=localAddr) /核对地址 crcData = crc16(receBuf,6); /核对校验码 if(crcData = receBuf7+(receBuf68) if(receBuf1 = 3) /读取保持寄存器(一个或多个) readRegisters(); receCount = 0; break; case 16:/设置多个寄存器 tempData = (receBuf4= tempData) if(receBuf0=localAddr ) crcData = crc16(receBuf,tempData-2); if(crcData = (receBuftempData-28)+ receBuftempData-1) presetMultipleRegisters(); receCount = 0; break; default: break; /void checkComm0(void)/*end*/*读取寄存器内容函数 */函数功能:根据寄存器地址读取相应寄存器内容/*/取寄存器值 返回0表示成功uint16 getRegisterVal(uint16 addr,uint16 *tempData)uint16 result = 0;uint16 tempAddr;tempAddr = addr & 0xfff;switch(tempAddr & 0xff) case 0x00: *tempData = TempRegister; break; /读取01开关A温度 case 0x01: *tempData = TempRegister; break; /读取01开关A温度 case 0x02: *tempData = TempRegister; break; /读取01开关A温度 case 0x03: *tempData = TempRegister; break; /读取01开关周边温度 case 0x04: *tempData = TempRegister; break; /读取02开关A温度 case 0x05: *tempData = TempRegister; break; /读取02开关A温度 case 0x06: *tempData = TempRegister; break; /读取02开关A温度 case 0x07: *tempData = TempRegister; break; /读取02开关周边温度 case 0x08: *tempData = TempRegister; break; /读取03开关A温度 case 0x09: *tempData = TempRegister; break; /读取03开关A温度 case 0x0a: *tempData = TempRegister; break; /读取03开关A温度 case 0x0b: *tempData = TempRegister; break; /读取03开关周边温度 case 0x0c: *tempData = TempRegister; break; /读取04开关A温度 case 0x0d: *tempData = TempRegister; break; /读取04开关A温度 case 0x0e: *tempData = TempRegister; break; /读取04开关A温度 case 0x0f: *tempData = TempRegister; break; /读取04开关周边温度 case 0x10: *tempData = TempRegister; break; /读取05开关A温度 case 0x11: *tempData = TempRegister; break; /读取05开关A温度 case 0x12: *tempData = TempRegister; break; /读取05开关A温度 case 0x13: *tempData = TempRegister; break; /读取05开关周边温度 case 0x14: *tempData = TempRegister; break; /读取06开关A温度 case 0x15: *tempData = TempRegister; break; /读取06开关A温度 case 0x16: *tempData = TempRegister; break; /读取06开关A温度 case 0x17: *tempData = TempRegister; break; /读取06开关周边温度 case 0x18: *tempData = TempRegister; break; /读取07开关A温度 case 0x19: *tempData = TempRegister; break; /读取07开关A温度 case 0x1a: *tempData = TempRegister; break; /读取07开关A温度 case 0x1b: *tempData = TempRegister; break; /读取07开关周边温度 case 0x1c: *tempData = TempRegister; break; /读取08开关A温度 case 0x1d: *tempData = TempRegister; break; /读取08开关A温度 case 0x1e: *tempData = TempRegister; break; /读取08开关A温度 case 0x1f: *tempData = TempRegister; break; /读取08开关周边温度 case 0x20: *tempData = TempRegister; break; /读取09开关A温度 case 0x21: *tempData = TempRegister; break; /读取09开关A温度 case 0x22: *tempData = TempRegister; break; /读取09开关A温度 case 0x23: *tempData = TempRegister; break; /读取09开关周边温度 case 0x24: *tempData = TempRegister; break; /读取10开关A温度 case 0x25: *tempData = TempRegister; break; /读取10开关A温度 case 0x26: *tempData = TempRegister; break; /读取10开关A温度 case 0x27: *tempData = TempRegister; break; /读取10开关周边温度 case 0x28: *tempData = TempRegister; break; /读取11开关A温度 case 0x29: *tempData = TempRegister; break; /读取11开关A温度 case 0x2a: *tempData = TempRegister; break; /读取11开关A温度 case 0x2b: *tempData = TempRegister; break; /读取11开关周边温度 case 0x2c: *tempData = TempRegister; break; /读取12开关A温度 case 0x2d: *tempData = TempRegister; break; /读取12开关A温度 case 0x2e: *tempData = TempRegister; break; /读取12开关A温度 case 0x2f: *tempData = TempRegister; break; /读取12开关周边温度 case 0x30: *tempData = TempRegister; break; /读取13开关A温度 case 0x31: *tempData = TempRegister; break; /读取13开关A温度 case 0x32: *tempData = TempRegister; break; /读取13开关A温度 case 0x33: *tempData = TempRegister; break; /读取13开关周边温度 case 0x34: *tempData = TempRegister; break; /读取14开关A温度 case 0x35: *tempData = TempRegister; break; /读取14开关A温度 case 0x36: *tempData = TempRegister; break; /读取14开关A温度 case 0x37: *tempData = TempRegister; break; /读取14开关周边温度 case 0x38: *tempData = TempRegister; break; /读取15开关A温度 case 0x39: *tempData = TempRegister; break; /读取15开关A温度 case 0x3a: *tempData = TempRegister; break; /读取15开关A温度 case 0x3b: *tempData = TempRegister; break; /读取15开关周边温度 case 0x3c: *tempData = TempRegister; break; /读取16开关A温度 case 0x3d: *tempData = TempRegister; break; /读取16开关A温度 case 0x3e: *tempData = TempRegister; break; /读取16开关A温度 case 0x3f: *tempData = TempRegister; break; /读取16开关周边温度 case 0x40: *tempData = TempRegister; break; /读取17开关A温度 case 0x41: *tempData = TempRegister; break; /读取17开关A温度 case 0x42: *tempData = TempRegister; break; /读取17开关A温度 case 0x43: *tempData = TempRegister; break; /读取17开关周边温度 case 0x44: *tempData = TempRegister; break; /读取18开关A温度 case 0x45: *tempData = TempRegister; break; /读取18开关A温度 case 0x46: *tempData = TempRegister; break; /读取18开关A温度 case 0x47: *tempData = TempRegister; break; /读取18开关周边温度 case 0x48: *tempData = TempRegister; break; /读取19开关A温度 case 0x49: *tempData = TempRegister; break; /读取19开关A温度 case 0x4a: *tempData = TempRegister; break; /读取19开关A温度 case 0x4b: *tempData = TempRegister; break; /读取19开关周边温度 case 0x4c: *tempData = TempRegister; break; /读取20开关A温度 case 0x4d: *tempData = TempRegister; break; /读取20开关A温度 case 0x4e: *tempData = TempRegister; break; /读取20开关A温度 case 0x4f: *tempData = TempRegister; break; /读取20开关周边温度 case 0x50: *tempData = TempRegister; break; /读取21开关A温度 case 0x51: *tempData = TempRegister; break; /读取21开关A温度 case 0x52: *tempData = TempRegister; break; /读取21开关A温度 case 0x53: *tempData = TempRegister; break; /读取21开关周边温度 case 0x54: *tempData = TempRegister; break; /读取22开关A温度 case 0x55: *tempData = TempRegister; break; /读取22开关A温度 case 0x56: *tempData = TempRegister; break; /读取22开关A温度 case 0x57: *tempData = TempRegister; break; /读取22开关周边温度 case 0x58: *tempData = TempRegister; break; /读取23开关A温度 case 0x59: *tempData = TempRegister; break; /读取23开关A温度 case 0x5a: *tempData = TempRegister; break; /读取23开关A温度 case 0x5b: *tempData = TempRegister; break; /读取23开关周边温度 case 0x5c: *tempData = TempRegister; break; /读取24开关A温度 case 0x5d: *tempData = TempRegister; break; /读取24开关A温度 case 0x5e: *tempData = TempRegister; break; /读取24开关A温度 case 0x5f: *tempData = TempRegister; break; /读取24开关周边温度 case 0x60: *tempData = TempRegister; break; /读取25开关A温度 case 0x61: *tempData = TempRegister; break; /读取25开关A温度 case 0x62: *tempData = TempRegister; break; /读取25开关A温度 case 0x63: *tempData = TempRegister; break; /读取25开关周边温度 case 0x64: *tempData = TempRegister; break; /读取26开关A温度 case 0x65: *tempData = TempRegister; break; /读取26开关A温度 case 0x66: *tempData = TempRegister; break; /读取26开关A温度 case 0x67: *tempData = TempRegister; break; /读取26开关周边温度 case 0x68: *tempData = TempRegister; break; /读取27开关A温度 case 0x69: *tempData = TempRegister; break; /读取27开关A温度 case 0x6a: *tempData = TempRegister; break; /读取27开关A温度 case 0x6b: *tempData = TempRegister; break; /读取27开关周边温度 case 0x6c: *tempData = TempRegister; break; /读取28开关A温度 case 0x6d: *tempData = TempRegister; break; /读取28开关A温度 case 0x6e: *tempData = TempRegister; break; /读取28开关A温度 case 0x6f: *tempData = TempRegister; break; /读取28开关周边温度 case 0x70: *tempData = TempRegister; break; /读取29开关A温度 case 0x71: *tempData = TempRegister; break; /读取29开关A温度 case 0x72: *tempData = TempRegister; break; /读取29开关A温度 case 0x73: *tempData = TempRegister; break; /读取29开关周边温度 case 0x74: *tempData = TempRegister; break; /读取30开关A温度 case 0x75: *tempData = TempRegister; break; /读取30开关A温度 case 0x76: *tempData = TempRegister; break; /读取30开关A温度 case 0x77: *tempData = TempRegister; break; /读取30开关周边温度 case 0x78: *tempData = localAddr; break; /读取设备地址 case 0x79: *tempData = BAUD; break; /读取串口通信波特率 case 0x7a: *tempData = TEMP_Alert; break; /读取报警温度上限 case 0x7b: *tempData = TempRegister; break; /读取年寄存器 case 0x7c: *tempData = TempRegister; break; /读取月寄存器 case 0x7d: *tempData = TempRegister; break; /读取日寄存器 case 0x7e: *tempData = TempRegister; break; /读取时寄存器 case 0x7f: *tempData = TempRegister; break; /读取分寄存器 case 0x80: *tempData = TempRegister; break; /读取秒寄存器 default: break; return result;/uint16 getRegisterVal(uint16 addr,uint16 &data)/*end*/*设置寄存器内容函数 */函数功能:根据寄存器地址设置相应寄存器内容/*/设置寄存器值 返回0表示成功uint16 setRegisterVal(uint16 addr,uint16 tempData)uint16 result = 0;uint16 tempAddr;tempAddr = addr & 0xfff;switch(tempAddr & 0xff) case 0x78: localAddr = tempData; break;/设置设备地址,设备地址初始值为0x01 case 0x79: BAUD = tempData; switch_BAUD(BAUD); break;/设置串口通信波特率,串口通信波特率寄存器值为 1:9600 , 2:14400 , 3:19200 ,初始值为1 case 0x7a: TEMP_Alert = tempData; break;/设置报警温度上限,报警温度值存储方式:精确到0.1C,以扩大10倍的数值放在寄存器里面,例如温度值为100.1,则读写值为1001 case 0x7b: TempRegister = tempData; break;/设置年寄存器 case 0x7c: TempRegister = tempData; break;/设置月寄存器 case 0x7d: TempRegister = tempData; break;/设置日寄存器 case 0x7e: TempRegister = tempData; break;/设置时寄存器 case 0x7f: TempRegister = tempData; break;/设置分寄存器 case 0x80: TempRegister = tempData; break;/设置
展开阅读全文