【单片机】I2C读写EEPROM 2K和64K的区别

事实原因:

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);

}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值