打印

新手,关于I2C问题求教

[复制链接]
1108|10
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
刀鱼|  楼主 | 2016-10-14 15:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 刀鱼 于 2016-10-14 17:05 编辑

在调一个g2553板子工程,其中有用到I2C通信,写了一个I2C主机写多个字节的函数
问题是这样的,第一个字节被写入缓冲 UCB0TXBUF 之后,中断标志IFG2的 UCB0TXIFG 位被置0了。
正常情况下,不是收到STOP指令后中断标志UCB0TXIFG才被置0吗?
求大神指导!
下面是我的I2C主机写多个字节的函数:
static int8_t I2CSendBytes(uint8_t I2CSlaveAddress, uint8_t *DataBuffer, uint16_t ByteCount, uint16_t *SentByte)
{
    uint16_t DelayCounter = 0;
    uint16_t NumberOfBytesSent = 0;
    uint8_t *DataPointer;

    UCB0CTL0 |= UCMST;  // set master
    UCB0I2CSA = I2CSlaveAddress;  // 从机地址

     DataPointer = DataBuffer;

     UCB0CTL1 |= UCTR; //data in transmit direction
     UCB0CTL1 |= UCTXSTT; //Generate Start Condition // UCB0TXIFG was set 1 automatically
                          //Send Start Byte

     while(!(IFG2 & UCB0TXIFG))  //if UCTXSTT != 0, wait here // wait until the slave acknowladged
     {
         DelayCounter ++;
         if (DelayCounter >= DELAY_LIMIT)
             break;
     }

     if (DelayCounter >= DELAY_LIMIT)   //check if NACK condition occurred
     {
         *SentByte = NumberOfBytesSent;
         UCB0CTL1 |= UCTXSTP;
         return -1;
     }

     for(NumberOfBytesSent = 0; NumberOfBytesSent < ByteCount; NumberOfBytesSent++)
     {
         UCB0TXBUF= *DataPointer;  // 这一步执行后,中断标志UCB0TXFIG置0了

         DelayCounter = 0;

         while(DelayCounter < DELAY_LIMIT && (!(IFG2 & UCB0TXIFG) || (UCB0CTL1 & UCTXSTT)))  // 然后程序卡在这个循环里
         {
             DelayCounter++;
         }

         if (DelayCounter >= DELAY_LIMIT)   //check if NACK condition occurred
         {
             *SentByte = NumberOfBytesSent;
             UCB0CTL1 |= UCTXSTP;               //send stop condition
             return -1;
         }

         DataPointer++;
     }

     IFG2 &= ~UCB0TXIFG;
     UCB0CTL1 |= UCTXSTP;       //send stop bit

     DelayCounter = 0;

     while(DelayCounter < DELAY_LIMIT && ((UCB0CTL1 & UCTXSTP)))  //check if the byte has been sent
     {
         DelayCounter++;
     }

     *SentByte =  NumberOfBytesSent;

     if (DelayCounter >= DELAY_LIMIT)   //check if NACK condition occurred
     {
         UCB0CTL1 |= UCSWRST;
         return -1;
     }
     else
         return 0;
}




相关帖子

沙发
刀鱼|  楼主 | 2016-10-14 16:27 | 只看该作者
求帮助啊,着急上火中

使用特权

评论回复
板凳
pl202| | 2016-10-14 22:43 | 只看该作者
这个还真不好看,建议模拟IIC吧。

使用特权

评论回复
地板
pl202| | 2016-10-14 22:44 | 只看该作者
使用模拟IIC编写简单一些,而且方便调试。

使用特权

评论回复
5
dirtwillfly| | 2016-10-15 15:45 | 只看该作者
官网的mspware有提供iic主机通讯的例程,建议先参考一下

使用特权

评论回复
6
aspoke| | 2016-10-15 22:30 | 只看该作者
看看例程怎么配置的函数

使用特权

评论回复
7
aspoke| | 2016-10-15 22:33 | 只看该作者
pl202 发表于 2016-10-14 22:44
使用模拟IIC编写简单一些,而且方便调试。

这个直接编写iic不错。

使用特权

评论回复
8
刀鱼|  楼主 | 2016-10-17 10:25 | 只看该作者
dirtwillfly 发表于 2016-10-15 15:45
官网的mspware有提供iic主机通讯的例程,建议先参考一下

谢谢指导,我先学习一下例程的code,看看能不能找到原因

使用特权

评论回复
9
uptown| | 2016-10-17 22:06 | 只看该作者
配置的问题

使用特权

评论回复
10
uptown| | 2016-10-17 22:07 | 只看该作者
使用io编写一个iic

使用特权

评论回复
11
刀鱼|  楼主 | 2016-10-18 09:35 | 只看该作者

您的意思是指什么配置有问题呢?

使用特权

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

本版积分规则

1

主题

4

帖子

0

粉丝