打印
[PIC®/AVR®/dsPIC®产品]

使用PIC24FJ192GB106做为从IIC读写的问题

[复制链接]
1484|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
zjm499767|  楼主 | 2015-4-11 17:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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口,就是不进中断呀。。。哪位帮看看
沙发
HAPPY小越越| | 2015-8-18 13:22 | 只看该作者
请问楼主,现在问题解决了么??怎么处理的呢?、我也遇到了问题,在第一步地址匹配的时候就出现了问题,导致后边永远没法进中断,请帮忙解答!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

11

帖子

0

粉丝