dsp28335 M24M02 EPROMM 读写程序
时间: 2023-11-20 12:05:06 浏览: 49
由于DSP28335没有内置的EEPROM,因此需要通过外部设备来进行数据的读写。本文介绍如何通过DSP28335与M24M02 EEPROM进行通信。
首先需要了解M24M02 EEPROM的基本特性:
- M24M02是2M位的串行EEPROM
- M24M02支持标准的I2C总线通信协议
- M24M02具有128个页,每页16字节,总共2048个字节
接下来是DSP28335与M24M02的接口电路图:
![image](https://user-images.githubusercontent.com/87290368/136768738-8a8c6d4a-62d7-4c4f-b4a6-3d3e3b6c3e6f.png)
其中,SCL和SDA分别连接到I2C总线的时钟线和数据线。WP和VCC分别连接到M24M02的写保护引脚和电源引脚,CS连接到M24M02的片选引脚。
接下来是DSP28335与M24M02的读写程序:
```c
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
#define M24M02_SLAVE_ADDR 0x50
void Init_I2C(void)
{
I2caRegs.I2CSAR = 0x0000; // Slave address
I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
I2caRegs.I2CCLKL = 10; // NOTE: must be non zero
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero
I2caRegs.I2CIER.all = 0x24; // Enable SCD & ARDY interrupts
I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
}
void I2C_WriteByte(Uint16 slaveAddr, Uint16 memAddr, Uint16 data)
{
while (I2caRegs.I2CSTR.bit.BB); // Wait until I2C bus is free
I2caRegs.I2CSAR = slaveAddr; // Set slave address
I2caRegs.I2CCNT = 3; // Set data count
I2caRegs.I2CDXR = (memAddr >> 8); // Set memory address MSB
I2caRegs.I2CDXR = (memAddr & 0xFF); // Set memory address LSB
I2caRegs.I2CDXR = data; // Set data
I2caRegs.I2CMDR.all = 0x6E20; // Send data
while (!I2caRegs.I2CSTR.bit.ARDY); // Wait for data to be sent
while (I2caRegs.I2CSTR.bit.BB); // Wait until I2C bus is free
}
Uint16 I2C_ReadByte(Uint16 slaveAddr, Uint16 memAddr)
{
Uint16 data;
while (I2caRegs.I2CSTR.bit.BB); // Wait until I2C bus is free
I2caRegs.I2CSAR = slaveAddr; // Set slave address
I2caRegs.I2CCNT = 2; // Set data count
I2caRegs.I2CDXR = (memAddr >> 8); // Set memory address MSB
I2caRegs.I2CDXR = (memAddr & 0xFF); // Set memory address LSB
I2caRegs.I2CMDR.all = 0x2620; // Send memory address
while (!I2caRegs.I2CSTR.bit.ARDY); // Wait for memory address to be sent
I2caRegs.I2CSAR = slaveAddr; // Set slave address
I2caRegs.I2CCNT = 1; // Set data count
I2caRegs.I2CMDR.all = 0x2C20; // Send read command
while (!I2caRegs.I2CSTR.bit.ARDY); // Wait for read command to be sent
while (I2caRegs.I2CSTR.bit.RRDY); // Wait for data to be received
data = I2caRegs.I2CDRR; // Get data
while (I2caRegs.I2CSTR.bit.BB); // Wait until I2C bus is free
return data;
}
void main()
{
Init_I2C(); // 初始化I2C总线
I2C_WriteByte(M24M02_SLAVE_ADDR, 0x0000, 0x55); // 向M24M02写入数据
Uint16 data = I2C_ReadByte(M24M02_SLAVE_ADDR, 0x0000); // 从M24M02读取数据
while(1);
}
```
上述程序中,Init_I2C()函数用于初始化I2C总线,I2C_WriteByte()函数用于向M24M02写入一个字节的数据,I2C_ReadByte()函数用于从M24M02读取一个字节的数据。
在main函数中,首先初始化I2C总线,然后向M24M02写入一个字节的数据0x55,并将读取到的数据存储在变量data中,最后进入死循环等待。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![](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)
![](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)