[Solved] How to use persistant vars in EEPROM
DavidZemon
Posts: 2,973
Hello all,
I could pick a random large number for the variable address and hardcode it to a pointer... but I'd like to know if there is a better way to persistently read from and write to a variable in EEPROM. I only need to 16 bits, so a full filesystem on an SD card seems like a bit of overkill. Any ideas?
Thanks,
David
I could pick a random large number for the variable address and hardcode it to a pointer... but I'd like to know if there is a better way to persistently read from and write to a variable in EEPROM. I only need to 16 bits, so a full filesystem on an SD card seems like a bit of overkill. Any ideas?
Thanks,
David

Comments
Using Chris Gadd's "I2C driver.spin" as an example:
If you're using C, it should be pretty similar, but I would probably make the variable volatile.
Volatile just means don't optimize this out, right?
/* Serial get/send integer routines */ void send_int(int32_t z) { char b; b = z>>24; fdserial_txChar(serial,b); b = z>>16; fdserial_txChar(serial,b); b = z>>8; fdserial_txChar(serial,b); b = z; fdserial_txChar(serial,b); } int32_t get_int(void) { char b; int32_t result; result = 0; b = fdserial_rxChar(serial); result = b<<24; b = fdserial_rxChar(serial); result = result + (b<<16); b = fdserial_rxChar(serial); result = result + (b<<8); b = fdserial_rxChar(serial); result = result + b; return result; } /* target: 1=fuel, 2=ign, 3=afr, 4 = CLTCOMP, 5 = MATCOMP, more to come */ /* op: 1 = EEPROM to mem, 2 = mem to EEPROM, 3 = mem to app, 4 = app to mem */ /* 5 = get PIFlow/SIFlow 6 = send PIFlow/SIFlow */ void DataShuttle(int target, int op) { int row,col; switch (target) { case 1 : for (row = 0; row < FUELBASEYSIZE; row++ ) for (col = 0; col < FUELBASEXSIZE; col++ ) if (op == 1) { //EE2Mem FuelBaseTable[col][row]=ee_getInt(FuelBaseTableOrg+(row*FUELBASEXSIZE*4)+(col*4)); } else if (op == 2) { //Mem2EE ee_putInt(FuelBaseTable[col][row],FuelBaseTableOrg+(row*FUELBASEXSIZE*4)+(col*4)); } else if (op == 3) { send_int(FuelBaseTable[col][row]); } else if(op == 4) { FuelBaseTable[col][row] = get_int(); } for (col = 0; col < FUELBASEXSIZE; col++ ) if (op == 1) { FuelBaseXAxis[col]=ee_getInt(FuelBaseXAxisOrg+(col*4)); } else if (op == 2) { ee_putInt(FuelBaseXAxis[col],FuelBaseXAxisOrg+(col*4)); } else if (op == 3) { send_int(FuelBaseXAxis[col]); } else if (op == 4) { FuelBaseXAxis[col] = get_int(); } for (row = 0; row < FUELBASEYSIZE; row++ ) if (op == 1) { FuelBaseYAxis[row]=ee_getInt(FuelBaseYAxisOrg+(row*4)); } else if (op == 2) { ee_putInt(FuelBaseYAxis[row],FuelBaseYAxisOrg+(row*4)); } else if (op == 3) { send_int(FuelBaseYAxis[row]); } else if (op == 4) { FuelBaseYAxis[row] = get_int(); } break;For anyone else stumbling across this thread, there are lots of ee_put* functions - there's one for byte, another for strings, etc.
All can be found in the doxygen documentation of Simple. Here's a link to ee_putInt() though:
http://david.zemon.name/PropWare/simpletools_8h.html#adf44a20755432a25076e28e94b809264