Try   HackMD

[5] Arduino講義:電算機 Serial Communication (I2C)


Introduction of I2C

  • I2C bus 為 Inter-Integrated Circuit Bus 的縮寫,中文叫做積體電路匯流排,其功用為提供 IC 之間的連接與通訊。
  • 目的:減少MCU與周邊晶片之間的腳位數
  • 只有兩條訊號線,SCL(Serial CLock Line) 負責傳時脈,SDA(Serial Data line)負責傳資料
  • I2C : 半雙工、串列、同步
  • 以匯流排型介接允許有多個master以及slave。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 由上圖可知,所有I2C裝置皆並接(Wired-AND)在SCL及SDA兩條線路上
  • 須接上拉電阻(Rp),將浮接狀態( float )轉變為邏輯高準位( High )狀態。

I2C Bus協定

  • 閒置( idle )時兩者皆為高電位。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • START condition:SCL為 HIGH時,SDA發生下緣
  • 先傳地址( slave address,決定跟哪個 slave 溝通),再傳資料。地址有 7bit跟 10bit兩種。
    • 7bit:最大可以有 128個 slave (但實際上有16個系統擴充位址是保留的)。
    • 10bit:最大可以有 1024個 slave 。(但很少見,不是所有使用 I2C 的裝置都支援 10bit mode)。
  • 地址:由於地址為 7bit,因此在傳第一個 byte的時候,前 7bit 為 address,最後的 bit 為 R/W bit,指定要"讀"或"寫"資料(0:write,1:Read)。
  • 只有在 SCL 為 HIGH 時,才會對 SDA 取樣。 SCL 為 LOW 時,不會取樣,但可以改變資料
  • STOP condition:SCL為 HIGH時,SDA發生上緣

ACK/NACK機制

  • 發送端在每組(8bits)訊號送出後,需讀取接收端所回應的一個ACK bit(Acknowledge bit)
    (注意:發送端不一定是master,ex:讀取資料時,發送端為slave)

實際傳輸的狀況

  • 寫入:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    • R/W為0時,代表 slave 端要讀取master端資料,此時 slave 會發出一個 acknowledge bit(就是由 slave 把 SDA 拉低),讓 master 確認 slave 是否有收到資料。
  • 讀取:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    • R/W 為1時,代表 master 端要讀取 slave 端資料,此時 master 為 Receiver,slave 為 transmitter,因此 acknowledge bit 會是由 master 進行發送。

note:傳地址後的 acknowledge bit 必為 slave 傳送給 master;傳送 Data 後的 acknowledge bit 則須視前面 R/W 的值而定。

  • 當 master 讀到 ACK 回應, 即可以繼續傳輸下一個 Byte。
  • 當 master 讀到 NACK 回應, 意思是 slave 無回應,此時 master 應送出 (結束) 訊號放棄該命令。
  • 參考資料:

Wire函數:

函數 作用
begin() 啟動 Wire 作為 master 或是 slave
beginTransmission(address) 開始對 address 的連線
endTransmission() 關閉之前的連線
write(data) 在連線上送出 一個 byte 的資料
read() 讀取連線上的一個 byte 的資料
available() 檢查連線上是否有可接收的資料
onReceive(function) 設定用來接收資料的函數

note:在無(address)時,Arduino 是作為I2C的 master;有(address)時,是作為 I2C 的 slave 。

LAB1

  • 實驗目標:利用I2C進行Arduino間通訊
  • schematic:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • master 把串口收到的資料,發送給 slave:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • slave 端直接處理接收回來的資料:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 參考影片:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

若使用的 Arduino IDE 版本不能同時開兩個 Serial Monitor,請點此下載Tera Term後,點選設定做為第二個Serial Monitor。
使用方法:開啟Tera Term後,點選設定 >連接埠 >更改端口與鮑率

Arduino講義:LCD

LCD介紹

  • 目前市面上的 LCD 模組可分文字型及繪圖型兩種。文字型 LCD 模組常見的規格有16 字x1 行,20 字x1 行,40 字x1 行,16 字x2 行,20 字x2 行,40 字x2行等多種。

  • 文字型 LCD 顯示器內部具有字元產生器,因此可以接受 ASCII 字元碼,並將字元顯示在 LCD 上。

LCD架構

  • LCD 模組的結構如圖所示,由 LCD 控制器、LCD 驅動器及 LCD 顯示器所組成。目前市售 LCD 模組所採用的 LCD 控制器都相容,使用方法也相同,因此幾乎所有的 LCD 模組都具有相同的控制方法。

I2C 16x2 LCD介紹

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • SDA – 接 Arduino 的 Analog Pin 4 (Arduino Mega 為 Pin 20)
  • SCL – 接 Arduino 的 Analog Pin 5 (Arduino Mega 為 Pin 21)
  • GND – 接 GND
  • VCC – 接 +5V

LCD接法

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

學習使用LCD貼出字串

  • LCD I2C Libraries下載:here
  • Keypad Libraries下載:here
  • code:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

LAB2

  • 實驗目標:將上面的字串改成自己的學號,使其以如下影片的跑馬燈方式繞行。
  • LiquidCrystal_I2C:

函數 作用
lcd.begin(cols, rows); 定義LCD的長寬(n列×n行)
lcd.clear() 清空LCD
lcd.setCursor(col, row); 設定輸入值起始位置
lcd.print(data); 在螢幕上顯示資料
lcd.scrollDisplayRight(); 將畫面向右捲動
lcd.scrollDisplayLeft(); 將畫面向左捲動
  • 參考影片:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

LAB3

簡易計算機

  • 實驗目標:利用薄膜鍵盤與LCD實現簡易計算機(二項式加減乘除)

  • 實驗要求:

    • A 鍵為加號、B 鍵為減號、C 為乘號、D 為除號、# 為等於、* 為清除鍵。
    • 由薄膜鍵盤輸入數字後,LCD 第一列顯示運算式,第二列顯示等號與運算結果。
  • code hint:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • 示範影片:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Bonus

四則運算計算機

  • 實驗目標:利用薄膜鍵盤與LCD實現四則運算計算機(多項式四則運算)

  • 實驗要求:

    • A 鍵為加號、B 鍵為減號、C 為乘號、D 為除號、# 為等於、* 為清除鍵。
    • 由薄膜鍵盤輸入數字後,LCD 第一列顯示運算式,第二列顯示等號與運算結果,並使其具有四則運算功能
  • code hint:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • 示範影片:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

課後問題

Q1.簡述 I2C 如何藉由 SCL 與 SDA 傳送資料 ?
Q2.上課的時候提到可以有多個master,請問若同時有兩個以上的master送出訊號會發生什麼事?並且描述該如何解決。
Q3.電算機為何總和超過 32767 時字串會溢位?如何解決 ?

作業繳交格式

W5結報_第XX組.zip
壓縮檔裡包含:
1.W(週數)結報.pdf
2.資料夾:W(週數)
Lab1-1.ino (Master)
Lab1-2.ino (Slave)
Lab2.ino
Lab3.ino
Bonus.ino

*LAB3與Bonus需附上註解