我不需要完整的代码,我从MCC插件生成的代码和我使用的代码一样。如果有人处理过MCC生成的I2C代码或者有任何示例代码,那么我需要很少的帮助来完成我的代码。代码M张贴。我是第一个从ESP开始的地址,他们两次写,一个是EEPROM的BW地址,下一个是数据。但是PIC不是在输入数据。定义了I2C1SLaveEX地址0x08,定义了I2C1SLaveEf掩码0x7fTyPulfEnUM {SLaveY-NealthyDATA,SLaveY-DATAAL地址,SLaveWWrdEnEdAdAyType;易失性UIT88T I2C1SLaveWreDeDATA=0x55;无效I2C1YSTATESULCALBACK(I2C1SLaveEx DRIVILION I2CY BuSUBSTATE);无效I2C1ILICALIZE(空){//初始化硬件/ /SMP标准速度;CSP1STAT= 0x80;//SSPN启用;CSPP禁用;SSP1 CON1= 0x26;/ACKEN禁用;GCEN禁用;笔禁用;ACKDT确认;RSEN禁用;RCEN禁用;SEN禁用;SSP1CON2= 0x00;//SBCDE;禁用;BOEN禁用;PCIE禁用;DHEN禁用;SDAHT 100NS;AHEN禁用;SSP1CON3= 0x00;//SSPMSK 127;SSP1MSK =(I2C1SLaveEyMask&L.<1);/ /调整UI掩码为R/NW位//SSPADD 8;SSP1ADAD=(I2C1SLaveEX地址和lt;lt;1);/调整R/NW位/ /的UI地址清除从中断标志Pr3BITS.SSP1IF=0;/ /启用主中断PIE3BITS.SSP1IE=1;}无效I2C1YISR(空隙){UIT88T I2CYDATA=0X55;/Note:从属驱动程序将始终确认/ /或任何地址匹配。PrI3BITS.SSP1IF=0;/ /清除从中断标志I2CyDATA=SSP1BUF;//读取SSPBUF以清除BF IF(1=SSP1STATBITS .RYNW){if((1=SSP1STATBIT.DYNA)和AMP;(1=SSP1CON2BIT.ACKSTAT){{//回调例程)可以执行任何后读取处理I2C1YSTATUSTCALLACK(I2C1SLaveEx Read SypCl)}{//CalpRead例程应将数据写入SSPBUF I2C1YSTATISULCALBACK(I2C1SLaveE.Read请求);}否则(0=SSP1STATBITS.DYNA){//这是I2C地址/ /回调例程应准备从主I2C1YSTATUS回调接收数据(I2C1SLaveWrWrreEngRead请求);} {I2C1SLaveWordEdAt= I2CyDATA;//回调例程应从主I2C1YSTATESCALLBACK(I2C1SLaveWrRead Error完成)处理I2C1SLaveWrad EDATA;} SSP1CON1BITS.CKP=1;//释放SCL}//端I2C1IISR()空I2C1StaseSCALLBACK(I2C1SLaveEI DRIVILION I2CY BuSUBSTATE){静压UIT88T EEPROMSPUBUffer[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f, 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f, 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f, 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f, 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f, 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f, 0x70,0x71,0x72,0x73,4,0x76,0x78,0x79,0x79,0x7a,0x7c,0x7d,0x7d,0x7e,0x7f};静态uTn8St SLaveWrreType=SLaveEngEngRoad数据;交换机(I2CbBuSuthStand){CaseI2C1SLaveWrWrreEngRead://主将发送EEPROM地址下一个SLaveWrreType=SavaveDATAY地址;BRIA. 0xx案例I2C1SLaveWrWreWEX完成:交换机(SLaveWreDype):EEPROMENTATION = I2C1SLaveWrad Dead;Sturn;SLaveEnEngimoDATA数据:默认值://主机有写入数据存储在EEPROM EEPROMETH缓冲器[EEPROMEDATION++] = I2C1SLaveWreDeDATA;a)(sieOf(EEPROMPEGROM)& lt;=EEPROMENT地址){EEPROM地址} 0;//开始EEPROM页中断;}//Stand开关(SLaveWrreType)SLaveWrreType=SlaveEngRead数据;Stage;I2C1SLaveE.Read请求:SSP1BUF=EEPROMETHOLD [EEPROMEDATION+++ ];IF(SIEPROFF缓冲器)& lt;= EEPROMADEclipse)EEPROM地址=0;//Eclipse启动EEPROM页};案例I2C1SLaveE.Read完成:默认:;//Enter开关(I2cBuSuthStand)}空main(空隙){StaseIn RealAlgIsIe();中断TypGraceDebug();而(1){}}空缺LeDyLink(UIT88T数据){if(data=0x80){Le0}=Re0;γ-DelayyMS(500);}空隙中断中断(中断)管理器(空隙){//中断处理程序if(iNCONTITES.PEI= 1){if(Py3BITSSP1IE=1和&;PIP3BITS.SSP1IF=1){I2C1YISR());否则{{//未处理中断}} { / /未处理中断}}
以上来自于百度翻译
以下为原文
I don't want full code, I generated the code from MCC plugin The same code I am using. if somebody had worked on I2c code generated from MCC or had any example code then I want little help to complete my code. code m posting. i am seding address first from esp and they two times write i.e. one will bw address of eeprom and next will be data. but pic is not intrpriting the data.
#define I2C1_SLAVE_ADDRESS 0x08
#define I2C1_SLAVE_MASK 0x7F
typedef enum
{
SLAVE_NORMAL_DATA,
SLAVE_DATA_ADDRESS,
} SLAVE_WRITE_DATA_TYPE;
volatile uint8_t I2C1_slaveWriteData = 0x55;
void I2C1_StatusCallback(I2C1_SLAVE_DRIVER_STATUS i2c_bus_state);
void I2C1_Initialize(void)
{
// initialize the hardware
// SMP Standard Speed; CKE disabled;
SSP1STAT = 0x80;
// SSPEN enabled; CKP disabled; SSPM 7 Bit Polling;
SSP1CON1 = 0x26;
// ACKEN disabled; GCEN disabled; PEN disabled; ACKDT acknowledge; RSEN disabled; RCEN disabled; SEN disabled;
SSP1CON2 = 0x00;
// SBCDE disabled; BOEN disabled; SCIE disabled; PCIE disabled; DHEN disabled; SDAHT 100ns; AHEN disabled;
SSP1CON3 = 0x00;
// SSPMSK 127;
SSP1MSK = (I2C1_SLAVE_MASK << 1); // adjust UI mask for R/nW bit
// SSPADD 8;
SSP1ADD = (I2C1_SLAVE_ADDRESS << 1); // adjust UI address for R/nW bit
// clear the slave interrupt flag
PIR3bits.SSP1IF = 0;
// enable the master interrupt
PIE3bits.SSP1IE = 1;
}
void I2C1_ISR ( void )
{
uint8_t i2c_data = 0x55;
// NOTE: The slave driver will always acknowledge
// any address match.
PIR3bits.SSP1IF = 0; // clear the slave interrupt flag
i2c_data = SSP1BUF; // read SSPBUF to clear BF
if(1 == SSP1STATbits.R_nW)
{
if((1 == SSP1STATbits.D_nA) && (1 == SSP1CON2bits.ACKSTAT))
{
// callback routine can perform any post-read processing
I2C1_StatusCallback(I2C1_SLAVE_READ_COMPLETED);
}
else
{
// callback routine should write data into SSPBUF
I2C1_StatusCallback(I2C1_SLAVE_READ_REQUEST);
}
}
else if(0 == SSP1STATbits.D_nA)
{
// this is an I2C address
// callback routine should prepare to receive data from the master
I2C1_StatusCallback(I2C1_SLAVE_WRITE_REQUEST);
}
else
{
I2C1_slaveWriteData = i2c_data;
// callback routine should process I2C1_slaveWriteData from the master
I2C1_StatusCallback(I2C1_SLAVE_WRITE_COMPLETED);
}
SSP1CON1bits.CKP = 1; // release SCL
} // end I2C1_ISR()
void I2C1_StatusCallback(I2C1_SLAVE_DRIVER_STATUS i2c_bus_state)
{
static uint8_t EEPROM_Buffer[] =
{
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,
0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f
};
static uint8_t eepromAddress = 0;
static uint8_t slaveWriteType = SLAVE_NORMAL_DATA;
switch (i2c_bus_state)
{
case I2C1_SLAVE_WRITE_REQUEST:
// the master will be sending the eeprom address next
slaveWriteType = SLAVE_DATA_ADDRESS;
break;
case I2C1_SLAVE_WRITE_COMPLETED:
switch(slaveWriteType)
{
case SLAVE_DATA_ADDRESS:
eepromAddress = I2C1_slaveWriteData;
break;
case SLAVE_NORMAL_DATA:
default:
// the master has written data to store in the eeprom
EEPROM_Buffer[eepromAddress++] = I2C1_slaveWriteData;
LED_blink(I2C1_slaveWriteData);
if(sizeof(EEPROM_Buffer) <= eepromAddress)
{
eepromAddress = 0; // wrap to start of eeprom page
}
break;
} // end switch(slaveWriteType)
slaveWriteType = SLAVE_NORMAL_DATA;
break;
case I2C1_SLAVE_READ_REQUEST:
SSP1BUF = EEPROM_Buffer[eepromAddress++];
if(sizeof(EEPROM_Buffer) <= eepromAddress)
{
eepromAddress = 0; // wrap to start of eeprom page
}
break;
case I2C1_SLAVE_READ_COMPLETED:
default:;
} // end switch(i2c_bus_state)
}
void main(void)
{
SYSTEM_Initialize();
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
while (1)
{
}
}
void LED_blink(uint8_t Data)
{
if(Data == 0x80){
LED0 = ~LED0;
__delay_ms(500);
}
}
void interrupt INTERRUPT_InterruptManager (void)
{
// interrupt handler
if(INTCONbits.PEIE == 1)
{
if(PIE3bits.SSP1IE == 1 && PIR3bits.SSP1IF == 1)
{
I2C1_ISR();
}
else
{
//Unhandled Interrupt
}
}
else
{
//Unhandled Interrupt
}
}