打印

DSP28027的I2C读写程序

[复制链接]
1996|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
engtafanzhuan|  楼主 | 2013-12-31 22:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
下面是我自己根据LMP9100还有TI给的例程自己写的程序,TI提供的例程用到了中断,我的没有用中断方式读写。帮我看下有错误没有,第二次读或者写的是就过不去了。一直查询总线是否忙,可是我已经都把STP为置1了。为什么总线还是忙呢?

相关帖子

沙发
engtafanzhuan|  楼主 | 2013-12-31 22:52 | 只看该作者
void LMP91000_Init(void)
{
   // Initialize I2C
   I2caRegs.I2CSAR = 0x0048;        // LMP9100 Slave address
   // I2CCLK = SYSCLK/(I2CPSC+1)

     I2caRegs.I2CPSC.all = 6;       // Prescaler - need 7-12 Mhz on module clk

   I2caRegs.I2CCLKL = 55;           // NOTE: must be non zero
   I2caRegs.I2CCLKH = 35;            // NOTE: must be non zero
   I2caRegs.I2CMDR.all = 0x0020;    // Take I2C out of reset                              
      // Stop I2C when suspended
   I2caRegs.I2CFFTX.all = 0x6000;   // Enable FIFO mode and TXFIFO
   I2caRegs.I2CFFRX.all = 0x2040;   // Enable RXFIFO, clear RXFFINT,
   return;
}
void LMP91000_Write(Uint16 address,Uint16 data)
{
TXByteCtr=2;         //发送多少位
   // Wait until the STP bit is cleared from any previous master communication.
   // Clearing of this bit by the module is delayed until after the SCD bit is
   // set. If this bit is not checked prior to initiating a new message, the
   // I2C could get confused.
   while (I2caRegs.I2CMDR.bit.STP == 1)
    I2caRegs.I2CSAR = 0x0048;
   while (I2caRegs.I2CSTR.bit.BB == 1);
I2caRegs.I2CCNT=TXByteCtr;
I2CBufferArray[1] = address;                 //先存储发送LMP91000需要写数据的寄存器地址到中间数组
I2CBufferArray[0] = data;      //再存储发送LMP91000需要到寄存器地址的数据到中间数组
I2caRegs.I2CDXR = I2CBufferArray[1];
I2caRegs.I2CDXR = I2CBufferArray[0];
    I2caRegs.I2CMDR.all = 0x6e20;      //使能I2C,主模式写,7位地址,internal data counter of the I2C module counts down to 0产生STOP
    return;
}
Uint16 LMP91000_Read(Uint16 address)
{
   // Wait until the STP bit is cleared from any previous master communication.
   // Clearing of this bit by the module is delayed until after the SCD bit is
   // set. If this bit is not checked prior to initiating a new message, the
   // I2C could get confused.
    while (I2caRegs.I2CMDR.bit.STP == 1);
    I2caRegs.I2CSAR = 0x0048;
       I2caRegs.I2CCNT = 1;
       I2caRegs.I2CDXR = address;
    I2caRegs.I2CMDR.all = 0x6e20;   //发送start位,主模式写,internal data counter of the I2C module counts down to 0产生STOP
     while (I2caRegs.I2CSTR.bit.BB == 1);  //总线是否忙
     I2caRegs.I2CSAR = 0x0048;
       I2caRegs.I2CCNT = 1;
       I2caRegs.I2CMDR.all = 0x6c20;  //发送start位,主模式读,不产生stop
       if(I2caRegs.I2CSTR.bit.NACKSNT == 1) //如果I2C模块是否发送发送NACK
       {
        I2caRegs.I2CMDR.bit.STP =1;    //stop位置1
        I2caRegs.I2CSTR.bit.NACKSNT=1;清除NACK标志位
     I2CBuffer = I2caRegs.I2CDRR;
       }
  return I2CBuffer;         //返回接收到的值
}

使用特权

评论回复
板凳
zhangmangui| | 2014-1-1 12:15 | 只看该作者
看看LMP9100这边是不是一直拉着总线

使用特权

评论回复
地板
拿起书本| | 2014-1-3 10:46 | 只看该作者
可以单步调试看看,确定寄存器写入是否在在成功。

使用特权

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

本版积分规则

80

主题

958

帖子

0

粉丝