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講義:藍芽模組、Keypad密碼鎖 ## Intoduction of UART * **UART** 全文為 **Universal Asynchronous Receiver/Transmitter** ,即**通用非同步收發傳輸器**,是一種硬體介面。 * 通用非同步收發傳輸器是一種異步收發傳輸器 ![](http://wiki.csie.ncku.edu.tw/embedded/USART_DataTransmission.gif) * **單工、半雙工與全雙工** | 單工 | 半雙工 | 全雙工 | | -------- | -------- | -------- | |![](https://i.imgur.com/NkVAamw.jpg)|![](https://i.imgur.com/rICbdJp.jpg) |![](https://i.imgur.com/RBbG5hN.jpg)| |只支持信號在一個方向上傳輸(正向或反向),任何時候不能改變訊號的方向。 EX:計算機和印表機之間的通信、B.B.Call。 | 允許信號在兩個方向上傳輸,但同一時間只允許信號在一個信道上單向傳輸。EX:傳統的對講機。 |允許數據同時在兩個方向上傳輸,即有兩個信道,因此允許同時進行雙向傳輸。 EX:電話、手機、RS232。 | * 原文網址:https://kknews.cc/zh-tw/other/y2av3rj.html * **串列 v.s 並列** * 串列 ![](https://i.imgur.com/iR4wqGW.jpg) * 一次傳送一個 bit,傳輸速率較慢、成本較低、可遠距離傳輸。 * EX :滑鼠、鍵盤、USB、HDMI、RS-232 介面。 * 並列 ![](https://i.imgur.com/WqHNHVN.jpg) * 一次傳送多個 bit,傳輸速率快、成本高、易受雜訊干擾,僅限短距離使用。 * EX :25Pin D-Type印表機、顯卡PCI介面。 * 注意:並列傳輸的線路較多,但傳輸速度不一定比較快(因為接收端要同時將傳進來的資料整合及排列,虛耗額外的位元及處理時間) * **非同步 (Asynchronous) v.s 同步 (Synchronous)** * 非同步 ![](https://i.imgur.com/gvCSISx.jpg) * 雙方有各自的時脈, 在傳送資料時插入額外資訊,表示資料起始、結束。優點是設定時間短、硬體成本低、機器時脈不同也能傳資料,缺點是單次傳輸的資料量較少。 * EX : RS-232 (實現方式簡單低廉)、 RS-422 (較長距離的傳輸)、RS-485 (較多的裝置連接數目)。 * 同步 ![](https://i.imgur.com/cDlmMqx.jpg) * 雙方共用時脈(一次只做一件事),額外提供時脈訊號,使兩端機器在溝通時能夠藉此同步收發資料。比起非同步傳輸,同步傳輸不需要 start/stop bit ,因此能夠一次傳較多的資料。 * EX : I2C 、 SPI 。 * **UART** * 全雙工 * 串列 * 非同步 * 由2個獨立的接收 (RX) 、發送 (TX) 緩衝器構成,可同時發送和接收資料。 * 鮑率即每秒鐘多少位元bits per second (bit/s,EX:1鮑即指每秒傳輸1個符號。 * 若 baud 為 9600 (bits/sec),表示每秒可以傳 960 Bytes 的資料量。 * UART 傳送1 Byte (=8 bits)資料,加上頭尾共為10 bits。 $$\frac{9600(bits)}{startbit(1 bits)+data(8 bits)+stop bit(1 bits)}= 960Bytes$$ [參考資料](https://zh.wikipedia.org/wiki/%E6%B3%A2%E7%89%B9%E7%8E%87) ## UART Protocol ![](https://i.imgur.com/EFrYOMa.png) * 每個符號由三種資料共10個位元所組成,共分為: 1. 起始位元 (Start Bit) 2. 資料內容 (Data) 3. 停止位元 (Stop Bit) * 平時沒用時UART的TX、RX腳都是處於高電位,每個資料在傳送前都會有一個低電位的起始位 (start bit) ;資料傳完之後會接著一組停止位元 (stop bit)通常為1位元,少數為2位元。 * 因此,若假設資料長度為 8bits,加上頭尾,每傳 1byte 至少需要 10bits。 * [UART參考](http://makerpro.cc/2016/04/understand-what-is-uart/) --- ## UART register description * **Receive buffer** : 只能讀出不能寫入。 * **Transmitter buffer** :只能寫入不能讀出。 * **Interrupt Enable Register** : CPU寫入暫存器告訴UART何時中斷。 ### UCSR0A(USART Control and Status Register 0 A) ![](https://i.imgur.com/DSOuooD.png) * 它是一個暫存器,用於指示串行通信的控制和狀態。 * **RXC0** ( USART Receive Complete ) : 當**接收緩衝區**中存在未讀取的數據時,旗標設為1。用來知道何時能收到一個位元組。 * **TXC0**( USART Transmit Complete ) : 當**發送緩衝區**中發送數據時,**TXC0**( USART 發送完成 )變為1。 * **UDRE0** ( USART Data Register Empty ) : 當數據可寫入**傳輸緩衝區**時,**UDRE0**(USART數據暫存器為空)變為1。 ### UCSR0B(USART Control and Status Register 0 B) ![](https://i.imgur.com/H73A9Qc.png) * 它同樣是一個暫存器,用於指示串行通信的控制和狀態。 * **UCSR0B** 暫存器比較重要的是 **RXEN0** 和 **TXEN0**,這兩個位元用來啟用接收器 (receiver) 和發射器 (transmitter),一般來說,我們會啟用這兩個元,可以用來開啟傳送、接收的位元。 * **UDRIE0** (USART Data Register Empty Interrupt Enable 0) : 決定要不要啟用中斷的位元。如果 **UDRIE0** 設置為 1,則允許UDRIE0標誌中斷。 ### UDR0(USART I/O Data Register 0) ![](https://i.imgur.com/4BfEyf4.png) * 它是用來存放資料的暫存器。 * **TXB** 和 **RXB** 具有相同的 I/O address (共用UDR0)。 * 當寫入資料到 **UDR0** 時,資料會寫入**TXB register**。 * 當要讀取 **UDR0** 的內容時,實際上是從 **RXB register** 讀取內容。 --- ## Bluetooth * 藍芽模組 HC-05、HC-06,使用 UART 介面與 Ardunio 溝通。 * 藍芽模組也可以視為一塊 MCU 並且可以透過 UART 介面進行設定。 * 設定方式: * 透過 USB to UART 轉換電路 ,從電腦開啟 Serial Port 做寫入動作。 * 利用 Ardunio uno 開發板本身就有的 IC 來做轉換即可寫入 AT 指令。 #### 藍芽開發環境設定: * 接線: | Arduino | 藍芽模組 | 備註 | | -------- | -------- | -------- | | 5V | VCC |注意電源不可接錯 | | GND | GND |注意電源不可接錯 | | RXD(pin10)| TXD | | | TXD(pin11)| RXD | | ![](https://i.imgur.com/pDl8ZEn.png) ![](https://i.imgur.com/IazhLIr.png) * 程式碼: ```c++= #include <SoftwareSerial.h> SoftwareSerial BTSerial(10, 11); // 宣告10腳位為Arduino的RX 、11為Arduino的 TX char val; //儲存接受到的資料變數 void setup() { Serial.begin(9600); BTSerial.begin(9600); //注意,HC-05要設定成38400 } void loop() { // 若收到「序列埠監控視窗」的資料,則送到藍牙模組 if (Serial.available()) { val=Serial.read(); BTSerial.print(val); } // 若收到藍牙模組的資料,則送到「序列埠監控視窗」 if (BTSerial.available()) { val=BTSerial.read(); Serial.print(val); } } ``` ### 進入AT模式的方法說明: 注意:若要進入AT模式則,要先確保不能有任何裝置與藍芽模組連線。 以下分為HC-05與HC-06說明: ### HC-05: * **同時按住紅框鈕及接上 5v 即可進入 AT 模式。** 在尚未與裝置連線之前,HC-05板子上的LED快速閃爍,成功進入AT模式後,LED將兩秒閃爍一次。 ![](https://i.imgur.com/K11ITRH.png) * **Serial Port設定:** * 打開 Serial Port。 * Serial Monitor 的鮑率選**9600**,另外一個選項要選 **NL&CR**,即可下 AT 指令。 * 輸入 AT 指令第一次會出現 ERROR:(0),第二次輸入 AT 出現 OK ,即可進入 AT 模式。 * 詳細AT指令請參考以下AT指令表。 ![](https://i.imgur.com/2QKLsch.png) ### HC-06: * HC-06與任何裝置連線之前,只要有通電,都是在AT模式的狀態。因此要進入AT模式,只須**確保HC-06沒有與其他裝置相連即可。** * **Serial Port設定:** * 打開 Serial Port。 * Serial Monitor 的鮑率選**9600**,另外一個選項要選 **沒有結尾行**,即可下 AT 指令。 * 輸入 AT 出現 OK ,即可進入 AT 模式。 * 詳細AT指令請參考以下AT指令表。 ![](https://i.imgur.com/S2VfocG.png) ### AT指令表: * **AT指令版本HC-05:** | 指令 | 序列埠回應 | 功能 | | -------- | -------- | -------- | | AT | OK | 確認藍芽與序列埠溝通正常 | |AT+UART? | +UART:XXXXX,0,0| 得知目前Baud rate設定 | |AT+ADDR? | +ADDR:98d3:b1:fd6ece| 查詢address | | AT+VERSION| 版本資訊 | 查看韌體版本 | | AT + NAME=XXXX | OK | 設定藍芽名稱 | | AT + PSWD=XXXX | OK | 設定密碼 | | AT + UART=38400,0,0 (鮑率 , 停止位元 , 同位位元) | OK |設定鮑率為38400 | * **AT指令版本HC-06** : | 指令 | 序列埠回應 | 功能 | | -------- | -------- | -------- | | AT | OK | 確認藍芽與序列埠溝通正常 | | AT+VERSION| 版本資訊 | 查看韌體版本 | | AT+NAMEXXX | OKsetname | 設定藍芽名稱 | |AT+PINXXXX | OKsetPIN | 設定密碼 | ### 將手機與藍芽模組連接: * 若是**HC-05**,請先確認不是在AT模式,在AT模式將無法配對。 * 將手機藍芽開啟,點選搜尋附近裝置,找到自己的藍芽名稱後進行配對。 * 確認配對成功後,利用 Android 手機下載“Serial Bluetooth Terminal”。 ![](https://i.imgur.com/nDhrXpD.png) * 進入該應用程式,點選左上方多功鍵,點選"Devices"。 * 確認自己手機的藍芽有開啟後,選定裝置後按下裝置,即可連線。 * 連線後呈現以下圖示: ![](https://i.imgur.com/X3Eh8PN.png) * <font color="red">成功連線時,藍芽模組上的警示燈呈快閃兩次循環。</font> :::info 藍芽燈狀態: 1. 不停閃爍:正常模式/等待連接模式 2. 2秒亮2秒滅(HC-05): AT 模式/配置模式 3. 每秒閃爍2次:連接成功 ::: ### 確認藍芽與手機溝通正常: * 在serial port 輸入test,則手機上會顯示:![](https://i.imgur.com/oYpRNUi.png) * 在手機上輸入test,serial port會顯示: ![](https://i.imgur.com/U0hyCLe.png) ### 若顯示出現亂碼: * 進入AT模式設定更改鮑率設定。 * 確認Serial port 介面是否正確設定。 * 確認程式藍芽連線速率是否正確設定。 --- # Lab1: ### 藍芽實作 * 實驗目的:學習使用藍芽控制。 * 實驗目標:透過手機藍芽來控制led明滅燈。 * 結果: <iframe width="916" height="458" src="https://www.youtube.com/embed/Th8U-sRj4uk" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> # Lab2: ### 藍芽實作 * 實驗目的:學習使用藍芽控制LED跑馬燈。 * 實驗目標:使用五顆LED燈與藍芽模組, 輸入0:停止目前動作。(除非輸入0,否則以下動作必須不斷重複執行) 輸入1:LED跑馬燈。 輸入2:LED燈由左而右點亮,全部點亮後由左而右依序熄滅。 輸入3:全部LED燈一起點亮。 * 結果: <iframe width="916" height="458" src="https://www.youtube.com/embed/8ZcgkfJ3loE" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> # Lab3: ### 密碼鎖實作 * 密碼鎖涵式庫ZIP檔:[Password Library](https://drive.google.com/drive/folders/1brIT2wvlJBPT8Vh8zl58tQ71kJK1dL0V?usp=sharing) * 函式庫說明: https://playground.arduino.cc/Code/Password/ * 實驗目的:學習使用Keypad及製作密碼鎖。(不一定要使用函式庫) * 實驗目標:自訂一個密碼(如1234),使用Keypad來輸入值,輸入完成後按A送出,若輸入值與密碼相同則印出Unlock!,錯誤則印出Wrong passwords!。 * 程式碼: ![](https://i.imgur.com/OYH56xc.png) # 加分題: ### 加分LAB1:藍芽控制碼錶 * 實驗目的:使用藍芽模組以及四位七段顯示器,實現藍芽控制碼錶。 * 實驗目標:設計一個四位數的碼錶,按下1則計時暫停,按下0則計時歸0。 * 結果:<iframe width="916" height="458" src="https://www.youtube.com/embed/I8OHp1oCR8o" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> ### 加分LAB2:進階密碼鎖 * 實驗目的:使用Keypad及密碼鎖函式庫,完善密碼鎖功能。 * 實驗目標:增加以下功能, 1.輸入兩次正確密碼才能解鎖。 2.可重設密碼。 按A為送出輸入的值,按B為重設密碼。 * 結果:![](https://i.imgur.com/Aoca2k9.png) ### 加分LAB3:進階密碼鎖藍芽版 * 實驗目的:使用藍芽模組及密碼鎖函式庫,完善密碼鎖功能。 * 實驗目標:承上題,但將鍵盤輸入改為使用手機藍芽輸入。 ## 課後問題 (3個) :::info * **Q1.** 請說明非同步傳輸中的"RS-232","RS-422"以及"RS-485",並討論這三者的差別為何? * **Q2.** 請說明 **HC-05** 作為 **master** 以及 **slave** 的功用是什麼? * **Q3.** 下方為Password.cpp,請為其做完整的註解。 ```c= #include "Password.h" //construct object in memory, set all variables Password::Password(char* pass){ set( pass ); reset(); } //set the password void Password::set(char* pass){ target = pass; } //evaluate a string, is it equal to the password? bool Password::is(char* pass){ byte i=0; while (*pass && i<MAX_PASSWORD_LENGTH){ guess[i] = pass[i]; i++; } return evaluate(); } //append a char to the guessed password bool Password::append(char character){ if (currentIndex+1==MAX_PASSWORD_LENGTH){ return false; }else{ guess[currentIndex++] = character; guess[currentIndex] = STRING_TERMINATOR; //ensure a valid c string } return true; } //reset the guessed password, one can guess again void Password::reset(){ currentIndex = 0; guess[currentIndex] = STRING_TERMINATOR; } //is the current guessed password equal to the target password? bool Password::evaluate(){ char pass = target[0]; char guessed = guess[0]; for (byte i=1; i<MAX_PASSWORD_LENGTH; i++){ //check if guessed char is equal to the password char if (pass==STRING_TERMINATOR && guessed==STRING_TERMINATOR){ return true; //both strings ended and all previous characters are equal }else if (pass!=guessed || pass==STRING_TERMINATOR || guessed==STRING_TERMINATOR){ return false; //difference OR end of string has been reached } //read next char pass = target[i]; guessed = guess[i]; } return false; //a 'true' condition has not been met } bool Password::check(){ int i=-1; if(strlen(target)==strlen(guess)){ do{ i++; if(target[i]>guess[i]) return 1; else if(target[i]<guess[i]) return 0; }while(target[i]==guess[i]); } else if(strlen(target)>strlen(guess)) return 1; else if(strlen(target)<strlen(guess)) return 0; } ``` ::: :::success ## 作業繳交格式 **W4結報_第XX組.zip** 壓縮檔裡包含: 1.W(週數)結報.pdf 2.資料夾:W(週數) Lab1.ino Lab2.ino ... :::

    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