Вы находитесь на странице: 1из 1

Bottom line: To write to a block of Flash: If the locations to which you are writing have never been programmed

and have no t been written to since the device was programmed, you can write anything you wa nt. (Still requires manipulation of the EECON bits.) If the part of Flash to which you are writing was previously programmed or has b een written to since programming, you erase an entire block and write your stuff . If there is other stuff in the block that you don't want to change, then read the entire block into temporary storage (RAM), modify the particular bytes you want to change, then write the entire block back to Flash. Note that the plib functions don't do all of the work for you. You still have t o read a block into a RAM buffer, erase the block in Flash, then (and only then) write the modified block back to Flash. The plib functions take care of specif ic manipulations of the EECON bits. unsigned char ProgMemWr32(unsigned int address, unsigned char *buffer_ptr) { // NOTE: program memory must also be erased fir st. near rom unsigned char *ptr; char i; ptr = (rom unsigned char *) (address & 0xFFE0); // ensure write starts on 32-byte boundary for (i = 0; i < 32; i++) { *(ptr + i) = buffer_ptr[i]; // write the data into the holding regi sters } EECON1bits.EEPGD = 1; // write to flash program memory EECON1bits.CFGS = 0; // not configuration registers EECON1bits.FREE = 0; // we're not erasing now. EECON1bits.WREN = 1; // enable write/erase operations // execute code sequence, which cannot be interrupted, then execute write32 INTCONbits.GIE = 0; EECON2 = 0x55; EECON2 = 0xAA; EECON1bits.WR = 1; INTCONbits.GIE = 1; EECON1bits.WREN = 0; } // Disable interrupts // Begin Write sequence // Set WR bit to begin 32-byte write // re-enable interrupts // disable write/erase operations

Note the dire warning that the block to which you are going to write must be era sed first. (If the block was erased during programming and has never been writt en to in this program you can just write the block. Otherwise you had better er ase it first.) Note that the 45K20 has a write block size of 32 bytes but an erase block size o f 64 bytes. (So, if the area was not erased, you save 64 bytes in temporary RAM buffer, erase an entire erase block, modify whatever you need to in the RAM buff er, and then write two write blocks back to Flash from the RAM buffer.) Other processors may have different values for the block sizes, and a program would be better off using the compiler-#defined macros FLASH_ERASE_BLOCK and FLA SH_WRITE_BLOCK rather than hard-coding 32 or 64 or whatever...

Вам также может понравиться