找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 4139|回复: 1

关于lego与i2c通信的文章,看到 It is very simple and elegent就忍不住转来了

[复制链接]
发表于 2012-2-21 15:43:29 | 显示全部楼层 |阅读模式
Sensors in the LEGO NXT MINDSTORM are classified into three categories

1) Passive Sensors like touch etc.
2) Active sensors like LightSensors.
3) Digital sensors like Sonar.

These Digital sensors have a brain of their own i.e. a microcontroller in them. So they can make
directly communicate with the ARM7 processor in the NXT brick using I2C protocol.

I have spent almost a week learning the I2C protocol and then trying to understand how the communication takes place in the NXT. Initially I was baffled and thought LEGO has made the
communication complex to prevent and third parties from understanding it, but i guess I was
wrong . It is very simple and elegent... well almost.

An understanding of the I2C protocol is mandatory to continue reading. Also have the LEGO
HARDWARE Developers kit ready at hand.

The Hardware Developers Kit says that the sensors are treated as memory devices and each
transaction is treated as a read or write into a memory location.
So each digital sensor has two types of addresses:
1) An I2C address unique to each digital sensor
2) Internal Addresses in the sensor to wirte to or read from.

Taking the UltraSonic sensor as an example, its I2C address is 1(7 bit address) and has a
number of internal addresses as given in Appendix 7 of the Hardwares Developers Kit.

The NXT can only act as a master and the sensor is always the slave.

Its best to proceed with an example. To read the version of the Ultrasonic sensor, Appendix 7
says that the NXT, which is the master, must send the byte sequence as follows
1) 0x02 (which is the 7bit sensor address 0x01 and the 8th bit is a zero which indicates
that Master is Transmitted and Slave is receiver)
2) 0x00 (The internal address to read from. So now the slave i.e. the sensor knows that the
master is interested in the address location 0x00 but still does not know if its a read
or write transaction)
3) R + 0x03 (This was the most baffling part! What is R?? What is 0x03?? i guess a seasoned
I2C protocol expert will takes seconds to decode it. It took me well 2 hours and lot
of goggling :( . )

The R indicates "Repeated Start" which is a well know state in I2C protocol. The master does
not relinquish the bus but starts a new message. 0x03 is the 7bit device address (0x01 and the
8th bit is a 1 indicating that Master is Receiver and Slave is Transmitter). So now the slave
knows that in this transaction the NXT brick wants to read some data from a memory location.

If the third byte is not preceeded by a repeated Start, the NXT indicates that it is a write
transaction.

Understanding till here, I could use an ATMega8 controller and get the NXT brick to read a few memory locations.

Since I am feeling a bit lazy now and have written almost everything I wanted to shere.. I will
stop for now. I still need to figure out some tiny but important details like how the master knows
exactly how many bytes to read etc etc... will keep blogging once I get it cleared!
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
发表于 2012-2-21 21:34:16 | 显示全部楼层
I2C协议确实不复杂,呵呵 所以给NXT做新的sensor并不是太复杂的事情,只是厂家用的接头不太容易搞到
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

手机版|中文乐高 ( 桂ICP备13001575号-7 )

GMT+8, 2025-4-30 23:55 , Processed in 0.219326 second(s), 24 queries .

Powered by Discuz! X3.5

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表