Jump to content

Sign in to follow this  

CRC16 speed and compact

Recommended Posts

Раз пошла такая пьянка ;), я делаю вот так - и быстро, и мало по памяти.

Для восьмой меги, для других - править конечный адрес FLASH


#include "iom8.h"

#include "inavr.h"

#include "pgmspace.h"


unsigned int CRC(void)


char __flash *p;

char h12=0;

char h0=0;

char h1;

char hl5;

char hh5;





h1=h12; //F E D C B A 9 8

h12=__swap_nibbles(h12); //B A 9 8 F E D C

hh5=h12; //B A 9 8 F E D C

h12^=h1; //FB EA D9 C8 BF AE 9D 8C

h12&=0xF0; //FB EA D9 C8 _ _ _ _

hh5&=0x0F; //_ _ _ _ F E D C

h1^=hh5; //F E D C FB EA D9 C8

hl5=h12; //FB EA D9 C8 _ _ _ _


unsigned int i=(hh5<<8)|hl5; //_ _ _ _ F E D C FB EA D9 C8 _ _ _ _


hh5=i>>8; //_ _ _ F E D C FB

hl5=(char)i; // EA D9 C8 _ _ _ _ _


h1^=hl5; //crclo // FEA ED9 DC8 C FB EA D9 C8

h12^=hh5; //crchi //FB EA D9 FC8 E D C FB



h0=h1^((unsigned int)p<0x1FFE?(*p):0);



while((unsigned int)p<0x2000);

return(((h12<<8)|h0)!=*(unsigned int __flash *)0x1FFE);



void main(void)


if (CRC())


printf_P((PGM_P)"Flash CRC error!");




Заточено специально для IAR - тест программной памяти. В опциях проекта (линкера) надо ставить:

Fill unused code memory

Generate checksum

Size 2 bytes

CRC16 (0x11021)

Complement As Is

Bit order: MSB first

Share this post

Link to post
Share on other sites
lichno pol'zoval v svoix project'ax neodnokratno


кстати, процедура для генерации этой таблицы такая:

void initcrc16tab(void)
      register  b, v, i;

      for( b = 0; b <= (1<<B)-1; ++b )
            for( v = b<<(W-B), i = B; --i >= 0; )
                  v = v&0x8000 ? (v<<1)^P : v<<1;
            crc16_table[b] = v;

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this