这是我的第一个I2C项目。好像出了问题。从属者没有接收正确的数据。下面提供了主和从两个代码。任何人都可以调试
主代码
*/
包括:
UIT8 8WRDATA〔2〕={0xAA,0xBB};
外部UIT8标志;
空主程序()
{
*将初始化/启动代码放在此处(例如MyS
timSistAd())*/
囊状的;
ISRYTXXSTART();
LCDX START();
I2C*START();
*取消注释此行以启用全局中断。*/
(;)
{
If(FLAG=1)
{
旗=0;
I2CJMistCurrStaseUs[/];/*清除任何先前状态*/
I2CJMistWrrestBuf(0x08,(Unt8*)WrDATA,2,I2CyMoDEi完全EXEXEXXFER);
(;)
{
如果(0U)!=(I2CJMistStaseUs)和(I2CYMSTATEA WRES-CMPLT)
{
*传输完成。检查主机状态以确保传输
无错误完成。*/
断裂;
}
}
LCDA- PrimtIn 8(WrDATA〔0〕);
}
*将您的应用程序代码放在这里。*/
}
}
/*[]文件结束*/
从属码
包括:
空主程序()
{
UIT8I;
UITN8 WRBUF〔10〕;
UIT8用户阵列〔10〕;
UIT8 8;
囊状的;
*在调用i2c*Stc**之前初始化写入缓冲器
I2CySLaveInWruteBuf((Unt8*)WrBuf,10);
LCDX START();
/*启动I2C从操作*
I2C*START();
*等待I2C主控器完成写入*/
对于(;;)/*循环永远**
{
*等待I2C主控器完成写入*/
如果(0U)!=(I2CySavavestUsUs)和(I2CysStista WRES-CMPLT)
{
BytCNT= I2CySLaveGeWruteBuffsiz();
I2CySLaveCaveRealStestAtUsUs();
对于(i=0;i<BytCNT;I++)
{
用户数组
=WrBuf;/*传输数据*/
}
I2CySLaveCaveRealeBuffe();
LCDA PrimtIn 8(USER数组〔0〕);
}
}
}
/*[]文件结束*/
以上来自于百度翻译
以下为原文
This is my first I2C project. Seems like something going wrong. Slave is not receiving correct data. The code is provided below for both master and slave. Can anyone debug
Master code
*/
#include
uint8 wrData[2]={0xAA, 0xBB};
extern uint8 flag;
void main()
{
/* Place your initialization/startup code here (e.g. MyInst_Start()) */
CyGlobalIntEnable;
isr_Tx_Start();
LCD_Start();
I2C_Start();
/* Uncomment this line to enable global interrupts. */
for(;;)
{
if(flag==1)
{
flag=0;
I2C_MasterClearStatus(); /* Clear any previous status */
I2C_MasterWriteBuf(0x08, (uint8 *) wrData, 2, I2C_MODE_COMPLETE_XFER);
for(;;)
{
if(0u != (I2C_MasterStatus() & I2C_MSTAT_WR_CMPLT))
{
/* Transfer complete. Check Master status to make sure that transfer
completed without errors. */
break;
}
}
LCD_PrintInt8(wrData[0]);
}
/* Place your application code here. */
}
}
/* [] END OF FILE */
Slave code
#include
void main()
{
uint8 i;
uint8 wrBuf[10];
uint8 userArray[10];
uint8 byteCnt;
CyGlobalIntEnable;
/* Initialize write buffer before call I2C_Start */
I2C_SlaveInitWriteBuf((uint8 *) wrBuf, 10);
LCD_Start();
/* Start I2C Slave operation */
I2C_Start();
/* Wait for I2C master to complete a write */
for(;;) /* loop forever */
{
/* Wait for I2C master to complete a write */
if(0u != (I2C_SlaveStatus() & I2C_SSTAT_WR_CMPLT))
{
byteCnt = I2C_SlaveGetWriteBufSize();
I2C_SlaveClearWriteStatus();
for(i=0; i < byteCnt; i++)
{
userArray = wrBuf; /* Transfer data */
}
I2C_SlaveClearWriteBuf();
LCD_PrintInt8(userArray[0]);
}
}
}
/* [] END OF FILE */
0