DSP28335通过IIC读写M24M02程序
时间: 2023-09-28 15:10:44 浏览: 251
以下是DSP28335通过IIC读写M24M02的参考代码:
#include "DSP2833x_Device.h"
#include "DSP2833x_I2C.h"
// 定义M24M02的地址
#define M24M02_ADDR 0x50
// 定义写入和读取的数据长度
#define WRITE_LENGTH 2
#define READ_LENGTH 2
// 定义写入和读取的数据缓冲区
Uint16 writeData[WRITE_LENGTH];
Uint16 readData[READ_LENGTH];
// 初始化I2C模块
void InitI2C(void)
{
// 设置I2C时钟频率为100kHz
I2caRegs.I2CPSC.all = 7;
I2caRegs.I2CCLKL = 10;
I2caRegs.I2CCLKH = 5;
// 使能I2C模块
I2caRegs.I2CMDR.all = 0x0020;
}
// 向M24M02写入数据
void WriteToM24M02(Uint16 address, Uint16 data)
{
// 将地址和数据存储到写入数据缓冲区
writeData[0] = address;
writeData[1] = data;
// 发送写入数据
I2CA_WriteData(M24M02_ADDR, WRITE_LENGTH, writeData);
// 等待写入完成
while (I2caRegs.I2CMDR.bit.STP == 1);
}
// 从M24M02读取数据
Uint16 ReadFromM24M02(Uint16 address)
{
// 将地址存储到写入数据缓冲区
writeData[0] = address;
// 发送读取地址
I2CA_WriteData(M24M02_ADDR, 1, writeData);
// 接收读取数据
I2CA_ReadData(M24M02_ADDR, READ_LENGTH, readData);
// 等待读取完成
while (I2caRegs.I2CMDR.bit.STP == 1);
// 返回读取数据
return readData[1];
}
void main()
{
// 初始化I2C模块
InitI2C();
// 向M24M02写入数据
WriteToM24M02(0x00, 0x1234);
// 从M24M02读取数据
Uint16 data = ReadFromM24M02(0x00);
}
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)