DSP28335通过I2C外扩M24M02存储多数据代码
时间: 2023-11-12 13:42:26 浏览: 74
以下是DSP28335通过I2C外扩M24M02存储多数据的代码:
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
#define I2C_SLAVE_ADDR 0x50
#define NUM_BYTES_TX 3
#define NUM_BYTES_RX 3
Uint16 data[NUM_BYTES_TX];
Uint16 dataRcv[NUM_BYTES_RX];
void main(void)
{
InitSysCtrl();
InitI2CGpio();
EALLOW;
GpioCtrlRegs.GPAMUX2.bit.GPIO20 = 1;
GpioCtrlRegs.GPAMUX2.bit.GPIO21 = 1;
EDIS;
InitI2C();
data[0] = 0x00;
data[1] = 0x01;
data[2] = 0x02;
I2CA_WriteData(I2C_SLAVE_ADDR, data, NUM_BYTES_TX);
I2CA_ReadData(I2C_SLAVE_ADDR, dataRcv, NUM_BYTES_RX);
while(1);
}
void InitI2CGpio(void)
{
EALLOW;
GpioCtrlRegs.GPAMUX2.bit.GPIO20 = 2;
GpioCtrlRegs.GPADIR.bit.GPIO20 = 1;
GpioCtrlRegs.GPAPUD.bit.GPIO20 = 0;
GpioCtrlRegs.GPAMUX2.bit.GPIO21 = 2;
GpioCtrlRegs.GPADIR.bit.GPIO21 = 1;
GpioCtrlRegs.GPAPUD.bit.GPIO21 = 0;
EDIS;
}
void InitI2C(void)
{
I2caRegs.I2CSAR.all = I2C_SLAVE_ADDR;
I2caRegs.I2CPSC.all = 6;
I2caRegs.I2CCLKL.all = 10;
I2caRegs.I2CCLKH.all = 5;
I2caRegs.I2CIER.all = 0x24;
I2caRegs.I2CMDR.all = 0x0020;
I2caRegs.I2CFFTX.all = 0x6000;
I2caRegs.I2CFFRX.all = 0x204f;
}
void I2CA_WriteData(Uint16 slaveAddr, Uint16 *data, Uint16 numBytes)
{
Uint16 i;
I2caRegs.I2CSAR.all = slaveAddr;
I2caRegs.I2CCNT.all = numBytes;
for(i = 0; i < numBytes; i++)
{
I2caRegs.I2CDXR.all = *data++;
}
I2caRegs.I2CMDR.all = 0x6E20;
while(I2caRegs.I2CSTR.bit.ARDY == 0);
while(I2caRegs.I2CMDR.bit.STP == 1);
}
void I2CA_ReadData(Uint16 slaveAddr, Uint16 *data, Uint16 numBytes)
{
Uint16 i;
I2caRegs.I2CSAR.all = slaveAddr;
I2caRegs.I2CCNT.all = numBytes;
I2caRegs.I2CMDR.all = 0x2620;
for(i = 0; i < numBytes; i++)
{
while(I2caRegs.I2CSTR.bit.RRDY == 0);
*data++ = I2caRegs.I2CDRR.all;
}
while(I2caRegs.I2CMDR.bit.STP == 1);
}
阅读全文