Hi,
I have a project which I was planning on using a CUBLOC 280CS but am considering switching to a PIC using Swordfish.
I struggled with some CRC calculation code which will be needed for my hardware. I have it working correctly on the CUBLOC hardware. I have tried converting it to swordfish and it comples OK but I have no idea if its syntax is correct, in fact I seriously doubt it is based upon the "hints" the compiler is giving me.
Could someone take a look and see if I'm even close? I don't have the hardware yet, not convinced I'm going with the PIC yet.
Working CUBLOC code (I don't know if it's in good form, but it works, be gentle):
Swordfish so farCode:' CRC-16 Calculation Const Device = CB220 Ramclear Wait 200 Dim CRC As Integer Dim InByte(9) As Byte Dim OutByte(2) As Byte Dim message(100) As Integer Dim MessageCount As Integer Dim j As Integer Dim i As Integer Dim LowB As Byte Dim HighB As Byte Dim ShiftBit As Byte Dim FINALCRC As Integer 'Test bytes for testing 'This will be the data passed to the function InByte(0) = 0xAA 'test input AA InByte(1) = 0x05 'test input "05" InByte(2) = 0x05 'test input "05" InByte(3) = 0x00 InByte(4) = 0x00 InByte(5) = 0x00 InByte(6) = 0x00 InByte(7) = 0x00 InByte(8) = 0x00 MessageCount = 2 'needs to be lengh of string to be CRCd CRC = 0xffff 'CRC seed per specification For j = 0 To MessageCount CRC = CRC Xor InByte(j) For i = 1 To 8 ShiftBit = (CRC And 1) CRC = ((CRC And 0xfffe)/2 ) If CRC <0 Then CRC = CRC + 0x8000 '32768 Dec. If ShiftBit = 1 Then CRC = CRC Xor 0x8408 Next Next CRC = Not CRC HighB = (CRC And 0xff) ' Mask hi bits LowB = ((CRC And 0xff00) /0x100) '256 Dec. - Mask low bits If HighB < 0 Then HighB = (HighB + 0x8000) HighB = (HighB And 0xff) FINALCRC.Byte0 = LowB 'swap low and high bytes FINALCRC.Byte1 = HighB '******************************************** '******************************************** ' This section for testing ' not part of final function ' sample data and known correct results: ' an input of "AA 05 05" should result in a CRC of "74 93" ' an Input of "AA 05 01" should result In a CRC of "50 D5" Dim MessageLength As Integer Dim z As Integer MessageLength = MessageCount + 1 Debug "Input =" For z = 0 To MessageCount Debug Hex InByte(z)," " Next Debug Cr, "Message Length = ", Hex MessageLength, Cr Debug "FINALCRC = ", Hex FINALCRC, Cr 'output '********************************************* '*********************************************
Thanks.Code:Dim CRC As Integer Dim InByte(9) As Byte Dim OutByte(2) As Byte Dim message(100) As Integer Dim MessageCount As Integer Dim j As Integer Dim i As Integer Dim LowB As Byte Dim HighB As Byte Dim ShiftBit As Byte Dim FINALCRC As Integer 'Test bytes for testing 'This will be the data passed to the function InByte(0) = $AA 'test input AA InByte(1) = $05 'test input "05" InByte(2) = $05 'test input "05" InByte(3) = $00 InByte(4) = $00 InByte(5) = $00 InByte(6) = $00 InByte(7) = $00 InByte(8) = $00 end MessageCount = 2 'needs to be lengh of string to be CRCd CRC = $ffff 'CRC seed per specification For j = 0 To MessageCount CRC = CRC Xor InByte(j) For i = 1 To 8 ShiftBit = (CRC And 1) CRC = ((CRC And $fffe)/2 ) If CRC <0 Then CRC = CRC + $8000 '32768 Dec. If ShiftBit = 1 Then CRC = CRC Xor $8408 Next Next CRC = Not CRC HighB = (CRC And $ff) ' Mask hi bits LowB = ((CRC And $ff00) /$100) '256 Dec. - Mask low bits If HighB < 0 Then HighB = (HighB + $8000) HighB = (HighB And $ff) FINALCRC.Byte0 = LowB 'swap low and high bytes FINALCRC.Byte1 = HighB

Reply With Quote