In pic18f45k80, the datasheet says one can read and write a byte (8bit) data in EEPROM. I need to write 3 bytes data at once and read it back at once because that 3 bytes data consists of two parameters of the lead acid battery which I have to measure. Are there any ways to do this, reading and writing a bulk of data in Pic Microcontroller? An example snippet will be more helpful.
-
2\$\begingroup\$ Its a common thing that EEPROM data needs to be written byte-wise. Writing multiple bytes or specific data in one step (using a specific function) is just another level of abstraction. Be creative... \$\endgroup\$– RevAug 25, 2014 at 7:27
-
1\$\begingroup\$ for (i=0;i<3;i++) write_eeprom(start_address+i,mybyte[i]); Where write_eeprom is a function that you create and mybyte is an array of char. \$\endgroup\$– Spehro PefhanyAug 25, 2014 at 13:33
-
\$\begingroup\$ Does this need to be thread-safe? You'd want a mutex if that's the case, to be sure all three bytes get written before another thread accesses the same memory. If no mutex, you could emulate one with a semaphore. \$\endgroup\$– TDHofstetterAug 29, 2014 at 3:00
1 Answer
If you're using XC8, include xc.h
header file, and use the functions
eeprom_write( addr , data );
and
data = eeprom_read( addr );
So, to write/read 3 bytes, just call these functions three consecutive times, with different addresses each time, of course. There's no way to write/read 3 bytes at once, if that's what you meant.