Arlet wrote: many thanx, i am going to test your code.
here is my first try with atMega
;** R0 tempory use ;** R16 counts Bits of a Byte ;** R17 counts Bytes of txbuf ;** R18:R19 contains polynom ;** R20:R21 CRC data ;** R30:R31 Z Register points to txbuf
crcgen: ldi r17,low(txend-txbuf+1) ;r16 rxbuf length ldi zl,low(txbuf) ; Z points to txbuf start ldi zh,high(txbuf) ldi r18,0x01 ldi r19,0xa0 ;r18:r19 = CRC Polynom = 0xa001 ldi r20,0xff ldi r21,0xff ;r20:r21 is CRC register 1) ld r0,z+ ;get first Byte from txbuf
bytlop: eor r20,r0 ;exor crc lowbyte with next data byte ldi r16,8+1 ;count 8 bits per byte bitlop: dec r16 ;one bit has been processed breq nbyte ;-> byte is complete lsl r20 ;shift 16 bit CRC Register rol r21 brcc bitlop ;-> shift continius if C=0 eor r20,r18 ;if Carry = 1 eor r21,r19 ;exor with 16 bit Polynom rjmp bitlop ;repeat for all 8 bits
nbyte: ld r0,z+ ;fetch next byte from rxbuf dec r17 ;one byte has been processed brne bytlop ;-> until all Bytes processed ret ;CRC should be in R20:R21 now
Assume txbuf contains the 6 byte modbus rtu message: 1,3,0,0,0,2 the crc should be 0xc40b (what I can see in a old trace record) but my code calculates 0x98e8 for crc result. Any bugs obvious?