/* ***************************************************************************** * uFlash876 sample code * A filing system for the uFlash876/876B i2c EEPROM * * Compile using CCS PIC C * ***************************************************************************** */ #include #include #define EEPROM 0x50 // I2C eeprom address WORD fptr=0; // eeprom file pointer typedef union {float f;BYTE b[4];}fval; // union allows floats to be // accessed as a float or bytes void file_seek(WORD x); // set the current file loc void file_writeb(BYTE x); // write a byte to the current file loc void file_writew(WORD x); // write a word to the current file loc void file_writef(float val); // write a float to the currnt file loc BYTE file_readb(void); // read a byte from the current file loc WORD file_readw(void); // read a word from the current file loc float file_readf(void); // read a float from the current file loc /* **************************** main ******************************************* */ BYTE buf[20]; void main(void) // place your code here { } /* **************************** EEPROM file module ****************************** */ void file_seek(WORD x) // set the current file loc { fptr=x; } void file_writeb(BYTE x) // write a byte to the current file loc { uc_wstart(EEPROM); // address the external EEPROM uc_write((BYTE)(fptr>>8)); // set the EEPROM write address uc_write((BYTE)fptr); uc_write(x); // write byte of data uc_stop(); // done fptr++; // advance the file pointer delay_ms(10); // allow time to write eeprom } void file_writew(WORD x) // write a word to the current file loc { uc_wstart(EEPROM); // address the external EEPROM uc_write((BYTE)(fptr>>8)); // set the EEPROM write address uc_write((BYTE)fptr); uc_write((BYTE)(x>>8)); // write high byte of data uc_write((BYTE)(x)); // write low byte of data uc_stop(); // done fptr+=2; // advance the file pointer delay_ms(10); // allow time to write eeprom } void file_writef(float val) // write a float to the currnt file loc { fval x; // union for byte access x.f=val; // copy to union to allow byte access uc_wstart(EEPROM); // address the external EEPROM uc_write((BYTE)(fptr>>8)); // set the EEPROM write address uc_write((BYTE)fptr); uc_write(x.b[0]); // write data as 4 bytes uc_write(x.b[1]); uc_write(x.b[2]); uc_write(x.b[3]); uc_stop(); // done fptr+=4; // advance the file pointer delay_ms(10); // allow time to write eeprom } BYTE file_readb(void) // read a byte from the current file loc { BYTE x; uc_wstart(EEPROM); // address the external EEPROM uc_write((BYTE)(fptr>>8)); // set the EEPROM write address uc_write((BYTE)fptr); uc_rstart(EEPROM); // switch to read x=uc_read(0); // read data uc_stop(); // done fptr++; // advance the file pointer return(x); // return the BYTE value } WORD file_readw(void) // read a word from the current file loc { WORD x; uc_wstart(EEPROM); // address the external EEPROM uc_write((BYTE)(fptr>>8)); // set the EEPROM write address uc_write((BYTE)fptr); uc_rstart(EEPROM); // switch to read x=uc_read(1); // read high byte of data x<<=8; // shift it up x+=uc_read(0); // read low byte uc_stop(); // done fptr+=2; // advance the file pointer return(x); // return the BYTE value } float file_readf(void) // read a float from the current file loc { fval x; uc_wstart(EEPROM); // address the external EEPROM uc_write((BYTE)(fptr>>8)); // set the EEPROM write address uc_write((BYTE)fptr); uc_rstart(EEPROM); // switch to read x.b[0]=uc_read(1); // read data as 4 bytes x.b[1]=uc_read(1); x.b[2]=uc_read(1); x.b[3]=uc_read(0); uc_stop(); // done fptr+=4; // advance the file pointer return(x.f); // return the float value }