DSP28335 IIC读写M24M02的多个数据
时间: 2024-05-14 18:15:04 浏览: 189
I2C 从24c02写读数据
以下是DSP28335使用IIC读写M24M02的多个数据的代码示例:
首先需要初始化IIC,如下所示:
```c
void InitI2C(void)
{
// 设置I2C引脚
EALLOW;
GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0;
GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 1;
GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 1;
EDIS;
// 初始化I2C
I2caRegs.I2CMDR.bit.IRS = 0; // I2C复位
I2caRegs.I2CPSC.all = 6; // 分频器值为6,时钟频率为150kHz
I2caRegs.I2CSAR = M24M02_ADDR; // 设置从设备地址
I2caRegs.I2CMDR.bit.IRS = 1; // 退出I2C复位
}
```
然后可以定义一个函数来写入多个数据:
```c
void WriteMultipleBytes(uint16_t addr, uint16_t num, uint8_t *data)
{
uint16_t i;
// 发送起始信号
I2caRegs.I2CMDR.bit.STP = 1;
I2caRegs.I2CMDR.bit.MST = 1;
I2caRegs.I2CDXR = (M24M02_ADDR << 1) & 0xFE;
I2caRegs.I2CMDR.bit.TRX = 1;
I2caRegs.I2CMDR.bit.STT = 1;
while (I2caRegs.I2CSTR.bit.ARDY == 0);
// 发送地址
I2caRegs.I2CDXR = (addr >> 8) & 0xFF;
while (I2caRegs.I2CSTR.bit.ARDY == 0);
I2caRegs.I2CDXR = addr & 0xFF;
while (I2caRegs.I2CSTR.bit.ARDY == 0);
// 发送数据
for (i = 0; i < num; i++)
{
I2caRegs.I2CDXR = data[i];
while (I2caRegs.I2CSTR.bit.ARDY == 0);
}
// 发送停止信号
I2caRegs.I2CMDR.bit.STP = 1;
while (I2caRegs.I2CSTR.bit.ARDY == 0);
}
```
然后可以定义一个函数来读取多个数据:
```c
void ReadMultipleBytes(uint16_t addr, uint16_t num, uint8_t *data)
{
uint16_t i;
// 发送起始信号
I2caRegs.I2CMDR.bit.STP = 1;
I2caRegs.I2CMDR.bit.MST = 1;
I2caRegs.I2CDXR = (M24M02_ADDR << 1) & 0xFE;
I2caRegs.I2CMDR.bit.TRX = 1;
I2caRegs.I2CMDR.bit.STT = 1;
while (I2caRegs.I2CSTR.bit.ARDY == 0);
// 发送地址
I2caRegs.I2CDXR = (addr >> 8) & 0xFF;
while (I2caRegs.I2CSTR.bit.ARDY == 0);
I2caRegs.I2CDXR = addr & 0xFF;
while (I2caRegs.I2CSTR.bit.ARDY == 0);
// 发送重复起始信号
I2caRegs.I2CMDR.bit.STP = 1;
I2caRegs.I2CMDR.bit.MST = 1;
I2caRegs.I2CDXR = (M24M02_ADDR << 1) | 0x01;
I2caRegs.I2CMDR.bit.TRX = 0;
I2caRegs.I2CMDR.bit.STT = 1;
while (I2caRegs.I2CSTR.bit.ARDY == 0);
// 读取数据
for (i = 0; i < num; i++)
{
while (I2caRegs.I2CSTR.bit.RRDY == 0);
data[i] = I2caRegs.I2CDRR;
}
// 发送停止信号
I2caRegs.I2CMDR.bit.STP = 1;
while (I2caRegs.I2CSTR.bit.ARDY == 0);
}
```
使用上述函数可以实现对M24M02的多个数据的读写操作。
阅读全文