在线时间132 小时
UID3065001
注册时间2014-10-4
NXP金币0
TA的每日心情 | 开心 2018-8-30 16:02 |
---|
签到天数: 5 天 连续签到: 1 天 [LV.2]偶尔看看I
金牌会员
 
- 积分
- 1851
- 最后登录
- 2019-11-19
|
我用IIC去读写AT24C16,先写入一串ASCII码"0x01","0x02","0x03","0x04","0x05",然后再按顺序把他们读出来,通过串口发送到上位机,但是得到的数据第一、二字节总是0xFF,0xFF,然后从第三字节开始才是"0x03","0x04","0x05。我的代码如下:
IIC基本操作用宏定义:
- #define i2c_Start(I2Cn) I2C_C1_REG(I2CN[I2Cn]) |= (I2C_C1_TX_MASK | I2C_C1_MST_MASK)
- #define i2c_Stop(I2Cn) I2C_C1_REG(I2CN[I2Cn]) &= ~(I2C_C1_TX_MASK | I2C_C1_MST_MASK)
- #define i2c_RepeatedStart(I2Cn) I2C_C1_REG(I2CN[I2Cn]) |= I2C_C1_RSTA_MASK
- //进入接受模式,不发送应答
- #define i2c_PutinRxMode(I2Cn) I2C_C1_REG(I2CN[I2Cn]) &= ~I2C_C1_TX_MASK; \
- I2C_C1_REG(I2CN[I2Cn]) |= I2C_C1_TXAK_MASK
- #define i2c_Wait(I2Cn) do \
- { \
- while((I2C_S_REG(I2CN[I2Cn]) & I2C_S_IICIF_MASK) == 0){}; \
- I2C_S_REG(I2CN[I2Cn]) |= I2C_S_IICIF_MASK; \
- }while(0)
- #define i2c_write_byte(I2Cn,data) do \
- { \
- I2C_D_REG(I2CN[I2Cn]) = (data); \
- i2c_Wait(I2Cn); \
- }while(0)
复制代码 然后写一串数据函数如下,在24C16的第0页第0字节开始写:- void i2c_MultiWR_EEPROM()
- {
- i2c_Stop(I2C0e);
- i2c_Start(I2C0e);
- i2c_write_byte(I2C0e, 0xa0);
- i2c_write_byte(I2C0e, 0x00);
-
- i2c_write_byte(I2C0e, 0x01);
- i2c_write_byte(I2C0e, 0x02);
- i2c_write_byte(I2C0e, 0x03);
- i2c_write_byte(I2C0e, 0x04);
- i2c_write_byte(I2C0e, 0x05);
- i2c_write_byte(I2C0e, 0x06);
- i2c_write_byte(I2C0e, 0x07);
- i2c_write_byte(I2C0e, 0x08);
- i2c_write_byte(I2C0e, 0x09);
- i2c_write_byte(I2C0e, 0x0a);
- i2c_Stop(I2C0e);
- Pause();
- }
复制代码 然后读函数如下:
- uint8_t i2c_read_reg(I2Cn_e i2cn, uint8_t SlaveID, uint8_t reg)
- {
- uint8_t result;
-
- ASSERT((SlaveID & 0x80) == 0);
-
- i2c_Start(i2cn);
- i2c_write_byte(i2cn,(SlaveID << 1)|MWSR);
- i2c_write_byte(i2cn,reg);
- i2c_RepeatedStart(i2cn);
- i2c_write_byte(i2cn, (SlaveID << 1)|MRSW);
- i2c_PutinRxMode(i2cn);
- result = I2C_D_REG(I2CN[i2cn]);
-
- i2c_Wait(i2cn);
-
- i2c_Stop(i2cn);
-
- result = I2C_D_REG(I2CN[i2cn]);
-
-
- Pause();
-
- return result ;
- }
复制代码 主函数如下:
- i2c_MultiWR_EEPROM();
-
- dataRec = i2c_read_reg(I2C0e, 0x50, 0x00);
- uart_putchar(UART1e,dataRec);
- dataRec = i2c_read_reg(I2C0e, 0x50, 0x01);
- uart_putchar(UART1e,dataRec);
- dataRec = i2c_read_reg(I2C0e, 0x50, 0x02);
- uart_putchar(UART1e,dataRec);
- dataRec = i2c_read_reg(I2C0e, 0x50, 0x03);
- uart_putchar(UART1e,dataRec);
- dataRec = i2c_read_reg(I2C0e, 0x50, 0x04);
- uart_putchar(UART1e,dataRec);
- dataRec = i2c_read_reg(I2C0e, 0x50, 0x05);
- uart_putchar(UART1e,dataRec);
复制代码 24C16的地址与读写时序如下图:
有没有友友遇到同样的问题呢?我对了半天的时序,没问题呀!!
|
|