simon said:
Hello all, can anyone point me in the right direction please?
I need to individually address 1 - 60 nodes up to 100 metres over a 485
connection. The nodes must be addressable, should respond to 4 commands
and in turn, each node must be able to pass back 4 data bits back to the
master controller.
I can handle the hardware OK. I'm after (hopefully PIC based) public
domain code with error checking that can do what I need. The system
needs to run up to 9600 bits/sec. Although I could attempt to write a
program to do the above, time is running out on this one and my
understanding of CRC is not that good. I need code that is robust, tried
and tested.
Can anyone advise please?
Cheers,
Simon
;************************************************************************
; crc The 8 bit, table_driven, CRC * *
;*********************************** *
; Adapted from a 32 bit, byte-at-a-time table driven version. *
; *
; If used on a "receive" stream then Z is also returned i.e. Z == true *
; means that W == 0 (it's easier for the caller to test Z than test W) *
; *
; Algorithm: r = 0; *
; while (--len) { *
; r = t [ r ^ (* p++)]; *
; } *
; *
; Entry: msgLength // in bytes *
; msgBufPtr // start address *
; crc_table[256] *
; *
; Uses: temp *
; byte_counter *
; FSR *
; INDF *
; *
; Exit W == crc *
; Z == true | false *
;************************************************************************
constant polynomial = H'41'
Crc macro
clrf temp
movf msgLength,w
movwf byte_counter
movf msgBufPtr,w
movwf FSR
crc_10
movf INDF,w ; w = next message byte
xorwf temp,w ; w = index
call crc_table ; w = crc_table[index]
movwf temp ; reg = "remainder in progress"
incf FSR,f
decfsz byte_counter,f
goto crc_10
addlw D'0' ; affect Z flag
return ; Exit, W == CRC, Z == true | false
crc_table
jumper
Crc_tab polynomial
endm
;************************************************************************
; crc_table 256 * 8 bit table for the 8 bit, table_driven, CRC * *
;************************************************************************
; This macro creates a 256 byte table required by the 8 bit table-driven*
; crc algorithm above. For good information about CRCs see: *
; ftp://ftp.rocksoft.com/papers/crc_v3.txt *
; *
; !!!This table takes twenty seconds to preprocess/compile!!! *
; *
; Algorithm:int const poly(<parameter>); *
; for(int i=0; i<256; ++i){ *
; int temp = i; *
; for(int b=0; b<8; ++b){ *
; temp <<= 1; *
; if(temp & 0x100) temp ^= poly *
; } *
; insert_table(i,(temp % 8)); *
; } *
; *
; Expects: key_value // the polynomial *
; *
; Usage: crc_tab <eight bit polynomial> *
;************************************************************************
Crc_tab macro poly_value
poly = poly_value
bit_count = D'8'
index = 0
msk = H'100' ; (bit 9 of 32 bit integer)
while (index < D'256')
i_copy = index
while (bit_count)
i_copy <<= 1 ;
if (i_copy & msk) ; test ninth bit
i_copy ^= poly
endif
bit_count -= 1
endw
retlw i_copy % D'256'
bit_count = 8
index += 1
endw
endm