?? flash_module.c
字號:
//read/write flash memory. // Uses HITECH macro for flash erase//prototypesunsigned char flash_readbyte(long addr);long flash_readstr(char *s, long addr, unsigned char max);void do_flash_erase(long addr);char flash_writebuf(long addr, char *buf);unsigned char flash_readbyte(long addr){#if defined(HI_TECH_C) TBLPTR = (far unsigned char *)addr; #endif#if defined(__18CXX) TBLPTR = (unsigned short long)addr; #endif TBLRDPOSTINC(); //asm("TBLRD*+"); return((unsigned char)TABLAT);}long flash_readstr(char *s, long addr, unsigned char max){ unsigned char c,cnt; cnt = 0; do { c = flash_readbyte(addr); addr++; *s = c; s++; cnt++; }while((c) && (cnt < (max-1))); // last byte non-zero, terminate this string if (c) *s = 0; return(addr);}void do_flash_erase(long addr){ char istat;#if defined(HI_TECH_C) TBLPTR = (far unsigned char *)addr; #endif#if defined(__18CXX) TBLPTR = (unsigned short long)addr; #endif EEPGD=1;FREE=1;WREN=1;CFGS=0; istat = GIE; GIE = 0; EECON2 = 0x55; EECON2 = 0xAA; WR = 1; // CPU stalls NOP(); // asm("nop") GIE = istat; FREE=0;WREN=0;}// write 64 bytes , assumes 'addr' is on 64 byte boundary!char flash_writebuf(long addr, char *buf){ char i, j,istat, *s; do_flash_erase(addr); // before write, decrement address addr--;#if defined(HI_TECH_C) TBLPTR = (far unsigned char *)addr; #endif#if defined(__18CXX) TBLPTR = (unsigned short long)addr; #endif i = 0; j= 0; s = buf; while (i < 64) { TABLAT=*s; TBLWTPREINC(); //asm("TBLWT+*"); j++;i++;s++; if (j == 8) { // 8 bytes written to internal buff, do write EEPGD=1;FREE=0;WREN=1;CFGS=0; istat = GIE; GIE = 0; EECON2 = 0x55; EECON2 = 0xAA; WR = 1; // CPU stalls NOP(); //asm("NOP"); GIE = istat; WREN = 0; j = 0; } } // verify contents written correctly i = 0;s = buf; addr++; // get back to start while(i < 64) { j = flash_readbyte(addr); if (j != *s) return(1); i++;s++;addr++; } return(0);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -