cli(); // Ensure atomic operation for the write operation.
// the folling code waits 3.3ms or 1.8ms for write if previous eeprom was written.
do {} while( EECR & (1<<EEPE) ); // Wait for completion of previous write.
#ifndef EEPROM_IGNORE_SELFPROG
do {} while( SPMCSR & (1<<SELFPRGEN) ); // Wait for completion of SPM.
#endif
// this is the end of the problematic code.
EEAR = addr; // Set EEPROM address register.
EECR = (1<<EERE); // Start EEPROM read operation.
old_value = EEDR; // Get old EEPROM value.
diff_mask = old_value ^ new_value; // Get bit differences.
// insert here cli() instead of above
// the reason for interrupt disabling is this, the write to EEMPE and the setting of EEPE need
// to be made quickly after
EECR = (1<<EEMPE) | // Set Master Write Enable bit...
(0<<EEPM1) | (0<<EEPM0); // ...and Erase+Write mode.
EECR |= (1<<EEPE); // Start Erase+Write operation.`
从数据表。
等到 EEPE 变为零。
等到 SPMCSR 中的 SPMEN 变为零。
将新的 EEPROM 地址写入 EEAR(可选)。
将新的 EEPROM 数据写入 EEDR(可选)。
向 EEMPE 位写入逻辑 1,同时向 EECR 中的 EEPE 写入零。
在设置 EEMPE 后的四个时钟周期内,向 EEPE 写入一个逻辑 1。
注意:( 第3步和第4步的顺序不是必须的) 关键点是Nr 6“Withing four clock cycles”这就是阻塞中断的原因, 中断阻塞只需要在第5步之前,在第5步之后就可以解除6.
修复了与eeprom相关的一些问题,
如果未启用则不会启用中断。
它消除了阻塞中断问题。