bokee.net

电子/电气工程师博客

正文 更多文章

适合单片机的半字节查表的Crc16方法

 

                                      适合单片机的半字节查表的Crc16方法

                                         (wbymcs51.blog.bokee.net)

                                                        王佰营

       半字节查表的Crc16方法,占用FLASH的空间小,执行速度快,非常适用于单片机应用。

一、C语言程序(wbymcs51.blog.bokee.net)

unsigned int crc_table[16]=

{

    0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,

    0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,

};

 

unsigned int Crc16(unsigned char *ptr,  unsigned char len)

{

    unsigned int crc;

    unsigned char da;

 

    crc=0;

    while(len--!=0)

    {

           da=crc>>12;

           crc<<=4;

           crc^= crc_table [da^(*ptr/16)];

                             

           da=crc>>12;

           crc<<=4;

           crc^=crc_table[da^(*ptr&0x0f)];

           ptr++;

    }

    return(crc);

}

二、汇编语言程序(wbymcs51.blog.bokee.net)

;====================================================
;             fuction:  crc16_verify:数据校验   
;             input:     r2---要校验数据的字节数
;                        r0---需要校验数据存放缓存区首地址
;             output:   r4---CRC-H
;                        r5---CRC-L
;             usage:    acc,r0,r2,r4,r5,r6,r7,dptr
;             (wbymcs51.blog.bokee.net
;====================================================
crc16_verify:       ;数据校验;

              clr                 a
              mov              r4,                 a
              mov              r5,                 a
              ;mov             r0,                 #crc_buff
              ;mov             r2,                 #len

 

cv_start:
              mov              a,                  r4
              swap             a
              anl                a,                  #0fh
              mov              r6,                 a

              mov              a,                  r5
              swap             a
              mov              r7,                 a
              anl                a,                  #0fh
              xch                a,                  r7
              xrl                 a,                  r7
              mov              r5,                 a

              mov              a,                  r4
              swap             a
              anl                 a,                  #0f0h
              orl                 a,                  r7
              mov              r4,                 a

              mov              a,                  @r0
              swap             a
              anl                a,                  #0fh
              xrl                  a,                  r6
              add              a,                  acc
              mov              r7,                 a
              mov              dptr,              #crc16_tab
              movc             a,                  @a+dptr
              xrl                  ar4,               a

              inc                r7
              mov              a,                  r7
              movc             a,                  @a+dptr
              xrl                 ar5,               a

              mov              a,                  r4
              swap             a
              anl                 a,                  #0fh
              mov              r6,                 a

              mov               a,                  r5
              swap             a
              mov              r7,                 a
              anl                a,                  #0fh
              xch                a,                  r7
              xrl                  a,                  r7
              mov              r5,                 a

              mov              a,                  r4
              swap             a
              anl                 a,                  #0f0h
              orl                  a,                  r7
              mov              r4,                 a

              mov              a,                  @r0
              anl                a,                  #0fh
              xrl                  a,                  r6
              add              a,                  acc
              mov              r7,                 a

              mov              dptr,              #crc16_tab
              movc             a,                  @a+dptr
              xrl                  ar4,               a

              inc                 r7
              mov               a,                  r7
              movc             a,                  @a+dptr
              xrl                 ar5,               a

              inc                r0
              djnz              r2,                 cv_start


             TRAP_RET
crc16_tab:
       DW              00000H,01021H,02042H,03063H,04084H,050a5H,060c6H,070e7H
       DW              08108H,09129H,0a14aH,0b16bH,0c18cH,0d1adH,0e1ceH,0f1efH

,0e1ceH,0f1efH

分享到:

上一篇:如何在原路由器的基础上再扩展一台路由

下一篇:病床思

评论 (2条) 发表评论

发表评论
验证码