picc_18v 8.35pl35 PIC18系列单片机开发C编译器

源代码在线查看: flashwrite.c

软件大小: 3991 K
上传用户: zhangpeng
关键词: picc 18 8.35 PIC
下载地址: 免注册下载 普通下载 VIP

相关代码

				#include 
				
				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						{
							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						{
							
							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;
					}
				}
				
							

相关资源