Forum: Mikrocontroller und Digitale Elektronik PIC - CRC16-CCIT-FALSE- Checksumme


von Christian (Gast)


Lesenswert?

Hallo, ich habe einen dsPIC33ep256mu805 µC und studiere gerade auf der 
Homepage von Microchip
http://www.microchip.com/wwwproducts/en/dsPIC33EP256MU806
das CodeExample zum Hardware CRC Modul 
http://ww1.microchip.com/downloads/en/DeviceDoc/ce451_%20crc_generation.zip

Dieses Code-Example funktioniert bei mir gut, wenn ich allerdings den 
"Initial-Value" auf beliebige Werte verstelle (also die Variable mit dem 
Namen "prev_CRC") dann wird bei mir die checksum nicht richtig 
berechnet.

Normalerweise kann ich ja durch den initialwert bestimmen, was für Arten 
von CRC´s berechnet werden. z.B. CRC16-CCIT-FALSE mit dem initial Value 
von 0xFFFF. Und genau dieses CRC-CCIT-FALSE möchte ich berechnen.

Hier ist das CodeExample (zuerst Poste ich das Header-File, dann das C - 
File)....wenn jemand eine Ahnung hat, wie man aus diesem CRC-CCIT-ZERO 
(funktioniert) eine CRC-CCIT-FALSE (funktioniert nicht bei mir) machen 
kann, bitte melden,

Danke und LG Christian

HEADERFILE############################################################## 
######################################################################## 
######################################################################## 
######################################################################## 
############
1
/*******************************************************************************
2
  CRC API header file
3
4
  Company:
5
    Microchip Technology Inc.
6
7
  File Name:
8
    crc.h
9
10
  Summary:
11
    Declarations for CRC functions.
12
13
  Description:
14
    This header file consists of the declaration of the 
15
    CRC initialization function and also the checksum calculation function. 
16
*******************************************************************************/
17
/*******************************************************************************
18
Copyright (c) 2012 released Microchip Technology Inc.  All rights reserved.
19
20
Microchip licenses to you the right to use, modify, copy and distribute
21
Software only when embedded on a Microchip microcontroller or digital signal
22
controller that is integrated into your product or third party product
23
(pursuant to the sublicense terms in the accompanying license agreement).
24
25
You should refer to the license agreement accompanying this Software for
26
additional information regarding your rights and obligations.
27
28
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
29
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
30
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
31
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
32
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
33
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
34
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
35
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
36
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
37
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
38
*******************************************************************************/
39
#ifdef __cplusplus          // Provide C++ Compatability
40
extern "C"
41
{
42
    #endif
43
44
    // *****************************************************************************
45
    // *****************************************************************************
46
    // Section: Constants
47
    // *****************************************************************************
48
    // *****************************************************************************
49
    #define POLYLEN 0x000F  // Length of polynomial-1"
50
    #define POLY    0x1021  // Generator Polynomial
51
52
    // X^12 + X^5
53
    //#define POLY  0X0810
54
    // *****************************************************************************
55
    // *****************************************************************************
56
    // Section: Interface Routines
57
    // *****************************************************************************
58
    // *****************************************************************************
59
    uint16_t    CRC_ChecksumWord( uint16_t *, uint16_t, uint16_t );
60
    uint16_t    CRC_ChecksumByte( uint8_t *, uint8_t, uint16_t );
61
    void        CRC_Init( void );
62
63
    #ifdef __cplusplus      // Provide C++ Compatibility
64
}
65
66
#endif
67
68
/*******************************************************************************
69
 End of File
70
*/

SOURCEFILE############################################################## 
######################################################################## 
######################################################################## 
######################################################################## 
############
1
/*******************************************************************************
2
  ce451 main faunction
3
  
4
  Company:
5
    Microchip Technology Inc.
6
7
  File Name:
8
    crc_generation.c
9
10
  Summary:
11
    This file is used to call CRC APIs
12
13
  Description:
14
    The main.c is used to call the CRC Initialization function and function to calculate checksum.
15
 *******************************************************************************/
16
/*******************************************************************************
17
Copyright (c) 2012 released Microchip Technology Inc.  All rights reserved.
18
19
Microchip licenses to you the right to use, modify, copy and distribute
20
Software only when embedded on a Microchip microcontroller or digital signal
21
controller that is integrated into your product or third party product
22
(pursuant to the sublicense terms in the accompanying license agreement).
23
24
You should refer to the license agreement accompanying this Software for
25
additional information regarding your rights and obligations.
26
27
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
28
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
29
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
30
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
31
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
32
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
33
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
34
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
35
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
36
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
37
 *******************************************************************************/
38
// *****************************************************************************
39
// *****************************************************************************
40
// Section: Included Files
41
// *****************************************************************************
42
// *****************************************************************************
43
#include <xc.h>
44
#include <stdint.h>
45
#include "crc.h"
46
47
48
// Expected result depends on the contents of messageString[8] Array. If the content is changed
49
// the below CRC value may not suit. The below CRC checksum is for data 0x01 to 0x08(given).
50
51
#define EXPECTED_RESULT 0x76AC
52
53
// *****************************************************************************
54
// *****************************************************************************
55
// Section: File Scope or Global Constants
56
// *****************************************************************************
57
// *****************************************************************************
58
uint8_t messageString[8] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
59
uint16_t    crcResult=0;
60
/******************************************************************************
61
 * Function:        int main(void)
62
 *
63
 * PreCondition:    None
64
 *
65
 * Input:           None
66
 *
67
 * Output:          None
68
 *
69
 * Side Effects:    None
70
 *
71
 * Overview:        Main function
72
 *****************************************************************************/
73
#ifdef TEST_MODE
74
int ce_main(void)
75
#else
76
int main(void)
77
#endif
78
{
79
    uint16_t    prev_CRC = 0x0000;
80
  
81
    CRC_Init();
82
83
     Nop();
84
     Nop();
85
86
    crcResult = CRC_ChecksumByte( messageString, 8, prev_CRC );
87
#ifdef TEST_MODE
88
    if(crcResult == EXPECTED_RESULT )
89
        return 0;
90
    else
91
        return 1;
92
#else
93
    while( 1 );
94
#endif
95
}
96
97
/******************************************************************************
98
 * Function:        void CRC_Init(void)
99
 *
100
 * PreCondition:    None   
101
 *
102
 * Input:           None
103
 *                  
104
 * Output:          None
105
 *
106
 * Side Effects:    None
107
 *
108
 * Overview:        This function configures CRC module
109
 *****************************************************************************/
110
void CRC_Init( void )
111
{
112
113
    CRCCON1bits.CRCEN = 0;
114
    CRCXORL = 0x0000;
115
         Nop();
116
         Nop();
117
118
    CRCWDATL = 0x0000;
119
120
    CRCWDATH = 0x0000;   //  NOTE:Non-direct Initial value fed to CRCWDAT
121
    CRCXORL = POLY;             // Generator Polynomial // X^12 + X^5
122
    CRCXORH = 0x0000;
123
    CRCCON1bits.CRCISEL = 0;
124
    CRCCON2bits.PLEN = POLYLEN; // Length of polynomial-1"
125
    CRCCON2bits.DWIDTH = 7;
126
    CRCCON1bits.LENDIAN = 0x0;
127
    CRCCON1bits.CRCEN = 1;
128
}
129
130
/******************************************************************************
131
 * Function:        uint16_t CRC_ChecksumByte(uint8_t* data, uint8_t Number_of_bytes, uint16_t prev_CRC)
132
 *
133
 * PreCondition:    None
134
 *
135
 * Input:           data - Pointer to the first data byte for which CRC needs to be
136
 *                  calculated.
137
 *                  Number_of_bytess - Total number of bytes for which CRC needs to be
138
 *                  calculated.
139
 *                  prev_CRC - previous CRC result.
140
 *
141
 * Output:          CRCWDATL Register content that is the two byte checksum
142
 *
143
 * Side Effects:    None
144
 *
145
 * Overview:        Calculates the checksum and returns the value
146
 *****************************************************************************/
147
uint16_t CRC_ChecksumByte( uint8_t *data, uint8_t Number_of_bytes, uint16_t prev_CRC )
148
{
149
    uint8_t volatile    *dest = ( uint8_t * ) &CRCDATL;
150
151
  
152
    CRCWDATL = prev_CRC;
153
    IFS4bits.CRCIF=0;        //CRC status Flag is Cleared
154
    do
155
    {
156
       while( (0 == CRCCON1bits.CRCFUL) && (0 < Number_of_bytes) )
157
        {
158
            *dest = *data;
159
            data++;
160
            Number_of_bytes--;
161
        }
162
    } while( 0 < Number_of_bytes );
163
 CRCCON1bits.CRCGO = 1;
164
 CRCDATL = 0x0000;   /* Do this to shift the last word out of the CRC shift register */
165
 while( CRCCON1bits.CRCFUL == 1 );
166
    
167
168
   
169
        Nop();
170
        Nop();
171
        Nop();
172
        Nop();
173
        Nop();
174
        Nop();
175
  while(CRCCON1bits.CRCMPT!=1);
176
177
        Nop();
178
        Nop();
179
        Nop();
180
        Nop();
181
        Nop();
182
        Nop();
183
  while(IFS4bits.CRCIF!=1);
184
185
        Nop();
186
        Nop();
187
        Nop();
188
        Nop();
189
        Nop();
190
        Nop();
191
  CRCCON1bits.CRCGO=0;
192
        Nop();
193
        Nop();
194
        Nop();
195
        Nop();
196
        Nop();
197
    return ( CRCWDATL );
198
}
199
200
/*******************************************************************************
201
 End of File
202
 */

Beitrag #5478331 wurde von einem Moderator gelöscht.
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.