void initialize(void) {
PORTE = 0x00; // Clear PORTC
TRISE = 0b11000000; // 使用I2C3 E6,E7口
I2C3CONbits.I2CEN=1; //使能I2C
I2C3CONbits.A10M=0; //七位地址
I2C3CONbits.GCEN = 1; // 广播呼叫使能
I2C3CONbits.STREN = 1; // 接收时,始终可延长
IEC5bits.SI2C3IE=1; //允许I2C3从接口中断
IPC21=1;//最高中断级别
I2C3ADD = I2C_slave_address; // Load the slave address
}
void __attribute__((interrupt,no_auto_psv)) _SI2C3Interrupt(void)
//void isr(void)
{
if (IFS5bits.SI2C3IF) // check to see if SSP interrupt
{
if (I2C3STATbits.R_W) // 主设备读取数据
{
if (!I2C3STATbits.D_A) // 开始读数据的初始地址
{
//SSPBUF = I2C_Array[index_i2c++]; // load with value from array,发送数据
// I2C3RCV = 0xff;
I2C3TRN= 0xff;
// index_i2c= SSPBUF;
// if (index_i2c ==2)
// {
// SSPBUF =0x12;
// }
I2C3CONbits.SCLREL = 1; // Release CLK
}
if (I2C3STATbits.D_A) // Last byte was data (D_nA = 1)
{
// I2C3RCV = data[p++]; // load with value from array,发送数据
I2C3TRN= data[p++];
//SSPBUF =0x14;
I2C3CONbits.SCLREL = 1; // Release CLK
}
}
if (!I2C3STATbits.R_W) // 主设备写入数据
{
if (!I2C3STATbits.D_A) // Last byte was an address (D_nA = 0)
{
first = 1; //last byte was address, next will be data location
junk = I2C3RCV; // read buffer to clear BF
I2C3CONbits.SCLREL = 1; // Release CLK
}
if (I2C3STATbits.D_A) // Last byte was data (D_nA = 1)
{
if (first)
{
index_i2c = I2C3RCV; // load index with array location
first = 0; // now clear this since we have
tongxunpanduan(index_i2c); // 读出命令进行判断
} //location to read from/write to
else
{
if (index_i2c < RX_ELMNTS) // make sure index is not
{ //out of range of array
I2C_Array[index_i2c++] = I2C3RCV; //load array with data
}
else
{
junk = I2C3RCV; //array location not valid, discard data
}
}
if (I2C3STATbits.IWCOL) // Did a write collision occur?
{
I2C3STATbits.IWCOL = 0; // clear WCOL
junk = I2C3RCV; // dummy read to clear BF bit
}
I2C3CONbits.SCLREL = 1; // Release CLK
}
}
}
if (I2C3STATbits.BCL) // Did a bus collision occur?
{
junk = I2C3RCV; // dummy read SSPBUF to clear BF bit
I2C3STATbits.BCL = 0; // clear bus collision Int Flag bit
I2C3CONbits.SCLREL = 1; // Release CLK
}
IFS5bits.SI2C3IF= 0; // clear SSPIF flag bit
}// end of ISR
使用的IIC3口,就是不进中断呀。。。哪位帮看看 |