Код: #if defined (FLASH_V1_1)
#define FLASH_WRITE_BLOCK 8 #define FLASH_ERASE_BLOCK 64
#elif defined (FLASH_V1_2)
#define FLASH_WRITE_BLOCK 32 #define FLASH_ERASE_BLOCK 64
#elif defined (FLASH_V1_3)
#define FLASH_WRITE_BLOCK 16 #define FLASH_ERASE_BLOCK 64
#elif defined (FLASH_V1_4)
#define FLASH_WRITE_BLOCK 64 #define FLASH_ERASE_BLOCK 64
#elif defined (FLASH_V1_5) || defined (FLASH_V2_1)
#define FLASH_WRITE_BLOCK 64 #define FLASH_ERASE_BLOCK 1024
#elif defined (FLASH_V1_6)
#define FLASH_WRITE_BLOCK 128 #define FLASH_ERASE_BLOCK 128
#endif
#if defined (FLASH_V1_1) || defined (FLASH_V1_2) || defined (FLASH_V1_4) || defined (FLASH_V1_6) void WriteBytesFlash(unsigned long startaddr, unsigned int num_bytes, unsigned char *flash_array) { unsigned char write_byte=0,flag=0; DWORD_VAL flash_addr;
flash_addr.Val = startaddr;
startaddr /= FLASH_WRITE_BLOCK ; //Allign the starting address block startaddr *= FLASH_WRITE_BLOCK ; startaddr += FLASH_WRITE_BLOCK ;
write_byte = startaddr - flash_addr.Val;
while(num_bytes) { TBLPTRU = flash_addr.byte.UB; //Load the address to Address pointer registers TBLPTRH = flash_addr.byte.HB; TBLPTRL = flash_addr.byte.LB;
while(write_byte--) { TABLAT = *flash_array++; _asm TBLWTPOSTINC _endasm
if (--num_bytes==0) break; }
TBLPTRU = flash_addr.byte.UB; //Load the address to Address pointer registers TBLPTRH = flash_addr.byte.HB; TBLPTRL = flash_addr.byte.LB; //*********** Flash write sequence *********************************** EECON1bits.EEPGD = 1; // point to Flash program memory EECON1bits.CFGS = 0; // access Flash program memory EECON1bits.WREN = 1;
if(INTCONbits.GIE) { INTCONbits.GIE = 0; flag=1; }
EECON2 = 0x55; EECON2 = 0xAA; EECON1bits.WR = 1; EECON1bits.WREN = 0 ;
if(flag) { INTCONbits.GIE = 1; flag=0; }
write_byte = FLASH_WRITE_BLOCK; flash_addr.Val = flash_addr.Val + FLASH_WRITE_BLOCK; //increment to one block of 64 bytes } }
#elif defined (FLASH_V1_3) || defined (FLASH_V1_5) || defined (FLASH_V2_1) void WriteBytesFlash(unsigned long startaddr, unsigned int num_bytes, unsigned char *flash_array) { unsigned char write_byte=0,flag=0; DWORD_VAL flash_addr;
flash_addr.Val = startaddr;
startaddr /= FLASH_WRITE_BLOCK ; //Allign the starting address block startaddr *= FLASH_WRITE_BLOCK ; startaddr += FLASH_WRITE_BLOCK ;
write_byte = startaddr - flash_addr.Val;
while(num_bytes) { TBLPTRU = flash_addr.byte.UB; //Load the address to Address pointer registers TBLPTRH = flash_addr.byte.HB; TBLPTRL = flash_addr.byte.LB;
while(write_byte--) { TABLAT = *flash_array++; _asm TBLWTPOSTINC _endasm
if(--num_bytes==0) break; }
TBLPTRU = flash_addr.byte.UB; //Load the address to Address pointer registers TBLPTRH = flash_addr.byte.HB; TBLPTRL = flash_addr.byte.LB; //*********** Flash write sequence *********************************** EECON1bits.WREN = 1;
if(INTCONbits.GIE) { INTCONbits.GIE = 0; flag=1; }
EECON2 = 0x55; EECON2 = 0xAA; EECON1bits.WR = 1; EECON1bits.WREN = 0 ;
if(flag) { INTCONbits.GIE = 1; flag=0; }
write_byte = FLASH_WRITE_BLOCK; flash_addr.Val = flash_addr.Val + FLASH_WRITE_BLOCK; //increment to one block of 64 bytes } }
void ReadFlash(unsigned long startaddr, unsigned int num_bytes, unsigned char *flash_array) { DWORD_VAL flash_addr;
flash_addr.Val = startaddr;
TBLPTRU = flash_addr.byte.UB; //Load the address to Address pointer registers TBLPTRH = flash_addr.byte.HB; TBLPTRL = flash_addr.byte.LB;
while(num_bytes--) { //*********** Table read sequence ****************************** _asm TBLRDPOSTINC _endasm
*flash_array++ = TABLAT; } }
|