18.08.2013 Views

cache

cache

cache

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

การคํานวณ 16 bits CRC Checking<br />

Example C-Code<br />

1. Load คา 0xFFFF ลงใน 16 bit register(CRC register)<br />

2. ทําการ XOR Byte แรกของขอมูลที่อานมาดวย<br />

Low Byte ของ CRC register แลวเก็บผล<br />

ใน CRC register<br />

3. Shift 1 bit ของ CRC register ออกทางขวา แลวแทน MSB ดวย 0<br />

4. ถา bit ที่<br />

Shift ออกมาตามขอ 3 มีคาเปน 1 ใหทําการ XOR CRC register ดวย 0xA001<br />

5. ทําซ้ําในขั้นตอนที่<br />

3 และ 4 จนครบ 8 ครั้งเทากับจบการทดสอบ<br />

1 Byte<br />

6. ทําซ้ําในขั้นตอนที่<br />

2 ถึง 5 โดยใช 8 bit byte ตอไปของขอมูลจนครบ<br />

7. ทําการสลับ Low กับ High Byte ของ CRC register<br />

unsigned int CHECK_CRC(unsigned char *buff,unsigned char start,unsigned char len)<br />

{ // Function CRC FOR RTU PROTOCAL //<br />

unsigned char byte_count,bit_count;<br />

unsigned int crc_reg;<br />

unsigned int CRCHi,CRCLo;<br />

crc_reg=0xFFFF;<br />

for(byte_count=start;byte_count1; crc_reg=crc_reg^0xA001; }<br />

else { crc_reg=crc_reg>>1; } }<br />

}<br />

CRCLo=crc_reg>>8; CRCHi=crc_reg

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!