|
|||||||||||
| 技術(shù)交流 | 電路欣賞 | 工控天地 | 數(shù)字廣電 | 通信技術(shù) | 電源技術(shù) | 測(cè)控之家 | EMC技術(shù) | ARM技術(shù) | EDA技術(shù) | PCB技術(shù) | 嵌入式系統(tǒng) 驅(qū)動(dòng)編程 | 集成電路 | 器件替換 | 模擬技術(shù) | 新手園地 | 單 片 機(jī) | DSP技術(shù) | MCU技術(shù) | IC 設(shè)計(jì) | IC 產(chǎn)業(yè) | CAN-bus/DeviceNe |
請(qǐng)教:半字節(jié)查表的crc8該怎么寫? |
| 作者:hiberhe 欄目:單片機(jī) |
使用的是CCITT-8,生成多項(xiàng)式為X^8 + X^5 + X^4 + 1,由于code限制需要使用半字節(jié)查表法,但是怎么都和1字節(jié)查表的結(jié)果不對(duì). 全查表的代碼為: unsigned CHAR code CCITT_CRC8[256] = { 0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41, 0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e, 0x5f, 0x01, 0xe3, 0xbd, 0x3e, 0x60, 0x82, 0xdc, 0x23, 0x7d, 0x9f, 0xc1, 0x42, 0x1c, 0xfe, 0xa0, 0xe1, 0xbf, 0x5d, 0x03, 0x80, 0xde, 0x3c, 0x62, 0xbe, 0xe0, 0x02, 0x5c, 0xdf, 0x81, 0x63, 0x3d, 0x7c, 0x22, 0xc0, 0x9e, 0x1d, 0x43, 0xa1, 0xff, 0x46, 0x18, 0xfa, 0xa4, 0x27, 0x79, 0x9b, 0xc5, 0x84, 0xda, 0x38, 0x66, 0xe5, 0xbb, 0x59, 0x07, 0xdb, 0x85, 0x67, 0x39, 0xba, 0xe4, 0x06, 0x58, 0x19, 0x47, 0xa5, 0xfb, 0x78, 0x26, 0xc4, 0x9a, 0x65, 0x3b, 0xd9, 0x87, 0x04, 0x5a, 0xb8, 0xe6, 0xa7, 0xf9, 0x1b, 0x45, 0xc6, 0x98, 0x7a, 0x24, 0xf8, 0xa6, 0x44, 0x1a, 0x99, 0xc7, 0x25, 0x7b, 0x3a, 0x64, 0x86, 0xd8, 0x5b, 0x05, 0xe7, 0xb9, 0x8c, 0xd2, 0x30, 0x6e, 0xed, 0xb3, 0x51, 0x0f, 0x4e, 0x10, 0xf2, 0xac, 0x2f, 0x71, 0x93, 0xcd, 0x11, 0x4f, 0xad, 0xf3, 0x70, 0x2e, 0xcc, 0x92, 0xd3, 0x8d, 0x6f, 0x31, 0xb2, 0xec, 0x0e, 0x50, 0xaf, 0xf1, 0x13, 0x4d, 0xce, 0x90, 0x72, 0x2c, 0x6d, 0x33, 0xd1, 0x8f, 0x0c, 0x52, 0xb0, 0xee, 0x32, 0x6c, 0x8e, 0xd0, 0x53, 0x0d, 0xef, 0xb1, 0xf0, 0xae, 0x4c, 0x12, 0x91, 0xcf, 0x2d, 0x73, 0xca, 0x94, 0x76, 0x28, 0xab, 0xf5, 0x17, 0x49, 0x08, 0x56, 0xb4, 0xea, 0x69, 0x37, 0xd5, 0x8b, 0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4, 0x95, 0xcb, 0x29, 0x77, 0xf4, 0xaa, 0x48, 0x16, 0xe9, 0xb7, 0x55, 0x0b, 0x88, 0xd6, 0x34, 0x6a, 0x2b, 0x75, 0x97, 0xc9, 0x4a, 0x14, 0xf6, 0xa8, 0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7, 0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35 }; unsigned CHAR GetCRC8(unsigned CHAR inbyte,unsigned CHAR crc) { crc = CCITT_CRC8_DATA[inbyte ^crc]; } 半字節(jié)查表應(yīng)該怎么寫呢? unsigned CHAR code CCITT_CRC8_NIBBLE[16] = { 0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41 }; unsigned CHAR GetCRC8_Nibble(unsigned CHAR inbyte,unsigned CHAR crc) { // 這兒如何寫???我寫的總是和上面的計(jì)算結(jié)果不同 } 看過一篇"CRC算法及C語(yǔ)言實(shí)現(xiàn)",上面主的是CRC16的半字節(jié)查表算法,我根據(jù)原理進(jìn)行推導(dǎo),可結(jié)果就是不對(duì). 請(qǐng)知道的講解一下吧! |
| 2樓: | >>參與討論 |
| 作者: hiberhe 于 2005/3/3 21:51:00 發(fā)布:
知道的幫個(gè)忙吧,謝謝! |
|
| 3樓: | >>參與討論 |
| 作者: freebirds 于 2005/3/3 23:45:00 發(fā)布:
這個(gè)應(yīng)該能看懂 http://soft.laogu.com:20038/download/crcver.pdf |
|
| 4樓: | >>參與討論 |
| 作者: hiberhe 于 2005/3/4 10:46:00 發(fā)布:
CRC算法及C語(yǔ)言實(shí)現(xiàn) 更容易看懂一些,不過寫出來的還是不對(duì)! unsigned CHAR code CCITT_CRC8_NIBBLE[16] = { 0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41 }; unsigned CHAR GetCRC8_Nibble(unsigned CHAR inbyte,unsigned CHAR crc) { // 這兒如何寫???我寫的總是和上面的計(jì)算結(jié)果不同 // 下面的寫法不對(duì)吧!!!!!! unsigned CHAR k; unsigned CHAR crc = 0; unsigned CHAR TEMP; for (k = 0; k < ucLen; k++) { TEMP = crc>>4;// crc右移4位(得到高半字節(jié)) crc <<= 4;// crc左移4位(低半字節(jié)) crc ^= CCITT_CRC8_NIBBLE[TEMP^(pBuffer][k]>>4)]; TEMP = crc>>4;// crc右移4位(得到高半字節(jié)) crc <<= 4;// crc左移4位(低半字節(jié)) crc^= CCITT_CRC8_NIBBLE[TEMP^(pBuffer][k]&0x0f)]; } return crc; } 請(qǐng)指教上面錯(cuò)在哪兒? |
|
| 5樓: | >>參與討論 |
| 作者: hiberhe 于 2005/3/4 19:15:00 發(fā)布:
找到一個(gè)例子,不過看不懂,改成CCITT-8也不對(duì) ;Firmware CRC-8 implementations for SMBus ;The code samples are based on a generic MOTOROLA 6805 MCU architecture and instruction set. ;Cx = X8 + X2 + X + 1 table $00 ;entry 0 $07 $0E $09 $1C $1B $12 $15 $38 $3F $36 $31 $24 $23 $2A $2D ;entry 15 * This code performs a table lookup to determine the new CRC VALUE for each byte received * Registers used: * Accumulator A * MEMORY register CRC * Temporary storage in_temp * * Input: MEMORY register CRC contains the previously calculated CRC VALUE * Accumulator A contains the input data byte * OUTPUT: MEMORY register CRC contains the current CRC VALUE init_byte: sta in_temp [4] ; store the two nibbles in a temporary storage high_nibble: * create the table index for the most significant nibble * A <- (MS nibble of input byte ^ MS nibble of CRC) eor CRC [4] ; get the XOR of the most significant nibble with CRC into ACC tax [2] ; prepare index register to point to the right table entry lsrx [3] ; shift contents of X 4 times to the right to obtain high nibble lsrx [3] ; table index. Fill the vacant bits with zeros lsrx [3] ; some MCUs have more efficient instructions to perform this lsrx [3] ; table index is now into X * calculate the new CRC VALUE: A <- (previous CRC) ^ table(index) lda CRC [4] ; retrieve the previous CRC VALUE mul #$10 [11] ; shift CRC VALUE left four positions eor table,x [5] ; get the new CRC VALUE. ACC contains now the new CRC sta CRC [4] ; save the new CRC VALUE low_nibble: * create the table index for the least significant nibble * A <- ( LS nibble of input byte ^ MS nibble of CRC) lsra [3] ; shift contents of ACC 4 times to the right to obtain high nibble lsra [3] ; of CRC. Fill the vacant bits with zeros lsra [3] ; some MCUs have more efficient instructions for this lsra [3] ; CRC high nibble is now into ACC eor in_temp [4] ; get the XOR of the high CRC nibble with the low nibble of input and #$0F [2] ; mask high bits (next nibble in the message) tax [2] ; prepare index register * calculate the new CRC VALUE: A <- (previous CRC) ^ table(index) lda CRC [4] ; get previous CRC VALUE mul #$10 [11] ;shift CRC VALUE left 4 positions eor table,x [5] ; get new CRC VALUE sta CRC [4] ; save it into the CRC register |
|
| 6樓: | >>參與討論 |
| 作者: freebirds 于 2005/3/9 0:10:00 發(fā)布:
你所列的余式表和多項(xiàng)式并不一致 多項(xiàng)式應(yīng)該是0x31才多,那么表中數(shù)據(jù)和你所提到的多項(xiàng)式并不一致。 這個(gè)沒關(guān)系,就假定采用多項(xiàng)式0x5e。那你的程序的思路應(yīng)該沒有錯(cuò),是這樣求的。 不過GetCRC8_Nibble的參數(shù)沒有用到inbyte,有點(diǎn)看不明白。 |
|
| 7樓: | >>參與討論 |
| 作者: jaygong 于 2005/3/9 10:33:00 發(fā)布:
試試我寫的! unsigned CHAR crc8(unsigned CHAR *pDst,unsigned CHAR length) { unsigned CHAR i,j,aa,check,flag; check=0; for (i=0;i<length;i++) { aa=*pDst++; for(j=0;j<8;j++) { flag=(check^aa)&1; check>>=1; if(flag){check^=0x0c;check|=0x80;} aa>>=1; } } return check; } |
|
| 8樓: | >>參與討論 |
| 作者: hiberhe 于 2005/3/10 12:19:00 發(fā)布:
你沒看明白我的意思,我需要是查表的,直接算速度來不及。 我想要是半字節(jié)查表的 |
|
|
|
| 免費(fèi)注冊(cè)為維庫(kù)電子開發(fā)網(wǎng)會(huì)員,參與電子工程師社區(qū)討論,點(diǎn)此進(jìn)入 |
Copyright © 1998-2006 m.58mhw.cn 浙ICP證030469號(hào) |