Modbus 8051 Code (ASM)

Status
Not open for further replies.

DwigB

Newbie level 1
Joined
Apr 2, 2015
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
10
I have done some exhausted search for ASM code for implementing MODBUS CRC calculation. I finally gave up and developed this simple code that works.


Code ASM - [expand]
1
2
3
4
5
6
; The registrars where the CRC results will reside have to be set to 0xFFFF. 
 
CRC16_Init:                             ; Reset CRC Registar
        MOV D_CRC16_Hi, #0FFh
        MOV D_CRC16_Lo,#0FFh
        RET

Once this is done the string of bytes to be transmitted are processed by calling the routine below.


Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
CRC16_AddCalc:                     ; Calc CRC
        PUSH R_0                       ; Save Reg 0
        PUSH Acc                       ; Save Acc
        MOV     R0,#08h                 ; 8 Bits
        XRL     D_CRC16_Lo,A         ; Lo ^=Data
L?P1:
        CLR     C                         ; Clear Carry
        MOV     A,D_CRC16_Hi        ; D_CRC << 1
        RRC     A                         ; Shift Left
        MOV     D_CRC16_Hi,A        ; Store Back
        MOV     A,D_CRC16_Lo        ; Get Hi Byte
        RRC     A                         ; Shift Left
        MOV     D_CRC16_Lo,A        ; Store Back
        JNC     L?P2                      ; Skip if Bit 15 wasn't set
        XRL     D_CRC16_Hi,#0A0h   ; XOR in Polynomial High
        XRL     D_CRC16_Lo,#01h     ; XOR in Polynomial Lo
L?P2:
        DJNZ    R0,L?P1
        POP     Acc
        POP     R_0
        RET

 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…