本帖最后由 z_jzhao 于 2016-8-17 19:13 编辑
可以接收数据,但是发送n个字节时,用示波器看SDA发现,只发送了最后一个字节,大家帮我看看代码有什么问题,多谢,代码如下
示波器截图见附件
file:///H:/scope_4.png
file:///H:/scope_5.PNG
//200K速率
//address:0xA0
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
void I2CA_Init(void);
interrupt void i2c_int1a_isr(void);
interrupt void i2c_int2a_isr(void);
void pass(void);
void fail(void);
void Delay(unsigned int x);
Uint16 PassCount;
Uint16 FailCount;
Uint16 buf[8];
int I2CWriteData();
int Tx_Data[8]={0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0x77};
int xx=0,ii=0;
void main(void)
{
InitSysCtrl(); //60Mhz
InitI2CGpio();
DINT;
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
EALLOW;
PieVectTable.I2CINT1A = &i2c_int1a_isr; //basic I2C interrupts
EDIS;
I2CA_Init();
PieCtrlRegs.PIEIER8.bit.INTx1 = 1;
PieCtrlRegs.PIECTRL.bit.ENPIE = 1;
IER |= M_INT8;
EINT;
// Application loop
for(;;)
{
}
} // end of main
interrupt void i2c_int1a_isr(void)
{
Uint16 status;
unsigned int I2C_Count=0;
status = I2caRegs.I2CISRC.all;
if(status == 0x05) // 传输数据准备好
{
}
else if(status ==0x02) //无响应条件检测
{
}
else if(status ==0x04) // 接收数据准备好
{
buf[xx] = I2caRegs.I2CDRR;
if(buf[0]==0x01) xx=xx+1;
else xx=0;
if(xx>=8)
{
I2C_Count=0;
Delay(200);
I2CWriteData();
Delay(500);
I2caRegs.I2CMDR.bit.IRS = 0;
Delay(100);
I2caRegs.I2CMDR.all = 0x0020;
xx=0;
for(ii=0;ii<8;ii++)
{
buf[ii]=0;
}
}
}
PieCtrlRegs.PIEACK.bit.ACK8 = 1;
}
int I2CWriteData()
{
unsigned int Count=0;
if(I2caRegs.I2CMDR.bit.STP == 1) //停止状态
return 2;
I2caRegs.I2CSAR = 0x01; //存储被I2C主机发送到从机的从机地址
I2caRegs.I2CCNT = 8; //发送数据字节数
while(Count<8)
{
Delay(200);
I2caRegs.I2CDXR = Tx_Data[Count];
Count++;
}
I2caRegs.I2CMDR.all = 0x2620; //0b 0110 0110 1010 0000
return 0;
}
1、从示波器上看出,只能发送Tx_Data[7]的数据,前面的数据没有发出去。
void I2CA_Init(void)
{
// Initialize I2C
I2caRegs.I2COAR = 0x01;
I2caRegs.I2CMDR.bit.IRS = 0;
I2caRegs.I2CPSC.all = 5; I2C输入时钟(SYSCLKOUT)/(IPSC+1)=60M/(5+1)=10M
I2caRegs.I2CCLKL = 120;
I2caRegs.I2CCLKH = 120; //Fscl = =40K
I2caRegs.I2CIER.bit.RRDY = 1;
I2caRegs.I2CIER.bit.NACK = 0;
I2caRegs.I2CIER.bit.XRDY = 1;
I2caRegs.I2CMDR.all = 0x0020;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>