81
逢甲大學 自動控制工程學系專題製作 超音波自動測距系統設計與應用 The Automatic Ranging System Design and Application Based on Ultrasonic Technology 指導教授:張興政 生:丁蒼毅 向榮威 中華民國九十四年五月

逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

逢甲大學

自動控制工程學系專題製作

專 題 論 文

超音波自動測距系統設計與應用

The Automatic Ranging System Design and Application Based on Ultrasonic

Technology

指導教授張興政 學 生丁蒼毅

向榮威

中華民國九十四年五月

感謝

這些日子以來承蒙 張興政博士的細心指導使學生能快速地進入專題研

製狀況並且在學生最須要幫助方向錯誤的時候拉學生一把除此之外還

經常教導我們待人接物的道理以及生活上應有的觀念再次特別感謝吾師

另外還要謝謝實驗室的學長明哲國驊同現彥儒偉民俊嘉郭鴻以

及煜亮每當遇到瓶頸時學長們總會不時過來關心進度為我們打氣還有

幫助過我們的同學感謝一路有你們互相扶持謝謝

i

中文摘要

近年來自走式機器人或自走車的研究及相關技術運用相當廣泛為了完成指

派任務機器人必須具備有基本運動以及感測周遭環境的能力在許多場合都需

要避免機器碰撞的發生以防止意外造成傷害因此本文針對距離的感測進行研

究以基本的超音波測距為基石發展出一套容易與其他系統整合的超音波測距

模組本文利用價格便宜之超音波感測器來設計自動距離量測系統此系統不僅

能量測距離亦能將量測數據做自動存檔之功能人機介面是利用Visual Basic

程式語言撰寫經R-S232串聯傳輸至電腦硬體是利用產生振盪源來觸發超音

波當接收端有信號進來即中止計數器並將計數值利用RS-232傳送到電腦

進行距離計算與分析

ii

Abstract

Recently the researches and the relative technology about autonomous mobile

robots or vehicles are widely used in many fields In order to complete the assignment

robots must have the ability of moving and sensing In addition preventing robots

from collision is quite important Therefore we are going to do research on the

distance measurement based on ultrasonic technology and then develop a module

which can be easily combined into different systems This thesis presents a method to

design an automatic ranging system by using the cheap ultrasonic transducer This

system not only does measurements but also save data automatically In reference to

the software we take advantage of Visual Basic to build the interface The ultrasonic

source is generated by an oscillator The oscillator will trigger the transducer to

generate ultrasonic wave In the other hand a designed electronic circuit is used to

deal with the reflective wave Finally the time of flight will be estimated and then

sent to the computer by the microprocessor

iii

目錄

感謝i

中文摘要ii

Abstracthellip iii

目錄iv

圖目錄vi

表目錄hellipviii

第一章 緒論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip1

11 研究動機及目的1

12 研究方法1

13 文獻回顧helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip3

第二章 基礎理論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

21 超音波特性helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

22 超音波感測原理helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip5

第三章 超音波測距系統硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

31 系統硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

32 微處理單晶片簡介helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

33 電源電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

34 超音波發射驅動電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

35 超音波接受端電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

351 主動式帶通濾波器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

352 電壓比較器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

36 RS-232 串聯通訊介面helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

361 RS-232 使用說明helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

362 串列資料格式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip19

iv

363 串列埠通訊鮑率helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

364 訊號準位轉換helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

第四章 超音波測距系統軟體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip21

41 電腦監控程式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip21

42 畫面規劃helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip23

第五章 實驗分析與討論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

51 超音波測距電路helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

52 超音波測距測試分析helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip33

53 超音波測距系統應用helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

第六章 結論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

61 研究成果helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

62 未來展望helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

參考文獻helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip38

附錄一 微處理機程式碼helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip39

附錄二 Visual Basic 程式碼helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip44

v

圖目錄

圖 11 超音波自動測距系統設計與應用流程圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip2

圖 21 人耳可以感受到的頻率範圍與振動的強度關係helliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

圖 31 系統架構方塊圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

圖 32 8051 單晶片腳位圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip8

圖 33 電源電路(+12V)helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

圖 34 常用 555振盪器電路helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip10

圖 35 NE555 應用電路設計圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip10

圖 36 NE555 內部方塊圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

圖 37 10cm內電壓與距離關係圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

圖 38 主動式帶通濾波器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

圖 39 主動式帶通濾波器之波德圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip16

圖 310 主動式帶通濾波器之波德圖(中心頻率3998kHz)helliphelliphelliphelliphelliphelliphelliphelliphellip16

圖 311 電壓比較器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

圖 312 RS-232 準位轉換專用 IC ndashMAX232 helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

圖 41 人機介面程式流程圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip22

圖 42 串列埠輸入區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip23

圖 43 單位換算區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 44 資料輸入區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 45 分析結果區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 46 數值讀取區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip25

圖 47 面積體積模擬區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip25

圖 48 掃描顯像區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 49 開啟通訊埠關閉通訊埠系統結束圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 410 通訊埠指示燈圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

vi

圖 411 選擇操作模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 412 基本檢測模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 413 Microsoft Excel之儲檔圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 414 面積體積模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 415 面積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 416 體積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 417 掃描顯像模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 418 掃描顯像模式執行順序圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip30

圖 51 實際量測距離與平均百分誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

圖 52 超音波測距硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 53 電源電路實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 54 發射及接收電路板實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip33

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體helliphelliphelliphelliphelliphelliphellip33

圖 56 基本檢測模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip34

圖 57 距離量測數據分析與平均誤差helliphelliphelliphelliphellip35

圖 58 動態旋轉量測上視圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

圖 59 掃瞄顯示模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

vii

表目錄

表 21 超音波感測器的檢出方式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip6

表 31 RS正反器真值表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip11

表 32 超音波相對式距離電壓關係表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

表 33 RS-232 9PIN 腳位和 25PIN 腳位定義helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip18

表 51 實際距離量測誤差分析表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

表 52 距離量測數據分析與平均誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

viii

第一章 緒論

11 研究動機及目的

近年來關於自走式機器人或自走車的研究及相關運用技術相當廣泛機器人的研發

主要目的之一是為了取代人力使人類避免身陷於危險之中或加快工作任務完成的速

度例如在農業上可利用機器人噴灑農藥水產養殖及田間作業hellip等在工業上基

於安全的考量也可利用機器人作為探勘的工具或用來減少人力支出以降低成本並

使意外傷害減到最低如核能發電場消防救災太空探測及物料搬運hellip等

為了完成我們所指派的任務機器人必須具備有基本運動以及周遭環境感測的能

力在無人操作的情況之下隨著環境的變動能作出邏輯判斷並進行自我調整的功能

以完成所付予的工作因此機器人須要配備有不同的感測器如溫度感測器振動感測

器CCD感測器和超音波感測器hellip等以獲取外在的環境資訊

本文中將針對距離的感測做電子電路的設計以完成價格低廉的距離感測器模組

距離量測可使用雷射或超音波雷射優點為精準度極高但價格相當昂貴在此將使用

容易取得且價格便宜之超音波感測器來完成本專題研究目的在於建立一超音波自動

測距系統

12 研究方法

整個系統架構的完整規劃以繪製系統流程圖展示主要分為硬體架構和軟體

設計可摘要分項敘述於下所有分項工作及軟硬體設計整體相關性請參考所附

之研究流程圖如圖 11 所示並以此流程圖為藍圖完成本專題研究

1 收集充足的相關文獻資料與整理分析

2 驗證超音波驅動電路之設計

3 微波信號之接收電路設計與測試以期發展最佳化的電路設計

4 超音波飛行時間之數據傳輸電路與介面設計

5 距離量測系統與 PC 通訊傳輸系統之連結

1

文獻收集與整理

接收端信號放大濾波設計收集軟體資料 超音波觸發振盪器設計

介面技術與周邊程式設計

人機介面程式撰寫

軟體測試

超音波量測系統模型建立

帶通濾波器測試

振盪器測試

電壓比較器設計

比較器測試

系統整合

系統分析與改善

系統測試

結論與論文寫作

專題發表

NO

YESNO

NO NO

NO

YES

YESYES

YES

系統應用

圖 11 超音波自動測距系統設計與應用流程圖

2

6 量測數據分析與電路改善

7 信號擷取系統應用與可視化人機介面的整合

8 自動量測分析討論與建議

9 專題發表及完成論文寫作

13 文獻回顧

在國內外有關本文的相關研究主要有超音波飛行時間的量測3D 物體的量測及頭部

追蹤的系統在超音波飛行時間的量測方面最常見的方法則是臨界電壓法是利用事

先預設之一電壓臨界值當發射器發射出超音波訊號時電腦即開始計數脈波直到接

收器接收到超音波訊號時再去判斷此接收到之訊號是否已經超過之前所設定之臨界

值如果尚未超出臨界值則電腦繼續計數脈波數一旦訊號超出設定之臨界值則電

腦即停止脈波的計數然後再經由計數到之脈波數目求得超音波的飛行時間

另外在智慧型機器人的路徑規劃和導航方面也是超音波應用的重點F J Toledo

等人提出了一個使用超音波配合類神經網路來建構地圖的方法[1]此方法在每次進入不

同環境的時候不須要經過訓練即可構出可靠度高的地圖

在此我們將利用 Visual Basic 以打點的方式在螢幕上構出地圖並期望使用此一較

簡單之方法亦能構出可靠度高的地圖

3

第二章 基礎理論

21 超音波特性

一般人耳能聽見的聲音頻率在20~20 的範圍內超音波意指超出此範圍之音波

圖 21 為人耳可以感受到的頻率範圍與振動的強度關係而本論文所使用的超音波中

心頻率為40 kHz

kHz

共振頻率隨著用途和元件的差異而有所不同它大部份被用來偵測物體的運動速

度短距離的量測與判定物體之有無等屬於一種非接觸型的感測元件超音波感測器

分成發射器和接收器兩者同樣具有「壓電效應」對超音波發射器而言稱為「電壓效

應」主要是將電氣信號轉換成機械的信號去壓縮周圍空氣而這些受壓縮的空氣將會

壓縮接收器的壓電材料並產生「壓電效應」進而將機械信號轉換成電氣信號[2]

圖21 人耳可以感受到的頻率範圍與振動的強度關係

超音波的強度會隨傳送距離而衰減主要是由於能量的散失及分佈球面積越來越大的

緣故且超音波會隨頻率之增加衰減的情形越嚴重因此若使用 50 以上之超音波

來量測一公尺以上的距離時反射波需加以放大極高之倍率[3]

kHz

4

22 超音波感測原理

超音波感測器使用之場合依對象物體之大小表面之狀態運動狀態能否接觸

等其頻率感測器之配置檢出方式感測器之種類指向性等皆有檢討之必要表

21 是依其特徵而將感測器分成三種不同的配置和檢出方式使用反射式的場合從目的

物以外產生之反射波或繞射波都有可能會對訊號造成干擾指向性及輸出大小也得對應

調節欲增強指向性可用喇叭型結構依其形狀有圓錐型喇叭及指數關係型喇叭等可用

反射方式(獨立型)為連續發射之場合直接波之繞射現象必須採取對策處理可以把發

射接收器之距離拉大來改善此問題若無法根據原理結構上拉大兩者之距離就得從

感測器之安裝方法及安裝位置下功夫[4]

在此使用的量測方法是飛行時間的檢測也就是超音波從發射開始到接收到反射波

的時間如此便可求得物體與探頭間的距離其關係如下式

D (距離)= (音速) C times2

TOF (時間) (21)

其中TOF(time of flight)為飛行時間 TC 60331+= ( ms )T是操作溫度( ) C0

本論文是使用微處理機內建之16位元計時器因為使用的石英振盪器頻率為

110592 MHz所以最小的計數時間為t = MHz05921112 = 1085 sμ 因此21式可以改寫

2

tCCountD timestimes= (22)

其中 為量測距離Count為Count0計數值C為音速(34600D seccm 25 )t為時

C0

5

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 2: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

感謝

這些日子以來承蒙 張興政博士的細心指導使學生能快速地進入專題研

製狀況並且在學生最須要幫助方向錯誤的時候拉學生一把除此之外還

經常教導我們待人接物的道理以及生活上應有的觀念再次特別感謝吾師

另外還要謝謝實驗室的學長明哲國驊同現彥儒偉民俊嘉郭鴻以

及煜亮每當遇到瓶頸時學長們總會不時過來關心進度為我們打氣還有

幫助過我們的同學感謝一路有你們互相扶持謝謝

i

中文摘要

近年來自走式機器人或自走車的研究及相關技術運用相當廣泛為了完成指

派任務機器人必須具備有基本運動以及感測周遭環境的能力在許多場合都需

要避免機器碰撞的發生以防止意外造成傷害因此本文針對距離的感測進行研

究以基本的超音波測距為基石發展出一套容易與其他系統整合的超音波測距

模組本文利用價格便宜之超音波感測器來設計自動距離量測系統此系統不僅

能量測距離亦能將量測數據做自動存檔之功能人機介面是利用Visual Basic

程式語言撰寫經R-S232串聯傳輸至電腦硬體是利用產生振盪源來觸發超音

波當接收端有信號進來即中止計數器並將計數值利用RS-232傳送到電腦

進行距離計算與分析

ii

Abstract

Recently the researches and the relative technology about autonomous mobile

robots or vehicles are widely used in many fields In order to complete the assignment

robots must have the ability of moving and sensing In addition preventing robots

from collision is quite important Therefore we are going to do research on the

distance measurement based on ultrasonic technology and then develop a module

which can be easily combined into different systems This thesis presents a method to

design an automatic ranging system by using the cheap ultrasonic transducer This

system not only does measurements but also save data automatically In reference to

the software we take advantage of Visual Basic to build the interface The ultrasonic

source is generated by an oscillator The oscillator will trigger the transducer to

generate ultrasonic wave In the other hand a designed electronic circuit is used to

deal with the reflective wave Finally the time of flight will be estimated and then

sent to the computer by the microprocessor

iii

目錄

感謝i

中文摘要ii

Abstracthellip iii

目錄iv

圖目錄vi

表目錄hellipviii

第一章 緒論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip1

11 研究動機及目的1

12 研究方法1

13 文獻回顧helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip3

第二章 基礎理論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

21 超音波特性helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

22 超音波感測原理helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip5

第三章 超音波測距系統硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

31 系統硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

32 微處理單晶片簡介helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

33 電源電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

34 超音波發射驅動電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

35 超音波接受端電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

351 主動式帶通濾波器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

352 電壓比較器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

36 RS-232 串聯通訊介面helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

361 RS-232 使用說明helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

362 串列資料格式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip19

iv

363 串列埠通訊鮑率helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

364 訊號準位轉換helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

第四章 超音波測距系統軟體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip21

41 電腦監控程式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip21

42 畫面規劃helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip23

第五章 實驗分析與討論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

51 超音波測距電路helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

52 超音波測距測試分析helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip33

53 超音波測距系統應用helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

第六章 結論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

61 研究成果helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

62 未來展望helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

參考文獻helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip38

附錄一 微處理機程式碼helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip39

附錄二 Visual Basic 程式碼helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip44

v

圖目錄

圖 11 超音波自動測距系統設計與應用流程圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip2

圖 21 人耳可以感受到的頻率範圍與振動的強度關係helliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

圖 31 系統架構方塊圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

圖 32 8051 單晶片腳位圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip8

圖 33 電源電路(+12V)helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

圖 34 常用 555振盪器電路helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip10

圖 35 NE555 應用電路設計圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip10

圖 36 NE555 內部方塊圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

圖 37 10cm內電壓與距離關係圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

圖 38 主動式帶通濾波器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

圖 39 主動式帶通濾波器之波德圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip16

圖 310 主動式帶通濾波器之波德圖(中心頻率3998kHz)helliphelliphelliphelliphelliphelliphelliphelliphellip16

圖 311 電壓比較器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

圖 312 RS-232 準位轉換專用 IC ndashMAX232 helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

圖 41 人機介面程式流程圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip22

圖 42 串列埠輸入區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip23

圖 43 單位換算區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 44 資料輸入區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 45 分析結果區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 46 數值讀取區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip25

圖 47 面積體積模擬區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip25

圖 48 掃描顯像區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 49 開啟通訊埠關閉通訊埠系統結束圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 410 通訊埠指示燈圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

vi

圖 411 選擇操作模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 412 基本檢測模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 413 Microsoft Excel之儲檔圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 414 面積體積模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 415 面積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 416 體積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 417 掃描顯像模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 418 掃描顯像模式執行順序圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip30

圖 51 實際量測距離與平均百分誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

圖 52 超音波測距硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 53 電源電路實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 54 發射及接收電路板實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip33

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體helliphelliphelliphelliphelliphelliphellip33

圖 56 基本檢測模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip34

圖 57 距離量測數據分析與平均誤差helliphelliphelliphelliphellip35

圖 58 動態旋轉量測上視圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

圖 59 掃瞄顯示模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

vii

表目錄

表 21 超音波感測器的檢出方式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip6

表 31 RS正反器真值表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip11

表 32 超音波相對式距離電壓關係表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

表 33 RS-232 9PIN 腳位和 25PIN 腳位定義helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip18

表 51 實際距離量測誤差分析表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

表 52 距離量測數據分析與平均誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

viii

第一章 緒論

11 研究動機及目的

近年來關於自走式機器人或自走車的研究及相關運用技術相當廣泛機器人的研發

主要目的之一是為了取代人力使人類避免身陷於危險之中或加快工作任務完成的速

度例如在農業上可利用機器人噴灑農藥水產養殖及田間作業hellip等在工業上基

於安全的考量也可利用機器人作為探勘的工具或用來減少人力支出以降低成本並

使意外傷害減到最低如核能發電場消防救災太空探測及物料搬運hellip等

為了完成我們所指派的任務機器人必須具備有基本運動以及周遭環境感測的能

力在無人操作的情況之下隨著環境的變動能作出邏輯判斷並進行自我調整的功能

以完成所付予的工作因此機器人須要配備有不同的感測器如溫度感測器振動感測

器CCD感測器和超音波感測器hellip等以獲取外在的環境資訊

本文中將針對距離的感測做電子電路的設計以完成價格低廉的距離感測器模組

距離量測可使用雷射或超音波雷射優點為精準度極高但價格相當昂貴在此將使用

容易取得且價格便宜之超音波感測器來完成本專題研究目的在於建立一超音波自動

測距系統

12 研究方法

整個系統架構的完整規劃以繪製系統流程圖展示主要分為硬體架構和軟體

設計可摘要分項敘述於下所有分項工作及軟硬體設計整體相關性請參考所附

之研究流程圖如圖 11 所示並以此流程圖為藍圖完成本專題研究

1 收集充足的相關文獻資料與整理分析

2 驗證超音波驅動電路之設計

3 微波信號之接收電路設計與測試以期發展最佳化的電路設計

4 超音波飛行時間之數據傳輸電路與介面設計

5 距離量測系統與 PC 通訊傳輸系統之連結

1

文獻收集與整理

接收端信號放大濾波設計收集軟體資料 超音波觸發振盪器設計

介面技術與周邊程式設計

人機介面程式撰寫

軟體測試

超音波量測系統模型建立

帶通濾波器測試

振盪器測試

電壓比較器設計

比較器測試

系統整合

系統分析與改善

系統測試

結論與論文寫作

專題發表

NO

YESNO

NO NO

NO

YES

YESYES

YES

系統應用

圖 11 超音波自動測距系統設計與應用流程圖

2

6 量測數據分析與電路改善

7 信號擷取系統應用與可視化人機介面的整合

8 自動量測分析討論與建議

9 專題發表及完成論文寫作

13 文獻回顧

在國內外有關本文的相關研究主要有超音波飛行時間的量測3D 物體的量測及頭部

追蹤的系統在超音波飛行時間的量測方面最常見的方法則是臨界電壓法是利用事

先預設之一電壓臨界值當發射器發射出超音波訊號時電腦即開始計數脈波直到接

收器接收到超音波訊號時再去判斷此接收到之訊號是否已經超過之前所設定之臨界

值如果尚未超出臨界值則電腦繼續計數脈波數一旦訊號超出設定之臨界值則電

腦即停止脈波的計數然後再經由計數到之脈波數目求得超音波的飛行時間

另外在智慧型機器人的路徑規劃和導航方面也是超音波應用的重點F J Toledo

等人提出了一個使用超音波配合類神經網路來建構地圖的方法[1]此方法在每次進入不

同環境的時候不須要經過訓練即可構出可靠度高的地圖

在此我們將利用 Visual Basic 以打點的方式在螢幕上構出地圖並期望使用此一較

簡單之方法亦能構出可靠度高的地圖

3

第二章 基礎理論

21 超音波特性

一般人耳能聽見的聲音頻率在20~20 的範圍內超音波意指超出此範圍之音波

圖 21 為人耳可以感受到的頻率範圍與振動的強度關係而本論文所使用的超音波中

心頻率為40 kHz

kHz

共振頻率隨著用途和元件的差異而有所不同它大部份被用來偵測物體的運動速

度短距離的量測與判定物體之有無等屬於一種非接觸型的感測元件超音波感測器

分成發射器和接收器兩者同樣具有「壓電效應」對超音波發射器而言稱為「電壓效

應」主要是將電氣信號轉換成機械的信號去壓縮周圍空氣而這些受壓縮的空氣將會

壓縮接收器的壓電材料並產生「壓電效應」進而將機械信號轉換成電氣信號[2]

圖21 人耳可以感受到的頻率範圍與振動的強度關係

超音波的強度會隨傳送距離而衰減主要是由於能量的散失及分佈球面積越來越大的

緣故且超音波會隨頻率之增加衰減的情形越嚴重因此若使用 50 以上之超音波

來量測一公尺以上的距離時反射波需加以放大極高之倍率[3]

kHz

4

22 超音波感測原理

超音波感測器使用之場合依對象物體之大小表面之狀態運動狀態能否接觸

等其頻率感測器之配置檢出方式感測器之種類指向性等皆有檢討之必要表

21 是依其特徵而將感測器分成三種不同的配置和檢出方式使用反射式的場合從目的

物以外產生之反射波或繞射波都有可能會對訊號造成干擾指向性及輸出大小也得對應

調節欲增強指向性可用喇叭型結構依其形狀有圓錐型喇叭及指數關係型喇叭等可用

反射方式(獨立型)為連續發射之場合直接波之繞射現象必須採取對策處理可以把發

射接收器之距離拉大來改善此問題若無法根據原理結構上拉大兩者之距離就得從

感測器之安裝方法及安裝位置下功夫[4]

在此使用的量測方法是飛行時間的檢測也就是超音波從發射開始到接收到反射波

的時間如此便可求得物體與探頭間的距離其關係如下式

D (距離)= (音速) C times2

TOF (時間) (21)

其中TOF(time of flight)為飛行時間 TC 60331+= ( ms )T是操作溫度( ) C0

本論文是使用微處理機內建之16位元計時器因為使用的石英振盪器頻率為

110592 MHz所以最小的計數時間為t = MHz05921112 = 1085 sμ 因此21式可以改寫

2

tCCountD timestimes= (22)

其中 為量測距離Count為Count0計數值C為音速(34600D seccm 25 )t為時

C0

5

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 3: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

中文摘要

近年來自走式機器人或自走車的研究及相關技術運用相當廣泛為了完成指

派任務機器人必須具備有基本運動以及感測周遭環境的能力在許多場合都需

要避免機器碰撞的發生以防止意外造成傷害因此本文針對距離的感測進行研

究以基本的超音波測距為基石發展出一套容易與其他系統整合的超音波測距

模組本文利用價格便宜之超音波感測器來設計自動距離量測系統此系統不僅

能量測距離亦能將量測數據做自動存檔之功能人機介面是利用Visual Basic

程式語言撰寫經R-S232串聯傳輸至電腦硬體是利用產生振盪源來觸發超音

波當接收端有信號進來即中止計數器並將計數值利用RS-232傳送到電腦

進行距離計算與分析

ii

Abstract

Recently the researches and the relative technology about autonomous mobile

robots or vehicles are widely used in many fields In order to complete the assignment

robots must have the ability of moving and sensing In addition preventing robots

from collision is quite important Therefore we are going to do research on the

distance measurement based on ultrasonic technology and then develop a module

which can be easily combined into different systems This thesis presents a method to

design an automatic ranging system by using the cheap ultrasonic transducer This

system not only does measurements but also save data automatically In reference to

the software we take advantage of Visual Basic to build the interface The ultrasonic

source is generated by an oscillator The oscillator will trigger the transducer to

generate ultrasonic wave In the other hand a designed electronic circuit is used to

deal with the reflective wave Finally the time of flight will be estimated and then

sent to the computer by the microprocessor

iii

目錄

感謝i

中文摘要ii

Abstracthellip iii

目錄iv

圖目錄vi

表目錄hellipviii

第一章 緒論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip1

11 研究動機及目的1

12 研究方法1

13 文獻回顧helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip3

第二章 基礎理論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

21 超音波特性helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

22 超音波感測原理helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip5

第三章 超音波測距系統硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

31 系統硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

32 微處理單晶片簡介helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

33 電源電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

34 超音波發射驅動電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

35 超音波接受端電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

351 主動式帶通濾波器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

352 電壓比較器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

36 RS-232 串聯通訊介面helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

361 RS-232 使用說明helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

362 串列資料格式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip19

iv

363 串列埠通訊鮑率helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

364 訊號準位轉換helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

第四章 超音波測距系統軟體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip21

41 電腦監控程式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip21

42 畫面規劃helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip23

第五章 實驗分析與討論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

51 超音波測距電路helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

52 超音波測距測試分析helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip33

53 超音波測距系統應用helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

第六章 結論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

61 研究成果helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

62 未來展望helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

參考文獻helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip38

附錄一 微處理機程式碼helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip39

附錄二 Visual Basic 程式碼helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip44

v

圖目錄

圖 11 超音波自動測距系統設計與應用流程圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip2

圖 21 人耳可以感受到的頻率範圍與振動的強度關係helliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

圖 31 系統架構方塊圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

圖 32 8051 單晶片腳位圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip8

圖 33 電源電路(+12V)helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

圖 34 常用 555振盪器電路helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip10

圖 35 NE555 應用電路設計圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip10

圖 36 NE555 內部方塊圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

圖 37 10cm內電壓與距離關係圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

圖 38 主動式帶通濾波器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

圖 39 主動式帶通濾波器之波德圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip16

圖 310 主動式帶通濾波器之波德圖(中心頻率3998kHz)helliphelliphelliphelliphelliphelliphelliphelliphellip16

圖 311 電壓比較器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

圖 312 RS-232 準位轉換專用 IC ndashMAX232 helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

圖 41 人機介面程式流程圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip22

圖 42 串列埠輸入區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip23

圖 43 單位換算區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 44 資料輸入區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 45 分析結果區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 46 數值讀取區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip25

圖 47 面積體積模擬區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip25

圖 48 掃描顯像區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 49 開啟通訊埠關閉通訊埠系統結束圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 410 通訊埠指示燈圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

vi

圖 411 選擇操作模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 412 基本檢測模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 413 Microsoft Excel之儲檔圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 414 面積體積模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 415 面積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 416 體積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 417 掃描顯像模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 418 掃描顯像模式執行順序圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip30

圖 51 實際量測距離與平均百分誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

圖 52 超音波測距硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 53 電源電路實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 54 發射及接收電路板實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip33

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體helliphelliphelliphelliphelliphelliphellip33

圖 56 基本檢測模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip34

圖 57 距離量測數據分析與平均誤差helliphelliphelliphelliphellip35

圖 58 動態旋轉量測上視圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

圖 59 掃瞄顯示模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

vii

表目錄

表 21 超音波感測器的檢出方式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip6

表 31 RS正反器真值表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip11

表 32 超音波相對式距離電壓關係表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

表 33 RS-232 9PIN 腳位和 25PIN 腳位定義helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip18

表 51 實際距離量測誤差分析表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

表 52 距離量測數據分析與平均誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

viii

第一章 緒論

11 研究動機及目的

近年來關於自走式機器人或自走車的研究及相關運用技術相當廣泛機器人的研發

主要目的之一是為了取代人力使人類避免身陷於危險之中或加快工作任務完成的速

度例如在農業上可利用機器人噴灑農藥水產養殖及田間作業hellip等在工業上基

於安全的考量也可利用機器人作為探勘的工具或用來減少人力支出以降低成本並

使意外傷害減到最低如核能發電場消防救災太空探測及物料搬運hellip等

為了完成我們所指派的任務機器人必須具備有基本運動以及周遭環境感測的能

力在無人操作的情況之下隨著環境的變動能作出邏輯判斷並進行自我調整的功能

以完成所付予的工作因此機器人須要配備有不同的感測器如溫度感測器振動感測

器CCD感測器和超音波感測器hellip等以獲取外在的環境資訊

本文中將針對距離的感測做電子電路的設計以完成價格低廉的距離感測器模組

距離量測可使用雷射或超音波雷射優點為精準度極高但價格相當昂貴在此將使用

容易取得且價格便宜之超音波感測器來完成本專題研究目的在於建立一超音波自動

測距系統

12 研究方法

整個系統架構的完整規劃以繪製系統流程圖展示主要分為硬體架構和軟體

設計可摘要分項敘述於下所有分項工作及軟硬體設計整體相關性請參考所附

之研究流程圖如圖 11 所示並以此流程圖為藍圖完成本專題研究

1 收集充足的相關文獻資料與整理分析

2 驗證超音波驅動電路之設計

3 微波信號之接收電路設計與測試以期發展最佳化的電路設計

4 超音波飛行時間之數據傳輸電路與介面設計

5 距離量測系統與 PC 通訊傳輸系統之連結

1

文獻收集與整理

接收端信號放大濾波設計收集軟體資料 超音波觸發振盪器設計

介面技術與周邊程式設計

人機介面程式撰寫

軟體測試

超音波量測系統模型建立

帶通濾波器測試

振盪器測試

電壓比較器設計

比較器測試

系統整合

系統分析與改善

系統測試

結論與論文寫作

專題發表

NO

YESNO

NO NO

NO

YES

YESYES

YES

系統應用

圖 11 超音波自動測距系統設計與應用流程圖

2

6 量測數據分析與電路改善

7 信號擷取系統應用與可視化人機介面的整合

8 自動量測分析討論與建議

9 專題發表及完成論文寫作

13 文獻回顧

在國內外有關本文的相關研究主要有超音波飛行時間的量測3D 物體的量測及頭部

追蹤的系統在超音波飛行時間的量測方面最常見的方法則是臨界電壓法是利用事

先預設之一電壓臨界值當發射器發射出超音波訊號時電腦即開始計數脈波直到接

收器接收到超音波訊號時再去判斷此接收到之訊號是否已經超過之前所設定之臨界

值如果尚未超出臨界值則電腦繼續計數脈波數一旦訊號超出設定之臨界值則電

腦即停止脈波的計數然後再經由計數到之脈波數目求得超音波的飛行時間

另外在智慧型機器人的路徑規劃和導航方面也是超音波應用的重點F J Toledo

等人提出了一個使用超音波配合類神經網路來建構地圖的方法[1]此方法在每次進入不

同環境的時候不須要經過訓練即可構出可靠度高的地圖

在此我們將利用 Visual Basic 以打點的方式在螢幕上構出地圖並期望使用此一較

簡單之方法亦能構出可靠度高的地圖

3

第二章 基礎理論

21 超音波特性

一般人耳能聽見的聲音頻率在20~20 的範圍內超音波意指超出此範圍之音波

圖 21 為人耳可以感受到的頻率範圍與振動的強度關係而本論文所使用的超音波中

心頻率為40 kHz

kHz

共振頻率隨著用途和元件的差異而有所不同它大部份被用來偵測物體的運動速

度短距離的量測與判定物體之有無等屬於一種非接觸型的感測元件超音波感測器

分成發射器和接收器兩者同樣具有「壓電效應」對超音波發射器而言稱為「電壓效

應」主要是將電氣信號轉換成機械的信號去壓縮周圍空氣而這些受壓縮的空氣將會

壓縮接收器的壓電材料並產生「壓電效應」進而將機械信號轉換成電氣信號[2]

圖21 人耳可以感受到的頻率範圍與振動的強度關係

超音波的強度會隨傳送距離而衰減主要是由於能量的散失及分佈球面積越來越大的

緣故且超音波會隨頻率之增加衰減的情形越嚴重因此若使用 50 以上之超音波

來量測一公尺以上的距離時反射波需加以放大極高之倍率[3]

kHz

4

22 超音波感測原理

超音波感測器使用之場合依對象物體之大小表面之狀態運動狀態能否接觸

等其頻率感測器之配置檢出方式感測器之種類指向性等皆有檢討之必要表

21 是依其特徵而將感測器分成三種不同的配置和檢出方式使用反射式的場合從目的

物以外產生之反射波或繞射波都有可能會對訊號造成干擾指向性及輸出大小也得對應

調節欲增強指向性可用喇叭型結構依其形狀有圓錐型喇叭及指數關係型喇叭等可用

反射方式(獨立型)為連續發射之場合直接波之繞射現象必須採取對策處理可以把發

射接收器之距離拉大來改善此問題若無法根據原理結構上拉大兩者之距離就得從

感測器之安裝方法及安裝位置下功夫[4]

在此使用的量測方法是飛行時間的檢測也就是超音波從發射開始到接收到反射波

的時間如此便可求得物體與探頭間的距離其關係如下式

D (距離)= (音速) C times2

TOF (時間) (21)

其中TOF(time of flight)為飛行時間 TC 60331+= ( ms )T是操作溫度( ) C0

本論文是使用微處理機內建之16位元計時器因為使用的石英振盪器頻率為

110592 MHz所以最小的計數時間為t = MHz05921112 = 1085 sμ 因此21式可以改寫

2

tCCountD timestimes= (22)

其中 為量測距離Count為Count0計數值C為音速(34600D seccm 25 )t為時

C0

5

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 4: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Abstract

Recently the researches and the relative technology about autonomous mobile

robots or vehicles are widely used in many fields In order to complete the assignment

robots must have the ability of moving and sensing In addition preventing robots

from collision is quite important Therefore we are going to do research on the

distance measurement based on ultrasonic technology and then develop a module

which can be easily combined into different systems This thesis presents a method to

design an automatic ranging system by using the cheap ultrasonic transducer This

system not only does measurements but also save data automatically In reference to

the software we take advantage of Visual Basic to build the interface The ultrasonic

source is generated by an oscillator The oscillator will trigger the transducer to

generate ultrasonic wave In the other hand a designed electronic circuit is used to

deal with the reflective wave Finally the time of flight will be estimated and then

sent to the computer by the microprocessor

iii

目錄

感謝i

中文摘要ii

Abstracthellip iii

目錄iv

圖目錄vi

表目錄hellipviii

第一章 緒論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip1

11 研究動機及目的1

12 研究方法1

13 文獻回顧helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip3

第二章 基礎理論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

21 超音波特性helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

22 超音波感測原理helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip5

第三章 超音波測距系統硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

31 系統硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

32 微處理單晶片簡介helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

33 電源電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

34 超音波發射驅動電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

35 超音波接受端電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

351 主動式帶通濾波器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

352 電壓比較器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

36 RS-232 串聯通訊介面helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

361 RS-232 使用說明helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

362 串列資料格式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip19

iv

363 串列埠通訊鮑率helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

364 訊號準位轉換helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

第四章 超音波測距系統軟體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip21

41 電腦監控程式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip21

42 畫面規劃helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip23

第五章 實驗分析與討論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

51 超音波測距電路helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

52 超音波測距測試分析helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip33

53 超音波測距系統應用helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

第六章 結論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

61 研究成果helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

62 未來展望helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

參考文獻helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip38

附錄一 微處理機程式碼helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip39

附錄二 Visual Basic 程式碼helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip44

v

圖目錄

圖 11 超音波自動測距系統設計與應用流程圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip2

圖 21 人耳可以感受到的頻率範圍與振動的強度關係helliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

圖 31 系統架構方塊圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

圖 32 8051 單晶片腳位圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip8

圖 33 電源電路(+12V)helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

圖 34 常用 555振盪器電路helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip10

圖 35 NE555 應用電路設計圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip10

圖 36 NE555 內部方塊圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

圖 37 10cm內電壓與距離關係圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

圖 38 主動式帶通濾波器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

圖 39 主動式帶通濾波器之波德圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip16

圖 310 主動式帶通濾波器之波德圖(中心頻率3998kHz)helliphelliphelliphelliphelliphelliphelliphelliphellip16

圖 311 電壓比較器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

圖 312 RS-232 準位轉換專用 IC ndashMAX232 helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

圖 41 人機介面程式流程圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip22

圖 42 串列埠輸入區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip23

圖 43 單位換算區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 44 資料輸入區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 45 分析結果區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 46 數值讀取區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip25

圖 47 面積體積模擬區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip25

圖 48 掃描顯像區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 49 開啟通訊埠關閉通訊埠系統結束圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 410 通訊埠指示燈圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

vi

圖 411 選擇操作模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 412 基本檢測模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 413 Microsoft Excel之儲檔圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 414 面積體積模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 415 面積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 416 體積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 417 掃描顯像模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 418 掃描顯像模式執行順序圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip30

圖 51 實際量測距離與平均百分誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

圖 52 超音波測距硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 53 電源電路實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 54 發射及接收電路板實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip33

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體helliphelliphelliphelliphelliphelliphellip33

圖 56 基本檢測模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip34

圖 57 距離量測數據分析與平均誤差helliphelliphelliphelliphellip35

圖 58 動態旋轉量測上視圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

圖 59 掃瞄顯示模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

vii

表目錄

表 21 超音波感測器的檢出方式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip6

表 31 RS正反器真值表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip11

表 32 超音波相對式距離電壓關係表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

表 33 RS-232 9PIN 腳位和 25PIN 腳位定義helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip18

表 51 實際距離量測誤差分析表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

表 52 距離量測數據分析與平均誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

viii

第一章 緒論

11 研究動機及目的

近年來關於自走式機器人或自走車的研究及相關運用技術相當廣泛機器人的研發

主要目的之一是為了取代人力使人類避免身陷於危險之中或加快工作任務完成的速

度例如在農業上可利用機器人噴灑農藥水產養殖及田間作業hellip等在工業上基

於安全的考量也可利用機器人作為探勘的工具或用來減少人力支出以降低成本並

使意外傷害減到最低如核能發電場消防救災太空探測及物料搬運hellip等

為了完成我們所指派的任務機器人必須具備有基本運動以及周遭環境感測的能

力在無人操作的情況之下隨著環境的變動能作出邏輯判斷並進行自我調整的功能

以完成所付予的工作因此機器人須要配備有不同的感測器如溫度感測器振動感測

器CCD感測器和超音波感測器hellip等以獲取外在的環境資訊

本文中將針對距離的感測做電子電路的設計以完成價格低廉的距離感測器模組

距離量測可使用雷射或超音波雷射優點為精準度極高但價格相當昂貴在此將使用

容易取得且價格便宜之超音波感測器來完成本專題研究目的在於建立一超音波自動

測距系統

12 研究方法

整個系統架構的完整規劃以繪製系統流程圖展示主要分為硬體架構和軟體

設計可摘要分項敘述於下所有分項工作及軟硬體設計整體相關性請參考所附

之研究流程圖如圖 11 所示並以此流程圖為藍圖完成本專題研究

1 收集充足的相關文獻資料與整理分析

2 驗證超音波驅動電路之設計

3 微波信號之接收電路設計與測試以期發展最佳化的電路設計

4 超音波飛行時間之數據傳輸電路與介面設計

5 距離量測系統與 PC 通訊傳輸系統之連結

1

文獻收集與整理

接收端信號放大濾波設計收集軟體資料 超音波觸發振盪器設計

介面技術與周邊程式設計

人機介面程式撰寫

軟體測試

超音波量測系統模型建立

帶通濾波器測試

振盪器測試

電壓比較器設計

比較器測試

系統整合

系統分析與改善

系統測試

結論與論文寫作

專題發表

NO

YESNO

NO NO

NO

YES

YESYES

YES

系統應用

圖 11 超音波自動測距系統設計與應用流程圖

2

6 量測數據分析與電路改善

7 信號擷取系統應用與可視化人機介面的整合

8 自動量測分析討論與建議

9 專題發表及完成論文寫作

13 文獻回顧

在國內外有關本文的相關研究主要有超音波飛行時間的量測3D 物體的量測及頭部

追蹤的系統在超音波飛行時間的量測方面最常見的方法則是臨界電壓法是利用事

先預設之一電壓臨界值當發射器發射出超音波訊號時電腦即開始計數脈波直到接

收器接收到超音波訊號時再去判斷此接收到之訊號是否已經超過之前所設定之臨界

值如果尚未超出臨界值則電腦繼續計數脈波數一旦訊號超出設定之臨界值則電

腦即停止脈波的計數然後再經由計數到之脈波數目求得超音波的飛行時間

另外在智慧型機器人的路徑規劃和導航方面也是超音波應用的重點F J Toledo

等人提出了一個使用超音波配合類神經網路來建構地圖的方法[1]此方法在每次進入不

同環境的時候不須要經過訓練即可構出可靠度高的地圖

在此我們將利用 Visual Basic 以打點的方式在螢幕上構出地圖並期望使用此一較

簡單之方法亦能構出可靠度高的地圖

3

第二章 基礎理論

21 超音波特性

一般人耳能聽見的聲音頻率在20~20 的範圍內超音波意指超出此範圍之音波

圖 21 為人耳可以感受到的頻率範圍與振動的強度關係而本論文所使用的超音波中

心頻率為40 kHz

kHz

共振頻率隨著用途和元件的差異而有所不同它大部份被用來偵測物體的運動速

度短距離的量測與判定物體之有無等屬於一種非接觸型的感測元件超音波感測器

分成發射器和接收器兩者同樣具有「壓電效應」對超音波發射器而言稱為「電壓效

應」主要是將電氣信號轉換成機械的信號去壓縮周圍空氣而這些受壓縮的空氣將會

壓縮接收器的壓電材料並產生「壓電效應」進而將機械信號轉換成電氣信號[2]

圖21 人耳可以感受到的頻率範圍與振動的強度關係

超音波的強度會隨傳送距離而衰減主要是由於能量的散失及分佈球面積越來越大的

緣故且超音波會隨頻率之增加衰減的情形越嚴重因此若使用 50 以上之超音波

來量測一公尺以上的距離時反射波需加以放大極高之倍率[3]

kHz

4

22 超音波感測原理

超音波感測器使用之場合依對象物體之大小表面之狀態運動狀態能否接觸

等其頻率感測器之配置檢出方式感測器之種類指向性等皆有檢討之必要表

21 是依其特徵而將感測器分成三種不同的配置和檢出方式使用反射式的場合從目的

物以外產生之反射波或繞射波都有可能會對訊號造成干擾指向性及輸出大小也得對應

調節欲增強指向性可用喇叭型結構依其形狀有圓錐型喇叭及指數關係型喇叭等可用

反射方式(獨立型)為連續發射之場合直接波之繞射現象必須採取對策處理可以把發

射接收器之距離拉大來改善此問題若無法根據原理結構上拉大兩者之距離就得從

感測器之安裝方法及安裝位置下功夫[4]

在此使用的量測方法是飛行時間的檢測也就是超音波從發射開始到接收到反射波

的時間如此便可求得物體與探頭間的距離其關係如下式

D (距離)= (音速) C times2

TOF (時間) (21)

其中TOF(time of flight)為飛行時間 TC 60331+= ( ms )T是操作溫度( ) C0

本論文是使用微處理機內建之16位元計時器因為使用的石英振盪器頻率為

110592 MHz所以最小的計數時間為t = MHz05921112 = 1085 sμ 因此21式可以改寫

2

tCCountD timestimes= (22)

其中 為量測距離Count為Count0計數值C為音速(34600D seccm 25 )t為時

C0

5

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 5: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

目錄

感謝i

中文摘要ii

Abstracthellip iii

目錄iv

圖目錄vi

表目錄hellipviii

第一章 緒論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip1

11 研究動機及目的1

12 研究方法1

13 文獻回顧helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip3

第二章 基礎理論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

21 超音波特性helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

22 超音波感測原理helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip5

第三章 超音波測距系統硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

31 系統硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

32 微處理單晶片簡介helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

33 電源電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

34 超音波發射驅動電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

35 超音波接受端電路設計helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

351 主動式帶通濾波器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

352 電壓比較器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

36 RS-232 串聯通訊介面helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

361 RS-232 使用說明helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

362 串列資料格式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip19

iv

363 串列埠通訊鮑率helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

364 訊號準位轉換helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

第四章 超音波測距系統軟體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip21

41 電腦監控程式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip21

42 畫面規劃helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip23

第五章 實驗分析與討論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

51 超音波測距電路helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

52 超音波測距測試分析helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip33

53 超音波測距系統應用helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

第六章 結論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

61 研究成果helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

62 未來展望helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

參考文獻helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip38

附錄一 微處理機程式碼helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip39

附錄二 Visual Basic 程式碼helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip44

v

圖目錄

圖 11 超音波自動測距系統設計與應用流程圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip2

圖 21 人耳可以感受到的頻率範圍與振動的強度關係helliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

圖 31 系統架構方塊圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

圖 32 8051 單晶片腳位圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip8

圖 33 電源電路(+12V)helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

圖 34 常用 555振盪器電路helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip10

圖 35 NE555 應用電路設計圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip10

圖 36 NE555 內部方塊圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

圖 37 10cm內電壓與距離關係圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

圖 38 主動式帶通濾波器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

圖 39 主動式帶通濾波器之波德圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip16

圖 310 主動式帶通濾波器之波德圖(中心頻率3998kHz)helliphelliphelliphelliphelliphelliphelliphelliphellip16

圖 311 電壓比較器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

圖 312 RS-232 準位轉換專用 IC ndashMAX232 helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

圖 41 人機介面程式流程圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip22

圖 42 串列埠輸入區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip23

圖 43 單位換算區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 44 資料輸入區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 45 分析結果區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 46 數值讀取區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip25

圖 47 面積體積模擬區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip25

圖 48 掃描顯像區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 49 開啟通訊埠關閉通訊埠系統結束圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 410 通訊埠指示燈圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

vi

圖 411 選擇操作模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 412 基本檢測模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 413 Microsoft Excel之儲檔圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 414 面積體積模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 415 面積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 416 體積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 417 掃描顯像模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 418 掃描顯像模式執行順序圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip30

圖 51 實際量測距離與平均百分誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

圖 52 超音波測距硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 53 電源電路實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 54 發射及接收電路板實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip33

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體helliphelliphelliphelliphelliphelliphellip33

圖 56 基本檢測模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip34

圖 57 距離量測數據分析與平均誤差helliphelliphelliphelliphellip35

圖 58 動態旋轉量測上視圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

圖 59 掃瞄顯示模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

vii

表目錄

表 21 超音波感測器的檢出方式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip6

表 31 RS正反器真值表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip11

表 32 超音波相對式距離電壓關係表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

表 33 RS-232 9PIN 腳位和 25PIN 腳位定義helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip18

表 51 實際距離量測誤差分析表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

表 52 距離量測數據分析與平均誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

viii

第一章 緒論

11 研究動機及目的

近年來關於自走式機器人或自走車的研究及相關運用技術相當廣泛機器人的研發

主要目的之一是為了取代人力使人類避免身陷於危險之中或加快工作任務完成的速

度例如在農業上可利用機器人噴灑農藥水產養殖及田間作業hellip等在工業上基

於安全的考量也可利用機器人作為探勘的工具或用來減少人力支出以降低成本並

使意外傷害減到最低如核能發電場消防救災太空探測及物料搬運hellip等

為了完成我們所指派的任務機器人必須具備有基本運動以及周遭環境感測的能

力在無人操作的情況之下隨著環境的變動能作出邏輯判斷並進行自我調整的功能

以完成所付予的工作因此機器人須要配備有不同的感測器如溫度感測器振動感測

器CCD感測器和超音波感測器hellip等以獲取外在的環境資訊

本文中將針對距離的感測做電子電路的設計以完成價格低廉的距離感測器模組

距離量測可使用雷射或超音波雷射優點為精準度極高但價格相當昂貴在此將使用

容易取得且價格便宜之超音波感測器來完成本專題研究目的在於建立一超音波自動

測距系統

12 研究方法

整個系統架構的完整規劃以繪製系統流程圖展示主要分為硬體架構和軟體

設計可摘要分項敘述於下所有分項工作及軟硬體設計整體相關性請參考所附

之研究流程圖如圖 11 所示並以此流程圖為藍圖完成本專題研究

1 收集充足的相關文獻資料與整理分析

2 驗證超音波驅動電路之設計

3 微波信號之接收電路設計與測試以期發展最佳化的電路設計

4 超音波飛行時間之數據傳輸電路與介面設計

5 距離量測系統與 PC 通訊傳輸系統之連結

1

文獻收集與整理

接收端信號放大濾波設計收集軟體資料 超音波觸發振盪器設計

介面技術與周邊程式設計

人機介面程式撰寫

軟體測試

超音波量測系統模型建立

帶通濾波器測試

振盪器測試

電壓比較器設計

比較器測試

系統整合

系統分析與改善

系統測試

結論與論文寫作

專題發表

NO

YESNO

NO NO

NO

YES

YESYES

YES

系統應用

圖 11 超音波自動測距系統設計與應用流程圖

2

6 量測數據分析與電路改善

7 信號擷取系統應用與可視化人機介面的整合

8 自動量測分析討論與建議

9 專題發表及完成論文寫作

13 文獻回顧

在國內外有關本文的相關研究主要有超音波飛行時間的量測3D 物體的量測及頭部

追蹤的系統在超音波飛行時間的量測方面最常見的方法則是臨界電壓法是利用事

先預設之一電壓臨界值當發射器發射出超音波訊號時電腦即開始計數脈波直到接

收器接收到超音波訊號時再去判斷此接收到之訊號是否已經超過之前所設定之臨界

值如果尚未超出臨界值則電腦繼續計數脈波數一旦訊號超出設定之臨界值則電

腦即停止脈波的計數然後再經由計數到之脈波數目求得超音波的飛行時間

另外在智慧型機器人的路徑規劃和導航方面也是超音波應用的重點F J Toledo

等人提出了一個使用超音波配合類神經網路來建構地圖的方法[1]此方法在每次進入不

同環境的時候不須要經過訓練即可構出可靠度高的地圖

在此我們將利用 Visual Basic 以打點的方式在螢幕上構出地圖並期望使用此一較

簡單之方法亦能構出可靠度高的地圖

3

第二章 基礎理論

21 超音波特性

一般人耳能聽見的聲音頻率在20~20 的範圍內超音波意指超出此範圍之音波

圖 21 為人耳可以感受到的頻率範圍與振動的強度關係而本論文所使用的超音波中

心頻率為40 kHz

kHz

共振頻率隨著用途和元件的差異而有所不同它大部份被用來偵測物體的運動速

度短距離的量測與判定物體之有無等屬於一種非接觸型的感測元件超音波感測器

分成發射器和接收器兩者同樣具有「壓電效應」對超音波發射器而言稱為「電壓效

應」主要是將電氣信號轉換成機械的信號去壓縮周圍空氣而這些受壓縮的空氣將會

壓縮接收器的壓電材料並產生「壓電效應」進而將機械信號轉換成電氣信號[2]

圖21 人耳可以感受到的頻率範圍與振動的強度關係

超音波的強度會隨傳送距離而衰減主要是由於能量的散失及分佈球面積越來越大的

緣故且超音波會隨頻率之增加衰減的情形越嚴重因此若使用 50 以上之超音波

來量測一公尺以上的距離時反射波需加以放大極高之倍率[3]

kHz

4

22 超音波感測原理

超音波感測器使用之場合依對象物體之大小表面之狀態運動狀態能否接觸

等其頻率感測器之配置檢出方式感測器之種類指向性等皆有檢討之必要表

21 是依其特徵而將感測器分成三種不同的配置和檢出方式使用反射式的場合從目的

物以外產生之反射波或繞射波都有可能會對訊號造成干擾指向性及輸出大小也得對應

調節欲增強指向性可用喇叭型結構依其形狀有圓錐型喇叭及指數關係型喇叭等可用

反射方式(獨立型)為連續發射之場合直接波之繞射現象必須採取對策處理可以把發

射接收器之距離拉大來改善此問題若無法根據原理結構上拉大兩者之距離就得從

感測器之安裝方法及安裝位置下功夫[4]

在此使用的量測方法是飛行時間的檢測也就是超音波從發射開始到接收到反射波

的時間如此便可求得物體與探頭間的距離其關係如下式

D (距離)= (音速) C times2

TOF (時間) (21)

其中TOF(time of flight)為飛行時間 TC 60331+= ( ms )T是操作溫度( ) C0

本論文是使用微處理機內建之16位元計時器因為使用的石英振盪器頻率為

110592 MHz所以最小的計數時間為t = MHz05921112 = 1085 sμ 因此21式可以改寫

2

tCCountD timestimes= (22)

其中 為量測距離Count為Count0計數值C為音速(34600D seccm 25 )t為時

C0

5

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 6: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

363 串列埠通訊鮑率helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

364 訊號準位轉換helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

第四章 超音波測距系統軟體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip21

41 電腦監控程式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip21

42 畫面規劃helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip23

第五章 實驗分析與討論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

51 超音波測距電路helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

52 超音波測距測試分析helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip33

53 超音波測距系統應用helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

第六章 結論helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

61 研究成果helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

62 未來展望helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip37

參考文獻helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip38

附錄一 微處理機程式碼helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip39

附錄二 Visual Basic 程式碼helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip44

v

圖目錄

圖 11 超音波自動測距系統設計與應用流程圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip2

圖 21 人耳可以感受到的頻率範圍與振動的強度關係helliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

圖 31 系統架構方塊圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

圖 32 8051 單晶片腳位圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip8

圖 33 電源電路(+12V)helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

圖 34 常用 555振盪器電路helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip10

圖 35 NE555 應用電路設計圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip10

圖 36 NE555 內部方塊圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

圖 37 10cm內電壓與距離關係圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

圖 38 主動式帶通濾波器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

圖 39 主動式帶通濾波器之波德圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip16

圖 310 主動式帶通濾波器之波德圖(中心頻率3998kHz)helliphelliphelliphelliphelliphelliphelliphelliphellip16

圖 311 電壓比較器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

圖 312 RS-232 準位轉換專用 IC ndashMAX232 helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

圖 41 人機介面程式流程圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip22

圖 42 串列埠輸入區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip23

圖 43 單位換算區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 44 資料輸入區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 45 分析結果區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 46 數值讀取區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip25

圖 47 面積體積模擬區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip25

圖 48 掃描顯像區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 49 開啟通訊埠關閉通訊埠系統結束圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 410 通訊埠指示燈圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

vi

圖 411 選擇操作模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 412 基本檢測模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 413 Microsoft Excel之儲檔圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 414 面積體積模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 415 面積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 416 體積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 417 掃描顯像模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 418 掃描顯像模式執行順序圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip30

圖 51 實際量測距離與平均百分誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

圖 52 超音波測距硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 53 電源電路實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 54 發射及接收電路板實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip33

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體helliphelliphelliphelliphelliphelliphellip33

圖 56 基本檢測模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip34

圖 57 距離量測數據分析與平均誤差helliphelliphelliphelliphellip35

圖 58 動態旋轉量測上視圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

圖 59 掃瞄顯示模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

vii

表目錄

表 21 超音波感測器的檢出方式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip6

表 31 RS正反器真值表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip11

表 32 超音波相對式距離電壓關係表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

表 33 RS-232 9PIN 腳位和 25PIN 腳位定義helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip18

表 51 實際距離量測誤差分析表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

表 52 距離量測數據分析與平均誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

viii

第一章 緒論

11 研究動機及目的

近年來關於自走式機器人或自走車的研究及相關運用技術相當廣泛機器人的研發

主要目的之一是為了取代人力使人類避免身陷於危險之中或加快工作任務完成的速

度例如在農業上可利用機器人噴灑農藥水產養殖及田間作業hellip等在工業上基

於安全的考量也可利用機器人作為探勘的工具或用來減少人力支出以降低成本並

使意外傷害減到最低如核能發電場消防救災太空探測及物料搬運hellip等

為了完成我們所指派的任務機器人必須具備有基本運動以及周遭環境感測的能

力在無人操作的情況之下隨著環境的變動能作出邏輯判斷並進行自我調整的功能

以完成所付予的工作因此機器人須要配備有不同的感測器如溫度感測器振動感測

器CCD感測器和超音波感測器hellip等以獲取外在的環境資訊

本文中將針對距離的感測做電子電路的設計以完成價格低廉的距離感測器模組

距離量測可使用雷射或超音波雷射優點為精準度極高但價格相當昂貴在此將使用

容易取得且價格便宜之超音波感測器來完成本專題研究目的在於建立一超音波自動

測距系統

12 研究方法

整個系統架構的完整規劃以繪製系統流程圖展示主要分為硬體架構和軟體

設計可摘要分項敘述於下所有分項工作及軟硬體設計整體相關性請參考所附

之研究流程圖如圖 11 所示並以此流程圖為藍圖完成本專題研究

1 收集充足的相關文獻資料與整理分析

2 驗證超音波驅動電路之設計

3 微波信號之接收電路設計與測試以期發展最佳化的電路設計

4 超音波飛行時間之數據傳輸電路與介面設計

5 距離量測系統與 PC 通訊傳輸系統之連結

1

文獻收集與整理

接收端信號放大濾波設計收集軟體資料 超音波觸發振盪器設計

介面技術與周邊程式設計

人機介面程式撰寫

軟體測試

超音波量測系統模型建立

帶通濾波器測試

振盪器測試

電壓比較器設計

比較器測試

系統整合

系統分析與改善

系統測試

結論與論文寫作

專題發表

NO

YESNO

NO NO

NO

YES

YESYES

YES

系統應用

圖 11 超音波自動測距系統設計與應用流程圖

2

6 量測數據分析與電路改善

7 信號擷取系統應用與可視化人機介面的整合

8 自動量測分析討論與建議

9 專題發表及完成論文寫作

13 文獻回顧

在國內外有關本文的相關研究主要有超音波飛行時間的量測3D 物體的量測及頭部

追蹤的系統在超音波飛行時間的量測方面最常見的方法則是臨界電壓法是利用事

先預設之一電壓臨界值當發射器發射出超音波訊號時電腦即開始計數脈波直到接

收器接收到超音波訊號時再去判斷此接收到之訊號是否已經超過之前所設定之臨界

值如果尚未超出臨界值則電腦繼續計數脈波數一旦訊號超出設定之臨界值則電

腦即停止脈波的計數然後再經由計數到之脈波數目求得超音波的飛行時間

另外在智慧型機器人的路徑規劃和導航方面也是超音波應用的重點F J Toledo

等人提出了一個使用超音波配合類神經網路來建構地圖的方法[1]此方法在每次進入不

同環境的時候不須要經過訓練即可構出可靠度高的地圖

在此我們將利用 Visual Basic 以打點的方式在螢幕上構出地圖並期望使用此一較

簡單之方法亦能構出可靠度高的地圖

3

第二章 基礎理論

21 超音波特性

一般人耳能聽見的聲音頻率在20~20 的範圍內超音波意指超出此範圍之音波

圖 21 為人耳可以感受到的頻率範圍與振動的強度關係而本論文所使用的超音波中

心頻率為40 kHz

kHz

共振頻率隨著用途和元件的差異而有所不同它大部份被用來偵測物體的運動速

度短距離的量測與判定物體之有無等屬於一種非接觸型的感測元件超音波感測器

分成發射器和接收器兩者同樣具有「壓電效應」對超音波發射器而言稱為「電壓效

應」主要是將電氣信號轉換成機械的信號去壓縮周圍空氣而這些受壓縮的空氣將會

壓縮接收器的壓電材料並產生「壓電效應」進而將機械信號轉換成電氣信號[2]

圖21 人耳可以感受到的頻率範圍與振動的強度關係

超音波的強度會隨傳送距離而衰減主要是由於能量的散失及分佈球面積越來越大的

緣故且超音波會隨頻率之增加衰減的情形越嚴重因此若使用 50 以上之超音波

來量測一公尺以上的距離時反射波需加以放大極高之倍率[3]

kHz

4

22 超音波感測原理

超音波感測器使用之場合依對象物體之大小表面之狀態運動狀態能否接觸

等其頻率感測器之配置檢出方式感測器之種類指向性等皆有檢討之必要表

21 是依其特徵而將感測器分成三種不同的配置和檢出方式使用反射式的場合從目的

物以外產生之反射波或繞射波都有可能會對訊號造成干擾指向性及輸出大小也得對應

調節欲增強指向性可用喇叭型結構依其形狀有圓錐型喇叭及指數關係型喇叭等可用

反射方式(獨立型)為連續發射之場合直接波之繞射現象必須採取對策處理可以把發

射接收器之距離拉大來改善此問題若無法根據原理結構上拉大兩者之距離就得從

感測器之安裝方法及安裝位置下功夫[4]

在此使用的量測方法是飛行時間的檢測也就是超音波從發射開始到接收到反射波

的時間如此便可求得物體與探頭間的距離其關係如下式

D (距離)= (音速) C times2

TOF (時間) (21)

其中TOF(time of flight)為飛行時間 TC 60331+= ( ms )T是操作溫度( ) C0

本論文是使用微處理機內建之16位元計時器因為使用的石英振盪器頻率為

110592 MHz所以最小的計數時間為t = MHz05921112 = 1085 sμ 因此21式可以改寫

2

tCCountD timestimes= (22)

其中 為量測距離Count為Count0計數值C為音速(34600D seccm 25 )t為時

C0

5

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 7: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

圖目錄

圖 11 超音波自動測距系統設計與應用流程圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip2

圖 21 人耳可以感受到的頻率範圍與振動的強度關係helliphelliphelliphelliphelliphelliphelliphelliphelliphellip4

圖 31 系統架構方塊圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip7

圖 32 8051 單晶片腳位圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip8

圖 33 電源電路(+12V)helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip9

圖 34 常用 555振盪器電路helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip10

圖 35 NE555 應用電路設計圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip10

圖 36 NE555 內部方塊圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

圖 37 10cm內電壓與距離關係圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

圖 38 主動式帶通濾波器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip13

圖 39 主動式帶通濾波器之波德圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip16

圖 310 主動式帶通濾波器之波德圖(中心頻率3998kHz)helliphelliphelliphelliphelliphelliphelliphelliphellip16

圖 311 電壓比較器helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip17

圖 312 RS-232 準位轉換專用 IC ndashMAX232 helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip20

圖 41 人機介面程式流程圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip22

圖 42 串列埠輸入區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip23

圖 43 單位換算區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 44 資料輸入區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 45 分析結果區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip24

圖 46 數值讀取區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip25

圖 47 面積體積模擬區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip25

圖 48 掃描顯像區圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 49 開啟通訊埠關閉通訊埠系統結束圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 410 通訊埠指示燈圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

vi

圖 411 選擇操作模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 412 基本檢測模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 413 Microsoft Excel之儲檔圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 414 面積體積模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 415 面積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 416 體積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 417 掃描顯像模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 418 掃描顯像模式執行順序圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip30

圖 51 實際量測距離與平均百分誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

圖 52 超音波測距硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 53 電源電路實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 54 發射及接收電路板實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip33

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體helliphelliphelliphelliphelliphelliphellip33

圖 56 基本檢測模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip34

圖 57 距離量測數據分析與平均誤差helliphelliphelliphelliphellip35

圖 58 動態旋轉量測上視圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

圖 59 掃瞄顯示模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

vii

表目錄

表 21 超音波感測器的檢出方式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip6

表 31 RS正反器真值表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip11

表 32 超音波相對式距離電壓關係表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

表 33 RS-232 9PIN 腳位和 25PIN 腳位定義helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip18

表 51 實際距離量測誤差分析表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

表 52 距離量測數據分析與平均誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

viii

第一章 緒論

11 研究動機及目的

近年來關於自走式機器人或自走車的研究及相關運用技術相當廣泛機器人的研發

主要目的之一是為了取代人力使人類避免身陷於危險之中或加快工作任務完成的速

度例如在農業上可利用機器人噴灑農藥水產養殖及田間作業hellip等在工業上基

於安全的考量也可利用機器人作為探勘的工具或用來減少人力支出以降低成本並

使意外傷害減到最低如核能發電場消防救災太空探測及物料搬運hellip等

為了完成我們所指派的任務機器人必須具備有基本運動以及周遭環境感測的能

力在無人操作的情況之下隨著環境的變動能作出邏輯判斷並進行自我調整的功能

以完成所付予的工作因此機器人須要配備有不同的感測器如溫度感測器振動感測

器CCD感測器和超音波感測器hellip等以獲取外在的環境資訊

本文中將針對距離的感測做電子電路的設計以完成價格低廉的距離感測器模組

距離量測可使用雷射或超音波雷射優點為精準度極高但價格相當昂貴在此將使用

容易取得且價格便宜之超音波感測器來完成本專題研究目的在於建立一超音波自動

測距系統

12 研究方法

整個系統架構的完整規劃以繪製系統流程圖展示主要分為硬體架構和軟體

設計可摘要分項敘述於下所有分項工作及軟硬體設計整體相關性請參考所附

之研究流程圖如圖 11 所示並以此流程圖為藍圖完成本專題研究

1 收集充足的相關文獻資料與整理分析

2 驗證超音波驅動電路之設計

3 微波信號之接收電路設計與測試以期發展最佳化的電路設計

4 超音波飛行時間之數據傳輸電路與介面設計

5 距離量測系統與 PC 通訊傳輸系統之連結

1

文獻收集與整理

接收端信號放大濾波設計收集軟體資料 超音波觸發振盪器設計

介面技術與周邊程式設計

人機介面程式撰寫

軟體測試

超音波量測系統模型建立

帶通濾波器測試

振盪器測試

電壓比較器設計

比較器測試

系統整合

系統分析與改善

系統測試

結論與論文寫作

專題發表

NO

YESNO

NO NO

NO

YES

YESYES

YES

系統應用

圖 11 超音波自動測距系統設計與應用流程圖

2

6 量測數據分析與電路改善

7 信號擷取系統應用與可視化人機介面的整合

8 自動量測分析討論與建議

9 專題發表及完成論文寫作

13 文獻回顧

在國內外有關本文的相關研究主要有超音波飛行時間的量測3D 物體的量測及頭部

追蹤的系統在超音波飛行時間的量測方面最常見的方法則是臨界電壓法是利用事

先預設之一電壓臨界值當發射器發射出超音波訊號時電腦即開始計數脈波直到接

收器接收到超音波訊號時再去判斷此接收到之訊號是否已經超過之前所設定之臨界

值如果尚未超出臨界值則電腦繼續計數脈波數一旦訊號超出設定之臨界值則電

腦即停止脈波的計數然後再經由計數到之脈波數目求得超音波的飛行時間

另外在智慧型機器人的路徑規劃和導航方面也是超音波應用的重點F J Toledo

等人提出了一個使用超音波配合類神經網路來建構地圖的方法[1]此方法在每次進入不

同環境的時候不須要經過訓練即可構出可靠度高的地圖

在此我們將利用 Visual Basic 以打點的方式在螢幕上構出地圖並期望使用此一較

簡單之方法亦能構出可靠度高的地圖

3

第二章 基礎理論

21 超音波特性

一般人耳能聽見的聲音頻率在20~20 的範圍內超音波意指超出此範圍之音波

圖 21 為人耳可以感受到的頻率範圍與振動的強度關係而本論文所使用的超音波中

心頻率為40 kHz

kHz

共振頻率隨著用途和元件的差異而有所不同它大部份被用來偵測物體的運動速

度短距離的量測與判定物體之有無等屬於一種非接觸型的感測元件超音波感測器

分成發射器和接收器兩者同樣具有「壓電效應」對超音波發射器而言稱為「電壓效

應」主要是將電氣信號轉換成機械的信號去壓縮周圍空氣而這些受壓縮的空氣將會

壓縮接收器的壓電材料並產生「壓電效應」進而將機械信號轉換成電氣信號[2]

圖21 人耳可以感受到的頻率範圍與振動的強度關係

超音波的強度會隨傳送距離而衰減主要是由於能量的散失及分佈球面積越來越大的

緣故且超音波會隨頻率之增加衰減的情形越嚴重因此若使用 50 以上之超音波

來量測一公尺以上的距離時反射波需加以放大極高之倍率[3]

kHz

4

22 超音波感測原理

超音波感測器使用之場合依對象物體之大小表面之狀態運動狀態能否接觸

等其頻率感測器之配置檢出方式感測器之種類指向性等皆有檢討之必要表

21 是依其特徵而將感測器分成三種不同的配置和檢出方式使用反射式的場合從目的

物以外產生之反射波或繞射波都有可能會對訊號造成干擾指向性及輸出大小也得對應

調節欲增強指向性可用喇叭型結構依其形狀有圓錐型喇叭及指數關係型喇叭等可用

反射方式(獨立型)為連續發射之場合直接波之繞射現象必須採取對策處理可以把發

射接收器之距離拉大來改善此問題若無法根據原理結構上拉大兩者之距離就得從

感測器之安裝方法及安裝位置下功夫[4]

在此使用的量測方法是飛行時間的檢測也就是超音波從發射開始到接收到反射波

的時間如此便可求得物體與探頭間的距離其關係如下式

D (距離)= (音速) C times2

TOF (時間) (21)

其中TOF(time of flight)為飛行時間 TC 60331+= ( ms )T是操作溫度( ) C0

本論文是使用微處理機內建之16位元計時器因為使用的石英振盪器頻率為

110592 MHz所以最小的計數時間為t = MHz05921112 = 1085 sμ 因此21式可以改寫

2

tCCountD timestimes= (22)

其中 為量測距離Count為Count0計數值C為音速(34600D seccm 25 )t為時

C0

5

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 8: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

圖 411 選擇操作模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip26

圖 412 基本檢測模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 413 Microsoft Excel之儲檔圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip27

圖 414 面積體積模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 415 面積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip28

圖 416 體積模式執行圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 417 掃描顯像模式圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip29

圖 418 掃描顯像模式執行順序圖例helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip30

圖 51 實際量測距離與平均百分誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

圖 52 超音波測距硬體架構helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 53 電源電路實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip32

圖 54 發射及接收電路板實體圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip33

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體helliphelliphelliphelliphelliphelliphellip33

圖 56 基本檢測模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip34

圖 57 距離量測數據分析與平均誤差helliphelliphelliphelliphellip35

圖 58 動態旋轉量測上視圖helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

圖 59 掃瞄顯示模式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip36

vii

表目錄

表 21 超音波感測器的檢出方式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip6

表 31 RS正反器真值表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip11

表 32 超音波相對式距離電壓關係表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

表 33 RS-232 9PIN 腳位和 25PIN 腳位定義helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip18

表 51 實際距離量測誤差分析表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

表 52 距離量測數據分析與平均誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

viii

第一章 緒論

11 研究動機及目的

近年來關於自走式機器人或自走車的研究及相關運用技術相當廣泛機器人的研發

主要目的之一是為了取代人力使人類避免身陷於危險之中或加快工作任務完成的速

度例如在農業上可利用機器人噴灑農藥水產養殖及田間作業hellip等在工業上基

於安全的考量也可利用機器人作為探勘的工具或用來減少人力支出以降低成本並

使意外傷害減到最低如核能發電場消防救災太空探測及物料搬運hellip等

為了完成我們所指派的任務機器人必須具備有基本運動以及周遭環境感測的能

力在無人操作的情況之下隨著環境的變動能作出邏輯判斷並進行自我調整的功能

以完成所付予的工作因此機器人須要配備有不同的感測器如溫度感測器振動感測

器CCD感測器和超音波感測器hellip等以獲取外在的環境資訊

本文中將針對距離的感測做電子電路的設計以完成價格低廉的距離感測器模組

距離量測可使用雷射或超音波雷射優點為精準度極高但價格相當昂貴在此將使用

容易取得且價格便宜之超音波感測器來完成本專題研究目的在於建立一超音波自動

測距系統

12 研究方法

整個系統架構的完整規劃以繪製系統流程圖展示主要分為硬體架構和軟體

設計可摘要分項敘述於下所有分項工作及軟硬體設計整體相關性請參考所附

之研究流程圖如圖 11 所示並以此流程圖為藍圖完成本專題研究

1 收集充足的相關文獻資料與整理分析

2 驗證超音波驅動電路之設計

3 微波信號之接收電路設計與測試以期發展最佳化的電路設計

4 超音波飛行時間之數據傳輸電路與介面設計

5 距離量測系統與 PC 通訊傳輸系統之連結

1

文獻收集與整理

接收端信號放大濾波設計收集軟體資料 超音波觸發振盪器設計

介面技術與周邊程式設計

人機介面程式撰寫

軟體測試

超音波量測系統模型建立

帶通濾波器測試

振盪器測試

電壓比較器設計

比較器測試

系統整合

系統分析與改善

系統測試

結論與論文寫作

專題發表

NO

YESNO

NO NO

NO

YES

YESYES

YES

系統應用

圖 11 超音波自動測距系統設計與應用流程圖

2

6 量測數據分析與電路改善

7 信號擷取系統應用與可視化人機介面的整合

8 自動量測分析討論與建議

9 專題發表及完成論文寫作

13 文獻回顧

在國內外有關本文的相關研究主要有超音波飛行時間的量測3D 物體的量測及頭部

追蹤的系統在超音波飛行時間的量測方面最常見的方法則是臨界電壓法是利用事

先預設之一電壓臨界值當發射器發射出超音波訊號時電腦即開始計數脈波直到接

收器接收到超音波訊號時再去判斷此接收到之訊號是否已經超過之前所設定之臨界

值如果尚未超出臨界值則電腦繼續計數脈波數一旦訊號超出設定之臨界值則電

腦即停止脈波的計數然後再經由計數到之脈波數目求得超音波的飛行時間

另外在智慧型機器人的路徑規劃和導航方面也是超音波應用的重點F J Toledo

等人提出了一個使用超音波配合類神經網路來建構地圖的方法[1]此方法在每次進入不

同環境的時候不須要經過訓練即可構出可靠度高的地圖

在此我們將利用 Visual Basic 以打點的方式在螢幕上構出地圖並期望使用此一較

簡單之方法亦能構出可靠度高的地圖

3

第二章 基礎理論

21 超音波特性

一般人耳能聽見的聲音頻率在20~20 的範圍內超音波意指超出此範圍之音波

圖 21 為人耳可以感受到的頻率範圍與振動的強度關係而本論文所使用的超音波中

心頻率為40 kHz

kHz

共振頻率隨著用途和元件的差異而有所不同它大部份被用來偵測物體的運動速

度短距離的量測與判定物體之有無等屬於一種非接觸型的感測元件超音波感測器

分成發射器和接收器兩者同樣具有「壓電效應」對超音波發射器而言稱為「電壓效

應」主要是將電氣信號轉換成機械的信號去壓縮周圍空氣而這些受壓縮的空氣將會

壓縮接收器的壓電材料並產生「壓電效應」進而將機械信號轉換成電氣信號[2]

圖21 人耳可以感受到的頻率範圍與振動的強度關係

超音波的強度會隨傳送距離而衰減主要是由於能量的散失及分佈球面積越來越大的

緣故且超音波會隨頻率之增加衰減的情形越嚴重因此若使用 50 以上之超音波

來量測一公尺以上的距離時反射波需加以放大極高之倍率[3]

kHz

4

22 超音波感測原理

超音波感測器使用之場合依對象物體之大小表面之狀態運動狀態能否接觸

等其頻率感測器之配置檢出方式感測器之種類指向性等皆有檢討之必要表

21 是依其特徵而將感測器分成三種不同的配置和檢出方式使用反射式的場合從目的

物以外產生之反射波或繞射波都有可能會對訊號造成干擾指向性及輸出大小也得對應

調節欲增強指向性可用喇叭型結構依其形狀有圓錐型喇叭及指數關係型喇叭等可用

反射方式(獨立型)為連續發射之場合直接波之繞射現象必須採取對策處理可以把發

射接收器之距離拉大來改善此問題若無法根據原理結構上拉大兩者之距離就得從

感測器之安裝方法及安裝位置下功夫[4]

在此使用的量測方法是飛行時間的檢測也就是超音波從發射開始到接收到反射波

的時間如此便可求得物體與探頭間的距離其關係如下式

D (距離)= (音速) C times2

TOF (時間) (21)

其中TOF(time of flight)為飛行時間 TC 60331+= ( ms )T是操作溫度( ) C0

本論文是使用微處理機內建之16位元計時器因為使用的石英振盪器頻率為

110592 MHz所以最小的計數時間為t = MHz05921112 = 1085 sμ 因此21式可以改寫

2

tCCountD timestimes= (22)

其中 為量測距離Count為Count0計數值C為音速(34600D seccm 25 )t為時

C0

5

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 9: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

表目錄

表 21 超音波感測器的檢出方式helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip6

表 31 RS正反器真值表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip11

表 32 超音波相對式距離電壓關係表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip12

表 33 RS-232 9PIN 腳位和 25PIN 腳位定義helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip18

表 51 實際距離量測誤差分析表helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip31

表 52 距離量測數據分析與平均誤差helliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphelliphellip35

viii

第一章 緒論

11 研究動機及目的

近年來關於自走式機器人或自走車的研究及相關運用技術相當廣泛機器人的研發

主要目的之一是為了取代人力使人類避免身陷於危險之中或加快工作任務完成的速

度例如在農業上可利用機器人噴灑農藥水產養殖及田間作業hellip等在工業上基

於安全的考量也可利用機器人作為探勘的工具或用來減少人力支出以降低成本並

使意外傷害減到最低如核能發電場消防救災太空探測及物料搬運hellip等

為了完成我們所指派的任務機器人必須具備有基本運動以及周遭環境感測的能

力在無人操作的情況之下隨著環境的變動能作出邏輯判斷並進行自我調整的功能

以完成所付予的工作因此機器人須要配備有不同的感測器如溫度感測器振動感測

器CCD感測器和超音波感測器hellip等以獲取外在的環境資訊

本文中將針對距離的感測做電子電路的設計以完成價格低廉的距離感測器模組

距離量測可使用雷射或超音波雷射優點為精準度極高但價格相當昂貴在此將使用

容易取得且價格便宜之超音波感測器來完成本專題研究目的在於建立一超音波自動

測距系統

12 研究方法

整個系統架構的完整規劃以繪製系統流程圖展示主要分為硬體架構和軟體

設計可摘要分項敘述於下所有分項工作及軟硬體設計整體相關性請參考所附

之研究流程圖如圖 11 所示並以此流程圖為藍圖完成本專題研究

1 收集充足的相關文獻資料與整理分析

2 驗證超音波驅動電路之設計

3 微波信號之接收電路設計與測試以期發展最佳化的電路設計

4 超音波飛行時間之數據傳輸電路與介面設計

5 距離量測系統與 PC 通訊傳輸系統之連結

1

文獻收集與整理

接收端信號放大濾波設計收集軟體資料 超音波觸發振盪器設計

介面技術與周邊程式設計

人機介面程式撰寫

軟體測試

超音波量測系統模型建立

帶通濾波器測試

振盪器測試

電壓比較器設計

比較器測試

系統整合

系統分析與改善

系統測試

結論與論文寫作

專題發表

NO

YESNO

NO NO

NO

YES

YESYES

YES

系統應用

圖 11 超音波自動測距系統設計與應用流程圖

2

6 量測數據分析與電路改善

7 信號擷取系統應用與可視化人機介面的整合

8 自動量測分析討論與建議

9 專題發表及完成論文寫作

13 文獻回顧

在國內外有關本文的相關研究主要有超音波飛行時間的量測3D 物體的量測及頭部

追蹤的系統在超音波飛行時間的量測方面最常見的方法則是臨界電壓法是利用事

先預設之一電壓臨界值當發射器發射出超音波訊號時電腦即開始計數脈波直到接

收器接收到超音波訊號時再去判斷此接收到之訊號是否已經超過之前所設定之臨界

值如果尚未超出臨界值則電腦繼續計數脈波數一旦訊號超出設定之臨界值則電

腦即停止脈波的計數然後再經由計數到之脈波數目求得超音波的飛行時間

另外在智慧型機器人的路徑規劃和導航方面也是超音波應用的重點F J Toledo

等人提出了一個使用超音波配合類神經網路來建構地圖的方法[1]此方法在每次進入不

同環境的時候不須要經過訓練即可構出可靠度高的地圖

在此我們將利用 Visual Basic 以打點的方式在螢幕上構出地圖並期望使用此一較

簡單之方法亦能構出可靠度高的地圖

3

第二章 基礎理論

21 超音波特性

一般人耳能聽見的聲音頻率在20~20 的範圍內超音波意指超出此範圍之音波

圖 21 為人耳可以感受到的頻率範圍與振動的強度關係而本論文所使用的超音波中

心頻率為40 kHz

kHz

共振頻率隨著用途和元件的差異而有所不同它大部份被用來偵測物體的運動速

度短距離的量測與判定物體之有無等屬於一種非接觸型的感測元件超音波感測器

分成發射器和接收器兩者同樣具有「壓電效應」對超音波發射器而言稱為「電壓效

應」主要是將電氣信號轉換成機械的信號去壓縮周圍空氣而這些受壓縮的空氣將會

壓縮接收器的壓電材料並產生「壓電效應」進而將機械信號轉換成電氣信號[2]

圖21 人耳可以感受到的頻率範圍與振動的強度關係

超音波的強度會隨傳送距離而衰減主要是由於能量的散失及分佈球面積越來越大的

緣故且超音波會隨頻率之增加衰減的情形越嚴重因此若使用 50 以上之超音波

來量測一公尺以上的距離時反射波需加以放大極高之倍率[3]

kHz

4

22 超音波感測原理

超音波感測器使用之場合依對象物體之大小表面之狀態運動狀態能否接觸

等其頻率感測器之配置檢出方式感測器之種類指向性等皆有檢討之必要表

21 是依其特徵而將感測器分成三種不同的配置和檢出方式使用反射式的場合從目的

物以外產生之反射波或繞射波都有可能會對訊號造成干擾指向性及輸出大小也得對應

調節欲增強指向性可用喇叭型結構依其形狀有圓錐型喇叭及指數關係型喇叭等可用

反射方式(獨立型)為連續發射之場合直接波之繞射現象必須採取對策處理可以把發

射接收器之距離拉大來改善此問題若無法根據原理結構上拉大兩者之距離就得從

感測器之安裝方法及安裝位置下功夫[4]

在此使用的量測方法是飛行時間的檢測也就是超音波從發射開始到接收到反射波

的時間如此便可求得物體與探頭間的距離其關係如下式

D (距離)= (音速) C times2

TOF (時間) (21)

其中TOF(time of flight)為飛行時間 TC 60331+= ( ms )T是操作溫度( ) C0

本論文是使用微處理機內建之16位元計時器因為使用的石英振盪器頻率為

110592 MHz所以最小的計數時間為t = MHz05921112 = 1085 sμ 因此21式可以改寫

2

tCCountD timestimes= (22)

其中 為量測距離Count為Count0計數值C為音速(34600D seccm 25 )t為時

C0

5

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 10: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

第一章 緒論

11 研究動機及目的

近年來關於自走式機器人或自走車的研究及相關運用技術相當廣泛機器人的研發

主要目的之一是為了取代人力使人類避免身陷於危險之中或加快工作任務完成的速

度例如在農業上可利用機器人噴灑農藥水產養殖及田間作業hellip等在工業上基

於安全的考量也可利用機器人作為探勘的工具或用來減少人力支出以降低成本並

使意外傷害減到最低如核能發電場消防救災太空探測及物料搬運hellip等

為了完成我們所指派的任務機器人必須具備有基本運動以及周遭環境感測的能

力在無人操作的情況之下隨著環境的變動能作出邏輯判斷並進行自我調整的功能

以完成所付予的工作因此機器人須要配備有不同的感測器如溫度感測器振動感測

器CCD感測器和超音波感測器hellip等以獲取外在的環境資訊

本文中將針對距離的感測做電子電路的設計以完成價格低廉的距離感測器模組

距離量測可使用雷射或超音波雷射優點為精準度極高但價格相當昂貴在此將使用

容易取得且價格便宜之超音波感測器來完成本專題研究目的在於建立一超音波自動

測距系統

12 研究方法

整個系統架構的完整規劃以繪製系統流程圖展示主要分為硬體架構和軟體

設計可摘要分項敘述於下所有分項工作及軟硬體設計整體相關性請參考所附

之研究流程圖如圖 11 所示並以此流程圖為藍圖完成本專題研究

1 收集充足的相關文獻資料與整理分析

2 驗證超音波驅動電路之設計

3 微波信號之接收電路設計與測試以期發展最佳化的電路設計

4 超音波飛行時間之數據傳輸電路與介面設計

5 距離量測系統與 PC 通訊傳輸系統之連結

1

文獻收集與整理

接收端信號放大濾波設計收集軟體資料 超音波觸發振盪器設計

介面技術與周邊程式設計

人機介面程式撰寫

軟體測試

超音波量測系統模型建立

帶通濾波器測試

振盪器測試

電壓比較器設計

比較器測試

系統整合

系統分析與改善

系統測試

結論與論文寫作

專題發表

NO

YESNO

NO NO

NO

YES

YESYES

YES

系統應用

圖 11 超音波自動測距系統設計與應用流程圖

2

6 量測數據分析與電路改善

7 信號擷取系統應用與可視化人機介面的整合

8 自動量測分析討論與建議

9 專題發表及完成論文寫作

13 文獻回顧

在國內外有關本文的相關研究主要有超音波飛行時間的量測3D 物體的量測及頭部

追蹤的系統在超音波飛行時間的量測方面最常見的方法則是臨界電壓法是利用事

先預設之一電壓臨界值當發射器發射出超音波訊號時電腦即開始計數脈波直到接

收器接收到超音波訊號時再去判斷此接收到之訊號是否已經超過之前所設定之臨界

值如果尚未超出臨界值則電腦繼續計數脈波數一旦訊號超出設定之臨界值則電

腦即停止脈波的計數然後再經由計數到之脈波數目求得超音波的飛行時間

另外在智慧型機器人的路徑規劃和導航方面也是超音波應用的重點F J Toledo

等人提出了一個使用超音波配合類神經網路來建構地圖的方法[1]此方法在每次進入不

同環境的時候不須要經過訓練即可構出可靠度高的地圖

在此我們將利用 Visual Basic 以打點的方式在螢幕上構出地圖並期望使用此一較

簡單之方法亦能構出可靠度高的地圖

3

第二章 基礎理論

21 超音波特性

一般人耳能聽見的聲音頻率在20~20 的範圍內超音波意指超出此範圍之音波

圖 21 為人耳可以感受到的頻率範圍與振動的強度關係而本論文所使用的超音波中

心頻率為40 kHz

kHz

共振頻率隨著用途和元件的差異而有所不同它大部份被用來偵測物體的運動速

度短距離的量測與判定物體之有無等屬於一種非接觸型的感測元件超音波感測器

分成發射器和接收器兩者同樣具有「壓電效應」對超音波發射器而言稱為「電壓效

應」主要是將電氣信號轉換成機械的信號去壓縮周圍空氣而這些受壓縮的空氣將會

壓縮接收器的壓電材料並產生「壓電效應」進而將機械信號轉換成電氣信號[2]

圖21 人耳可以感受到的頻率範圍與振動的強度關係

超音波的強度會隨傳送距離而衰減主要是由於能量的散失及分佈球面積越來越大的

緣故且超音波會隨頻率之增加衰減的情形越嚴重因此若使用 50 以上之超音波

來量測一公尺以上的距離時反射波需加以放大極高之倍率[3]

kHz

4

22 超音波感測原理

超音波感測器使用之場合依對象物體之大小表面之狀態運動狀態能否接觸

等其頻率感測器之配置檢出方式感測器之種類指向性等皆有檢討之必要表

21 是依其特徵而將感測器分成三種不同的配置和檢出方式使用反射式的場合從目的

物以外產生之反射波或繞射波都有可能會對訊號造成干擾指向性及輸出大小也得對應

調節欲增強指向性可用喇叭型結構依其形狀有圓錐型喇叭及指數關係型喇叭等可用

反射方式(獨立型)為連續發射之場合直接波之繞射現象必須採取對策處理可以把發

射接收器之距離拉大來改善此問題若無法根據原理結構上拉大兩者之距離就得從

感測器之安裝方法及安裝位置下功夫[4]

在此使用的量測方法是飛行時間的檢測也就是超音波從發射開始到接收到反射波

的時間如此便可求得物體與探頭間的距離其關係如下式

D (距離)= (音速) C times2

TOF (時間) (21)

其中TOF(time of flight)為飛行時間 TC 60331+= ( ms )T是操作溫度( ) C0

本論文是使用微處理機內建之16位元計時器因為使用的石英振盪器頻率為

110592 MHz所以最小的計數時間為t = MHz05921112 = 1085 sμ 因此21式可以改寫

2

tCCountD timestimes= (22)

其中 為量測距離Count為Count0計數值C為音速(34600D seccm 25 )t為時

C0

5

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 11: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

文獻收集與整理

接收端信號放大濾波設計收集軟體資料 超音波觸發振盪器設計

介面技術與周邊程式設計

人機介面程式撰寫

軟體測試

超音波量測系統模型建立

帶通濾波器測試

振盪器測試

電壓比較器設計

比較器測試

系統整合

系統分析與改善

系統測試

結論與論文寫作

專題發表

NO

YESNO

NO NO

NO

YES

YESYES

YES

系統應用

圖 11 超音波自動測距系統設計與應用流程圖

2

6 量測數據分析與電路改善

7 信號擷取系統應用與可視化人機介面的整合

8 自動量測分析討論與建議

9 專題發表及完成論文寫作

13 文獻回顧

在國內外有關本文的相關研究主要有超音波飛行時間的量測3D 物體的量測及頭部

追蹤的系統在超音波飛行時間的量測方面最常見的方法則是臨界電壓法是利用事

先預設之一電壓臨界值當發射器發射出超音波訊號時電腦即開始計數脈波直到接

收器接收到超音波訊號時再去判斷此接收到之訊號是否已經超過之前所設定之臨界

值如果尚未超出臨界值則電腦繼續計數脈波數一旦訊號超出設定之臨界值則電

腦即停止脈波的計數然後再經由計數到之脈波數目求得超音波的飛行時間

另外在智慧型機器人的路徑規劃和導航方面也是超音波應用的重點F J Toledo

等人提出了一個使用超音波配合類神經網路來建構地圖的方法[1]此方法在每次進入不

同環境的時候不須要經過訓練即可構出可靠度高的地圖

在此我們將利用 Visual Basic 以打點的方式在螢幕上構出地圖並期望使用此一較

簡單之方法亦能構出可靠度高的地圖

3

第二章 基礎理論

21 超音波特性

一般人耳能聽見的聲音頻率在20~20 的範圍內超音波意指超出此範圍之音波

圖 21 為人耳可以感受到的頻率範圍與振動的強度關係而本論文所使用的超音波中

心頻率為40 kHz

kHz

共振頻率隨著用途和元件的差異而有所不同它大部份被用來偵測物體的運動速

度短距離的量測與判定物體之有無等屬於一種非接觸型的感測元件超音波感測器

分成發射器和接收器兩者同樣具有「壓電效應」對超音波發射器而言稱為「電壓效

應」主要是將電氣信號轉換成機械的信號去壓縮周圍空氣而這些受壓縮的空氣將會

壓縮接收器的壓電材料並產生「壓電效應」進而將機械信號轉換成電氣信號[2]

圖21 人耳可以感受到的頻率範圍與振動的強度關係

超音波的強度會隨傳送距離而衰減主要是由於能量的散失及分佈球面積越來越大的

緣故且超音波會隨頻率之增加衰減的情形越嚴重因此若使用 50 以上之超音波

來量測一公尺以上的距離時反射波需加以放大極高之倍率[3]

kHz

4

22 超音波感測原理

超音波感測器使用之場合依對象物體之大小表面之狀態運動狀態能否接觸

等其頻率感測器之配置檢出方式感測器之種類指向性等皆有檢討之必要表

21 是依其特徵而將感測器分成三種不同的配置和檢出方式使用反射式的場合從目的

物以外產生之反射波或繞射波都有可能會對訊號造成干擾指向性及輸出大小也得對應

調節欲增強指向性可用喇叭型結構依其形狀有圓錐型喇叭及指數關係型喇叭等可用

反射方式(獨立型)為連續發射之場合直接波之繞射現象必須採取對策處理可以把發

射接收器之距離拉大來改善此問題若無法根據原理結構上拉大兩者之距離就得從

感測器之安裝方法及安裝位置下功夫[4]

在此使用的量測方法是飛行時間的檢測也就是超音波從發射開始到接收到反射波

的時間如此便可求得物體與探頭間的距離其關係如下式

D (距離)= (音速) C times2

TOF (時間) (21)

其中TOF(time of flight)為飛行時間 TC 60331+= ( ms )T是操作溫度( ) C0

本論文是使用微處理機內建之16位元計時器因為使用的石英振盪器頻率為

110592 MHz所以最小的計數時間為t = MHz05921112 = 1085 sμ 因此21式可以改寫

2

tCCountD timestimes= (22)

其中 為量測距離Count為Count0計數值C為音速(34600D seccm 25 )t為時

C0

5

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 12: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

6 量測數據分析與電路改善

7 信號擷取系統應用與可視化人機介面的整合

8 自動量測分析討論與建議

9 專題發表及完成論文寫作

13 文獻回顧

在國內外有關本文的相關研究主要有超音波飛行時間的量測3D 物體的量測及頭部

追蹤的系統在超音波飛行時間的量測方面最常見的方法則是臨界電壓法是利用事

先預設之一電壓臨界值當發射器發射出超音波訊號時電腦即開始計數脈波直到接

收器接收到超音波訊號時再去判斷此接收到之訊號是否已經超過之前所設定之臨界

值如果尚未超出臨界值則電腦繼續計數脈波數一旦訊號超出設定之臨界值則電

腦即停止脈波的計數然後再經由計數到之脈波數目求得超音波的飛行時間

另外在智慧型機器人的路徑規劃和導航方面也是超音波應用的重點F J Toledo

等人提出了一個使用超音波配合類神經網路來建構地圖的方法[1]此方法在每次進入不

同環境的時候不須要經過訓練即可構出可靠度高的地圖

在此我們將利用 Visual Basic 以打點的方式在螢幕上構出地圖並期望使用此一較

簡單之方法亦能構出可靠度高的地圖

3

第二章 基礎理論

21 超音波特性

一般人耳能聽見的聲音頻率在20~20 的範圍內超音波意指超出此範圍之音波

圖 21 為人耳可以感受到的頻率範圍與振動的強度關係而本論文所使用的超音波中

心頻率為40 kHz

kHz

共振頻率隨著用途和元件的差異而有所不同它大部份被用來偵測物體的運動速

度短距離的量測與判定物體之有無等屬於一種非接觸型的感測元件超音波感測器

分成發射器和接收器兩者同樣具有「壓電效應」對超音波發射器而言稱為「電壓效

應」主要是將電氣信號轉換成機械的信號去壓縮周圍空氣而這些受壓縮的空氣將會

壓縮接收器的壓電材料並產生「壓電效應」進而將機械信號轉換成電氣信號[2]

圖21 人耳可以感受到的頻率範圍與振動的強度關係

超音波的強度會隨傳送距離而衰減主要是由於能量的散失及分佈球面積越來越大的

緣故且超音波會隨頻率之增加衰減的情形越嚴重因此若使用 50 以上之超音波

來量測一公尺以上的距離時反射波需加以放大極高之倍率[3]

kHz

4

22 超音波感測原理

超音波感測器使用之場合依對象物體之大小表面之狀態運動狀態能否接觸

等其頻率感測器之配置檢出方式感測器之種類指向性等皆有檢討之必要表

21 是依其特徵而將感測器分成三種不同的配置和檢出方式使用反射式的場合從目的

物以外產生之反射波或繞射波都有可能會對訊號造成干擾指向性及輸出大小也得對應

調節欲增強指向性可用喇叭型結構依其形狀有圓錐型喇叭及指數關係型喇叭等可用

反射方式(獨立型)為連續發射之場合直接波之繞射現象必須採取對策處理可以把發

射接收器之距離拉大來改善此問題若無法根據原理結構上拉大兩者之距離就得從

感測器之安裝方法及安裝位置下功夫[4]

在此使用的量測方法是飛行時間的檢測也就是超音波從發射開始到接收到反射波

的時間如此便可求得物體與探頭間的距離其關係如下式

D (距離)= (音速) C times2

TOF (時間) (21)

其中TOF(time of flight)為飛行時間 TC 60331+= ( ms )T是操作溫度( ) C0

本論文是使用微處理機內建之16位元計時器因為使用的石英振盪器頻率為

110592 MHz所以最小的計數時間為t = MHz05921112 = 1085 sμ 因此21式可以改寫

2

tCCountD timestimes= (22)

其中 為量測距離Count為Count0計數值C為音速(34600D seccm 25 )t為時

C0

5

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 13: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

第二章 基礎理論

21 超音波特性

一般人耳能聽見的聲音頻率在20~20 的範圍內超音波意指超出此範圍之音波

圖 21 為人耳可以感受到的頻率範圍與振動的強度關係而本論文所使用的超音波中

心頻率為40 kHz

kHz

共振頻率隨著用途和元件的差異而有所不同它大部份被用來偵測物體的運動速

度短距離的量測與判定物體之有無等屬於一種非接觸型的感測元件超音波感測器

分成發射器和接收器兩者同樣具有「壓電效應」對超音波發射器而言稱為「電壓效

應」主要是將電氣信號轉換成機械的信號去壓縮周圍空氣而這些受壓縮的空氣將會

壓縮接收器的壓電材料並產生「壓電效應」進而將機械信號轉換成電氣信號[2]

圖21 人耳可以感受到的頻率範圍與振動的強度關係

超音波的強度會隨傳送距離而衰減主要是由於能量的散失及分佈球面積越來越大的

緣故且超音波會隨頻率之增加衰減的情形越嚴重因此若使用 50 以上之超音波

來量測一公尺以上的距離時反射波需加以放大極高之倍率[3]

kHz

4

22 超音波感測原理

超音波感測器使用之場合依對象物體之大小表面之狀態運動狀態能否接觸

等其頻率感測器之配置檢出方式感測器之種類指向性等皆有檢討之必要表

21 是依其特徵而將感測器分成三種不同的配置和檢出方式使用反射式的場合從目的

物以外產生之反射波或繞射波都有可能會對訊號造成干擾指向性及輸出大小也得對應

調節欲增強指向性可用喇叭型結構依其形狀有圓錐型喇叭及指數關係型喇叭等可用

反射方式(獨立型)為連續發射之場合直接波之繞射現象必須採取對策處理可以把發

射接收器之距離拉大來改善此問題若無法根據原理結構上拉大兩者之距離就得從

感測器之安裝方法及安裝位置下功夫[4]

在此使用的量測方法是飛行時間的檢測也就是超音波從發射開始到接收到反射波

的時間如此便可求得物體與探頭間的距離其關係如下式

D (距離)= (音速) C times2

TOF (時間) (21)

其中TOF(time of flight)為飛行時間 TC 60331+= ( ms )T是操作溫度( ) C0

本論文是使用微處理機內建之16位元計時器因為使用的石英振盪器頻率為

110592 MHz所以最小的計數時間為t = MHz05921112 = 1085 sμ 因此21式可以改寫

2

tCCountD timestimes= (22)

其中 為量測距離Count為Count0計數值C為音速(34600D seccm 25 )t為時

C0

5

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 14: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

22 超音波感測原理

超音波感測器使用之場合依對象物體之大小表面之狀態運動狀態能否接觸

等其頻率感測器之配置檢出方式感測器之種類指向性等皆有檢討之必要表

21 是依其特徵而將感測器分成三種不同的配置和檢出方式使用反射式的場合從目的

物以外產生之反射波或繞射波都有可能會對訊號造成干擾指向性及輸出大小也得對應

調節欲增強指向性可用喇叭型結構依其形狀有圓錐型喇叭及指數關係型喇叭等可用

反射方式(獨立型)為連續發射之場合直接波之繞射現象必須採取對策處理可以把發

射接收器之距離拉大來改善此問題若無法根據原理結構上拉大兩者之距離就得從

感測器之安裝方法及安裝位置下功夫[4]

在此使用的量測方法是飛行時間的檢測也就是超音波從發射開始到接收到反射波

的時間如此便可求得物體與探頭間的距離其關係如下式

D (距離)= (音速) C times2

TOF (時間) (21)

其中TOF(time of flight)為飛行時間 TC 60331+= ( ms )T是操作溫度( ) C0

本論文是使用微處理機內建之16位元計時器因為使用的石英振盪器頻率為

110592 MHz所以最小的計數時間為t = MHz05921112 = 1085 sμ 因此21式可以改寫

2

tCCountD timestimes= (22)

其中 為量測距離Count為Count0計數值C為音速(34600D seccm 25 )t為時

C0

5

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 15: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

表21 超音波感測器的檢出方式

類型 配置方式 用途 特徵

對向性

1遙控 2物體的檢知

1檢知感度可以自

由設定易於設

計 2需要兩個設置場

反射方式 (獨立型)

1物體的檢知

2距離之測量

1要有對策來處理

T 到 R 之直接繞

射問題 2可以使用T與R的

專用感測器 3近距離(10cm以

下)之情形居多

反射方式 (兼具型)

1物體的檢知

2距離之測量

1感測器要使用寬

頻帶型 2要有發送接收切

換電路 3近距離無法量測

6

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 16: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

第三章 超音波測距系統硬體架構

31 系統硬體架設

本研究使用一對超音波發射接受器為測距所使用之元件系統架設概略圖如下

超 音 波驅 動 電 路

超 音 波 接 收 器

微 處理 機

主 動 式 帶 通 濾 波器

超 音 波 發 射 器

比較 器

圖 31 系統架構方塊圖

將發射和感測探頭擺設在同一平面利用發射超音波反彈之音波使感測器之壓電

材料產生 40 之共振進而輸出微弱電壓訊號吾人需將感測探頭接收到之微小信號

放大且因放大倍率高為減少雜訊之影響故本研究採用主動式帶通濾波器以提升訊

雜比爾後再經電壓比較器來當靈敏度之調整最後將電氣訊號交與微處理機做比較

即可得知飛行時間只要將此飛行時間利用 RS-232 傳送致電腦經過運算之後即能

顯示量測距離

kHz

32 微處理單晶片簡介

本文所使用之控制晶片為8051微電腦單晶片控制器是超音波發射接收的核心元

件此控制器是由一個8位元的CPU組成另有32個IO腳共分為4個輸出入埠[5]本文

所使用的微處理器程式碼請參考附錄一圖32 為8051單晶片腳位圖8051 單晶片的

7

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 17: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

重要特性說明如下

(1) 8 位元微電腦晶片

(2) 4K Bytes 的內部程式記體

(3) 128 Bytes 可供讀寫的內部資料記憶體

(4) 可在外部擴充到64K Bytes 程式記憶體及64K Bytes 資料記憶體

(5) 2 組16 位元的計時器計數器(TimerCounter)

(6) 4 組8 位元的IO 並列埠共32 條可單獨規劃為輸入或輸出的IO 點

(7) 1 組全雙工的串列埠可連接RS-232 等標準的串列通信介面

(8) 可擴充為128K Bytes 的外部記憶體其中64K Bytes 為程式記憶體另外64K Bytes

為資料記憶體

(9) 可處理5 個中斷來源並可規劃其中斷優先權

(10) 內部具有時脈振盪器最高工作時脈可達12 MHz

圖32 8051單晶片腳位圖

8

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 18: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

33 電源電路設計

本專題電子電路所需的電壓源為plusmn12V 以及+5V乃是由市電之 AC 110V 經過中央抽

頭式變壓器 PT-16 降壓至 AC 12V後經橋式整流器整成直流此時之峰值電壓為 1697V

再利用電解電容 2200μF 耐壓為 35V 當電壓調整器 7812 之輸入電容並加上 01μF 之陶瓷

電容以防止雜訊及電壓的不穩定電壓調整器之輸出端電容則是選用電解電容 1000μF

耐壓為 35V 及陶瓷電容 01μF此時即可得到+12v 的輸出電壓-12V 以及+5V 以相同的

手法即可獲得此外為求電壓之穩定在穩壓器的部份皆使用兩個穩壓器並聯可

獲得穩定之輸入如圖 33 所示

圖 33 電源電路(+12V)

34 超音波發射驅動電路設計

在音波驅動源方面選用價格便宜功能強大且極易購得的 NE555 IC其電流輸出

最大 200 mA 且供應電壓範圍為 45~16 伏特電壓選擇性大常見使用 555 來設計

振盪的方波輸出接線圖如下[6]

9

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 19: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

圖 34 常用 555 振盪器電路

圖 34 之電路輸出之方波頻率會有隨時間變化而衰減的現象所以頻率極不穩定

本研究所須頻率要能固定在 40 所以在此利用回授觀念將輸出拉回輸入端再經

由調整可變電阻即可準確獲得所需之頻率且可完全改善頻率衰減的現象如圖 35

kHz

圖 35 NE555 應用電路設計圖

10

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 20: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

當 NE555 操作在無穩態時觸發端和臨界端是互相連接使得在NE555 內部形成自

我觸發當輸出是HIGH的時候 將會以時間常數1C ( ) 121 CRR times+ 開始充電一直到 超

過了

1CV

CCV32 後臨界電壓比較器會輸出HIGH致內部RS正反器之R使得 輸出為

HIGH再將 經反相器後輸出變為LOW表 31 為RS正反器之真值表此時 就會以

電阻 ( 所形成的放電路徑開始放電直到 的電壓低於

_

Q

_

Q 1C

)21 RR + 1C CCV31 時輸出才會又開始

轉態其充放電時間公式如下[6]圖 36 為NE555內部方塊圖

其充電上升時間

( ) 1216930 CRRTh +times= (31)

放電下降時間

( ) 1216930 CRRTl +times= (32)

理 論 上 要 獲 得 40 只 要 設 定kHz sk

TlTh μ512240

1=divide== 可 求 得

( ) sCRR μ03818121 =+ 在此選擇 nFC 11 = 故只要調整 ( ) Ω=+ kRR 0381821 即可獲得所

需之頻率但實際測試之結果若要獲得40 需選擇kHz ( ) Ω=+ kRR 941421 以此條件

所得之電容值為 故可得知電容本身的誤差對電路設計佔有極大之影響 nFC 2111 =

表 31 RS正反器真值表

R S Q _

Q Comment

0 0 Q _

Q Hold state

0 1 1 0 Set

1 0 0 1 Reset

1 1 Avoid

11

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 21: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

圖 36 NE555 內部方塊圖

35 超音波接受端電路設計

在接受端電路部份因為超音波壓電效應所產生之電壓一旦距離拉遠後其值會

類似指數衰減在此以主動式帶通濾波器將信號放大並濾除高低雜訊後經由一電壓

比較器將超音波的弦波信號轉為數位信號亦即表示若有收到超音波即可在電壓比

較器輸出端得到 HIGH 的信號

表 32 為每相距 10 公分相對式所測得的振幅此表是利用設計完發射電路後於發

射端接上超音波探頭並固定之爾後利用數位示波器觀察隨著接受器的距離拉遠而產

生的變化圖 37 為 10cm 內電壓與距離之間的關係圖

表 32 超音波相對式距離電壓關係表

距離 cm 0 10 20 30 40 50 60 70 80 90 100

振幅 mV PPV 21600 2240 2000 560 228 260 440 390 360 300 300

12

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 22: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

0

5000

10000

15000

20000

25000

009 18

927

934

4 42 51 62 723

825

899

987

距離(cm)

振幅

Vpp

(mV

)

圖 37 為 10cm 內電壓與距離關係圖

由圖 37 可知一現象發射波與反射波造成建設性干涉以及破壞性干涉因此在設計

帶通濾波器時須加以考慮此現象為求系統穩定需將放大倍率調高

351 主動式帶通濾波器

由於超音波發射接收器所使用的頻率為40 再加上先前所設計的超音波發射電

路亦為40 因此在超音波接收電路的設計也必須使用同一條件在此必須選用帶通

濾波電路以防止其它電磁波信號干擾造成誤動作且又需要將接收器所接收到的訊號

加以放大到所希望的範圍內因此本研究採用「主動式帶通濾波器」完成接收電路的

設計圖38 為本專題研究所使用「主動式帶通濾波器」之架構

kHz

kHz

圖 38 主動式帶通濾波器

13

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 23: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

在設計時必須先考慮超音波接收電路其增益值頻帶寬度中心頻率等條件在此為

了使其能達到最好的特性因此針對圖38 之「主動式帶通濾波器」來詳加分析[4][6][7]

在此設定 經整理可得該電路之轉移函數如﹙33﹚式所示 21 CC =

( )

3212

21

3

2

1

2

1

RRRCRR

SCR

S

SCRSG

+++

minus= (33)

將(33)式與主動式二階帶通濾波器之轉移函數標準式如(34)比較

( )2

002

0

ωωω

++=

SQ

S

SSG (34)

其中(33)式之負號為反相在此可先行忽略因此可得

10

1CR

=ω (35)

3

0 2CRQ

=ω (36)

321

2120 RRCR

RR +=ω (37)

當 0ωω = 時帶通濾波器的電壓增益大小 G

( )1

30 2R

RjGG == ω (38)

在此先決定CfC

R00

0 211πω

== BWfQ 0=

因此可由(35)式至(38)式導出電路中之 之公式由(37)式可導出 將

代入(38)式可導出 再將 和 代入(37)式導出 之值為

1R 2R 3R 1R 1R

3R 1R 3R 2R

⎪⎪⎪

⎪⎪⎪

=

⎟⎠⎞⎜

⎝⎛minus

=

=

QRRQ

GQ

RR

GQRR

03

02

01

2

2 (39)

藉由(39)式之公式可以求得最理想的電路之電阻值在此取 FC μ0010= 中心頻率 0f

14

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 24: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

為 40 頻帶寬度 BW 為 2 以及電壓增益為 100 倍則可求出 kHz kHz

Ω=

Ω=Ω=

159155113795

3

2

1

RRR

將所求出之 帶回電路轉移函數值(33)式中可得其電路轉移函數為 1R 2R 3R

( ) 1042

6

103516102571102581

times+times+times

=SS

SG (310)

當求出電路之轉移函數後為了驗證是否正確在此利用Matlab 軟體來模擬理想狀況

下波德圖的圖形如圖39 及圖310 所示

此時放大倍率僅約25倍左右原因之一為被動元件的誤差所造成帶通濾波中心頻

率的偏移當所設計的帶通濾波器中心頻率不正確的時候可適當調整 的大小以得

正確的中心頻率[7]如(311)式

2R

2

22 ⎟⎟⎠

⎞⎜⎜⎝

⎛=

cn

cn f

fRR (311)

調整之後放大倍率約為55倍另一原因則是運算放大器之高頻參數-增益頻寬積(GBP)

與超音波感測器工作的頻率範圍所造成的高頻增益衰減[6]

15

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 25: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

圖39 主動式帶通濾波器之波德圖

圖310 主動式帶通濾波器之波德圖(中心頻率3998kHz)

16

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 26: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

352 電壓比較器

為了便於後續的電路處理在此將超音波信號在經過主動式帶通濾波後之信號接

到一電壓比較器並加上一參考電壓如下圖 311 所示為一電壓比較器

圖 311 電壓比較器

36 RS-232串聯通訊介面

串列通訊的方式可分為同步式(Synchronous) 及非同步(Asynchronous)兩種同步式

在通訊的兩端使用同步訊號作為通訊的依據而非同步式則使用起始位元( Start Bit )及停

止位元( Stop Bit )作為通訊的判斷現在則是以使用非同步傳輸較多

361 RS-232 使用說明

非同步傳輸只要用9支腳就夠了若要採用同步傳輸則需使用到25支腳在本專題中

我們PC端透過串列埠並採用非同步傳輸的方式與8051單晶片作資料接收而在串列埠

上的9支接腳中我們主要使用Pin2Pin3及Pin5這3支接腳來作資料的處理這9支接腳的

定義與腳位如下表所示[8]

17

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 27: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

表 33 RS-232 9PIN腳位和25PIN腳位定義

9PIN 腳位 25PIN 腳位 簡稱 意義

1 8 DCD 資料載波偵測(Data Carrier Detection)

2 3 RXD 資料接收(Receive Data)

3 2 TXD 資料傳送(Transmit Data)

4 20 DTR 資料終端備妥(Data Terminal Ready)

5 7 GND 接地準位(Ground)

6 6 DSR 資料設定備妥(Data Set Ready)

7 4 RTS 要求發送(Request To Send)

8 5 CTS 清楚以待發送(Clear To Send)

9 22 RI 響鈴偵測(Ring Indicator)

由於RS-232設計之初是用來接數據機作傳輸之用因此它的腳位意義通常也和數據

機傳輸有關以下是這9支腳位的相關說明

(1) DCD此腳位是由數據機控制當電話接通後傳送的訊號是載在載波訊號上

面數據機利用此腳位通知電腦載波被偵測到即表示現在狀態為

On-Line而若電腦未收到此訊號均會回應訊息並將數據機掛線

(2) RXD此腳位會將遠端所傳送過來的資料接收進來

(3) TXD此腳位將電腦所欲傳送出去的資料傳送出去

(4) DTR此腳位由電腦控制用以通知數據機可以進行傳輸

(5) GND此腳位為地線作為電腦與遠端設備之間的準位參考兩端設備的地線

準位必須一樣以避免準位不同而造成資料傳送錯誤

(6) DSR此腳位由數據機控制用以通知電腦一切均準備就緒可以傳送資料

(7) RTS此腳位由電腦控制用以通知數據機馬上傳送資料至電腦而當數據機

接收到此訊號後便會將它所收到的資料傳送給電腦

18

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 28: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

(8) CTS此腳位由數據機控制用以通知電腦將欲傳送的資料送至數據機而當

電腦接收到此訊號後便會將準備送出的資料送給至數據機

(9) RI 數據機通知電腦有電話進來是否接聽電話則由電腦決定

362 串列資料格式

採用非同步串列資料傳送其資料格式如下

標記 起始位元 資料位元 同位元 停止位元

(1) 標記當串列傳輸線上不傳送資料時它所處的狀態稱為標記狀態

用以告知對方目前處於待機閒置的狀態此信號一直保持在高準位

(2) 起始位元在真正傳送資料位元前會先送出一個低電位的位元以告知接收

端馬上就要送資料出去了標記一直保持在高電位下一旦送出起

始位元電位後在這轉態的瞬間接收端與發送端便取得同步

(3) 資料位元真正傳送的資料在起始位元送出後便逐一將位元一個一個送出去

(位元0 最先送出)資料的長度可以是5 到8 個位元

(4) 同位位元在傳送完每一個位元資料後接著送出同位檢查位元用來檢查資

料在傳送的過程中是否發生錯誤同位位元檢查可以是奇同位或偶

同位採用奇同位做同位位元檢查表示所有資料位元加上同位位

元後ldquo1rdquo的總數要為奇數反之偶同位則所有資料位元加上同位

位元ldquo1rdquo的總數為偶數個

(5) 停止位元在一連串的傳送位元的最後一個位元稱為停止位元用以表示一個

位元組的資料已傳送完畢停止位元可以是1 個15 個或2 個

依需要而做選擇由以上可知在串列傳輸中加入起始位元及停

止位元的主要功能是讓收發兩端可以隨時取得同步

19

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 29: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

363 RS-232 串列埠通訊鮑率

鮑率代表著串列資料的傳輸速度也就是每秒鐘可以傳送幾個位元的資料其單位

是 BPS( Bit Per Second )典型的傳輸率有 120024004800 和 9600BPS鮑率越高傳

送時間越短至於應採用何種傳輸率來傳送資料呢只要收發雙方的速度一致便不會

有問題現階段我們所採用的非同步串列傳輸通訊協定為( 96008N1 )即鮑率為

9600BPS傳送或接收 8 個資料位元沒有同位檢查1 個停止位元而起始位元會一

直存在著

364 訊號準位轉換

由於8051使用的電氣信號為TTL準位而RS-232電氣的「0」處於-3V~ -12V範圍 「1」

處於+3V~ +12V範圍因此PC端串列埠與8051的串列介面溝通需要一個訊號準位轉換

IC通稱MAX232利用MAX232這顆IC可以很容易地達到我們的需求圖312 為準位

調整接線圖

圖312 RS-232準位轉換專用IC ndashMAX232

20

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 30: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

第四章 超音波測距系統軟體架構

41 電腦監控程式

過去利用 SDK(Software development kit)MFC(Microsoft foundation class library)

OWL(Object windows library)C 及 C++之 Windows 應用程式但往往艱深難學需要長

時間的練習與研讀現今的視窗介面只要使用滑鼠即可操作所有的功能皆能由選單

上一目了然而設計視窗應用程式繁多經由深思孰慮之後我們則選用了 Visual

Basic(VB)為此一專題的監控程式Visual Basic 是 1991 年的產物以它設計 Windows 應

用程式較單純的原因是一般 Windows 複雜的介面處理動作例如捲軸控制(scrollbar)

功能表的建立(menu)對話方框(dialog box)helliphellip等複雜的運作皆以被隱藏使用者可

以用幾個輕鬆步驟來完成工作Visual Basic 是一種事件驅動(Event-Driven)的程式觀念

你必須在程式內設計各種事件(Event)的處理方式(相當於程式碼)未來當此事件發生

時隨即執行該事件的程式碼傳統以 C 語言設計 Windows 應用程式將可發現程是冗

長但在冗長的程式中大約 80~90的程式碼適用於建立使用者介面而只有約

10~20左右的程式碼是真正在應用程式本身使用 Visual Basic 所提供的工具建立使用

者介面而將精力花費在應用程式的本身如此將可以大大的提升設計程式的工作效率

如圖 41 為介面主體程式流程圖Visual Basic 程式碼請參考附錄二

21

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 31: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

按 下 開 啟 通 訊 埠

通 訊 埠 指 示 燈 亮

選 擇 基 本 檢 測 模 式

儲 存 建 檔

將 判 斷 結 果 顯 示 於分 析 結 果 區

接 收 M C S - 5 1傳 來 的計 數 值

結 束

將 計 數 值 顯 示 於ldquo 串 列 阜 輸 入 區

在 單 位 換 算 區 中選 擇 制 式 單 位

選 擇 面 積 體 積 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

將 測 量 結 果 顯 示 於ldquo 資 料 顯 示 區 及面 積 體 積 模 擬 區

結 束

在 資 料 輸 入 區 中 依序 輸 入 資 料 並 顯 示 於

輸 入 資 料 顯 示 區

在 數 值 讀 取 區 擷 取 X 軸 Y 軸 Z 軸 距 離

選 擇 掃 描 顯 像 模 式

在 單 位 換 算 區 中選 擇 制 式 單 位

在 串 列 阜 輸 入 區 擷 取 距 離

將 測 量 結 果 顯 示 於掃 描 顯 像 區

結 束

圖 41 人機介面程式流程圖

22

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 32: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

42 畫面規劃

本系統以電腦監控畫面作為輸出介面因此使用專為設計視窗軟體而發展的 Visual

Basic 60 來設計監控畫面以作為系統與操作者之間溝通的人機介面其優點具備操作

容易系統狀況容易了解只要使用滑鼠即能對系統控制及適時取得我們所需的資料

且系統也必須能經由畫面顯示清楚的紀錄並分析所得的資料

(一) 畫面分頁規劃

監控畫面將主要由三個模式所組成其包涵

(1) 基本檢測模式在此分頁模式中我們將透過軟體編寫把由串列通訊得來的資料

呈現出來為了便於整理及分析資料更在過程中加入幾道數值處理工作最後

再以 Microsoft Excel 作數值儲存的工作本區的目的在讓使用者了解所使用的超

音波測距機的功能範圍及有序地處理其資料

(2) 面積體積模式在此分頁模式中我們將利用超音波檢測距離的原理應用在計算

面積及體積的大小並在面積體積模擬區中顯示出結果

(3) 掃描顯像模式在此分頁模式中我們也同樣利用超音波測距的原理應用在掃描

顯像上現今大部分掃描顯像多透過影像擷取的方式再做後處理我們則利用測

距的同時在掃描顯像區上記點連線來呈現我們所要的曲面

(二)相關功能區包含串列阜輸入區單位換算區資料輸入區分析結果區數值

讀取區面積體積模擬區以及掃描顯像區等七種功能區其各項功能區分述如下

(1) 串列埠輸入區 此區為將串列埠輸入的資料顯示的區域可分別透過 ASCII 碼顯示串列埠輸

入並以十進位顯示 89C51 的計數值最後代入飛行時間計算公式取得所需的

距離值如圖 42

圖 42 串列埠輸入區圖例

23

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 33: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

(2) 單位換算區

此區為選擇顯示單位的工作區可分別選擇選擇列示框內的工程常用單位以

利於在紀錄及介面上能有效了解資料內容如圖 43

圖 43 單位換算區圖例

(3) 資料輸入區

此區為記錄所需的資料設定其範圍及所在狀態下的工作區可分別輸入取值溫

度取值次數實際距離取值時間以方便系統在接下來能順利處理範圍內

的資料並將其顯示如圖 44

圖 44 資料輸入區圖例

(4) 分析結果區

本區對輸入系統的資料分析後的結果作顯示的區域可分別顯示取值次數實

際距離量測距離及量測的誤差值以方便使用者分析並紀錄所得結果如

圖 45

圖 45 分析結果區圖例

24

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 34: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

(5) 數值讀取區

本區為系統取得量測範圍的輸入工作區可分別擷取 X 軸Y 軸Z 軸的距離

以方便系統計算所需平面及空間的面積及體積如圖 46

圖 46 數值讀取區圖例

(6) 面積體積模擬區

此區為系統在電腦介面模擬使用者所要的平面及空間範圍的顯示區可分別依

據數值讀取區所擷取的資料將範圍顯示在介面上以利使用者對所需範圍能

界定其大小如圖 47

圖 47 面積體積模擬區圖例

(7) 掃描顯像區

本區為使用掃瞄模式以建構出周遭環境之功用如圖 48 所示

25

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 35: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

圖 48 掃描顯像區圖例

(三) 監控系統操作說明

以下我們將整個監控系統的操作步驟及方法做逐一介紹

(1) 開啟通訊埠如圖 49 所示在ldquo超音波測距表rdquo中當按下ldquo開啟通訊埠rdquo的按鈕後

則ldquo通訊埠指示燈rdquo會亮起綠燈表示 Windows 系統已經使用所設定的通訊埠與外

界作通訊如圖 410若要關閉通訊埠時則須點選ldquo關閉通訊埠rdquo若要關閉此

監控系統則只要點選ldquo系統結束rdquo的按鈕即可

圖 49 開啟通訊埠關閉通訊埠系統結束圖例

圖 410 通訊埠指示燈圖例

(2) 選擇操作模式在頁籤上選擇我們欲使用的操作模式我們選擇基本檢測模式作

為我們所使用的模式如圖 411

圖 411 選擇操作模式圖例

26

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 36: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

(3) 基本檢測模式如圖 412 所示首先在ldquo單位換算區rdquo中選擇制式單位在點選

的同時ldquo存檔rdquo 的功能也會啟動點選完單位後在ldquo資料輸入區rdquo中依實驗紀錄

需要設定參數結束之後便按下ldquo啟動rdquo已開始系統運作系統所擷取的輸入資料

與分析的結果將以 Microsoft Excel 的檔案儲存紀錄圖 413 Microsoft Excel 之儲

檔圖以方便分析實驗的結果

圖 412 基本檢測模式圖例

圖 413 Microsoft Excel 之儲檔圖例

27

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 37: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

(4) 面積體積模式如圖 414 所示我們選擇面積體積模式作為我們所使用的模式

首先在ldquo單位換算區rdquo中選擇制式單位接著在ldquo數值讀取區rdquo中擷取欲量測的 X 軸

Y 軸Z 軸範圍並在ldquo資料顯示區rdquo中點選ldquo轉換面積rdquo或ldquo轉換體積rdquo在點選的同

時所欲量測的範圍將在ldquo面積體積模擬區rdquo中顯示其範圍的大小也會顯示於ldquo資

料顯示區rdquo圖 415 面積模式執行圖圖 416 體積模式執行圖

圖 414 面積體積模式圖例

圖 415 面積模式執行圖例

28

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 38: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

圖 416 體積模式執行圖例

(5) 掃描顯像模式如圖 417我們選擇掃描顯像模式作為我們所使用的模式首先

在ldquo單位換算區rdquo中選擇制式單位接著將滑鼠游標指在掃描顯像區中按下滑鼠

左鍵便可執行程式如圖 418掃描的方式是透過步進馬達帶動超音波測距機作

掃描角為三百六十度的迴旋並可藉由介面軟體同步控制馬達轉動並利用記點

連線的方式將我們所要的空間呈現出來最後程式結束後只要按右鍵便可清除

掃描顯像區

圖 417 掃描顯像模式圖例

29

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 39: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

圖 418 掃描顯像模式執行順序圖例

30

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 40: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

第五章 實驗分析與討論

當我們在麵包板上所有信號測試皆完成之後使用於超音波測距表中資料自動擷

取並存檔於 Excel 之功能進行每 10cm 為量測之點每點進行十次量測下列表 51 為

量測與實際距離間之關係圖 51 為實際距離與平均誤差百分比的曲線圖由表 51 可知

平均誤差大約在 1cm 以內且圖 51 顯示了平均誤差百分比有越來越小的趨勢是符合

我們所期待的故即可將電路轉移至印刷電路板上

表 51 實際距離量測誤差分析表

實際距離 (cm)

量測數據

(cm) 平均誤差

(cm) 平均誤差

百分比()

10 881 119 119 20 2006 06 3 30 3075 075 25 40 4041 041 11 50 5017 017 03 60 6072 072 12 70 7034 034 05 80 8056 056 07 90 9009 009 01 100 10061 061 06

02468

101214

1 2 3 4 5 6 7 8 9 10

實際距離 (單位10cm)

誤差百分比()

圖 51 實際量測距離與平均百分誤差

31

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 41: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

51 超音波測距電路

在圖 52 中為本專題研究之硬體架設主要分成三塊印刷電路板第一為電源電路

如圖 53第二為整合發射及接收之電路板如圖 54最後一部份則包括了微處理器

串列通訊以及步進馬達驅動之電路如圖 55

圖 52 超音波測距硬體架構

圖 53 電源電路實體圖

32

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 42: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

圖 54 發射及接收電路板實體圖

圖 55 微處理器串列通訊以及步進馬達驅動之電路實體圖

52 超音波測距測試分析

在硬體電路完成之後緊接著進行基本之超音波測距系統之測試使用基本檢測模

式如下圖 56 所示量測到的數據如下表 52圖 57 為表 52 數據之關係圖

由表 52 可明顯地發現可量測的有效距離縮短了成為 4~10cm之所以最小的有效

距離是 4cm 的原因是我們所設定超音波每次觸發的時間是 100μs 相當於 4 個週期4

33

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 43: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

個週期之後微處理器才會進行判斷是否有接收到信號飛行時間 100μs 在 25 下換

算所得距離相當於 346cm故在 4cm 以下所量得的數據是不可靠的

C0

另外最大距離 10cm 的原因是歸咎於雜訊所影響因為在將所有的感測探頭拿掉的

情況下只要發射超音波的振盪電路有在工作超音波接收端會同時收到相同頻率的正

弦波信號造成我們所設計的電路誤動作經實驗發現將發射與接收的頻率錯開此

一雜訊即會衰減下來因此一方面我們將比較器的參考電壓調高另一方面將帶通濾

波器的中心頻率調離 40 如此一來方能使電路正常工作但卻造成反射回來的原

始信號若不夠大時電路根本就不能動也就是說距離短的話反射回來的能量就會比較

大也因此而造成了能夠量測得到的最長有效距離是 10cm

kHz

圖 56 基本檢測模式

34

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 44: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

表 52 距離量測數據分析與平均誤差

實際距離(cm) 量測平均數據(cm) 平均誤差(cm) 平均誤差百分比()

1 369 269 2690 2 532 332 1660

3 499 199 663

4 429 029 72

5 481 019 -38

6 529 071 -118

7 667 033 -47

8 799 001 -01

9 909 009 10

10 995 005 -05

-100

0

100

200

300

1 2 3 4 5 6 7 8 9 10

實際距離(cm)

平均

誤差

百分

數列1

圖 57 距離量測數據分析與平均誤差

53 超音波測距系統應用

在此我們希望能利用步進馬達的轉動促使超音波感測器也跟著旋轉如此一來

即可對周遭的環境有進一步的了解如圖 58 所示將探頭放入一圓筒狀之容器內

並使步進馬達旋轉的中心軸位在圓筒的正中央然後啟動掃瞄顯示模式如圖 59此一

模式之功能是每次定點測距完將微處理器內部的計數值傳至電腦後由電腦控制步進

馬達旋轉一步由於我們所使用的馬達一步是 72 度所以每走 50 步後會自動反轉 50

35

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 45: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

步以確保線不會被卡住

另外我們所使用的桶子直徑約為 14cm扣除掉探頭本身封裝的高度 1cm 後所要

量得的數據應是 6cm 才是但從圖 59 中可發現只有約 422cm 而已主要原因是由於

圓筒的表面和探頭發射音波的方向並非垂直所以由此可知發射超音波的方向和待測

物表面若有存在一角度的話這個系統所量測得的數據便不可靠

圖 58 動態旋轉量測上視圖

圖 59 掃瞄顯示模式

36

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 46: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

第六章 結論

61 研究成果

在完成此專題研究的過程中學生遭遇帶通濾波器中心頻率漂移的問題在分析之

後發現市售電容值的誤差度為影響的主要因素相關應用應注意將此誤差列入考慮

接著本文為利用反射式的原理來做超音波的感測在實驗中發現障礙物與感測探頭

間將有干涉產生此現象之破壞性干涉將造成音波信號能量的衰減對此問題以提高

放大倍率的方式來解決此問題

本文已建立一套量測誤差控制在 05cm 以內的超音波測距應用系統具有以一人機

介面量測紀錄超音波感測器的功能範圍計算面積體積以及動態掃描的功能此系統

不僅讓使用者能簡易的操作並獲取所需的資料外也易於跟其他系統做整合如自走車

避障系統主要缺點是量測距離僅能到達 10cm此肇因於雜訊所影響若能在電路接地

上的規劃多作改善以及雜訊抑制方面的善加處理量測距離達 1m 並不是問題其次是

待測物表面須與超音波發射方向垂直才能獲得較可靠的量測數據關於此一問題若

能將超音波檢測的方式由一發射一接收改為同一個探頭當傳感器如此一來此系統

也就更有應用的價值

62 未來展望

事實上環境的溫度氣流和超音波感測器特性等因素均會影響到所計數的飛行

時間不過反過來說我們亦可使用超音波來進行溫度氣流等數據的量測目前我們

所使用之超音波測距系統仍須手動輸入溫度參數未來除了可再進一步透過溫度感測器

自動進行溫度補償外亦可利用超音波測距的特性來做溫度感測的功能

未來若能解決本文所提及雜訊之問題再付予本系統溫度感測的功能後必定能應

用在許多環境的探測方面也因為本系統所需的成本低因此本系統將具有應用價值

37

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 47: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

參考文獻

[1] F J Toledo J D Luis L M Tomas M A Zamora and H Martinez ldquoMap Building with Ultrasonic Sensors of Indoor Environments Using Neural Networksrdquo IEEE International Conference on Systems Man and Cybernetics vol2 pp 920-925 2000

[2] 陳瑞和感測器全華科技圖書股份有限公司1993

[3] 黃克穠採用超音波相差法之震動及溫度量測系統國立成功大學電機工程學系博

士論文2003

[4] 黃立坤超音波頭控系統之研製逢甲大學自動控制工程學系碩士論文2002

[5] 蔡朝洋單晶片微電腦 80518951 原理與應用全華出版社2002

[6] 吳明貴超音波三維頭控人機介面系統之研製逢甲大學自動控制工程學系碩士論

文2003

[7] 盧明智黃敏祥OP Amp 應用+ 實驗模擬全華科技圖書股份有限公司1994

[8] 方志鵬IBM PC 系統設計與介面技術東華書局2003

38

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 48: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

附錄一 微處理機程式碼

org 00h start mov SP70H 把堆疊指標移到70H mov DPTRTABLE 把指標暫存器指向TABLE mov SCON01110000B 設定串列埠控制存器模式1SM2=1 REN=1允許接收 mov tmod00100001b mov TH10FDH 石英振盪器為11059MHz串列傳輸飽率9600bps mov TL10FDH TH1=0FDH如果為12Mhz則TH1=243鮑率為2400bps mov 20H00H 把00H放入20H內 mov 21H00H 把00H放入21H內 setb TR1 啟動計時器1 mov 30H50 轉的次數 mov 31H11001100B 馬達激磁相位 mov p231H 一開機馬達定位 loop clr p12 clr p11 clr RI jnb RI$ mov ASBUF cjne A1next ajmp main1 next call counter0 2 call TIINT 2 共花2387us+21085us=2504us call delay ajmp loop =========================== 判斷哪個功能鍵被按下 =========================== main1 clr p12 1 花1085us clr p11 1 花1085us call counter0 2 call TIINT 2 共花2387us+21085us=2504us

39

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 49: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

call delay moto mov a31H FOR RL A mov P2A mov 31Ha 最後一次激磁位置 acall delay djnz 30Hmain1 acall HOLD mov R150 REV mov a31H RR A mov 31Ha 紀錄激磁位置 mov P2A acall delay1 djnz R1REV acall HOLD HOLD mov R05 延時05秒 DL4 acall delay djnz R0DL4 ret nop nop ljmp loop =========================== 發射超音波和計時 =========================== counter0 從這到起動計時器 要花61085us=651us mov tl0 00h 2 mov th0 00h 2 setb p11 1 setb TR0 1 啟動計時器 setb p12 1 觸發超音波感測器 call delayt clr p12

40

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 50: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

jb p11$ 2 判斷是否有東西進來 clr TR0 1 停止計時器 mov 20hth0 2 從這開始到ret回去要花281085us=3038us mov 21htl0 2 mov a20H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r1a 1 mov a20H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r2a 1 mov a21H 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r3a 1 mov a21H 1 swap a 1 anl a0fh 1 movc AA+DPTR 2 取A內容相對之ASCII碼 mov r4a 1 ret 2 ======================== 串列傳輸 ======================== TIINT clr TI 1 共花221085=2387us mov Ar2 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar1 1 mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar4 1

41

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 51: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

mov SBUFA 1 jnb TI$ 2 clr TI 1 mov Ar3 1 mov SBUFA 1 jnb TI$ 2 ret 2 ============================ 延時200ms ============================ delay mov R52 DL1 mov R6250 DL2 mov R7200 DL3 djnz R7DL3 djnz R6DL2 djnz R5DL1 ret delay1 mov R51 DL5 mov R6250 DL6 mov R720 DL7 djnz R7DL7 djnz R6DL6 djnz R5DL5 ret delayt mov R050 DL8 djnz r0DL8 ret TABLE DB 30H 0 DB 31H 1 DB 32H 2 DB 33H 3 DB 34H 4

42

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 52: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

DB 35H 5 DB 36H 6 DB 37H 7 DB 38H 8 DB 39H 9 DB 41H A DB 42H B DB 43H C DB 44H D DB 45H E DB 46H F -------------------------------- END

43

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 53: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

附錄二 Visual Basic 程式碼

定義程式中相關變數 Option Explicit Private myexcel As Object Private myworkbook As Object Private myworksheet As Object Private myrange As Object Private mychart As Object Dim a(1000) As String Dim q damp Dim i sum adv e j m k xl yl zl X1 Y1 As String Const pi = 314159 Dim counter As Integer Dim xpt(300) As Integer Dim ypt(300) As Integer Dim num As Integer 開啟通訊阜~啟動串列傳輸 (基本檢測模式) Private Sub OPENcom_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command2Enabled = True MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸將參數歸零並清除空格 (基本檢測模式) Private Sub CLOSEcom_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能

44

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 54: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command2Enabled = True Dim i sum adv e j As String 將參數歸零並清除空格 Label7Caption = 分析結果區~取值次數 Label8Caption = 分析結果區~平均值 Label12Caption = 分析結果區~誤差 Label14Caption = 分析結果區~總數 Label16Caption = 分析結果區~量測距離 List1Clear List2Clear Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 資料輸入區~取值溫度 adv = 0 j = 0 innumEnabled = True End Sub 系統關閉鈕~跳離程式 (基本檢測模式)

Private Sub Command2_Click() End End Sub 串列埠通訊事件串利用ComEvReceive 事件當接收緩衝區中的接收位元組數達 到RThreshold屬性設定值後即引發事件 制式單位選擇輸出

(基本檢測模式) Private Sub MSComm1_OnComm()

45

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 55: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Select Case MSComm1CommEvent Case comEvReceive 收到最小

接收字元數的字元 Dim buf As String Dim p tem As String buf = MSComm1Input 定義buf

為字串 Label1Caption = buf Label5Caption = hex_dec(buf$) 把MCS-51傳

送過來的資料放在buf中 p = Val(Label30Caption) Text11Text = Text12Text tem = Round((331 + 06 Val(Text12Text)) 100 921659 2 5) Select Case p 制式單位選

擇輸出 Case 1 SEGMENTCaption = Round(Label5Caption tem 2) Case 2 SEGMENTCaption = Round(Label5Caption tem 001 2) Case 3 SEGMENTCaption = Round(Label5Caption tem 03937 2) Case 4 SEGMENTCaption = Round(Label5Caption tem 00328 2) End Select End Select End Sub 16進位轉換成10進位副程式 (基本檢測模式) Public Function hex_dec(hexcode$) Dim num i dec As Integer Dim code As String num = 0 For i = 0 To 3 取4次 code = Mid(hexcode$ 4 - i 1) 由hexcode$字串的第(2-i)個開啟取字元取1個 Select Case code 判斷code碼的值

46

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 56: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Case 0 To 9 如果是0-9的字串則轉換成0-9的數值 dec = CInt(code) Case A 如果是A的字串則轉換成10的數值 dec = 10 Case B 如果是B的字串則轉換成11的數值 dec = 11 Case C 如果是C的字串則轉換成12的數值 dec = 12 Case D 如果是D的字串則轉換成13的數值 dec = 13 Case E 如果是E的字串則轉換成14的數值 dec = 14 Case F 如果是F的字串則轉換成15的數值 dec = 15 End Select num = num + dec (16 ^ i) 最後把第一個數值乘上16再加上第二個數值 Next i hex_dec = num 傳回轉換的值至hex_dec End Function 制式單位(公分)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option1_Click() q = 1 制式單位(公分)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公分 Label18Caption = 公分 Label19Caption = 公分

47

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 57: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Label25Caption = 平方公分 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公分) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公分) myrangeCells(1 6)FormulaR1C1 = 平均(公分) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公分) Text3SetFocus End Sub 制式單位(公尺)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option2_Click() q = 2 制式單位(公尺)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體

48

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 58: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 公尺 Label18Caption = 公尺 Label19Caption = 公尺 Label25Caption = 平方公尺 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(公尺) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(公尺) myrangeCells(1 6)FormulaR1C1 = 平均(公尺) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(公尺)

49

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 59: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Text3SetFocus End Sub 制式單位(英吋)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option3_Click() q = 3 制式單位(英吋)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英吋 Label18Caption = 英吋 Label19Caption = 英吋 Label25Caption = 平方英吋 Set myexcel = CreateObject(excelapplication) 讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1)

50

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 60: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英吋) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英吋) myrangeCells(1 6)FormulaR1C1 = 平均(英吋) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英吋) Text3SetFocus End Sub 制式單位(英呎)的相關顯示 引進excel作資料儲存 (基本檢測模式) Private Sub Option4_Click() q = 4 制式單位(英呎)的相關顯示 Label30Caption = q Label17FontName = 標楷體 Label18FontName = 標楷體 Label19FontName = 標楷體 Label25FontName = 標楷體 Label17FontSize = 14 Label18FontSize = 14 Label19FontSize = 14 Label25FontSize = 12 Label17FontBold = True Label18FontBold = True Label19FontBold = True Label17Caption = 英呎 Label18Caption = 英呎 Label19Caption = 英呎 Label25Caption = 平方英呎 Set myexcel = CreateObject(excelapplication)

51

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 61: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

讓Excel在前景工作 myexcelVisible = True 新增一新活頁簿 Set myworkbook = myexcelWorkbooksAdd 將myworksheet物件變數設定為sheet1工作表 Set myworksheet = myworkbookWorksheets(sheet1) 指定myrange為Worksheet的Range物件 Set myrange = myworksheetRange(a1) 在儲存格(12)(13)(14)(15)(16)(17)(18)中分別填入取值溫度距離取值次數總數

平均 誤差()細項 myrangeCells(1 2)FormulaR1C1 = 取值溫度 myrangeCells(1 3)FormulaR1C1 = 距離(英呎 ) myrangeCells(1 4)FormulaR1C1 = 取值次數 myrangeCells(1 5)FormulaR1C1 = 總數(英呎 ) myrangeCells(1 6)FormulaR1C1 = 平均(英呎 ) myrangeCells(1 7)FormulaR1C1 = 誤差() myrangeCells(1 8)FormulaR1C1 = 細項(英呎 ) Text3SetFocus End Sub 輸入資料顯示~取值次數 (基本檢測模式) Private Sub Command4_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) Text1SetFocus End Sub 輸入資料顯示~實際距離 (基本檢測模式)

52

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 62: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Private Sub Command5_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) Text4SetFocus End Sub 輸入資料顯示~取值時間 (基本檢測模式) Private Sub Command6_Click() Text2 = 取值次數為 amp ( amp Text3Text amp 次) amp vbCrLf amp 實際距離為 amp ( amp

Text1Text amp 單位長) amp vbCrLf amp 取值時間為 amp ( amp Text4Text amp ms) Text12SetFocus End Sub 輸入資料~更改鈕 (基本檢測模式) Private Sub Command7_Click() Text1Text = 資料輸入區~實際距離 Text2Text = 資料輸入區~輸入資料顯示區 Text3Text = 資料輸入區~取值次數 Text4Text = 資料輸入區~取值時間 Text12Text = 串列阜輸入區~溫度顯示 資料輸

入區~取值溫度 Text3SetFocus End Sub 結果顯示輸出 數值處理分析 引進excel作資料儲存 (基本檢測模式) Private Sub innum_Click() Dim t k t = Text4Text

53

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 63: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

sum = 0 d = Val(Text3Text) e = Val(Text1Text) Label16Caption = Val(Text1Text) k = 0 將Range物件範圍的列總數加1 k = myrangeRowsCount + 1 設定myange為目前作用中的儲存格 Set myrange = myexcelActiveCell For i = 1 To d TimeDelay t 1 list取值時間 Text9Text = Val(SEGMENTCaption) sum = Round(sum + Val(SEGMENTCaption) 2) List1AddItem Text9Text Text9Text = Text9SetFocus Label7Caption = List1ListCount myrangeCells(k 7 + i)FormulaR1C1 = Val(SEGMENTCaption) Next i adv = sum d Label8Caption = Round(adv 2) j = Round(Label8Caption - e 2) Label12Caption = Round(j e 100 2) m = Val(Label14Caption) a(m) = 取值總數 amp Val(Text3Text) amp 次 平均值 amp Val(adv) amp 單位長 誤差 amp

Val(Label12Caption) amp Text6 = a(m) List2AddItem Text6Text Text6Text = Text6SetFocus Label14Caption = List2ListCount innumEnabled = False Command1Enabled = True TimeDelay t 1 將輸入的資料分別存入儲存格中 myrangeCells(k 1)FormulaR1C1 = 第 amp Label14Caption amp 次 myrangeCells(k 2)FormulaR1C1 = Text12Text myrangeCells(k 3)FormulaR1C1 = e

54

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 64: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

myrangeCells(k 4)FormulaR1C1 = Text3Text myrangeCells(k 5)FormulaR1C1 = sum myrangeCells(k 6)FormulaR1C1 = Label8Caption myrangeCells(k 7)FormulaR1C1 = Label12Caption 將Range物件範圍列總數加1 k = myrangeRowsCount + 1 將下一列的儲存格選起來 myrange(k 1)Select End Sub 分析結果清除鈕~參數歸零清除空格 (基本檢測模式) Private Sub Command1_Click() Dim i sum adv e j As String Label7Caption = Label8Caption = Label12Caption = Label16Caption = adv = 0 j = 0 List1Clear innumEnabled = True Command1Enabled = False End Sub 繪圖分析功能模式 (基本檢測模式) Private Sub Command16_Click() 新增一Chart物件並指定給Mychart物件變數 Set mychart = myexcelChartsAdd 將工作表中有用到的儲存格傳回給Myrange Set myrange = myworksheetUsedRange mychartSetSourceData Source=myworksheetUsedRange

55

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 65: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

myexcelVisible = True End Sub 開啟通訊阜~啟動串列傳輸 (面積體積模式) Private Sub Command13_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮 OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 Command15Enabled = True End Sub 關閉通訊阜~中斷串列傳輸 (面積體積模式) Private Sub Command14_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (面積體積模式) Private Sub Command15_Click() End End Sub 表單載入 設定空間模擬區原點 (面積體積模式) Private Sub Form_Load() Frame1Left = tempScaleLeft + 480 Frame1Top = tempScaleTop + 2760

56

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 66: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Frame6Left = tempScaleLeft + 480 Frame6Top = tempScaleTop + 2760 Frame13Left = tempScaleLeft + 480 Frame13Top = tempScaleTop + 2760 Lin1X2 = tempScaleLeft + 3120 Lin1Y2 = tempScaleTop + 4200 Lin2X2 = tempScaleLeft + 3120 Lin2Y2 = tempScaleTop + 4200 Lin3X2 = tempScaleLeft + 3120 Lin3Y2 = tempScaleTop + 4200 Lin11X2 = tempScaleLeft + 3120 Lin11Y2 = tempScaleTop + 4200 Lin21X2 = tempScaleLeft + 3120 Lin21Y2 = tempScaleTop + 4200 Lin12X2 = tempScaleLeft + 3120 Lin12Y2 = tempScaleTop + 4200 Lin22X2 = tempScaleLeft + 3120 Lin22Y2 = tempScaleTop + 4200 Lin13X2 = tempScaleLeft + 3120 Lin13Y2 = tempScaleTop + 4200 Lin23X2 = tempScaleLeft + 3120 Lin23Y2 = tempScaleTop + 4200 Lin31X2 = tempScaleLeft + 3120 Lin31Y2 = tempScaleTop + 4200 Lin32X2 = tempScaleLeft + 3120 Lin32Y2 = tempScaleTop + 4200 Lin33X2 = tempScaleLeft + 3120 Lin33Y2 = tempScaleTop + 4200 Move 0 0 Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False

57

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 67: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Lin31Visible = False Lin32Visible = False Lin33Visible = False Command11Enabled = False End Sub 制式單位(公分)的相關顯示 Private Sub Option8_Click() q = 1 Label30Caption = q Label25FontName = 標楷體 Label27FontName = 標楷體 Label25FontSize = 12 Label27FontSize = 12 Label25FontBold = True Label27FontBold = True Label25Caption = 平方公分 Label27Caption = 立方公分 Picture10Visible = True 比例尺顯示 Picture9Visible = False Picture8Visible = False Picture7Visible = False End Sub 制式單位(公尺)的相關顯示 (面積體積模式) Private Sub Option7_Click() q = 2 Label30Caption = q Picture9Visible = True 比例尺顯示 Picture8Visible = False Picture7Visible = False

58

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 68: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Picture10Visible = False End Sub 制式單位(英吋)的相關顯示 (面積體積模式) Private Sub Option6_Click() q = 3 Label30Caption = q Picture8Visible = True 比例尺顯示 Picture7Visible = False Picture10Visible = False Picture9Visible = False End Sub 制式單位(英呎)的相關顯示 (面積體積模式)

Private Sub Option5_Click() q = 4 Label30Caption = q Picture7Visible = True 比例尺顯示 Picture8Visible = False Picture9Visible = False Picture10Visible = False End Sub 數值讀取區~x軸距離 (面積體積模式) Private Sub Command8_Click() Dim secx1 secy1 As Single Dim secangle1 As Double

59

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 69: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

secangle1 = 2 pi 90 360 x軸角度設定 For i = 0 To 1 Label20Caption = Val(SEGMENTCaption) Next i xl = Val(Label20Caption) secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 Lin2Visible = False Lin2X1 = Lin2X2 + secx1 Lin2Y1 = Lin2Y2 - secy1 Lin2Visible = True Text5 = X軸距離為 amp ( amp xl amp 單位長) Command8Enabled = False End Sub 數值讀取區~y軸距離 (面積體積模式) Private Sub Command3_Click() Dim secx2 secy2 As Single Dim secangle2 As Double secangle2 = 2 pi 30 360 y軸角度設定 For i = 0 To 1 Label21Caption = Val(SEGMENTCaption) Next i yl = Val(Label21Caption) secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 Lin1Visible = False Lin1X1 = Lin1X2 + secx2 Lin1Y1 = Lin1Y2 - secy2 Lin1Visible = True Text8 = Y軸距離為 amp ( amp yl amp 單位長) Command3Enabled = False End Sub 數值讀取區~z軸距離 (面積體積模式)

60

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 70: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Private Sub Command9_Click() Dim secx3 secy3 As Single Dim secangle3 As Double secangle3 = 2 pi 0 z軸角度設定 For i = 0 To 1 Label23Caption = Val(SEGMENTCaption) Next i zl = Val(Label23Caption) secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin3Visible = False Lin3X1 = Lin3X2 + secx3 Lin3Y1 = Lin3Y2 - secy3 Lin3Visible = True Text7 = Z軸距離為 amp ( amp zl amp 單位長) Command9Enabled = False End Sub 轉換面積模式 (面積體積模式) Private Sub Command10_Click() Dim secx1 secy1 As String Dim secx2 secy2 As String Dim secx3 secy3 As String Dim secangle1 As String Dim secangle2 As String Dim secangle3 As String secangle1 = 2 pi 90 360 x軸角度設定 secangle2 = 2 pi 30 360 y軸角度設定 secangle3 = 2 pi 0 z軸角度設定 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1

61

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 71: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Lin13X2 = Lin3X1 Lin13Y2 = Lin3Y1 Lin23X2 = Lin3X1 Lin23Y2 = Lin3Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin21Visible = False Lin31Visible = False Lin33Visible = False Lin23Visible = False Lin13Visible = False If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin1Visible = True Lin2Visible = True Lin11Visible = True Lin21Visible = True Label24Caption = Round(xl yl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin2Visible = True

62

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 72: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Lin3Visible = True Lin23Visible = True Lin31Visible = True Label24Caption = Round(xl zl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin1Visible = True Lin3Visible = True Lin13Visible = True Lin33Visible = True Label24Caption = Round(yl zl 2) End If End If Command11Enabled = True Command10Enabled = False End Sub 轉換體積模式 (面積體積模式) Private Sub Command11_Click() Dim secx1 secy1 As Single Dim secx2 secy2 As Single Dim secx3 secy3 As Single Dim secangle1 As Double Dim secangle2 As Double Dim secangle3 As Double secangle1 = 2 pi 90 360 secangle2 = 2 pi 30 360 secangle3 = 2 pi 0 Lin11X2 = Lin2X1 Lin11Y2 = Lin2Y1

63

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 73: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Lin12X2 = Lin3X1 Lin12Y2 = Lin3Y1 Lin21X2 = Lin1X1 Lin21Y2 = Lin1Y1 Lin22X2 = Lin3X1 Lin22Y2 = Lin3Y1 Lin31X2 = Lin2X1 Lin31Y2 = Lin2Y1 Lin33X2 = Lin1X1 Lin33Y2 = Lin1Y1 secx1 = xl Sin(secangle1) 10 secy1 = xl Cos(secangle1) 10 secx2 = yl Sin(secangle2) 10 secy2 = yl Cos(secangle2) 10 secx3 = zl Sin(secangle3) 10 secy3 = zl Cos(secangle3) 10 Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Lin11X1 = Lin11X2 + secx2 Lin11Y1 = Lin11Y2 - secy2 Lin12X1 = Lin12X2 + secx2 Lin12Y1 = Lin12Y2 - secy2 Lin21X1 = Lin21X2 + secx1 Lin21Y1 = Lin21Y2 - secy1 Lin22X1 = Lin22X2 + secx1 Lin22Y1 = Lin22Y2 - secy1 Lin31X1 = Lin31X2 + secx3 Lin31Y1 = Lin31Y2 - secy3 Lin33X1 = Lin33X2 + secx3 Lin33Y1 = Lin33Y2 - secy3 Lin23X2 = Lin12X1

64

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 74: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Lin23Y2 = Lin12Y1 Lin13X2 = Lin22X1 Lin13Y2 = Lin22Y1 Lin32X2 = Lin11X1 Lin32Y2 = Lin11Y1 Lin13X1 = Lin13X2 + secx2 Lin13Y1 = Lin13Y2 - secy2 Lin23X1 = Lin23X2 + secx1 Lin23Y1 = Lin23Y2 - secy1 Lin32X1 = Lin32X2 + secx3 Lin32Y1 = Lin32Y2 - secy3 Lin1Visible = True Lin11Visible = True Lin12Visible = True Lin13Visible = True Lin2Visible = True Lin21Visible = True Lin22Visible = True Lin23Visible = True Lin3Visible = True Lin31Visible = True Lin32Visible = True Lin33Visible = True If Lin2Visible = True Then X軸Y軸 If Lin1Visible = True Then Label26Caption = Round(Val(Label24Caption) zl 2) End If End If If Lin2Visible = True Then X軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) yl 2) End If End If If Lin1Visible = True Then Y軸Z軸 If Lin3Visible = True Then Label26Caption = Round(Val(Label24Caption) xl 2) End If End If

65

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 75: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

End Sub 清除空間模擬區 (面積體積模式) Private Sub Command12_Click() Lin1Visible = False Lin2Visible = False Lin3Visible = False Lin11Visible = False Lin12Visible = False Lin13Visible = False Lin21Visible = False Lin22Visible = False Lin23Visible = False Lin31Visible = False Lin32Visible = False Lin33Visible = False Label26Caption = Label24Caption = Text5Text = Text7Text = Text8Text = Label20Caption = Label21Caption = Label23Caption = Command11Enabled = False Command8Enabled = True Command3Enabled = True Command9Enabled = True Command10Enabled = True End Sub 開啟通訊阜~啟動串列傳輸 (空間模擬模式) Private Sub Command17_Click() MSComm1PortOpen = True 開啟通訊埠 PWRFillColor = RGB(0 255 0) 通訊埠指示燈亮

66

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 76: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

OPENcomEnabled = False 把OPENcom按鈕失效 CLOSEcomEnabled = True 把CLOSEcom按鈕致能 MSComm1Output = 1 End Sub 關閉通訊阜~中斷串列傳輸 (空間模擬模式) Private Sub Command18_Click() MSComm1PortOpen = False 關閉通訊埠 PWRFillColor = RGB(0 0 0) 通訊埠指示燈熄滅 OPENcomEnabled = True 把OPENco按鈕致能 CLOSEcomEnabled = False 把CLOSEcom按鈕失效 Command15Enabled = True End Sub 系統關閉鈕~跳離程式 (空間模擬模式) Private Sub Command19_Click() End End Sub 制式單位(公分)的相關顯示 (空間模擬模式) Private Sub Option9_Click() q = 1 Label30Caption = q Picture3Visible = True 比例尺顯示 Picture6Visible = False Picture4Visible = False Picture5Visible = False End Sub 制式單位(公尺)的相關顯示 (空間模擬模式) Private Sub Option10_Click() q = 2

67

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 77: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Label30Caption = q Picture4Visible = True 比例尺顯示 Picture3Visible = False Picture6Visible = False Picture5Visible = False End Sub 制式單位(英吋)的相關顯示 (空間模擬模式) Private Sub Option11_Click() q = 3 Label30Caption = q Picture5Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture6Visible = False End Sub 制式單位(英呎)的相關顯示 (空間模擬模式) Private Sub Option12_Click() q = 4 Label30Caption = q Picture6Visible = True 比例尺顯示 Picture3Visible = False Picture4Visible = False Picture5Visible = False End Sub

空間模擬程式 (空間模擬模式)

Private Sub Picture1_MouseMove(Button As Integer Shift As Integer X As Single y As Single)

Dim n As String

68

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 78: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

Dim return_value Dim anglex angley S j scanx scany y4 Dim angle As String Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) j = 0 tempPicture1Line (0 1500)-(0 -500) tempPicture1Line (-1000 0)-(1000 0) Picture1Scale (-1000 1500)-(1000 -500) Picture2Scale (-500 500)-(500 -500) Shape3Left = 0 - Shape3Width 2 Shape3Top = 0 + Shape3Height 100 X1 = Fix(X) Y1 = Fix(y) tempCaption = 目前滑鼠座標值 X= amp X1 amp Y= amp Y1 tempPicture1ToolTipText = ( amp X1 amp amp Y1 amp ) m = 確定要啟動掃描模式 If Button = 1 Then return_value = MsgBox(m 32 + 1 確認) End If If return_value = 1 Then Timer1Enabled = True End If If Button = 2 Then tempPicture1Cls j = 0 Label31Caption = Label35Caption = End If End Sub

切換功能模式 Private Sub TabStrip1_Click()

69

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 79: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

If TabStrip1SelectedItemIndex = 1 Then TabStrip1ZOrder 0 Frame1ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 2 Then TabStrip1ZOrder 0 Frame6ZOrder 0 ElseIf TabStrip1SelectedItemIndex = 3 Then TabStrip1ZOrder 0 Frame13ZOrder 0 End If End Sub Private Sub Timer1_Timer() Dim red Dim green red = RGB(255 0 0) green = RGB(0 255 0) Dim n As String Dim u Dim return_value Dim anglex angley S j scanx scany Dim angle As String angle = 270 num = 0 For num = 0 To 199 n = 2 pi angle 360 角度Val(SEGMENTCaption) 10 Cos(Round(n 2)) anglex = 30 10 Sin(n) angley = 30 10 Cos(n) scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) xpt(num) = anglex ypt(num) = angley tempPicture1PSet (anglex angley) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label31Caption = Round(angle - 270 2) Label35Caption = num + 1

70

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc
Page 80: 逢甲大學 自動控制工程學系專題製作 專 題 論 文 · 模組。本文利用價格便宜之超音波感測器來設計自動距離量測系統。此系統不僅 能量測距離,亦能將量測數據做自動存檔之功能,人機介面是利用Visual

TimeDelay 50 掃描取值時間 tempPicture2Cls Next num For j = 0 To 40 angle = 270 For i = 0 To 198 n = 2 pi angle 360 角度 scanx = 500 Sin(Round(n 2)) scany = 500 Cos(Round(n 2)) tempPicture1Line (xpt(i) ypt(i) + j 20)-(xpt(i + 1) ypt(i + 1) + j 20) tempPicture2Line (0 0)-(scanx scany) red tempPicture2DrawWidth = 2 angle = angle + 18 Label35Caption = i + 2 Label31Caption = Round(angle - 270 + 18 2) TimeDelay 50 平面掃描取值時間 tempPicture2Cls Next i Next j Timer1Enabled = False End Sub

71

  • 專題封面0601doc
    • The Automatic Ranging System Design and Application Based on Ultrasonic Technology
      • 簽名檔doc
      • 感謝0511doc
        • Abstract
          • 論文0511-2doc