PIC microcontroller CRC check program

Publisher:zukeq2009Latest update time:2012-10-30 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When using the PIC microcontroller to communicate with Dallas' iButtON, a subroutine was written to generate and check CRC.

#include

;-------------------------------

cblock 0x20

datBuff:8 ;assign 8 byte data buffer

bit_cnt ;bit counting

CRC_COUNT ;number of bytes for CRC

CRC_RESULT ;CRC resulr

crc_temp ;temporary data buffer during CRC

endc

;-------------------------------

#define skp0 btfsc

#define skp1 btfss

;===============================

org 0x000

movlw .7

movwf CRC_COUNT

movlw datBuff

movwf FSR ;FSR point to data buffer

call CRC_CHECK

movwf datBuff+7

goto $

;===============================

;CHECK THE CRC FOR ? BYTES DATA

;For the polynomial of X^8 + X^5 + X^4 + 1

;Derived from Dallas's iButton STandard

;Before calling, FSR = data buffer

; CRC_COUNT = number of bytes for CRC

CRC_CHECK ;~~~~~~~~~~~~~~~

clrf CRC_RESULT ;initialize the CRC buffer

_crc_00

movlw .8 ;number of bits for one byte

movwf bit_cnt ;set bit counter

movf INDF,w ;get one data byte

movwf crc_temp ;copy to temporary location

_crc_01

rrf crc_temp,f ;get LSB in C

skpc ;is this LSB=1?

goto _crc_02 ;go if LSB=0

movlw 0x01 ;do if LSB=1

xorwf CRC_RESULT,f

_crc_02

rrf CRC_RESULT,w ;get LSB of CRC

skpc ;test LSB

goto _crc_03 ;go if LSB=0

movlw 0x18 ;do if LSB=1

xorwf CRC_RESULT,f ;polynomial implementation

_crc_03

rrf CRC_RESULT,w ;whole byte right rotate

rrf CRC_RESULT,f

decfsz bit_cnt,f ;bit counting

goto _crc_01 ;go on until aa 8 bits done

incf FSR,f ;pointer update to next byte

decfsz CRC_COUNT,f ;byte counting

goto _crc_00 ;go on until all byte done

movf CRC_RESULT,w ;get CRC, Z set if CRC=0

return ;return with CRC in W END

Reference address:PIC microcontroller CRC check program

Previous article:PIC single-chip dual-machine asynchronous communication program
Next article:PIC microcontroller software programming skills

Recommended ReadingLatest update time:2024-11-16 17:48

The principle and implementation method of CRC check
1. Introduction to CRC Check         Cyclic Redundancy Check (CRC) is a commonly used checksum with error detection and correction capabilities. It is widely used in early communications. Cyclic Redundancy Check is often used for data verification of external storage and computer synchronous communication. Cyclic Redu
[Microcontroller]
The principle and implementation method of CRC check
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号