事实原因:
24LC512T
之前没有写过eeprom的经验,然后项目更换芯片,demo中有一个I2C读写EEPROM的实验
发现读写地址只有8位,读写的是2k的EEPROM
因为用的是M4系列,32位,8位最多寻址256,根本不够64KB
发送完设备地址后,然后发送设备内地址,此时注意设备内地址是发送两次,高位+低位
组成16位 可以寻址64kB
修改:下边标红的地方,需要发送两次设备内地址
void eeprom_byte_write(uint8_t* p_buffer, uint8_t write_address)
{
/* wait until I2C bus is idle 首先确认总线不忙 */
while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
/* send a start condition to I2C bus 发送起始信号 */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set 起始信号发送成功 */
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
/* send slave address to I2C bus 设置I2C地址 发送模式 或者 接受模式 */
i2c_master_addressing(I2C0, eeprom_address, I2C_TRANSMITTER);
/* wait until ADDSEND bit is set 等待设置成功 */
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit 清除标志 */
i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty 等待传输数据为空的时候 */
while(SET != i2c_flag_get(I2C0, I2C_FLAG_TBE));
/* send the EEPROM's internal address to write to : only one byte address 设置写设备内地址*/
i2c_data_transmit(I2C0, write_address);
/* wait until BTC bit is set 当发送完数据 */
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* send the byte to be written 开始向指定地址发送数据 */
i2c_data_transmit(I2C0, *p_buffer);
/* wait until BTC bit is set 发送数据完成 */
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* send a stop condition to I2C bus 停止发送 发送结束信号 */
i2c_stop_on_bus(I2C0);
/* wait until the stop condition is finished 应答 */
while(I2C_CTL0(I2C0)&0x0200);
}