![]() ZBasic System Library
85
ZBasic Microcontrollers
Type
Function returning UnsignedInteger
Invocation
CRC16(data, count, crcPoly, crcInit, crcFlags)
Parameter
Method
Type
Description
data
ByRef
anyType
The data bytes to add to the CRC value.
count
ByVal
integral
The number of bytes to process.
crcPoly
ByVal
UnsignedInteger
The CRC polynomial to use.
crcInit
ByVal
UnsignedInteger
The initial value of the CRC.
crcFlags
ByVal
integral
Flag bits that control the CRC computation.
Discussion
This function computes the CRC value over a number of data bytes using a specified polynomial and
initial value. The values to use for the polynomial and the initial value depend on the style of CRC that
you need to generate. See the discussion below for further details. The flags parameter contains bits
that control aspects of the CRC computation as described in the table below.
Flag Values for the CRC Compuation
Constant
Hex
Binary
Description
zxCRCRefIn
&H01
xxxx xxx1
Each input data bytes will be reflected.
zxCRCRefOut
&H02
xxxx xx1x
The final CRC value will be reflected.
The remaining bits are reserved for future use and should always be zero.
In this context, the term reflection refers to reversing the order of the bits in a data item so that the most
significant becomes the least significant and vice versa. For a multi-byte data item, the bits in each byte
are reversed and the order of the bytes is reversed as well.
Although this function will typically be used to compute the CRC value for an entire block of data at once,
it may also be used in a byte-by-byte or data burst mode. To do so, you would pass the computed CRC
value from the previous iteration as the initial value. Note, however, that you shouldnt use the zxRefOut
flag bit in this case. Rather, if you need reflected output you would perform the bit reversal on the final
CRC value when you reach the end of the data stream. You can reverse the bit order of a 16-bit value by
using the following code fragment.
crc = MakeWord(FlipBits(HiByte(crc)), FlipBits(LoByte(crc)))
CRC algorithms can be described by a parametric model known as the RockSoft model (see
http://www.repairfaq.org/filipg/LINK/F_crc_v34.html#CRCV_005). This CRC implementation supports the
POLY, INIT, REFIN and REFOUT parameters of the model with WIDTH=16 and XOROUT=0. If
necessary, you can easily implement a non-zero XOROUT parameter by using the following code
fragment.
crc = crc Xor XorOutValue
The Rocksoft model parameters for commonly used CRC computations are given in the table below.
|