⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flash_module.c

📁 PIC_Hi-Tech_C_Mike_Pearces_I2C_routines Microchi PIC_Hi-Tech C_I2C_for_EEPROM_24LC01B Code
💻 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 + -