I2C介紹
I2C裡有兩個角色,一個是Master,另一個是Slave。Master的角色只能有一個,而Slave可以有很多個。每個Slave都有自己的位址(address)編號,所以當Master要與特定的Slave溝通時,就會使用這個位址。
I2C使用了兩根接腳,一根是資料線(SDA),另一根是時脈線(SCL)。Master藉由控制SCL讓Slave知道有東西要傳,而資料就可以在SDA上面傳送。
I2C在當初Arduino的範例裡,為了親易近人而稱呼I2C為Wire,雖然後來Arduino也承認取名為Wire看來是個錯誤,但在Arduino的example仍然常常看到。
(Wire取名的細節請參考 https://www.arduino.cc/en/Reference/APIStyleGuide , section “Use full, everyday words.”)
I2C使用了兩根接腳,一根是資料線(SDA),另一根是時脈線(SCL)。Master藉由控制SCL讓Slave知道有東西要傳,而資料就可以在SDA上面傳送。
I2C在當初Arduino的範例裡,為了親易近人而稱呼I2C為Wire,雖然後來Arduino也承認取名為Wire看來是個錯誤,但在Arduino的example仍然常常看到。
(Wire取名的細節請參考 https://www.arduino.cc/en/Reference/APIStyleGuide , section “Use full, everyday words.”)
材料準備
Ameba x 1
Arduino UNOx 1
範例說明
這個範例裡,我們將Ameba當作I2C Master Writer, 而Arduino為I2C Slave Receiver。
I2C Master會傳字串到I2C Slave,並且I2C Slave將字串印出來。
準備Arduino Uno為 I2C Slave
首先我們先準備 Arduino,先將Arduino IDE選成Arduino, “Tools” -> “Board” -> “Arduino/Genuino Uno”:
I2C Master會傳字串到I2C Slave,並且I2C Slave將字串印出來。
首先我們先準備 Arduino,先將Arduino IDE選成Arduino, “Tools” -> “Board” -> “Arduino/Genuino Uno”:
接著打開 Slave Receiver 的範例,"Examples" -> "Wire" -> "slave_receiver"
然後點選 “Sketch” -> “Upload” 將程式編譯並上傳到 Arduino Uno。
我們額外再開一個Arduino IDE,確定板子是 Ameba, “Tools” -> “Board” -> “Arduino Ameba”
然後打開 Master Writer 的範例, “File” -> “Examples” -> “AmebaWire” -> “master_writer”
然後點選 “Sketch” -> “Upload” 將程式編譯並上傳到 Ameba。
Arduino的範例裡,使用A4當作I2C SCL, A5為I2C SDA。這些接腳與Ameba一樣。
另外很重要的是,兩塊板子的GND要互接。
打開包含Arduino Uno的Arduino IDE,開啟Serial Monitor, “Tools” -> “Serial Monitor”, 會出現Serial Monitor的視窗,這個視窗可以讓你看到Arduino Uno印出來的訊息。
接著按下Arduino Uno的Reset按鈕,讓Arduino Uno等待I2C master連接。
再按下Ameba的Reset按鈕,讓Ameba開始傳訊息。這時候會看到Serial Monitor每隔0.5秒出現新的訊息,這樣就代表成功了。
程式碼說明
Ameba使用同樣的Arduino範例,Ardunio官方網站的範例說明可以參考這裡:
https://www.arduino.cc/en/Tutorial/MasterWriter
首先Wire.begin()會先設定address, 如果是Master可以不用設定address:
https://www.arduino.cc/en/Reference/WireBegin
接著Master使用Wire. beginTransmission()來設定要傳到哪一個Slave:
https://www.arduino.cc/en/Reference/WireBeginTransmission
使用Wire.write()送資料, 並且最後使用Wire.endTransmission()將所有資料傳出去:
https://www.arduino.cc/en/Reference/WireEndTransmission
https://www.arduino.cc/en/Tutorial/MasterWriter
首先Wire.begin()會先設定address, 如果是Master可以不用設定address:
https://www.arduino.cc/en/Reference/WireBegin
接著Master使用Wire. beginTransmission()來設定要傳到哪一個Slave:
https://www.arduino.cc/en/Reference/WireBeginTransmission
使用Wire.write()送資料, 並且最後使用Wire.endTransmission()將所有資料傳出去:
https://www.arduino.cc/en/Reference/WireEndTransmission
Introduction of I2C
There are two roles in the operation of I2C, one is "Master", the other is "Slave". Only one Master is allowed, and can be connected to many Slaves. Each Slave has its unique address, which is used in the communication between Master and the Slave.
I2C uses two pins, one is for data transmission (SDA), the other is for the clock (SCL). Master uses the SCL to inform Slave of the upcoming data transmission, and the data is transmitted through SDA.
The I2C example was named "Wire" in the Arduino example. Although Arduino admitted this name seems to be a mistake, yet the name "Wire" often appear in the examples of Arduino.
(Detail of the name "Wire": https://www.arduino.cc/en/Reference/APIStyleGuide>, section “Use full, everyday words.”)
I2C uses two pins, one is for data transmission (SDA), the other is for the clock (SCL). Master uses the SCL to inform Slave of the upcoming data transmission, and the data is transmitted through SDA.
The I2C example was named "Wire" in the Arduino example. Although Arduino admitted this name seems to be a mistake, yet the name "Wire" often appear in the examples of Arduino.
(Detail of the name "Wire": https://www.arduino.cc/en/Reference/APIStyleGuide>, section “Use full, everyday words.”)
Materials
Ameba x 1
Arduino UNOx 1
Procedure
In this example, we use Ameba as the I2C Master Writer, and use Arduino as the I2C Slave Receiver.
When the I2C Slave receives string sent from I2C Master, it prints the received string.
Setting up Arduino Uno to be I2C Slave
First, select Arduino in the Arduino IDE in "Tools" -> "Board" -> "Arduino/Genuino Uno":
When the I2C Slave receives string sent from I2C Master, it prints the received string.
First, select Arduino in the Arduino IDE in "Tools" -> "Board" -> "Arduino/Genuino Uno":
Open the "Slave Receiver" example in "Examples" -> "Wire" -> "slave_receiver":
Then click "Sketch" -> "Upload" to compile and upload the example to Arduino Uno。
Open another Arduino IDE window, make sure to choose Ameba in the IDE: "Tools" -> "Board" -> "Arduino Ameba"
Then open the "Master Writer" exmaple, "File" -> "Examples" -> "AmebaWire" -> "master_writer"
The Arduino example uses A4 as the I2C SCL and A5 as the I2C SDA, we can use the same pins on Ameba.
Another important thing is that the GND pins of Arduino and Ameba should be connected to each other.
Open the Arduino IDE of the Arduino Uno, and open the Serial Monitor ("Tools" -> "Serial Monitor").
In the Serial Monitor, you can see the messages printed from Arduino Uno.
In the Serial Monitor, you can see the messages printed from Arduino Uno.
Next, press the reset button on Arduino Uno. Now the Arduino Uno is waiting for the connection from I2C Master.
We press the reset button on Ameba to start to send messages. Then observe the Serial Monitor, you can see the messages show up every 0.5 second.
Code Reference
You can find detailed information of this example in the documentation of Arduino:
https://www.arduino.cc/en/Tutorial/MasterWriter
First use Wire.begin()/Wire.begin(address) to join the I2C bus as a master or slave, in the Master case the address is not required.
https://www.arduino.cc/en/Reference/WireBegin
Next, the Master uses Wire.beginTransmission(address) to begin a transmission to the I2C slave with the given address:
https://www.arduino.cc/en/Reference/WireBeginTransmission
Uses Wire.write() to send data, and finally use Wire.endTransmission() to end a transmission to a Slave and transmits the bytes that were queued:
https://www.arduino.cc/en/Reference/WireEndTransmission
https://www.arduino.cc/en/Tutorial/MasterWriter
First use Wire.begin()/Wire.begin(address) to join the I2C bus as a master or slave, in the Master case the address is not required.
https://www.arduino.cc/en/Reference/WireBegin
Next, the Master uses Wire.beginTransmission(address) to begin a transmission to the I2C slave with the given address:
https://www.arduino.cc/en/Reference/WireBeginTransmission
Uses Wire.write() to send data, and finally use Wire.endTransmission() to end a transmission to a Slave and transmits the bytes that were queued:
https://www.arduino.cc/en/Reference/WireEndTransmission







0 comments:
Post a Comment