📄 crc16.c
字号:
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f020.h> // SFR declarations
#include <stdio.h>
#include <string.h>
#include <intrins.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F02x
//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0 = 0xbe; // ADC0 data
sfr16 ADC0GT = 0xc4; // ADC0 greater than window
sfr16 ADC0LT = 0xc6; // ADC0 less than window
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 T2 = 0xcc; // Timer2
sfr16 RCAP4 = 0xe4; // Timer4 capture/reload
sfr16 T4 = 0xf4; // Timer4
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd5; // DAC1 data
unsigned int crc;
unsigned char test[6]={0x01,0x03,0x00,0x00,0x00,0x02};
unsigned char *pp;
unsigned char temp,temp2;
unsigned int crc16(unsigned int num);
void SYSCLK_Init (void);
void main(void)
{
WDTCN = 0xde; // disable watchdog timer
WDTCN = 0xad;
SYSCLK_Init ();
crc=crc16(0x0006);
while(1);
}
unsigned int crc16(unsigned int num)
{
int i,j;
unsigned int c,crc=0xFFFF;
for (i=0;i<num;i++)
{
c=test[i] & 0x00FF;
crc^=c;
for(j=0;j<8;j++)
{
if (crc & 0x0001)
{
crc>>=1;
crc^=0xA001;
}
else
{
crc>>=1;
}
}
}
return(crc);
}
void SYSCLK_Init (void)
{
int i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 22.1184MHz crystal
for (i=0; i < 256; i++) ; // wait for crystal osc. to start up
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -