📄 modbus16.s
字号:
.module modbus16.c
.area lit(rom, con, rel)
_auchCRCHi::
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.byte 1,192
.byte 128,'A
.byte 1,192
.byte 128,'A
.byte 0,193
.byte 129,64
.dbfile E:\我的网页\mcuc\download\M16-23板程序模板\新建文件夹\modbus16.c
.dbsym e auchCRCHi _auchCRCHi A[256:256]kc
_auchCRCLo::
.byte 0,192
.byte 193,1
.byte 195,3
.byte 2,194
.byte 198,6
.byte 7,199
.byte 5,197
.byte 196,4
.byte 204,12
.byte 13,205
.byte 15,207
.byte 206,14
.byte 10,202
.byte 203,11
.byte 201,9
.byte 8,200
.byte 216,24
.byte 25,217
.byte 27,219
.byte 218,26
.byte 30,222
.byte 223,31
.byte 221,29
.byte 28,220
.byte 20,212
.byte 213,21
.byte 215,23
.byte 22,214
.byte 210,18
.byte 19,211
.byte 17,209
.byte 208,16
.byte 240,48
.byte 49,241
.byte 51,243
.byte 242,50
.byte 54,246
.byte 247,55
.byte 245,53
.byte 52,244
.byte 60,252
.byte 253,61
.byte 255,63
.byte 62,254
.byte 250,58
.byte 59,251
.byte 57,249
.byte 248,56
.byte 40,232
.byte 233,41
.byte 235,43
.byte 42,234
.byte 238,46
.byte 47,239
.byte 45,237
.byte 236,44
.byte 228,36
.byte 37,229
.byte 39,231
.byte 230,38
.byte 34,226
.byte 227,35
.byte 225,33
.byte 32,224
.byte 160,96
.byte 'a,161
.byte 'c,163
.byte 162,'b
.byte 'f,166
.byte 167,'g
.byte 165,'e
.byte 'd,164
.byte 'l,172
.byte 173,'m
.byte 175,'o
.byte 'n,174
.byte 170,'j
.byte 'k,171
.byte 'i,169
.byte 168,'h
.byte 'x,184
.byte 185,'y
.byte 187,123
.byte 'z,186
.byte 190,126
.byte 127,191
.byte 125,189
.byte 188,124
.byte 180,'t
.byte 'u,181
.byte 'w,183
.byte 182,'v
.byte 'r,178
.byte 179,'s
.byte 177,'q
.byte 'p,176
.byte 'P,144
.byte 145,'Q
.byte 147,'S
.byte 'R,146
.byte 150,'V
.byte 'W,151
.byte 'U,149
.byte 148,'T
.byte 156,92
.byte 93,157
.byte 95,159
.byte 158,94
.byte 'Z,154
.byte 155,91
.byte 153,'Y
.byte 'X,152
.byte 136,'H
.byte 'I,137
.byte 'K,139
.byte 138,'J
.byte 'N,142
.byte 143,'O
.byte 141,'M
.byte 'L,140
.byte 'D,132
.byte 133,'E
.byte 135,'G
.byte 'F,134
.byte 130,'B
.byte 'C,131
.byte 'A,129
.byte 128,64
.dbsym e auchCRCLo _auchCRCLo A[256:256]kc
.area data(ram, con, rel)
.dbfile E:\我的网页\mcuc\download\M16-23板程序模板\新建文件夹\modbus16.c
_localAddr::
.blkb 1
.area idata
.byte 1
.area data(ram, con, rel)
.dbfile E:\我的网页\mcuc\download\M16-23板程序模板\新建文件夹\modbus16.c
.dbsym e localAddr _localAddr c
.area text(rom, con, rel)
.dbfile E:\我的网页\mcuc\download\M16-23板程序模板\新建文件夹\modbus16.c
.dbfunc e crc16 _crc16 fi
; uchCRCLo -> R20
; uchCRCHi -> R22
; uIndex -> y+0
; usDataLen -> R18,R19
; puchMsg -> R16,R17
.even
_crc16::
xcall push_gset2
sbiw R28,4
.dbline -1
.dbline 77
; #include "main.h"
;
; //字地址 0 - 255
; //位地址 0 - 255
;
; /* CRC 高位字节值表 */
; const uint8 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, 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 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
; } ;
;
; uint8 testCoil; //用于测试 位地址1
; uint16 testRegister; //用于测试 字地址0
;
; uint8 localAddr = 1; //地址
; uint8 sendCount; //发送字节个数
; uint8 receCount; //接收字节个数
; uint8 sendPosi; //发送位置
; uint8 sendBuf[32],receBuf[32]; //发送,接收缓冲区
; uint8 checkoutError; //校验结果
; uint8 receTimeOut; //接收超时
;
; uint16 crc16(uint8 *puchMsg, uint16 usDataLen)
; {
.dbline 78
; uint8 uchCRCHi = 0xFF ; /* 高CRC字节初始化 */
ldi R22,255
.dbline 79
; uint8 uchCRCLo = 0xFF ; /* 低CRC 字节初始化 */
ldi R20,255
xjmp L3
L2:
.dbline 82
.dbline 83
movw R30,R16
ld R2,Z+
movw R16,R30
clr R3
mov R4,R22
clr R5
eor R4,R2
eor R5,R3
movw R2,R4
clr R4
sbrc R3,7
com R4
clr R5
sbrc R4,7
com R5
movw R30,R28
std z+0,R2
std z+1,R3
std z+2,R4
std z+3,R5
.dbline 84
ldi R24,<_auchCRCHi
ldi R25,>_auchCRCHi
push R26
push R27
ldd R26,z+0
ldd R27,z+1
movw R30,R26
pop R27
pop R26
add R30,R24
adc R31,R25
lpm R30,Z
mov R22,R20
eor R22,R30
.dbline 85
ldi R24,<_auchCRCLo
ldi R25,>_auchCRCLo
movw R30,R28
push R26
push R27
ldd R26,z+0
ldd R27,z+1
movw R30,R26
pop R27
pop R26
add R30,R24
adc R31,R25
lpm R20,Z
.dbline 86
L3:
.dbline 81
; uint32 uIndex ; /* CRC循环中的索引 */
; while (usDataLen--) /* 传输消息缓冲区 */
movw R2,R18
subi R18,1
sbci R19,0
tst R2
breq X1
xjmp L2
X1:
tst R3
breq X2
xjmp L2
X2:
X0:
.dbline 87
; {
; uIndex = uchCRCHi ^ *puchMsg++ ; /* 计算CRC */
; uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
; uchCRCLo = auchCRCLo[uIndex] ;
; }
; return (uchCRCHi << 8 | uchCRCLo) ;
mov R16,R20
mov R17,R22
.dbline -2
L1:
adiw R28,4
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r uchCRCLo 20 c
.dbsym r uchCRCHi 22 c
.dbsym l uIndex 0 l
.dbsym r usDataLen 18 i
.dbsym r puchMsg 16 pc
.dbend
.dbfunc e beginSend _beginSend fV
.even
_beginSend::
.dbline -1
.dbline 92
; }//uint16 crc16(uint8 *puchMsg, uint16 usDataLen)
;
; //开始发送
; void beginSend(void)
; {
.dbline 93
; sendEnable(); //设为发送
sbi 0x18,7
.dbline 95
;
; sendPosi = 0;
clr R2
sts _sendPosi,R2
.dbline 96
; if(sendCount > 1)
ldi R24,1
lds R2,_sendCount
cp R24,R2
brsh L6
.dbline 97
; sendCount--;
mov R24,R2
subi R24,1
sts _sendCount,R24
L6:
.dbline 98
; UDR = sendBuf[0];
lds R2,_sendBuf
out 0xc,R2
.dbline -2
.dbline 99
; }//void beginSend(void)
L5:
.dbline 0 ; func end
ret
.dbend
.dbfunc e readCoil _readCoil fV
; result -> <dead>
; crcData -> R22,R23
; byteCount -> R20
; k -> R22
; exit -> y+4
; addr -> y+3
; bitCount -> R10
; position -> R12
; tempData -> y+0
; tempAddr -> y+2
; i -> R14
.even
_readCoil::
xcall push_gset5
sbiw R28,5
.dbline -1
.dbline 104
;
;
; //读线圈状态
; void readCoil(void)
; {
.dbline 114
; uint8 addr;
; uint8 tempAddr;
; uint8 byteCount;
; uint8 bitCount;
; uint16 crcData;
; uint8 position;
; uint8 i,k;
; uint8 result;
; uint16 tempData;
; uint8 exit = 0;
clr R0
std y+4,R0
.dbline 118
;
; //addr = (receBuf[2]<<8) + receBuf[3];
; //tempAddr = addr & 0xfff;
; addr = receBuf[3];
lds R2,_receBuf+3
std y+3,R2
.dbline 119
; tempAddr = addr;
mov R0,R2
std y+2,R0
.dbline 122
;
; //bitCount = (receBuf[4]<<8) + receBuf[5]; //读取的位个数
; bitCount = receBuf[5];
lds R10,_receBuf+5
.dbline 124
;
; byteCount = bitCount / 8; //字节个数
ldi R17,8
mov R16,R10
xcall div8u
mov R20,R16
.dbline 125
; if(bitCount%8 != 0)
ldi R17,8
mov R16,R10
xcall mod8u
tst R16
breq L11
.dbline 126
; byteCount++;
inc R20
L11:
.dbline 128
;
; for(k=0;k<byteCount;k++)
clr R22
xjmp L16
L13:
.dbline 129
; {//字节位置
.dbline 130
; position = k + 3;
mov R24,R22
subi R24,253 ; addi 3
mov R12,R24
.dbline 131
; sendBuf[position] = 0;
ldi R24,<_sendBuf
ldi R25,>_sendBuf
mov R30,R12
clr R31
add R30,R24
adc R31,R25
clr R2
std z+0,R2
.dbline 132
; for(i=0;i<8;i++)
clr R14
xjmp L20
L17:
.dbline 133
; {
.dbline 134
; getCoilVal(tempAddr,&tempData);
movw R18,R28
ldd R16,y+2
clr R17
xcall _getCoilVal
.dbline 136
;
; sendBuf[position] |= tempData << i;
ldi R24,<_sendBuf
ldi R25,>_sendBuf
mov R2,R12
clr R3
add R2,R24
adc R3,R25
mov R18,R14
clr R19
ldd R16,y+0
ldd R17,y+1
xcall lsl16
movw R30,R2
ldd R4,z+0
clr R5
or R4,R16
or R5,R17
std z+0,R4
.dbline 137
; tempAddr++;
ldd R0,y+2
inc R0
std y+2,R0
.dbline 138
; if(tempAddr >= addr+bitCount)
ldd R2,y+3
add R2,R10
cp R0,R2
brlo L21
.dbline 139
; { //读完
.dbline 140
; exit = 1;
clr R0
inc R0
std y+4,R0
.dbline 141
; break;
xjmp L19
L21:
.dbline 143
L18:
.dbline 132
inc R14
L20:
.dbline 132
mov R24,R14
cpi R24,8
brlo L17
L19:
.dbline 144
; }
; }
; if(exit == 1)
ldd R24,y+4
cpi R24,1
brne L23
.dbline 145
; break;
xjmp L15
L23:
.dbline 146
L14:
.dbline 128
inc R22
L16:
.dbline 128
cp R22,R20
brsh X3
xjmp L13
X3:
L15:
.dbline 148
; }
;
; sendBuf[0] = localAddr;
lds R2,_localAddr
sts _sendBuf,R2
.dbline 149
; sendBuf[1] = 0x01;
ldi R24,1
sts _sendBuf+1,R24
.dbline 150
; sendBuf[2] = byteCount;
sts _sendBuf+2,R20
.dbline 151
; byteCount += 3;
subi R20,253 ; addi 3
.dbline 152
; crcData = crc16(sendBuf,byteCount);
mov R18,R20
clr R19
ldi R16,<_sendBuf
ldi R17,>_sendBuf
xcall _crc16
movw R22,R16
.dbline 153
; sendBuf[byteCount] = crcData >> 8;
ldi R24,<_sendBuf
ldi R25,>_sendBuf
mov R30,R20
clr R31
add R30,R24
adc R31,R25
movw R2,R22
mov R2,R3
clr R3
std z+0,R2
.dbline 154
; byteCount++;
inc R20
.dbline 155
; sendBuf[byteCount] = crcData & 0xff;
mov R30,R20
clr R31
add R30,R24
adc R31,R25
movw R24,R22
andi R25,0
std z+0,R24
.dbline 156
; sendCount = byteCount + 1;
mov R24,R20
subi R24,255 ; addi 1
sts _sendCount,R24
.dbline 158
;
; beginSend();
xcall _beginSend
.dbline -2
.dbline 159
; }//void readCoil(void)
L8:
adiw R28,5
xcall pop_gset5
.dbline 0 ; func end
ret
.dbsym l result 1 c
.dbsym r crcData 22 i
.dbsym r byteCount 20 c
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -