electronics forum

Rules | Recent posts | topic RSS | Search | Register  | Log in

CRC Calculation - need to know the CRC calculation procedure


Post new topic  Reply to topic    EDAboard.com Forum Index -> Microcontrollers -> CRC Calculation - need to know the CRC calculation procedure
Author Message
mak_goel



Joined: 30 Nov 2006
Posts: 30


Post28 May 2009 8:28   

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
Back to top
Google
AdSense
Google Adsense




Post28 May 2009 8:28   

Ads




Back to top
lehio



Joined: 12 Mar 2007
Posts: 7


Post28 May 2009 10:57   

Re: CRC Calculation


where is ........ support
Back to top
wade_hassler



Joined: 07 Aug 2008
Posts: 13
Helped: 3


Post28 May 2009 18:08   

Re: CRC Calculation


I went through this, too. The best help I found was in
http://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
Back to top
Arabic versionBulgarian versionCatalan versionCzech versionDanish versionGerman versionGreek versionEnglish versionSpanish versionFinnish versionFrench versionHindi versionCroatian versionIndonesian versionItalian versionHebrew versionJapanese versionKorean versionLithuanian versionLatvian versionDutch versionNorwegian versionPolish versionPortuguese versionRomanian versionRussian versionSlovak versionSlovenian versionSerbian versionSwedish versionTagalog versionUkrainian versionVietnamese versionChinese version
Post new topic  Reply to topic    EDAboard.com Forum Index -> Microcontrollers -> CRC Calculation - need to know the CRC calculation procedure
Page 1 of 1 All times are GMT + 1 Hour
Similar topics:
CRC calculation (4)
crc calculation (2)
Fast CRC calculation... (7)
CRC calculation problem (8)
crc calculation in matlab (2)
URGENT CRC Calculation required.............. (2)
10 POINTS for PROVING CRC...........CALCULATION (4)
Ethernet.....10 POINTS for PROVING CRC...........CALCULATION (1)
Hardware for Reverse CCITT CRC parallel calculation (1)
CRC 16 for DLLP and CRC 32 for LCRC and ECRC (2)


Abuse || Administrator || Moderators || Support us || sitemap
topic RSS