⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fletcher.c

📁 pic单片机例程
💻 C
字号:
#include <htc.h>

// Bounds of the checksum
#define START_ADDR	0
#define END_ADDR	0x1EFD
#define RESULT_ADDR	0x1F00

#if _FLETCHER == 8
 #define _FLETCHER8
unsigned int sumA;
unsigned int sumB;
unsigned int temp0;
unsigned int temp1;
unsigned int volatile result;
extern const unsigned int _checksum0;	// defined by driver
#elif _FLETCHER == 16
 #define _FLETCHER16
unsigned long sumA;
unsigned long sumB;
unsigned long temp0;
unsigned long temp1;
unsigned long volatile result;
extern const unsigned long _checksum0;	// defined by driver
#else
 #error symbol _FLETCHER must be defined as 8 or 16
#endif

unsigned int cptr;

void main(void){
	sumA=0;
	sumB=0;
	cptr=START_ADDR;
	temp0=0;
	temp1 = 1;
	do {
		temp1 = flash_read(cptr);
		#ifdef _FLETCHER8
			sumA += (temp1 & 0xFF);
			sumB += sumA;
			sumA += ((temp1>>8)&0xFF);
		#else
			sumA += temp1;
		#endif
		sumB += sumA;
		// If full Flecther Algorithm is implemented (overflow considered)
		/*
		if(++temp0 == 128){
			#ifdef _FLETCHER8
				sumA = (sumA & 0xFF) + ((sumA>>8)&0xFF);
				sumB = (sumB & 0xFF) + ((sumB>>8)&0xFF);
			#else
				sumA = (sumA & 0xFFFF) + ((sumA>>16)&0xFFFF);
				sumB = (sumB & 0xFFFF) + ((sumB>>16)&0xFFFF);
			#endif
			temp0 = 0;
		}
		*/
	} while((cptr++ < END_ADDR));
// If full Flecther Algorithm is implemented (overflow considered)
/*
if(temp0){
	#ifdef _FLETCHER8
		sumA = (sumA & 0xFF) + ((sumA>>8)&0xFF);
		sumB = (sumB & 0xFF) + ((sumB>>8)&0xFF);
	#else
		sumA = (sumA & 0xFFFF) + ((sumA>>16)&0xFFFF);
		sumB = (sumB & 0xFFFF) + ((sumB>>16)&0xFFFF);
	#endif
}
*/
#ifdef _FLETCHER8
	// If full Flecther Algorithm is implemented (overflow considered)
	// Repeat this just in case there was an overflow in the last addition
	/*
	sumA = (sumA & 0xFF) + ((sumA>>8)&0xFF);
	sumB = (sumB & 0xFF) + ((sumB>>8)&0xFF);
	*/
	result = (sumA & 0xFF) | ((sumB & 0xFF)<<8);
#else
	// If full Flecther Algorithm is implemented (overflow considered)
	// Repeat this just in case there was an overflow in the last addition
	/*
	sumA = (sumA & 0xFFFF) + ((sumA>>16)&0xFFFF);
	sumB = (sumB & 0xFFFF) + ((sumB>>16)&0xFFFF);
	*/
	result = (sumA & 0xFFFF) | ((sumB & 0xFFFF)<<16);
#endif

	if(result != _checksum0){
		while(1)
			asm("nop");	// failure
	}else{
		while(1)
			PORTB++;	// success
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -