3.d.02. Command 240: Write to EEPROM
Effect: This command writes a byte to the specified address of the mega168’s EEPROM. The EEPROM is addressed by a 9-bit value (512 bytes of memory). Using this command can potentially be dangerous as it’s possible to overwrite saved mega168 settings. Addresses 24 – 32 are used to hold pointers to melodies, so these may be changed freely so long as you’re not using the EEPROM to store melodies. Melody notes are stored sequentially in three-byte blocks starting at EEPROM address 33. If you wish to store both melodies and custom data in the mega168’s EEPROM, we suggest you start storing your data at the end of the EEPROM as the melodies are stored from the front.
If EEPROM is already being written, it is not possible to perform the write. When processing the command byte, the SPI interrupt routine checks to see if it’s currently possible to write to the EEPROM. It will load the value of the EECR’s EEPE bit (programming enable bit of the EEPROM control register) into bit zero of SPDR. It is recommended you do not begin transmission of the first data byte until 3 us after the transmission of the command byte has completed. This is to make sure the mega168 has time to load the SPDR. If SPDR is zero after transmission of this first data byte, it is possible to perform the write, otherwise the write will not be allowed.
This command should be used to save settings so that they persist after a hardware reset. This command can also be used to restore settings to their factory defaults by clearing EEPROM byte zero.
Warning: An EEPROM memory address can be written thousands of times, but not infinitely many. .
Values sent: EEPROM address (9 bits), write data (8 bits)
command byte = 240 | (MSB of write data byte << 2) | (bit 7 of write address << 1) | bit 8 of write address
data byte 1 = seven lowest bits of write address (bits 6 – 0)
data byte 2 = seven lowest bits of write data