@LIB_NAME OneWire @LIB_NOTES @LIB_SHARED ' Code and data shared by more than one subroutine in this library @LOCAL ' Add Local (non-persistent) variables here @CODE ' Add any shared code here..... @END @MACRO_START OWCRC @TARGET 16 Bit OWCRC MACRO P1, P2 #IF (PRM_COUNT > 2) #ERROR "OWCRC - Too many parameters" #ELSE #IF (PRM_COUNT < 2) #ERROR "OWCRC - Too few parameters" #ELSE #IF(PRM_1 != BYTE_ARRAY_PTR) #ERROR "OWCRC - OW_DATA(Param1) Should be a BYTE array pointer" #ENDIF #IF(PRM_2 != Byte) && (PRM_2 != NUM8) && (PRM_2 != NUM16) && (PRM_2 != NUM32) #ERROR "OWCRC - OW_LEN(Param2) Should be a Byte variable or number" #ENDIF #IF (PRM_1 == BYTE_ARRAY_PTR) @ NUM_FSR1 P1 #ENDIF #IF (PRM_2 == Byte) BYTE_BYTE P2, OW_Len #ENDIF #IF (PRM_2 == NUM8) NUM_BYTE P2, OW_Len #ENDIF #IF (PRM_2 == NUM16) NUM_BYTE P2, OW_Len #ENDIF #IF (PRM_2 == NUM32) NUM_BYTE P2, OW_Len #ENDIF GoSub Get_OW_Calc #ENDIF #IF (OWCRC_RETURN != 1) #ERROR "OWCRC - Mandatory return parameter misssing" #ELSE #IF(RETURN_TYPE !=Byte) && (RETURN_TYPE !=Word) && (RETURN_TYPE !=DWord) #ERROR "OWCRC - Return variable should be a Byte variable or number" #ELSE #IF (RETURN_TYPE == Byte) BYTE_BYTE OW_CRC_Calc, RETURN_VAR #ENDIF #ENDIF #ENDIF #ENDIF ENDM @LOCAL 'Add Local (non-persistent) variables here Dim OW_CRC_Calc As Byte Dim OW_Len As Byte Dim OW_Byte_Ptr As Byte Dim OW_TmpDat As Byte Dim OW_Idx As Byte Dim OW_TestByte As Byte @CODE Get_OW_Calc: OW_CRC_Calc = 0 ' Set up initial CRC value For OW_Byte_Ptr = 0 To OW_Len -1 ' Run through all the data bytes OW_TmpDat = POSTINC1 ' Get the next data byte ASM movlw 8 movwf OW_Idx ; Do this for all 8 bits of CRCByte OW_CRCLOOP movf OW_CRC_Calc,0 ; Put CRC into W xorwf OW_TmpDat,0 ; XOR CRC with CRCData movwf OW_TestByte ; Put results in TestByte rrcf OW_TestByte,1 ; Shift TestByte.0 into Carry bit btfss STATUS,C ; If Carry = 1, account for EXOR feedback gates GoTo OW_CRCSHIFT ; Otherwise just shift CRC and CRCByte movf OW_CRC_Calc,0 ; Get latest copy of CRC xorlw 0x18 ; XOR with 18Hex movwf OW_CRC_Calc ; Keep a copy of results OW_CRCSHIFT rrcf OW_CRC_Calc,1 ; Rotate CRC incorporating carry rrcf OW_TmpDat,1 ; Rotate CRCData, carry shifts in but not important decfsz OW_Idx,1 ; Keep track of #times through loop GoTo OW_CRCLOOP ; back to start if not done all 8 bits ENDASM Next ' Last byte is the received CRC value should Return ' result in a CRC of 0. @END @MACRO_START OWCRC @TARGET 14 Bit OWCRC MACRO P1, P2 #IF (PRM_COUNT > 2) #ERROR "OWCRC - Too many parameters" #ELSE #IF (PRM_COUNT < 2) #ERROR "OWCRC - Too few parameters" #ELSE #IF(PRM_1 != BYTE_ARRAY_PTR) #ERROR "OWCRC - OW_DATA(Param1) Should be a BYTE array pointer" #ENDIF #IF(PRM_2 != Byte) && (PRM_2 != NUM8) && (PRM_2 != NUM16) && (PRM_2 != NUM32) #ERROR "OWCRC - OW_LEN(Param2) Should be a Byte variable or number" #ENDIF #IF (PRM_1 == BYTE_ARRAY_PTR) @ NUM_FSR P1 #ENDIF #IF (PRM_2 == Byte) BYTE_BYTE P2, OW_Len #ENDIF #IF (PRM_2 == NUM8) NUM_BYTE P2, OW_Len #ENDIF #IF (PRM_2 == NUM16) NUM_BYTE P2, OW_Len #ENDIF #IF (PRM_2 == NUM32) NUM_BYTE P2, OW_Len #ENDIF GoSub Get_OW_Calc_C14 #ENDIF #IF (OWCRC_RETURN != 1) #ERROR "OWCRC - Mandatory return parameter misssing" #ELSE #IF(RETURN_TYPE !=Byte) && (RETURN_TYPE !=Word) && (RETURN_TYPE !=DWord) #ERROR "OWCRC - Return variable should be a Byte variable or number" #ELSE #IF (RETURN_TYPE == Byte) BYTE_BYTE OW_CRC_Calc, RETURN_VAR #ENDIF #ENDIF #ENDIF #ENDIF ENDM @LOCAL 'Add Local (non-persistent) variables here Dim OW_CRC_Calc As Byte Dim OW_Len As Byte Dim OW_Byte_Ptr As Byte Dim OW_TmpDat As Byte Dim OW_Idx As Byte Dim OW_TestByte As Byte @CODE Get_OW_Calc_C14: OW_CRC_Calc = 0 ' Set up initial CRC value For OW_Byte_Ptr = 0 To OW_Len -1 ' Run through all the data bytes OW_TmpDat = INDF ' Get the next data byte incf FSR STATUS.0 = STATUS.7 'if status.0 = 1 then status.7 = 1 ASM movlw 8 movwf OW_Idx ; Do this for all 8 bits of CRCByte OW_CRCLOOP movf OW_CRC_Calc,0 ; Put CRC into W xorwf OW_TmpDat,0 ; XOR CRC with CRCData movwf OW_TestByte ; Put results in TestByte rrcf OW_TestByte,1 ; Shift TestByte.0 into Carry bit btfss STATUS,C ; If Carry = 1, account for EXOR feedback gates GoTo OW_CRCSHIFT ; Otherwise just shift CRC and CRCByte movf OW_CRC_Calc,0 ; Get latest copy of CRC xorlw 0x18 ; XOR with 18Hex movwf OW_CRC_Calc ; Keep a copy of results OW_CRCSHIFT rrcf OW_CRC_Calc,1 ; Rotate CRC incorporating carry rrcf OW_TmpDat,1 ; Rotate CRCData, carry shifts in but not important decfsz OW_Idx,1 ; Keep track of #times through loop GoTo OW_CRCLOOP ; back to start if not done all 8 bits ENDASM Next ' Last byte is the received CRC value should Return @END @LIB_END