DS18S20 CRC predictor

I have coded up the CRC diagram to work out a test vector. Do a view source to see the code.

The code below predicts the CRC byte value


var b = new Array()
var newb = new Array()

var A,ip;

b[0] = 0;
b[1] = 0;
b[2] = 0;
b[3] = 0;
b[4] = 0;
b[5] = 0;
b[6] = 0;
b[7] = 0;

function crc( ip ) {

  a = b[0] ^ip

  newb[ 2 ] =  a ^ b[3]
  newb[ 3 ] =  a ^ b[4]

  b[0] = b[1]
  b[1] = b[2]
  b[2] = a ^ b[3]
  b[3] = a ^ b[4]
  b[4] = b[5]
  b[5] = b[6]
  b[6] = b[7]
  b[7] = a

  var opStr= " " + b[7] + " " + b[6] + " " + b[5] + " " + b[4] + " " + b[3] + " " + b[2] + " " + b[1] + " " + b[0] + "
" var opNum= ( b[7]*128 + b[6]*64 + b[5]*32 + b[4]*16 + b[3]*8 + b[2]*4 + b[1]*2 + b[0] ) self.document.write( " 0x "+opNum.toString(16) + opStr ) } crc(1) crc(0) crc(1) crc(0) crc(1) crc(0) crc(1) crc(0) crc(1) crc(1) crc(1) crc(1) crc(1)
This predicts the sequence.

The PIC code is:


;--------------------------------------------------------------------------
; Sec 4.2 	Main Program Init Code
;--------------------------------------------------------------------------


	cblock
CRCcrc
CRCa
	endc

CRCts

;  CRC polynomial for DS18S20 page 8 of 23  
;                                                     
;   a > [8] > [7] > [6] > [5] > [4]  + a > [3]  + a > [2] > [1] > [0] + i/p > a
;                         

; ip is in bit 0
	; work out a = crc:bit0  xor  i/p 
	xorwf	CRCcrc,w
	andlw	0x01
	; fill w with 1s or 0s based on bit 0
	xorlw	0x01
	addlw	0xFF
	; store  A
	movwf	CRCa

	;
	; now prepare crc, shift CRC to new position, opens up bit 7 
	;

	rrf		CRCcrc,f

	;
	; work out bit3 xor a 
	; work out bit2 xor a 
	;
	
	movlw 	( 0<<7 | 1<<3  | 1<<2 )
	andwf	CRCcrc,w
	xorwf	CRCa,f

	
	;
	; update CRC but orint new values for bit 7 , bit 3 and bit2.
	;

	;
	;  zero 	bit 7, bit 3, bit 2 in CRC
	;
	movlw 	~( 1<<7 | 1<<3  | 1<<2 )
	andwf	CRCcrc,f

	;
	; mask of new values for bit 7, bit 3, bit 2 
	;
	movfw	CRCa	
	; add   bit 7, bit 3, bit 2 to CRC
	andlw	( 1<<7 | 1<<3  | 1<<2 )
	iorwf	CRCcrc,f

	return


Start

	movlw	0x00
	movwf	CRCcrc

	movlw	1
	call		CRCts
	movlw	0

	call		CRCts
	movlw	1
	call		CRCts
	movlw	0
	call		CRCts

	movlw	1
	call		CRCts
	movlw	0
	call		CRCts

	movlw	1
	call		CRCts
	movlw	0
	call		CRCts


	movlw	1
	call		CRCts
	movlw	1
	call		CRCts
	movlw	1
	call		CRCts
	movlw	1
	call		CRCts
	movlw	1
	call		CRCts
	movlw	1
	call		CRCts
	movlw	1

I single stepped the code and it followed the sequence predicted by this web page.