基于單片機的 modbus例程 詳細
#include <reg51.h>#define uint8 unsigned char#define uint16 unsigned int#define FOSC 16000000uint16 BAUD=9600;uint16 TEMP_Alert=1000;/字地址 0 - 255 (只取低8位)/位地址 0 - 255 (只取低8位)/* CRC 高位字節(jié)值表 */ 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低位字節(jié)值表*/ 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; /發(fā)送字節(jié)個數(shù)uint8 receCount; /接收到的字節(jié)個數(shù)/uint8 sendPosi; /發(fā)送位置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);/*波特率調(diào)整函數(shù) */函數(shù)功能:調(diào)整串口通信波特率/串口工作在工作方式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; /關(guān)閉串口中斷 TH1=TL1=-(FOSC/12/32/BAUD); /設(shè)置波特率 TR1=1; /開啟定時器1 ES=1; /使能串口中斷/*end*/*CRC校驗碼生成函數(shù) */函數(shù)功能:生成CRC校驗碼/本代碼中使用查表法,以提高運算速度/*/uint16 crc16(uint8 *puchMsg, uint16 usDataLen) uint8 uchCRCHi = 0xFF ; /* 高CRC字節(jié)初始化 */ uint8 uchCRCLo = 0xFF ; /* 低CRC 字節(jié)初始化 */ uint16 uIndex ; /* CRC循環(huán)中的索引 */ while (usDataLen-) /* 傳輸消息緩沖區(qū) */ uIndex = uchCRCHi *puchMsg+ ; /* 計算CRC */ uchCRCHi = uchCRCLo auchCRCHiuIndex ; uchCRCLo = auchCRCLouIndex ; return (uchCRCLo << 8 | uchCRCHi) ; /uint16 crc16(uint8 *puchMsg, uint16 usDataLen)/*end*/*串口發(fā)送函數(shù) */函數(shù)功能:將數(shù)據(jù)包通過串口發(fā)送至主機/有待修改./*/開始發(fā)送void Begin_send(void) uint16 i=0; while(sendCount-) SBUF = sendBufi+; while(!TI); /*end*/*從機響應(yīng)主機問詢函數(shù),function code : 03,讀取多個寄存器值 */函數(shù)功能:叢機根據(jù)串口接收到的數(shù)據(jù)包receBuf里面的內(nèi)容,根據(jù)被讀取寄存器/的起始地址和讀取的寄存器個數(shù),去讀相應(yīng)的寄存器的值,并將讀取的數(shù)據(jù)以MODBUS/響應(yīng)數(shù)據(jù)的標(biāo)準(zhǔn)格式打包,經(jīng)過串口發(fā)送到主機。數(shù)據(jù)包格式同上。/*/ 詢問數(shù)據(jù)包格式:/ receBuf0 receBuf1 receBuf2 receBuf3 receBuf4 receBuf5 receBuf6 receBuf7 /詢問數(shù)據(jù)格式:receBuf=從站地址, 功能碼, 起始地址高位,起始地址低位,寄存器數(shù)高位, 寄存器數(shù)低位, 校驗碼低位, 校驗碼高位/ 響應(yīng)數(shù)據(jù)包格式:/ sendBuf0 sendBuf1 sendBuf2 sendBuf3 sendBuf4 sendBuf5. receBuf6 receBuf7 /響應(yīng)數(shù)據(jù)格式:receBuf=從站地址, 功能碼, 字節(jié)計數(shù), 數(shù)據(jù)1, 數(shù)據(jù)2, 數(shù)據(jù)3,. 校驗碼低位, 校驗碼高位void readRegisters(void) uint8 addr; uint8 tempAddr; uint16 crcData; uint8 readCount; uint8 byteCount; uint16 i; uint16 tempData = 0; /addr = (receBuf2<<8) + receBuf3; /tempAddr = addr & 0xfff; addr = receBuf3; tempAddr = addr; /readCount = (receBuf4<<8) + receBuf5; /要讀的個數(shù) readCount = receBuf5; byteCount = readCount * 2; /每個寄存器內(nèi)容占高,低兩個字節(jié) for(i=0;i<byteCount;i+=2,tempAddr+) getRegisterVal(tempAddr,&tempData); sendBufi+3 = tempData >> 8; sendBufi+4 = tempData & 0xff; sendBuf0 = localAddr; sendBuf1 = 3; /function code : 03 sendBuf2 = byteCount; byteCount += 3; /加上前面的地址,功能碼,地址 共3+byteCount個字節(jié) crcData = crc16(sendBuf,byteCount); sendBufbyteCount = crcData & 0xff; / CRC代碼低位在前 byteCount+; sendBufbyteCount = crcData >> 8 ; /高位在后 sendCount = byteCount + 1;/例如byteCount=49,則sendBuf中實際上有49+1個元素待發(fā) Begin_send();/void readRegisters(void)/*end*/ /*從機響應(yīng)主機問詢函數(shù),function code : 16,設(shè)置多個寄存器值 */函數(shù)功能:叢機根據(jù)串口接收到的數(shù)據(jù)包receBuf里面的內(nèi)容,根據(jù)被強制寄存器/的起始地址,去設(shè)置相應(yīng)寄存器的值,響應(yīng)數(shù)據(jù)包同詢問數(shù)據(jù)包/的內(nèi)容相同,經(jīng)過串口發(fā)送到主機。/*/詢問數(shù)據(jù)包格式:/ receBuf0 receBuf1 receBuf2 receBuf3 receBuf4 receBuf5 receBuf6 receBuf7 receBuf8 . receBuf9 receBuf10 /詢問數(shù)據(jù)格式:receBuf=從站地址, 功能碼, 起始地址高位,起始地址低位,寄存器數(shù)高位,寄存器數(shù)低位, 字節(jié)計數(shù), 數(shù)據(jù)高位, 數(shù)據(jù)低位,. 校驗碼低位, 校驗碼高位/響應(yīng)數(shù)據(jù)包內(nèi)容為除去詢問數(shù)據(jù)包中的字節(jié)計數(shù)和數(shù)據(jù)高低位的其他內(nèi)容void presetMultipleRegisters(void)uint8 addr;uint8 tempAddr;uint8 byteCount;uint8 setCount;uint16 crcData;uint16 tempData;uint8 i;/addr = (receBuf2<<8) + receBuf3;/tempAddr = addr & 0xfff;addr = receBuf3;tempAddr = addr & 0xff;/setCount = (receBuf4<<8) + receBuf5;setCount = receBuf5;byteCount = receBuf6; for(i=0;i<setCount;i+,tempAddr+) tempData = (receBufi*2+7<<8) + receBufi*2+8;/待設(shè)置寄存器值 setRegisterVal(tempAddr,tempData); sendBuf0 = localAddr;sendBuf1 = 16; /function code : 16sendBuf2 = addr >> 8; /寄存器地址高位sendBuf3 = addr & 0xff;/寄存器地址低位sendBuf4 = setCount >> 8;/待設(shè)置寄存器數(shù)量高位sendBuf5 = setCount & 0xff;/待設(shè)置寄存器數(shù)量低位crcData = crc16(sendBuf,6);/生成CRC校驗碼sendBuf6 = crcData & 0xff; /CRC代碼低位在前sendBuf7 = crcData >> 8; /高位在后sendCount = 8;Begin_send(); /void presetMultipleRegisters(void)/*end*/*查詢uart接收的數(shù)據(jù)包內(nèi)容函數(shù) */函數(shù)功能:叢機根據(jù)串口接收到的數(shù)據(jù)包receBuf1里面的內(nèi)容,即function code執(zhí)行/相應(yīng)的命令/*/void checkComm0Modbus(void)uint16 crcData;uint16 tempData;if(receCount > 4) switch(receBuf1) case 3:/讀取保持寄存器(一個或多個) if(receCount >= 8) /從詢問數(shù)據(jù)包格式可知,receCount應(yīng)該等于8 /接收完成一組數(shù)據(jù) /應(yīng)該關(guān)閉接收中斷 if(receBuf0=localAddr) /核對地址 crcData = crc16(receBuf,6); /核對校驗碼 if(crcData = receBuf7+(receBuf6<<8) if(receBuf1 = 3) /讀取保持寄存器(一個或多個) readRegisters(); receCount = 0; break; case 16:/設(shè)置多個寄存器 tempData = (receBuf4<<8) + receBuf5; tempData = tempData * 2; /數(shù)據(jù)個數(shù) tempData += 9; /從詢問數(shù)據(jù)包格式可知,receCount應(yīng)該等于9+byteCount if(receCount >= tempData) if(receBuf0=localAddr ) crcData = crc16(receBuf,tempData-2); if(crcData = (receBuftempData-2<<8)+ receBuftempData-1) presetMultipleRegisters(); receCount = 0; break; default: break; /void checkComm0(void)/*end*/*讀取寄存器內(nèi)容函數(shù) */函數(shù)功能:根據(jù)寄存器地址讀取相應(yīng)寄存器內(nèi)容/*/取寄存器值 返回0表示成功uint16 getRegisterVal(uint16 addr,uint16 *tempData)uint16 result = 0;uint16 tempAddr;tempAddr = addr & 0xfff;switch(tempAddr & 0xff) case 0x00: *tempData = TempRegister; break; /讀取01開關(guān)A溫度 case 0x01: *tempData = TempRegister; break; /讀取01開關(guān)A溫度 case 0x02: *tempData = TempRegister; break; /讀取01開關(guān)A溫度 case 0x03: *tempData = TempRegister; break; /讀取01開關(guān)周邊溫度 case 0x04: *tempData = TempRegister; break; /讀取02開關(guān)A溫度 case 0x05: *tempData = TempRegister; break; /讀取02開關(guān)A溫度 case 0x06: *tempData = TempRegister; break; /讀取02開關(guān)A溫度 case 0x07: *tempData = TempRegister; break; /讀取02開關(guān)周邊溫度 case 0x08: *tempData = TempRegister; break; /讀取03開關(guān)A溫度 case 0x09: *tempData = TempRegister; break; /讀取03開關(guān)A溫度 case 0x0a: *tempData = TempRegister; break; /讀取03開關(guān)A溫度 case 0x0b: *tempData = TempRegister; break; /讀取03開關(guān)周邊溫度 case 0x0c: *tempData = TempRegister; break; /讀取04開關(guān)A溫度 case 0x0d: *tempData = TempRegister; break; /讀取04開關(guān)A溫度 case 0x0e: *tempData = TempRegister; break; /讀取04開關(guān)A溫度 case 0x0f: *tempData = TempRegister; break; /讀取04開關(guān)周邊溫度 case 0x10: *tempData = TempRegister; break; /讀取05開關(guān)A溫度 case 0x11: *tempData = TempRegister; break; /讀取05開關(guān)A溫度 case 0x12: *tempData = TempRegister; break; /讀取05開關(guān)A溫度 case 0x13: *tempData = TempRegister; break; /讀取05開關(guān)周邊溫度 case 0x14: *tempData = TempRegister; break; /讀取06開關(guān)A溫度 case 0x15: *tempData = TempRegister; break; /讀取06開關(guān)A溫度 case 0x16: *tempData = TempRegister; break; /讀取06開關(guān)A溫度 case 0x17: *tempData = TempRegister; break; /讀取06開關(guān)周邊溫度 case 0x18: *tempData = TempRegister; break; /讀取07開關(guān)A溫度 case 0x19: *tempData = TempRegister; break; /讀取07開關(guān)A溫度 case 0x1a: *tempData = TempRegister; break; /讀取07開關(guān)A溫度 case 0x1b: *tempData = TempRegister; break; /讀取07開關(guān)周邊溫度 case 0x1c: *tempData = TempRegister; break; /讀取08開關(guān)A溫度 case 0x1d: *tempData = TempRegister; break; /讀取08開關(guān)A溫度 case 0x1e: *tempData = TempRegister; break; /讀取08開關(guān)A溫度 case 0x1f: *tempData = TempRegister; break; /讀取08開關(guān)周邊溫度 case 0x20: *tempData = TempRegister; break; /讀取09開關(guān)A溫度 case 0x21: *tempData = TempRegister; break; /讀取09開關(guān)A溫度 case 0x22: *tempData = TempRegister; break; /讀取09開關(guān)A溫度 case 0x23: *tempData = TempRegister; break; /讀取09開關(guān)周邊溫度 case 0x24: *tempData = TempRegister; break; /讀取10開關(guān)A溫度 case 0x25: *tempData = TempRegister; break; /讀取10開關(guān)A溫度 case 0x26: *tempData = TempRegister; break; /讀取10開關(guān)A溫度 case 0x27: *tempData = TempRegister; break; /讀取10開關(guān)周邊溫度 case 0x28: *tempData = TempRegister; break; /讀取11開關(guān)A溫度 case 0x29: *tempData = TempRegister; break; /讀取11開關(guān)A溫度 case 0x2a: *tempData = TempRegister; break; /讀取11開關(guān)A溫度 case 0x2b: *tempData = TempRegister; break; /讀取11開關(guān)周邊溫度 case 0x2c: *tempData = TempRegister; break; /讀取12開關(guān)A溫度 case 0x2d: *tempData = TempRegister; break; /讀取12開關(guān)A溫度 case 0x2e: *tempData = TempRegister; break; /讀取12開關(guān)A溫度 case 0x2f: *tempData = TempRegister; break; /讀取12開關(guān)周邊溫度 case 0x30: *tempData = TempRegister; break; /讀取13開關(guān)A溫度 case 0x31: *tempData = TempRegister; break; /讀取13開關(guān)A溫度 case 0x32: *tempData = TempRegister; break; /讀取13開關(guān)A溫度 case 0x33: *tempData = TempRegister; break; /讀取13開關(guān)周邊溫度 case 0x34: *tempData = TempRegister; break; /讀取14開關(guān)A溫度 case 0x35: *tempData = TempRegister; break; /讀取14開關(guān)A溫度 case 0x36: *tempData = TempRegister; break; /讀取14開關(guān)A溫度 case 0x37: *tempData = TempRegister; break; /讀取14開關(guān)周邊溫度 case 0x38: *tempData = TempRegister; break; /讀取15開關(guān)A溫度 case 0x39: *tempData = TempRegister; break; /讀取15開關(guān)A溫度 case 0x3a: *tempData = TempRegister; break; /讀取15開關(guān)A溫度 case 0x3b: *tempData = TempRegister; break; /讀取15開關(guān)周邊溫度 case 0x3c: *tempData = TempRegister; break; /讀取16開關(guān)A溫度 case 0x3d: *tempData = TempRegister; break; /讀取16開關(guān)A溫度 case 0x3e: *tempData = TempRegister; break; /讀取16開關(guān)A溫度 case 0x3f: *tempData = TempRegister; break; /讀取16開關(guān)周邊溫度 case 0x40: *tempData = TempRegister; break; /讀取17開關(guān)A溫度 case 0x41: *tempData = TempRegister; break; /讀取17開關(guān)A溫度 case 0x42: *tempData = TempRegister; break; /讀取17開關(guān)A溫度 case 0x43: *tempData = TempRegister; break; /讀取17開關(guān)周邊溫度 case 0x44: *tempData = TempRegister; break; /讀取18開關(guān)A溫度 case 0x45: *tempData = TempRegister; break; /讀取18開關(guān)A溫度 case 0x46: *tempData = TempRegister; break; /讀取18開關(guān)A溫度 case 0x47: *tempData = TempRegister; break; /讀取18開關(guān)周邊溫度 case 0x48: *tempData = TempRegister; break; /讀取19開關(guān)A溫度 case 0x49: *tempData = TempRegister; break; /讀取19開關(guān)A溫度 case 0x4a: *tempData = TempRegister; break; /讀取19開關(guān)A溫度 case 0x4b: *tempData = TempRegister; break; /讀取19開關(guān)周邊溫度 case 0x4c: *tempData = TempRegister; break; /讀取20開關(guān)A溫度 case 0x4d: *tempData = TempRegister; break; /讀取20開關(guān)A溫度 case 0x4e: *tempData = TempRegister; break; /讀取20開關(guān)A溫度 case 0x4f: *tempData = TempRegister; break; /讀取20開關(guān)周邊溫度 case 0x50: *tempData = TempRegister; break; /讀取21開關(guān)A溫度 case 0x51: *tempData = TempRegister; break; /讀取21開關(guān)A溫度 case 0x52: *tempData = TempRegister; break; /讀取21開關(guān)A溫度 case 0x53: *tempData = TempRegister; break; /讀取21開關(guān)周邊溫度 case 0x54: *tempData = TempRegister; break; /讀取22開關(guān)A溫度 case 0x55: *tempData = TempRegister; break; /讀取22開關(guān)A溫度 case 0x56: *tempData = TempRegister; break; /讀取22開關(guān)A溫度 case 0x57: *tempData = TempRegister; break; /讀取22開關(guān)周邊溫度 case 0x58: *tempData = TempRegister; break; /讀取23開關(guān)A溫度 case 0x59: *tempData = TempRegister; break; /讀取23開關(guān)A溫度 case 0x5a: *tempData = TempRegister; break; /讀取23開關(guān)A溫度 case 0x5b: *tempData = TempRegister; break; /讀取23開關(guān)周邊溫度 case 0x5c: *tempData = TempRegister; break; /讀取24開關(guān)A溫度 case 0x5d: *tempData = TempRegister; break; /讀取24開關(guān)A溫度 case 0x5e: *tempData = TempRegister; break; /讀取24開關(guān)A溫度 case 0x5f: *tempData = TempRegister; break; /讀取24開關(guān)周邊溫度 case 0x60: *tempData = TempRegister; break; /讀取25開關(guān)A溫度 case 0x61: *tempData = TempRegister; break; /讀取25開關(guān)A溫度 case 0x62: *tempData = TempRegister; break; /讀取25開關(guān)A溫度 case 0x63: *tempData = TempRegister; break; /讀取25開關(guān)周邊溫度 case 0x64: *tempData = TempRegister; break; /讀取26開關(guān)A溫度 case 0x65: *tempData = TempRegister; break; /讀取26開關(guān)A溫度 case 0x66: *tempData = TempRegister; break; /讀取26開關(guān)A溫度 case 0x67: *tempData = TempRegister; break; /讀取26開關(guān)周邊溫度 case 0x68: *tempData = TempRegister; break; /讀取27開關(guān)A溫度 case 0x69: *tempData = TempRegister; break; /讀取27開關(guān)A溫度 case 0x6a: *tempData = TempRegister; break; /讀取27開關(guān)A溫度 case 0x6b: *tempData = TempRegister; break; /讀取27開關(guān)周邊溫度 case 0x6c: *tempData = TempRegister; break; /讀取28開關(guān)A溫度 case 0x6d: *tempData = TempRegister; break; /讀取28開關(guān)A溫度 case 0x6e: *tempData = TempRegister; break; /讀取28開關(guān)A溫度 case 0x6f: *tempData = TempRegister; break; /讀取28開關(guān)周邊溫度 case 0x70: *tempData = TempRegister; break; /讀取29開關(guān)A溫度 case 0x71: *tempData = TempRegister; break; /讀取29開關(guān)A溫度 case 0x72: *tempData = TempRegister; break; /讀取29開關(guān)A溫度 case 0x73: *tempData = TempRegister; break; /讀取29開關(guān)周邊溫度 case 0x74: *tempData = TempRegister; break; /讀取30開關(guān)A溫度 case 0x75: *tempData = TempRegister; break; /讀取30開關(guān)A溫度 case 0x76: *tempData = TempRegister; break; /讀取30開關(guān)A溫度 case 0x77: *tempData = TempRegister; break; /讀取30開關(guān)周邊溫度 case 0x78: *tempData = localAddr; break; /讀取設(shè)備地址 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*/*設(shè)置寄存器內(nèi)容函數(shù) */函數(shù)功能:根據(jù)寄存器地址設(shè)置相應(yīng)寄存器內(nèi)容/*/設(shè)置寄存器值 返回0表示成功uint16 setRegisterVal(uint16 addr,uint16 tempData)uint16 result = 0;uint16 tempAddr;tempAddr = addr & 0xfff;switch(tempAddr & 0xff) case 0x78: localAddr = tempData; break;/設(shè)置設(shè)備地址,設(shè)備地址初始值為0x01 case 0x79: BAUD = tempData; switch_BAUD(BAUD); break;/設(shè)置串口通信波特率,串口通信波特率寄存器值為 1:9600 , 2:14400 , 3:19200 ,初始值為1 case 0x7a: TEMP_Alert = tempData; break;/設(shè)置報警溫度上限,報警溫度值存儲方式:精確到0.1°C,以擴大10倍的數(shù)值放在寄存器里面,例如溫度值為100.1,則讀寫值為1001 case 0x7b: TempRegister = tempData; break;/設(shè)置年寄存器 case 0x7c: TempRegister = tempData; break;/設(shè)置月寄存器 case 0x7d: TempRegister = tempData; break;/設(shè)置日寄存器 case 0x7e: TempRegister = tempData; break;/設(shè)置時寄存器 case 0x7f: TempRegister = tempData; break;/設(shè)置分寄存器 case 0x80: TempRegister = tempData; break;/設(shè)置