109單晶片
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: 單晶片助教 --- # Arduino講義:電算機 Serial Communication (I2C) --- <!-- > **重點 or 縮寫** 請標**黑粗** 且 英文記得空格ㄚㄚㄚㄚ --> <!-- > --> <!-- > [name=YuanHaoHo][color=#c8ff05] --> ## Introduction of I2C * I2C bus 為 Inter-Integrated Circuit Bus 的縮寫,中文叫做積體電路匯流排,其功用為提供 IC 之間的連接與通訊。 * 目的:減少MCU與周邊晶片之間的腳位數 * 只有兩條訊號線,<font color="red">SCL(Serial CLock Line) 負責傳時脈,SDA(Serial Data line)負責傳資料</font>。 * **I2C** : <font color="red"> 半雙工、串列、同步</font> * 以匯流排型介接允許有多個master以及slave。 ![](https://i.imgur.com/jOQudK1.png) * 由上圖可知,所有I2C裝置皆並接(Wired-AND)在SCL及SDA兩條線路上 * 須接<font color="red">上拉電阻(Rp)</font>,將浮接狀態( float )轉變為邏輯高準位( High )狀態。 ## I2C Bus協定 * 閒置( idle )時兩者皆為高電位。 * START condition:<font color="red">SCL為 HIGH時,SDA發生下緣</font> ![](https://i.imgur.com/X4RwG5W.gif) * 先傳地址( slave address,決定跟哪個 slave 溝通),再傳資料。地址有 7bit跟 10bit兩種。 * 7bit:最大可以有 128個 slave (但實際上有16個系統擴充位址是保留的)。 * 10bit:最大可以有 1024個 slave 。(但很少見,不是所有使用 I2C 的裝置都支援 10bit mode)。 * 地址:由於地址為 7bit,因此在傳第一個 byte的時候,前 7bit(MSB 為 address,最後的 bit 為 R/W bit,指定要"讀"或"寫"資料(0:write,1:Read)。 * 只在 <font color="red">SCL 為 HIGH 時,才會對 SDA 取樣。 SCL 為 LOW 時,不會取樣,但可改變資料</font>。 * STOP condition:<font color="red">SCL為 HIGH時,SDA發生上緣</font> ## ACK/NACK機制 * 發送端在每組(8bits)訊號送出後,需讀去接收端所回應的一個ACK bit(Acknowledge bit) <font color="red">(注意:發送端不一定是master,ex:讀取資料時,發送端為slave)</font> ## 實際傳輸的狀況 * 寫入: ![](https://i.imgur.com/ThlOXjN.gif) * R/W為0時,代表 slave 端要讀取master端資料,此時 slave 會發出一個 ==acknowledge bit==(就是由 slave 把 SDA 拉低),讓 master 確認 slave 是否有收到資料。 * 讀取: ![](https://i.imgur.com/PRWtSDN.gif) * R/W 為1時,代表 master 端要讀取 slave 端資料,此時 master 為 Receiver,slave 為 transmiter,因此 acknowledge bit 會是由 master 進行發送。 :::info note:傳地址後的 acknowledge bit 必為 slave 傳送給 master;傳送 Data 後的 acknowledge bit 則須視前面 R/W 的值而定。 ::: * 當 master 讀到 ACK 回應, 即可以繼續傳輸下一個 Byte。 * 當 master 讀到 NACK 回應, 意思是 **slave 無回應**,此時 master 應送出 (結束) 訊號放棄該命令。 * 參考資料: * [資料來源1](http://i2c.info/i2c-bus-specification) * [資料來源2](http://magicjackting.pixnet.net/blog/post/173061691-i2c-bus-%E7%B0%A1%E4%BB%8B-(inter-integrated-circuit-b)) --- #### Wire函數: | 函數 | 作用 | | --- | --- | | begin()| 啟動 Wire 作為 master 或是 slave | | beginTransmission(address)| 開始對 address 的連線 | | endTransmission() |關閉之前的連線 | | write(data) | 在連線上送出 一個 byte 的資料 | |read() | 讀取連線上的一個 byte 的資料 | |available() | 檢查連線上是否有可接收的資料 | |onReceive(function) | 設定用來接收資料的函數 | :::info note:在無(address)時,Arduino 是作為I2C的 master;有(address)時,是作為 I2C 的 slave 。 ::: <!-- :::info note: Arduino 沒有 I2C ,只有**2 wire serial interface(TWI)** ,但是這個TWI**相容於飛利浦的I2C協定**。詳見 [ATmega328P datasheet](http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-328P_Datasheet.pdf) p.260。 ::: --> ## LAB1 * 實驗目標:利用I2C進行Arduino間通訊 * schematic: ![](https://lh3.googleusercontent.com/Zuw2QyEtARMvNPyQhm72J0vKZbImM-SgRYTSvgxM_vK2aZaJSFz80pgbLX-CxtcEfyTvyur7rJBPvsqy19RRLYB-49Q0CsMxrd2Ygr1VRxxlyDRu8R2J_v2MvzWFI_iLIxEEUv-ppXdPGpPDfU0j5hzEeOyPlAZy78A-5by_OYQgobjxNRHDdEkDBpoaGpu3vdg3qsbXvBQihFtwOCf9cUvcYH26-bDkTCXM_WvvChB7QyY-vsmyhk_bJTb36AbqxnTcxlTY392h-PM0fRj0Sv-d5aXFYoxPgee8KlnGzP2yVS5E0xQfTRMcWl04polf8nYHATMgbF2JqHKa_b2o5kra9LAID0sARbarG42-hMdeFmz-Y_EnAefclJ0jOMWV3S7WaRizZG3JBXi9KDz66GjlzuwSrk7w2ogbBAHqfAJGeyDOWlcabNk8JON_REXswwdF4IHfYn72Yo7fF-O5FsaeGjgFrWl9erxqvgAhX6JknX6PYhSBMZt6uXwS_OgxCZ-tz6mntN5y27mjjVSvfZkjJ6pnVemA6m9ivJfQkWeDsSzEslzneN4CiHhauXeeM1Glly42qNm973rxCADrB1KYeSR2--4ym61XbOYuDz1NEWU0bUuCyS3X95I8NH7w7eiEccBbQYGaPRd74r5GOuxTepcNqMTf1kXbydyBhvvOHbFT1pSXxrtT=w941-h740-no) * master 把串口收到的資料,發送給 slave: ![](https://i.imgur.com/DlWNGZv.png) * slave 端直接處理接收回來的資料: ![](https://i.imgur.com/jZSH5V9.png) :::warning 若使用的 Arduino IDE 版本不能同時開兩個 Serial Monitor,請[點此](http://ttssh2.osdn.jp/)下載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介紹 ![](https://i.imgur.com/AOFuvA4.png) * SDA – 接 Arduino 的 Analog Pin 4 (Arduino Mega 為 Pin 20) * SCL – 接 Arduino 的 Analog Pin 5 (Arduino Mega 為 Pin 21) * GND – 接 GND * VCC – 接 +5V ## LCD接法 ![](https://i.imgur.com/1YCyEvb.png) ## 學習使用LCD貼出字串 * LCD I2C Libraries下載:[here](https://drive.google.com/file/d/11hV6xfWycVe5Hg2bahq_UgWgoPMLBCK9/view?usp=sharing) * Keypad Libraries下載:[here](https://drive.google.com/file/d/1mnzf--VuktjYytxyOUm0J5sxz0J_YV6g/view?usp=sharing) * code: ![](https://i.imgur.com/VnLFbL9.png) ## LAB2 * 實驗目標:將上面的字串改成自己的學號,使其以如下影片的跑馬燈方式繞行。 * #### LiquidCrystal_I2C: | 函數 | 作用 | | --- | --- | | lcd.begin(cols, rows); | 定義LCD的長寬(n列×n行) | | lcd.clear() | 清空LCD | | lcd.setCursor(col, row); | 設定輸入值起始位置 | | lcd.print(data); | 在螢幕上顯示資料 | | lcd.scrollDisplayRight(); | 將畫面向右捲動 | | lcd.scrollDisplayLeft(); | 將畫面向左捲動 | * 參考影片: <iframe width="800" height="600" src="https://www.youtube.com/embed/5w0y1xAFMtc" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> ## LAB3 ### 簡易計算機 * 實驗目標:利用薄膜鍵盤與LCD實現簡易計算機 * 實驗要求: * A 鍵為加號、B 鍵為減號、C 為乘號、D 為除號、# 為等於、* 為清除鍵。 * 由薄膜鍵盤輸入數字後,LCD 第一列顯示運算式,第二列顯示等號與運算結果。 * code hint: ![](https://i.imgur.com/LPe36Gu.png) * 示範影片: <iframe width="800" height="600" src="https://www.youtube.com/embed/-bGQgrrkhio" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> ## LAB4 ### 四則運算計算機 * 實驗目標:利用薄膜鍵盤與LCD實現四則運算計算機 * 實驗要求: * A 鍵為加號、B 鍵為減號、C 為乘號、D 為除號、# 為等於、* 為清除鍵。 * 由薄膜鍵盤輸入數字後,LCD 第一列顯示運算式,第二列顯示等號與運算結果,並使其具有四則運算功能 * code hint: ![](https://i.imgur.com/Qu6Iuan.jpg) * 示範影片: <iframe width="800" height="600" src="https://www.youtube.com/embed/6is9SvDOb7o" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> ## 課後問題 :::info Q1.簡述 I2C 如何藉由 SCL 與 SDA 傳送資料 ? Q2.上課的時候提到可以有多個master,請問若同時有兩個以上的master送出訊號會發生什麼事?並且描述該如何解決。 Q3.電算機為何總和超過 32767 時字串會溢位?如何解決 ? ::: :::success ## 作業繳交格式 **W4結報_第XX組.zip** 壓縮檔裡包含: 1.W(週數)結報.pdf 2.資料夾:W(週數) Lab3.ino Lab4.ino *LAB3、LAB4需附上註解 :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully