Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

CRC Calculation - need to know the CRC calculation procedure

Status
Not open for further replies.

mak_goel

Junior Member level 3
Joined
Nov 30, 2006
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,541
CRC Calculation

Dear all,

CRC calculation is required to append CRC check with below string to communicate at RS485 Link having modbus protocol.

For ex: String is 01H 03H 75H 30H 00H 12H.

Actualy i am in need to know the CRC calculation procedure to incorporate the same in 8051 coding (Assembly Language).

I have read many data on net but couldn't get some fruitfull results.


Waiting for your kind support......

Regards
Mak
 

Re: CRC Calculation

where is ........ support
 

Re: CRC Calculation

I went through this, too. The best help I found was in
https://www.modbus.org/docs/Modbus_over_serial_line_V1.pdf
... near the end there is 'C'-code for a CRC calculator.

Unfortunately, my need was for HCS08 assembly-code. For what it's worth, I have added it to this message.
Good luck.

Wade Hassler

;===============================================
; a 'Lookup-Table CRC Calculator' for Freescale 'S08
; MODBUS algorithm (poly=A001, initialize to FFFF)
; messages <256 chars
; no globals (except tables)
; adapted from
; "MODBUS over Serial Line V1.02" pg 43-44

;==========================================
; Calculate CRC16 of a string of bytes
; at entry:
; H:X-> string of characters
; A= length of string to be encoded
; at exit:
; H:X= CRC16
; A=?
CRC_INIT_VAL equ $FFFF ; spec says to initialize with this

;---- stack offsets for string-encode routine -----
CRCHigh_ set 1
CRCLow_ set 2
CRCLng_ set 3
CRCAdrH_ set 4
CRCAdrL_ set 5

Calc_CRC16:
;==== setup: performed once per calculation ===========
ais #-(CRCAdrL_) ; make space on stack for temporaries
sthx CRCAdrH_,sp ; save start-pointer
sta CRCLng_,sp ; save length-count
ldhx #CRC_INIT_VAL ; get initial-value and go

;==== loop: performed once per byte of message ========
CRCCalc_Loop:
sthx CRCHigh_,sp ; save current CRC at start of repeats
ldhx CRCAdrH_,sp ; point to next character
lda 0,x ; load next character
aix #1 ; increase address
sthx CRCAdrH_,sp ; save increased address
ldhx CRCHigh_,sp ; re-load CRC-so-far
bsr CRC16_Update ; put next character into CRC
dbnz CRCLng_,sp,CRCCalc_Loop ; repeat until finished

ais #(CRCAdrL_) ; fix stack before exit
rts

;=======================================
; Update CRC16 with one byte
; at entry
; HX = CRC so far
; A = byte to add to CRC
; at exit
; HX = updated CRC
; A = ?
;---- stack-offsets for byte-add routine ----
CRCInx_ set 1
CRCInp_ set 2
CRCHsf_ set 3
CRCLsf_ set 4

CRC16_Update:
ais #-(CRCLsf_) ; make room for temporaries
sta CRCInx_,sp ; temporarily save input here
sthx CRCHsf_,sp ; store CRCHigh,CRCLow

lda CRCInx_,sp ; get input byte
eor CRCLsf_,sp ; xor with CRCL
tax ; put index in X (H=0 : tables are 256-bytes long )
clrh ; this HX-pair is used for both lookups

lda CRCHsf_,sp ; merge in the low byte
eor CRCTbl_H,x
sta CRCLsf_,sp

lda CRCTbl_L,x ; get the high byte
sta CRCHsf_,sp

ldhx CRCHsf_,sp ; retrun the new CRC
ais #CRCLsf_ ; fix stack before leaving
rts

nolist

; Table of CRC values for high–order byte
CRCTbl_H:
fcb $00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40
fcb $01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41
fcb $01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41
fcb $00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40
fcb $01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41
fcb $00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40
fcb $00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40
fcb $01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41
fcb $01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41
fcb $00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40
fcb $00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40
fcb $01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41
fcb $00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40
fcb $01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41
fcb $01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41
fcb $00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40

; Table of CRC values for low–order byte
CRCTbl_L:
fcb $00, $C0, $C1, $01, $C3, $03, $02, $C2, $C6, $06, $07, $C7, $05, $C5, $C4, $04
fcb $CC, $0C, $0D, $CD, $0F, $CF, $CE, $0E, $0A, $CA, $CB, $0B, $C9, $09, $08, $C8
fcb $D8, $18, $19, $D9, $1B, $DB, $DA, $1A, $1E, $DE, $DF, $1F, $DD, $1D, $1C, $DC
fcb $14, $D4, $D5, $15, $D7, $17, $16, $D6, $D2, $12, $13, $D3, $11, $D1, $D0, $10
fcb $F0, $30, $31, $F1, $33, $F3, $F2, $32, $36, $F6, $F7, $37, $F5, $35, $34, $F4
fcb $3C, $FC, $FD, $3D, $FF, $3F, $3E, $FE, $FA, $3A, $3B, $FB, $39, $F9, $F8, $38
fcb $28, $E8, $E9, $29, $EB, $2B, $2A, $EA, $EE, $2E, $2F, $EF, $2D, $ED, $EC, $2C
fcb $E4, $24, $25, $E5, $27, $E7, $E6, $26, $22, $E2, $E3, $23, $E1, $21, $20, $E0
fcb $A0, $60, $61, $A1, $63, $A3, $A2, $62, $66, $A6, $A7, $67, $A5, $65, $64, $A4
fcb $6C, $AC, $AD, $6D, $AF, $6F, $6E, $AE, $AA, $6A, $6B, $AB, $69, $A9, $A8, $68
fcb $78, $B8, $B9, $79, $BB, $7B, $7A, $BA, $BE, $7E, $7F, $BF, $7D, $BD, $BC, $7C
fcb $B4, $74, $75, $B5, $77, $B7, $B6, $76, $72, $B2, $B3, $73, $B1, $71, $70, $B0
fcb $50, $90, $91, $51, $93, $53, $52, $92, $96, $56, $57, $97, $55, $95, $94, $54
fcb $9C, $5C, $5D, $9D, $5F, $9F, $9E, $5E, $5A, $9A, $9B, $5B, $99, $59, $58, $98
fcb $88, $48, $49, $89, $4B, $8B, $8A, $4A, $4E, $8E, $8F, $4F, $8D, $4D, $4C, $8C
fcb $44, $84, $85, $45, $87, $47, $46, $86, $82, $42, $43, $83, $41, $81, $80, $40
list
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top