程序的功能是给24c02 第0x00个字节写入0x20,然后再读回到变量b中
#include "DSP28x_Project.h"
#define I2C_SLAVE_ADDR 0x50
#define I2C_NUMBYTES 15
#define I2C_EEPROM_HIGH_ADDR 0x00
#define I2C_EEPROM_LOW_ADDR 0x00
void I2CA_SendData(void);
void I2CA_ReceiveData(void);
void main(void)
{
InitSysCtrl();
InitI2CGpio();
DINT;
InitPieCtrl();
IER = 0xffff;
IFR = 0x0000;
InitPieVectTable();
InitI2C();
I2CA_SendData();
I2CA_ReceiveData();
while(1);
}
void I2CA_SendData(void)
{
Uint16 i;
I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address
I2caRegs.I2CCNT = 1 + 1; //Set count to 5 characters plus 2 address bytes
I2caRegs.I2CDXR = 0x02;//Send eeprom high address
I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode
I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode
I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode
I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0
I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow
while(I2caRegs.I2CSTR.bit.XRDY == 0); //Do nothing till data is shifted out
I2caRegs.I2CDXR = 0x20; //Send eeprom low address
}
void I2CA_ReceiveData(void)
{
Uint16 i;
char b[20];
b[0]=0;
I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address
I2caRegs.I2CCNT = 1; //Set count to 2 address bytes
I2caRegs.I2CDXR = 0x00; //Send eeprom high address
I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode
I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode
I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode
I2caRegs.I2CMDR.bit.STP = 0; //Dont release the bus after Tx
I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow
I2caRegs.I2CCNT = 1; //read 5 bytes from eeprom
I2caRegs.I2CMDR.bit.TRX = 0; //Set to Recieve mode
I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode
I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode
I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0
I2caRegs.I2CMDR.bit.STT = 1; //Repeated start, Reception will follow
b[0] = I2caRegs.I2CDRR;
while(1);
}
void InitI2C(void)
{
// Initialize I2C-A:
I2caRegs.I2CSAR = 0x0050; // Slave address - EEPROM control code
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.bit.ARDY = 0;
I2caRegs.I2CIER.bit.XRDY = 0;
I2caRegs.I2CFFTX.all = 0x0000; // Disable FIFO mode and TXFIFO
I2caRegs.I2CFFRX.all = 0x0000; // Disable RXFIFO, clear RXFFINT,
I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
// Stop I2C when suspended
}
本帖最后由 jetlin1992 于 2014-7-8 15:20 编辑
其实,我那篇文章已经写得很清楚了。哪怕是单纯的把你的程序和我的比较,你也能发现些不同的地方。同时建议你看程序的同时,看看TI关于I2C的手册.