DSP28335通过I2C外扩M24M02存储大量数据的代码
时间: 2023-12-02 09:03:17 浏览: 179
以下是DSP28335通过I2C外扩M24M02存储大量数据的代码,包括初始化、写入和读取操作。
初始化:
```c
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
#include "DSP2833x_I2C.h"
#define M24M02_I2C_ADDR 0x50 // M24M02 I2C地址
void InitI2C(void)
{
// 初始化I2C总线
I2caRegs.I2CSAR = M24M02_I2C_ADDR; // 设置I2C从设备地址
I2caRegs.I2CPSC.all = 6; // 设置I2C时钟分频,SYSCLKOUT/(I2CPSC+1) = 150MHz/(6+1) = 21.43MHz
I2caRegs.I2CCLKL = 10; // 设置I2C时钟低电平时间,21.43MHz/(10+1) = 1.87MHz
I2caRegs.I2CCLKH = 5; // 设置I2C时钟高电平时间,21.43MHz/(5+1) = 3.57MHz
I2caRegs.I2CIER.all = 0x24; // 使能I2C中断,接收数据中断和总线错误中断
I2caRegs.I2CMDR.all = 0x0020; // 使能I2C模块,无主模式
}
```
写入数据:
```c
void WriteToM24M02(unsigned int address, unsigned char *data, unsigned int length)
{
unsigned int i;
I2caRegs.I2CCNT = length; // 设置数据长度
I2caRegs.I2CDXR = address >> 8; // 发送数据地址高字节
I2caRegs.I2CDXR = address & 0xFF; // 发送数据地址低字节
for (i = 0; i < length; i++)
{
I2caRegs.I2CDXR = data[i]; // 发送数据
while (I2caRegs.I2CSTR.bit.ARDY == 0); // 等待发送完成
}
}
```
读取数据:
```c
void ReadFromM24M02(unsigned int address, unsigned char *data, unsigned int length)
{
unsigned int i;
I2caRegs.I2CCNT = 2; // 先发送数据地址
I2caRegs.I2CDXR = address >> 8; // 发送数据地址高字节
I2caRegs.I2CDXR = address & 0xFF; // 发送数据地址低字节
I2caRegs.I2CCNT = length; // 再接收数据
I2caRegs.I2CDXR = (M24M02_I2C_ADDR << 1) | 1; // 发送读命令
for (i = 0; i < length; i++)
{
while (I2caRegs.I2CSTR.bit.RRDY == 0); // 等待接收完成
data[i] = I2caRegs.I2CDRR; // 读取数据
}
}
```
阅读全文