打印
[AVR单片机]

熟悉TWI做I2C的看过来.疑问...

[复制链接]
3289|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
newmankinds|  楼主 | 2011-2-15 16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
//*************************************************
uchar TWI_Transceiver_Busy( void )
{
  return ( TWCR & (1<<TWIE) );                  // IF TWI Interrupt is enabled then the Transceiver is busy
}
//***************************************************
看到人家写的程序AVR315里的.TWIE为TWI中断使能位,为什么用来检测是否是忙啊?

相关帖子

沙发
chenczy| | 2011-2-16 10:18 | 只看该作者
你还是多看下资料吧
    TWCR=0xa4;            //开始
    while((TWCR&0x80)==0);
      TWDR=0xa0;            
      TWCR=0x84;
      while((TWCR&0x80)==0);
      TWDR=(address>>8)&0xff;
      TWCR=0x84;
      while((TWCR&0x80)==0);  
      TWDR=address;
      TWCR=0x84;
      while((TWCR&0x80)==0);
这是我做的TWI的开头部分,是读数的

使用特权

评论回复
板凳
newmankinds|  楼主 | 2011-2-16 12:23 | 只看该作者
楼上检测的是TWINT,是可以理解的.
我之所以疑问,就是看到与人这么写.才不理解....ATMEL的官网上的是检测TWIE..很不理解

使用特权

评论回复
地板
yfei_tech| | 2011-2-16 14:50 | 只看该作者
M48规格书218页:

1. The first step in a TWI transmission is to transmit a START condition. This is done by
writing a specific value into TWCR, instructing the TWI hardware to transmit a STARTcondition. Which value to write is described later on. However, it is important that the
TWINT bit is set in the value written. Writing a one to TWINT clears the flag. The TWI will
not start any operation as long as the TWINT bit in TWCR is set. Immediately after the
application has cleared TWINT, the TWI will initiate transmission of the START condition.
2. When the START condition has been transmitted, the TWINT Flag in TWCR is set, and
TWSR is updated with a status code indicating that the START condition has success-
fully been sent.
3. The application software should now examine the value of TWSR, to make sure that the
START condition was successfully transmitted. If TWSR indicates otherwise, the applica-
tion software might take some special action, like calling an error routine. Assuming that
the status code is as expected, the application must load SLA+W into TWDR. Remember
that TWDR is used both for address and data. After TWDR has been loaded with the
desired SLA+W, a specific value must be written to TWCR, instructing the TWI hardware
to transmit the SLA+W present in TWDR. Which value to write is described later on.
However, it is important that the TWINT bit is set in the value written. Writing a one to
TWINT clears the flag. The TWI will not start any operation as long as the TWINT bit in
TWCR is set. Immediately after the application has cleared TWINT, the TWI will initiate
transmission of the address packet.
4. When the address packet has been transmitted, the TWINT Flag in TWCR is set, and
TWSR is updated with a status code indicating that the address packet has successfully
been sent. The status code will also reflect whether a Slave acknowledged the packet or
not.
5. The application software should now examine the value of TWSR, to make sure that the
address packet was successfully transmitted, and that the value of the ACK bit was as
expected. If TWSR indicates otherwise, the application software might take some special
action, like calling an error routine. Assuming that the status code is as expected, the
application must load a data packet into TWDR. Subsequently, a specific value must be
written to TWCR, instructing the TWI hardware to transmit the data packet present in
TWDR. Which value to write is described later on. However, it is important that the
TWINT bit is set in the value written. Writing a one to TWINT clears the flag. The TWI will
not start any operation as long as the TWINT bit in TWCR is set. Immediately after the
application has cleared TWINT, the TWI will initiate transmission of the data packet.
6. When the data packet has been transmitted, the TWINT Flag in TWCR is set, and TWSR
is updated with a status code indicating that the data packet has successfully been sent.
The status code will also reflect whether a Slave acknowledged the packet or not.
7. The application software should now examine the value of TWSR, to make sure that the
data packet was successfully transmitted, and that the value of the ACK bit was as
expected. If TWSR indicates otherwise, the application software might take some special
action, like calling an error routine. Assuming that the status code is as expected, the
application must write a specific value to TWCR, instructing the TWI hardware to transmit
a STOP condition. Which value to write is described later on. However, it is important that
the TWINT bit is set in the value written. Writing a one to TWINT clears the flag. The TWI
will not start any operation as long as the TWINT bit in TWCR is set. Immediately after
the application has cleared TWINT, the TWI will initiate transmission of the STOP condi-
tion. Note that TWINT is NOT set after a STOP condition has been sent.
Even though this example is simple, it shows the principles involved in all TWI transmissions.
These can be summarized as follows:
• When the TWI has finished an operation and expects application response, the TWINT Flag is
set. The SCL line is pulled low until TWINT is cleared.
• When the TWINT Flag is set, the user must update all TWI Registers with the value relevant for
the next TWI bus cycle. As an example, TWDR must be loaded with the value to be transmitted
in the next bus cycle.
• After all TWI Register updates and other pending application software tasks have been
completed, TWCR is written. When writing TWCR, the TWINT bit should be set. Writing a one
to TWINT clears the flag. The TWI will then commence executing whatever operation was
specified by the TWCR setting.

使用特权

评论回复
5
smt0755| | 2011-2-17 17:10 | 只看该作者
:handshake 谢谢分享。

使用特权

评论回复
6
newmankinds|  楼主 | 2011-2-17 20:32 | 只看该作者
没说可以啊

使用特权

评论回复
7
yfei_tech| | 2011-2-18 18:24 | 只看该作者
不是说的很清楚了么:
The TWI will not start any operation as long as the TWINT bit in TWCR is set
只要TWI工作,TWINT 必须设置啊。
那么你要检测TWI是否在工作状态,只需检测TWINT 就可以了。

使用特权

评论回复
8
newmankinds|  楼主 | 2011-2-21 22:41 | 只看该作者
我想的和你说的一样.问题是我奇怪人家的为什么不是检测TWINT而是TWIE位.....这才是我的问题....请看清楚...

使用特权

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

本版积分规则

42

主题

99

帖子

4

粉丝