flashwrite.c

来自「picc_18v 8.35pl35 PIC18系列单片机开发C编译器」· C语言 代码 · 共 61 行

C
61
字号
#include <pic18.h>

void flash_write(far unsigned char * source_addr,unsigned char length,far unsigned char * dest_addr)
{
	/* variable declaration */
	unsigned char BUFFER[64];
	unsigned char index;
	unsigned char offset;

	offset=(unsigned char)dest_addr;
	while(offset>64)
		offset-=64;
	dest_addr-=offset;
	
	while(length)
	{
		// fill the 64 byte data buffer either from ...
		for(index=0;index<64;index++)
		{
			if((index>=offset)&&(length))	// specifed data area or ...
			{
				BUFFER[index]=*(source_addr++);
				length--;
			}
			else				// otherwise from destination sector.
				BUFFER[index]=*(dest_addr+index);
		}

		FLASH_ERASE(dest_addr);
	
// now begin to copy the buffer to the destination area
#if defined(SMALL_DATA)
		TBLPTRU=0;	// if 16 bit code pointers selected, TBLPTRU must be cleared manually
#endif
		TBLPTR=--dest_addr;	// load the destination address
		
		offset=0;
		for(index=0;index<64;index++)
		{
			
			TABLAT=BUFFER[index];	// copy the data buffer to the
			asm("\tTBLWT+*");	// internal write buffer	
			
			if(++offset==8)	// after every 8th byte, the
			{			// the write buffer is written to flash.
				EEPGD=1;WREN=1;		// this is the required sequence
				CARRY=0;if(GIE)CARRY=1;GIE=0;
				EECON2=0x55;
				EECON2=0xAA;
				WR=1;
				asm("\tNOP");
				if(CARRY)GIE=1;
				WREN=0;
				offset=0;
			}
		}
		dest_addr+=65;
	}
}

⌨️ 快捷键说明

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