The code example uses MPLAB® Code Configurator Melody CRC Driver to configure CRC using the standard CRC-16-CCITT settings and calculate the hardware CRC result. The hardware CRC computation is then compared with a software implementation to verify results. The result is displayed on the terminal.
- MPLAB® X IDE 6.20 or newer (https://www.microchip.com/MPLABXIDE)
- MPLAB® XC-DSC Compiler 3.10 or a newer compiler (https://www.microchip.com/xcdsc)
- MPLAB® Code Configurator (MCC) Plugin 5.5.1 or newer (https://www.microchip.com/mcc)
- MPLAB® Code Configurator (MCC) Core 5.7.1 or newer (https://www.microchip.com/mcc)
- MPLAB® Code Configurator (MCC) Melody 2.7.1 or newer (https://www.microchip.com/melody)
- MCC PIC24/dsPIC Devices 5.12.2 or newer (MCC Content Manager)
- CRC Driver 1.0.4 or newer (MCC Content Manager)
- CRC PLIB 1.0.1 or newer (MCC Content Manager)
- UART Driver 1.10.2 or newer (MCC Content Manager)
- UART PLIB 1.0.1 or newer (MCC Content Manager)
- Any terminal program, like MPLAB® Data Visualizer (https://www.microchip.com/datavisualizer) or Tera Term (https://ttssh2.osdn.jp/index.html.en)
- PKOB (PICkit On-Board) or MPLAB® PICkit™ 5 In-Circuit Debugger
- dsPIC33A Curiosity Development Board
- dsPIC33AK128MC106 DIM
-
Launch the MPLAB® Data Visualizer.
-
Find the correct COM Port from the list on the left and click the play button.
-
Select the "Send to Terminal" button.
-
Click on the settings icon next to the source dropdown in the input section.
-
Verify that the serial port settings match the following:
-
Launch Tera Term
-
Go to File -> New Connection.
-
Select the "Serial" option and select the correct COM Port from the dropdown menu.
-
Go to Setup -> Serial port and ensure that the settings match the following:
- Launch MPLAB® X IDE and load the dspic33a-crc-basic project.
- Build and program the device.
The following settings are used in the calculation of CRC values:
- CRC Order: Length (in bits) of the polynomial
- Polynomial: Generator polynomial used in the calculation of the CRC value. Many effective polynomials exist, but some of the most common are CRC-16-CCITT (0x1021) and CRC-32 (0x04C11DB7).
- Initial Value: The initial value of the CRC result. Can theoretically be anything, but traditionally is either zero (0x0000 for 16-bit or 0x00000000 for 32-bit) or -1 (0xFFFF for 16-bit or 0xFFFFFFFF for 32-bit).
- Final XOR Value: The value XOR'd with the CRC result after the calculation and any reverses. Can theoretically be anything, but traditionally is either zero (0x0000 for 16-bit or 0x00000000 for 32-bit) or -1 (0xFFFF for 16-bit or 0xFFFFFFFF for 32-bit).
- Shift Direction: The direction the data is shifted through the registers. Can start with the most significant bit (MSB) or the least significant bit (LSB)
- Reverse: The result of the CRC calculation can be reversed immediately after calculation. The value is true (perform reverse) or false (no reverse).
- Seed Input Method: Can be direct (which is just the initial value) or nondirect (the result of a CRC calculation where the initial value is the input).
- Seed Shift Direction: The direction the seed is shifted through the registers. Can start with the most significant bit (MSB) or the least significant bit (LSB).
For CRC-16-CCITT, the settings are as follows:
- CRC Order: 16
- Polynomial: 0x1021
- Initial Value: -1 (0xFFFF)
- Final XOR Value: 0 (0x0000)
- Shift Direction: MSB
- Reverse: No Reverse
- Seed Input Method: Direct
- Seed Shift Direction: MSB
Online calculators can be used to test different configurations and try different settings. Most developers compare results with an online calculator for comparison purposes. An example that was used in the development of this code example is the Online Calculator by Sven Reifegerste (Zorc).
Online Calculator:
Note: Number 8, Seed Shift Direction, is assumed to be MSB in this online calculator.
MCC Melody:
Note: The "Reverse CRC value" and "Final XOR Value" only work for the simulator, they will not be used in the firmware calculations.
The firmware function CRC_CalculationGet() is where the Reverse and Final XOR Value settings are implemented.
For this code example, the calculation performed by the MCC Melody CRC Driver can be replicated with the online calculator by the following steps:
- Select "CRC-CCITT" button.
- Enter the folowing for "data sequence": %6c%93
- Click the "compute" button.
The result should be 0xE092, matching the calculation performed by the MCC Melody CRC Driver.
Once the project is built and the device is programmed, the terminal program will print the results of both the hardware and software calculations.