You are on page 1of 373

Chapter 0

C8051微處理機實習
課程說明

授課教師:張凱雄

1
0-1 課程介紹與說明
• 多晶片微電腦系統
– CPU:Z80、6500、8080、8085、80X86、Pentium
、 ARM…等
– 儲存:ROM(PROM、EPROM、EEPROM、Flash
Memory 、 Floppy Disk 、 HDD)
– 記憶:RAM(SRAM、DRAM 、 SDRAM)
– 輸出/輸入:8255A、8254A、8259A、8237、8250A
、8251A…等

K.H. Chang, System Control Lab. 2


0-1 課程介紹與說明

• 多晶片微電腦系統應用場合
– 語音控制(近來單晶片也能做到):聲控撥號。

– 影像處理:數位相機、人臉/車牌辨識。

– 衛星定位:尋車、導航。

– 氣象分析:記錄器、大量數據分析運算。

K.H. Chang, System Control Lab. 3


0-1 課程介紹與說明
• 單晶片微電腦系統
– 將ROM、RAM、I/O、Timer、ALU…等功能以積體電
路技術實現在一顆晶片內。
– ROM:儲存程式碼
– RAM:程式中可使用的變數
– Timer:程式內可使用計時器功能
– ALU:算術單元(複雜運算可用軟體或硬體實現,
但成本與應用場合不同)

K.H. Chang, System Control Lab. 4


0-1 微電腦系統與應用簡介

• 單晶片微電腦系統應用場合
– 家電控制:電視、電扇、冷氣、音響。

– 防盜系統:機車、汽車、家庭保全。

– 消費型電子產品:MP3 Player(iPod)、Mouse、
Keyboard 。

– 工業控制:恆溫控制、恆溼控制、工業儀表。

K.H. Chang, System Control Lab. 5


0-1 課程介紹與說明
• 各晶片IC廠所推出之51族系單晶片微電腦
– INTEL : MCS-48 、MCS-51 、MCS-96
– ATMEL :AT80C31/C32/C51/C52 、AT87C51/C52 、
AT89C51/C52 、 AT89S51/S52。
– PHILIPS :P89C51RB/RC/RD+
– WINBOND :W78E516B/D
– Silicon Labs:C8051Fxxx 系列

K.H. Chang, System Control Lab. 6


0-1 課程介紹與說明
• ATMEL 8051 Architecture Microcontroller

K.H. Chang, System Control Lab. 7


0-1 課程介紹與說明
• Silicon Lab. C8051F38x Microcontrollers

K.H. Chang, System Control Lab. 8


0-2 參考用書
• 實習課以老師的講義為主,請印成紙本,以
方便上課做筆記。

K.H. Chang, System Control Lab. 9


0-3 上課注意事項

• 微處理機實習課程若需設計與製作電路板,
請準備工具箱:
– 尖嘴鉗、斜口鉗、鑷子、烙鐵、焊錫、三用電表、
鑽針組、線鋸或鋸片…等相關實習工具

– 實驗過程,注意安全!嚴禁嘻鬧!

– 洗電路板與使用鑽台需全程配帶護目鏡

K.H. Chang, System Control Lab. 10


0-3 上課注意事項

• 若有製作電路板,請將組員學號印製在電路
板上,課程實作會由助教檢查,並做為平時
成績。

K.H. Chang, System Control Lab. 11


0-3 上課注意事項

• 「微處理機」與「微處理機實習」課程有密
切相關性,若本學期有任一課程沒有通過,
且未來要重修老師的課,老師得依情況要求
另一門有通過的課仍需要旁聽。

K.H. Chang, System Control Lab. 12


0-4 評分標準

• 平時成績(出席率、課程實作):30%

• 期中測驗:30%,Open Book上機考。

• 期末測驗:40%,Open Book上機考。

K.H. Chang, System Control Lab. 13


助教聯絡方式
• 電機館五樓系統控制研究室,進去後坐在左
半部位置的研究生。
• 電話:05-6315605,分機轉503。

• Note: 讀大學是學習找答案與解決問題的能
力,遇到問題先試著自己查查看。真的查不
出來再來問助教或老師。

K.H. Chang, System Control Lab. 14


Chapter 1
C8051微控制器

授課教師:張凱雄

1
1-1 晶片接腳
• C8051F340/F380

K.H. Chang, System Control Lab. 2


1-1 晶片接腳
• Pin腳定義請參考
– C8051F34x_Rev1.3 Page 28
– C8051F38x_Rev1.3 Page 22
• 由於C8051F340與F380架構都相同,未來課
程以F380為例說明。

K.H. Chang, System Control Lab. 3


1-2 Package尺寸

K.H. Chang, System Control Lab. 4


1-2 Package尺寸

K.H. Chang, System Control Lab. 5


1-3 功能簡要說明
• Analog Peripherals
– 10-Bit ADC
(C8051F380/1/2/3 only)
• Up to 500 ksps
• Built-in analog multiplexer
with single-ended and
differential mode
• VREF from external pin,
internal reference, or VDD

K.H. Chang, System Control Lab. 6


1-3 功能簡要說明
• Analog Peripherals
– 10-Bit ADC
(C8051F380/1/2/3 only)
• Built-in temperature
sensor
• External conversion start
input option

K.H. Chang, System Control Lab. 7


1-3 功能簡要說明
• Analog Peripherals
– Two comparators
– Internal voltage
reference
(C8051F380/1/2/3 only)
– Brown-out detector(欠
壓偵測) and POR(
Power-On Reset)
Circuitry

K.H. Chang, System Control Lab. 8


1-3 功能簡要說明

K.H. Chang, System Control Lab. 9


1-3 功能簡要說明
• USB Function Controller
– USB specification 2.0
compliant
– Full speed (12 Mbps) or
low speed (1.5 Mbps)
operation
– Integrated clock
recovery; no external
crystal required for full
speed or low speed

K.H. Chang, System Control Lab. 10


1-3 功能簡要說明
• USB Function Controller
– Supports eight flexible
endpoints
– 1 kB USB buffer memory
– Integrated transceiver;
no external resistors
required

K.H. Chang, System Control Lab. 11


1-3 功能簡要說明
• On-Chip Debug
– On-chip debug circuitry
facilitates full speed,
non-intrusive in-system
debug (No emulator
required)
– Provides breakpoints,
single stepping,
inspect/modify memory
and registers

K.H. Chang, System Control Lab. 12


1-3 功能簡要說明
• Voltage Supply Input:
2.7 to 5.25 V
– Voltages from 2.7 to 5.25
V supported using On-
Chip Voltage Regulator

K.H. Chang, System Control Lab. 13


1-3 功能簡要說明
• High Speed 8051 μC
Core
– Pipelined instruction
architecture; executes
70% of Instructions in 1
or 2 system clocks
– Up to 48 MIPS operation
– Expanded interrupt
handler

K.H. Chang, System Control Lab. 14


1-3 功能簡要說明
• Memory
– 4352 or 2304 Bytes RAM
– 64 or 32 kB Flash; In-
system programmable in
512-byte sectors

K.H. Chang, System Control Lab. 15


1-3 功能簡要說明
• Digital Peripherals
– 40/25 Port I/O; All 5 V
tolerant with high sink
current
– Hardware enhanced
SPI™, SMBus™, and one
or two enhanced UART
serial ports

K.H. Chang, System Control Lab. 16


1-3 功能簡要說明
• Digital Peripherals
– Six general purpose 16-
bit counter/timers
– 16-bit programmable
counter array (PCA) with
five capture/compare
modules
– External Memory
Interface (EMIF)

K.H. Chang, System Control Lab. 17


1-3 功能簡要說明
• Clock Sources
– Internal Oscillator:
±0.25% accuracy with
clock recovery enabled.
Supports all USB and
UART modes
– External Oscillator:
Crystal, RC, C, or clock (1
or 2 Pin modes)

K.H. Chang, System Control Lab. 18


1-3 功能簡要說明
• Clock Sources
– Low Frequency (80 kHz)
Internal Oscillator
– Can switch between
clock sources on-the-fly

K.H. Chang, System Control Lab. 19


1-3 功能簡要說明
• Packages
– 48-pin TQFP
(C8051F380/2/4/6)
– 32-pin LQFP
(C8051F381/3/5/7)
– 5x5 mm 32-pin QFN
(C8051F381/3/5/7)

K.H. Chang, System Control Lab. 20


1-3 功能簡要說明
• Temperature Range: –
40 to +85 °C

K.H. Chang, System Control Lab. 21


1-4 Voltage Regulator

K.H. Chang, System Control Lab. 22


1-4 Voltage Regulator
• Voltage Regulator Electrical Specifications

K.H. Chang, System Control Lab. 23


1-4 Voltage Regulator
• Voltage Regulator Used and No USB

K.H. Chang, System Control Lab. 24


1-4 Voltage Regulator
• Voltage Regulator Not Used and No USB

K.H. Chang, System Control Lab. 25


1-4 Voltage Regulator
• Voltage Regulator Used and USB Connected
(Bus-Powered)

K.H. Chang, System Control Lab. 26


1-4 Voltage Regulator
• Voltage Regulator Used and USB Connected
(Self-Powered)

K.H. Chang, System Control Lab. 27


1-4 Voltage Regulator
• Exercise 1-1:外部提供12V電源,但
C8051F340僅能接受2.7~5.25V,請問該如
何處理?

K.H. Chang, System Control Lab. 28


1-4 Voltage Regulator
• Exercise 1-2:我們常會在Regulator的輸出端
並聯電容,請問目地為何?

K.H. Chang, System Control Lab. 29


1-5 USB
• Connection Diagram for USB Pins

K.H. Chang, System Control Lab. 30


Chapter 2
C8051開發板

授課教師:張凱雄

1
2-1 硬體設置
• Hardware Setup using a USB Debug Adapter

K.H. Chang, System Control Lab. 2


2-2 C8051F380開發板

K.H. Chang, System Control Lab. 3


2-3 開發板電路
• 外接電源輸入電路

K.H. Chang, System Control Lab. 4


2-3 開發板電路
• 微控制器主要電路

K.H. Chang, System Control Lab. 5


2-3 開發板電路
• LED/SW/RS232介面電路

K.H. Chang, System Control Lab. 6


2-3 開發板電路
• PORT I/O輸出入介面

K.H. Chang, System Control Lab. 7


2-3 開發板電路
• UART介面轉換電路

K.H. Chang, System Control Lab. 8


Chapter 3
Altium Designer電路設計
基本教學

授課教師:張凱雄

1
3-1 快速開始
• 建立New Design Workspace

K.H. Chang, System Control Lab. 2


3-1 快速開始
• 另存Design Workspace

K.H. Chang, System Control Lab. 3


3-1 快速開始
• 存檔名稱為c8051_extend_board

K.H. Chang, System Control Lab. 4


3-1 快速開始
• 建立New Project

K.H. Chang, System Control Lab. 5


3-1 快速開始
• 選擇PCB Project,並依如下設定

K.H. Chang, System Control Lab. 6


3-1 快速開始
• 在quick_start資料夾下建立sch與pcb資料夾

K.H. Chang, System Control Lab. 7


3-1 快速開始
• 加入Schematic檔

K.H. Chang, System Control Lab. 8


3-1 快速開始
• 加入PCB檔

K.H. Chang, System Control Lab. 9


3-1 快速開始
• Save All

K.H. Chang, System Control Lab. 10


3-1 快速開始
• 將PcbDoc檔存在pcb資料夾下

K.H. Chang, System Control Lab. 11


3-1 快速開始
• 將SchDoc檔存在sch資料夾下

K.H. Chang, System Control Lab. 12


3-1 快速開始
• 點選Sheet1,並點選右列Libraries選擇元件

K.H. Chang, System Control Lab. 13


3-1 快速開始
• 將元件依如下放置

K.H. Chang, System Control Lab. 14


3-1 快速開始
• 用滑鼠連點兩下元件,設定相關屬性

K.H. Chang, System Control Lab. 15


3-1 快速開始
• 完成Schematic檔設計

K.H. Chang, System Control Lab. 16


3-1 快速開始
• 點選Design\Update PCB Document PCB1.PcbDoc

K.H. Chang, System Control Lab. 17


3-1 快速開始
• 點選Validate Changes

K.H. Chang, System Control Lab. 18


3-1 快速開始
• 確認Status都OK後執行Execute Changes

K.H. Chang, System Control Lab. 19


3-1 快速開始
• 點選PCB1.PcbDoc

K.H. Chang, System Control Lab. 20


3-1 快速開始
• 點選元件框後刪除

K.H. Chang, System Control Lab. 21


3-1 快速開始
• 將元件擺放至格紙中

K.H. Chang, System Control Lab. 22


3-1 快速開始
• 進入Options\Preferences

K.H. Chang, System Control Lab. 23


3-1 快速開始
• Enable Smart Component Snap

K.H. Chang, System Control Lab. 24


3-1 快速開始
• 使用Interactively Route Connections進行Layout

K.H. Chang, System Control Lab. 25


3-1 快速開始
• 常用PCB板層介紹
Layer Description
Top Layer 上層電路板跑線
Bottom Layer 下層電路板跑線
Mechanical 1 機構層,在給PCB板廠製作印刷電路板時,通常指裁切線
Mechanical 13/15 其它機構層,通常不使用,有使用時大多為破板
Top Overlay 上層文字層
Bottom Overlay 下層文字層
Keep-Out Layer 禁止佈線層,通常做為板框

K.H. Chang, System Control Lab. 26


3-1 快速開始
• Schematic環境下常用熱鍵

Ctrl 按下Ctrl鍵,使用滑鼠拖曳元件可以位置微調

K.H. Chang, System Control Lab. 27


3-1 快速開始
• PCB Layout環境下常用熱鍵
Key Function
Ctrl + M 量測兩點距離
按住滑鼠右鍵拖曳 移動圖紙
按住Crtl再滾動滑鼠滾輪 放大或縮小
G 切換Grid
Q 切換公制/英制單位
Shift + H 切換座標顯示
以滑鼠左鍵按住選擇元件 + 空白鍵 旋轉元件

K.H. Chang, System Control Lab. 28


3-1 快速開始
• PCB Layout環境下常用熱鍵
Key Function
Shift + 空白鍵 切換跑線轉角形式
3 切換線徑(需在Design\Rule先設定)

K.H. Chang, System Control Lab. 29


3-1 快速開始
• 在Bottom Layer完成如下Layout

K.H. Chang, System Control Lab. 30


3-1 快速開始
• 加入Keep-Out Layer並設定左上角為原點

K.H. Chang, System Control Lab. 31


3-1 快速開始
• 加入Mechanical 1 ,完成PCB Layout

K.H. Chang, System Control Lab. 32


3-1 快速開始
• 這次的Layout有沒有什麼問題?

K.H. Chang, System Control Lab. 33


3-1 快速開始
• 連點兩下線段並設定寬度

K.H. Chang, System Control Lab. 34


3-1 快速開始
• 完成如下Layout

K.H. Chang, System Control Lab. 35


3-1 快速開始
• 進入Design\Rules

K.H. Chang, System Control Lab. 36


3-1 快速開始
• 做如下設定

K.H. Chang, System Control Lab. 37


3-1 快速開始
• 完成quick_start Project Layout

K.H. Chang, System Control Lab. 38


3-2 列印輸出
• 點選File\Fabrication Outputs\Final

K.H. Chang, System Control Lab. 39


3-2 列印輸出
• 選擇Bottom Layer,滑鼠右鍵點選Page Setup

K.H. Chang, System Control Lab. 40


3-2 列印輸出
• 依如下參數做設定

K.H. Chang, System Control Lab. 41


3-2 列印輸出
• 滑鼠右鍵點選Configuration

K.H. Chang, System Control Lab. 42


3-2 列印輸出
• 將Bottom Layer做如下設定後確認

K.H. Chang, System Control Lab. 43


3-2 列印輸出
• 點選Print

K.H. Chang, System Control Lab. 44


3-2 列印輸出
• 只需印第二頁Bottom Layer

K.H. Chang, System Control Lab. 45


3-2 列印輸出
• quick_start_final_output.pdf

K.H. Chang, System Control Lab. 46


3-3 自訂元件庫

• Altium Designer內建元件庫路徑
– C:\Users\Public\Documents\Altium\AD14\Library

• Download Altium Libraries

• 若要使用的元件沒有在元件庫中,且廠商沒
有提供,則需要自行建立。

K.H. Chang, System Control Lab. 47


3-3 自訂元件庫
• 建立New\Project

K.H. Chang, System Control Lab. 48


3-3 自訂元件庫
• 建立Integrated Library

K.H. Chang, System Control Lab. 49


3-3 自訂元件庫
• 加入Schematic Library

K.H. Chang, System Control Lab. 50


3-3 自訂元件庫
• 加入PCB Library

K.H. Chang, System Control Lab. 51


3-3 自訂元件庫
• 點選File\Save All

K.H. Chang, System Control Lab. 52


3-3 自訂元件庫
• 將PcbLib檔更名並存檔在lib資料夾下

K.H. Chang, System Control Lab. 53


3-3 自訂元件庫
• 將SchLib檔更名並存檔在lib資料夾下

K.H. Chang, System Control Lab. 54


3-3 自訂元件庫
• 新建元件庫完成,開始製作元件

K.H. Chang, System Control Lab. 55


3-3 自訂元件庫
• 10 LED Bar Displays (LB1003SRWK)

K.H. Chang, System Control Lab. 56


3-3 自訂元件庫

K.H. Chang, System Control Lab. 57


3-3 自訂元件庫
• 點選SCH Library

K.H. Chang, System Control Lab. 58


3-3 自訂元件庫
• 點選Edit進入元件屬性編輯畫面

K.H. Chang, System Control Lab. 59


3-3 自訂元件庫
• 編輯元件參數後點選OK

K.H. Chang, System Control Lab. 60


3-3 自訂元件庫
• 修改Schematic Preferences

K.H. Chang, System Control Lab. 61


3-3 自訂元件庫
• 選擇英制/公制尺寸單位

K.H. Chang, System Control Lab. 62


3-3 自訂元件庫
• 定義英制Pin腳參數

K.H. Chang, System Control Lab. 63


3-3 自訂元件庫

K.H. Chang, System Control Lab. 64


3-3 自訂元件庫
• 在Altium Designer的Schematic圖紙上格線一
格為2.54mm(100mil)。
• Altium Design的Schematic原始格線公制/英
制比例沒有對應,因此採用英制。
• 因此在定義Pin腳長度時取格線的整數倍。

K.H. Chang, System Control Lab. 65


3-3 自訂元件庫

K.H. Chang, System Control Lab. 66


3-3 自訂元件庫

K.H. Chang, System Control Lab. 67


3-3 自訂元件庫

K.H. Chang, System Control Lab. 68


• 使用繪圖工具繪製LED Symbol

按下Ctrl鍵,使用滑鼠拖曳元件可以
位置微調,按下G鍵可以切換Grid

K.H. Chang, System Control Lab. 69


將LED Symbol放在元件上

K.H. Chang, System Control Lab. 70


開始建立Footprint

K.H. Chang, System Control Lab. 71


3-3 自訂元件庫

K.H. Chang, System Control Lab. 72


3-3 自訂元件庫

K.H. Chang, System Control Lab. 73


3-3 自訂元件庫
• 放置Pad

K.H. Chang, System Control Lab. 74


3-3 自訂元件庫

K.H. Chang, System Control Lab. 75


3-3 自訂元件庫
• 設定原點

K.H. Chang, System Control Lab. 76


3-3 自訂元件庫
• Pin 1設定為方形

K.H. Chang, System Control Lab. 77


3-3 自訂元件庫
• 以座標設定每一個Pad位置

K.H. Chang, System Control Lab. 78


3-3 自訂元件庫
• 完成Pad位置排列

K.H. Chang, System Control Lab. 79


3-3 自訂元件庫
• 使用Top Overlay畫元件文字層

K.H. Chang, System Control Lab. 80


3-3 自訂元件庫
• 完成文字層

K.H. Chang, System Control Lab. 81


3-3 自訂元件庫
• 設定Footprint參數

K.H. Chang, System Control Lab. 82


3-3 自訂元件庫
• 連結元件與Footprint

K.H. Chang, System Control Lab. 83


3-3 自訂元件庫
• 連結元件與Footprint

K.H. Chang, System Control Lab. 84


3-3 自訂元件庫
• 連結元件與Footprint

K.H. Chang, System Control Lab. 85


3-3 自訂元件庫
• 完成新建元件

K.H. Chang, System Control Lab. 86


3-3 自訂元件庫
• 編譯元件

K.H. Chang, System Control Lab. 87


3-3 自訂元件庫
• 可以在Schematic使用新建元件

K.H. Chang, System Control Lab. 88


3-4 製作第一塊電路板
• 建立Lab1_IO_Port_Programming PCB Project

K.H. Chang, System Control Lab. 89


3-4 製作第一塊電路板
• 請完成下圖的Schematic與PCB Layout

K.H. Chang, System Control Lab. 90


3-4 製作第一塊電路板
• PCB Layout(請自行設計)

K.H. Chang, System Control Lab. 91


3-4 製作第一塊電路板
• 用蝕刻法製作電路板流程
– 使用描圖紙輸出電路
– 裁切電路板框形狀的感光電路板
– 曝光機曝光
– 顯影
– 鑽孔
– 蝕刻
– 上件

K.H. Chang, System Control Lab. 92


3-4 製作第一塊電路板
• 網路教學影片參考(雙面板)
– 暨大吳俊德實驗室_PCB製作_all

K.H. Chang, System Control Lab. 93


Chapter 4
Keil C編譯環境與基礎C語言入門

授課教師:張凱雄

1
4-1 建立新專案
• Step 1:
– 建立專案資料夾ex4_1

– 在專案資料夾下開兩個
子資料夾
• \config

• \src

K.H. Chang, System Control Lab. 2


4-1 建立新專案
• Step 2:執行Keil uVision4開發程式

K.H. Chang, System Control Lab. 3


4-1 建立新專案
• Step 3:執行 Project\New uVision Project

K.H. Chang, System Control Lab. 4


4-1 建立新專案
• Step 4:指定專案路徑與檔名

K.H. Chang, System Control Lab. 5


4-1 建立新專案
• Step 5:選擇CPU Data Base File

K.H. Chang, System Control Lab. 6


4-1 建立新專案
• Step 6:指定使用晶片C8051F380

K.H. Chang, System Control Lab. 7


4-1 建立新專案
• Step 7:不要加入8051 Startup Code

K.H. Chang, System Control Lab. 8


4-1 建立新專案
• Step 8:檢視Project Workspace

K.H. Chang, System Control Lab. 9


4-1 建立新專案
• Step 9:建立新程式檔,執行File\New

K.H. Chang, System Control Lab. 10


4-1 建立新專案
• Step 10:
– 執行File\Save As

– 將文字檔檔名修改為
main.c

– 將c8051f380.h與原始檔
檔案儲存在\src子資料

K.H. Chang, System Control Lab. 11


4-1 建立新專案
• Step 11:選Source Group 1按右鍵功能表

K.H. Chang, System Control Lab. 12


4-1 建立新專案
• Step 12:將\src\main.c加入專案

K.H. Chang, System Control Lab. 13


4-1 建立新專案
• Step 13:檢視Project Workspace

K.H. Chang, System Control Lab. 14


4-1 建立新專案
• Step 14:先在程式檔輸入如下程式後存檔

K.H. Chang, System Control Lab. 15


4-1 建立新專案
• Step 15:執行Rebuild all target files

K.H. Chang, System Control Lab. 16


4-1 建立新專案
• Step 16:進入專案設定選單

K.H. Chang, System Control Lab. 17


4-1 建立新專案
• Step 17:點選Debug表單並做如下設定

K.H. Chang, System Control Lab. 18


4-1 建立新專案
• Step 18:點選Setting選單並做如下設定

K.H. Chang, System Control Lab. 19


4-1 建立新專案
• Step 19:點選Start/Stop Debug Session下載程式

K.H. Chang, System Control Lab. 20


4-1 建立新專案
• Step 20:進入Debug模式,關掉Disassembly

K.H. Chang, System Control Lab. 21


4-1 建立新專案
• Step 21:按下Run可執行程式

K.H. Chang, System Control Lab. 22


4-1 建立新專案
• Step 22:按下Stop 可停止程式執行

K.H. Chang, System Control Lab. 23


4-1 建立新專案
• Step 23:再按Start/Stop Debug Session回編輯畫

K.H. Chang, System Control Lab. 24


4-1 建立新專案

K.H. Chang, System Control Lab. 25


4-2 程式說明
#include “C8051F380.h” //含括檔

void main(void) // 程式進入點


{
unsigned int i=0; // 區域變數宣告區
P2MDOUT = 0x0C; // 特殊功能暫存器,請查C8051F38x_Rev1.3.pdf Page 166
PCA0MD = 0x00; // 特殊功能暫存器,請查C8051F38x_Rev1.3.pdf Page 311
XBR1 = 0x40; // 特殊功能暫存器,請查C8051F38x_Rev1.3.pdf Page 159
while(1) // 迴圈指令
{
P2 |= 0x0c; // 特殊功能暫存器,請查C8051F38x_Rev1.3.pdf Page 165
for(i=0;i<10000;i++); // 迴圈指令
P2 &= ~0x0c; // 特殊功能暫存器,請查C8051F38x_Rev1.3.pdf Page 165
for(i=0;i<10000;i++); // 迴圈指令
}
}

K.H. Chang, System Control Lab. 26


4-2 程式說明

K.H. Chang, System Control Lab. 27


4-2 程式說明

K.H. Chang, System Control Lab. 28


4-2 程式說明

K.H. Chang, System Control Lab. 29


4-2 程式說明

K.H. Chang, System Control Lab. 30


4-2 程式說明

K.H. Chang, System Control Lab. 31


4-2 程式說明

K.H. Chang, System Control Lab. 32


4-2 程式說明

• 要透過程式來控制微控制器的功能,必需經
由設定特殊功能暫存器SFR。

• 早期撰寫程式碼,須大量翻查SFR的定義,
但現在我們可經由『程式架構精靈』做系統
初始化SFR設定。

K.H. Chang, System Control Lab. 33


4-3 使用程式架構精靈
• Step 1:執行程式集\Silicon Labs\
Configuration Wizard 2

K.H. Chang, System Control Lab. 34


4-3 使用程式架構精靈
• Step 2:選擇使用晶片C8051F380

K.H. Chang, System Control Lab. 35


4-3 使用程式架構精靈
• Step 3:執行Peripherals\PCA

K.H. Chang, System Control Lab. 36


4-3 使用程式架構精靈
• Step 4:
– 點選Module/WDT選單

– 取消勾選『 Enable
Watchdog Timer』

– 按下OK按鈕,回到主畫

K.H. Chang, System Control Lab. 37


4-3 使用程式架構精靈
• Step 5:執行Peripherals\Port I/O

K.H. Chang, System Control Lab. 38


4-3 使用程式架構精靈
• Step 6:做如下設定,按下OK

K.H. Chang, System Control Lab. 39


4-3 使用程式架構精靈
• Step 7:
– 建立資料夾ex4_2與src
與config子資料夾

– 如前述建立專案步驟,
建立專案ex4_2

K.H. Chang, System Control Lab. 40


4-3 使用程式架構精靈
• Step 8:執行File\Save Project As

K.H. Chang, System Control Lab. 41


4-3 使用程式架構精靈
• Step 9:將架構檔ex4_2存在\config下

K.H. Chang, System Control Lab. 42


4-3 使用程式架構精靈
• Step 10:全選複製架構檔程式內容

K.H. Chang, System Control Lab. 43


4-3 使用程式架構精靈
• Step 11:轉貼在專案ex4_2\main.c

K.H. Chang, System Control Lab. 44


4-3 使用程式架構精靈

• Step 12:
– 將c8051f380.h放在src資料夾

– 修改include File Name

– 編寫主程式

– 加入副程式宣告於main.h

– 執行『Rebuild all target files』,並下載程式執行。

K.H. Chang, System Control Lab. 45


4-3 使用程式架構精靈

K.H. Chang, System Control Lab. 46


4-4 單晶片C語言入門
• 詳細內容請參考C語言相關書籍
C語言原始碼

組合語言

指令(機械)碼

目地晶片

K.H. Chang, System Control Lab. 47


4-4-1 C語言的程式架構
#include <AT89X51.H> // 加入含括檔 :
void delayms(unsigned int time) // 副程式
void delayms(unsigned int time); // 副程式宣告 {
/* ====================== unsigned int n; // 應改為 n=0
== 主程式 == while(time>0)
====================== */ {
void main(void) n=120;
{ while(n>0) n--;
while(1) time--;
{ }
P1 = 0x00; }
delayms(200);
P1 = 0xff;
delayms(200);
}
}
:
:

K.H. Chang, System Control Lab. 48


4-4-1 C語言的程式架構
• 函數(function)
– C語言的程式結構為函 Prototype:
返回值資料型態 函數名稱(引數1, 引數2, 引數3..)
數的組成。
範例:
– 函數的範圍由左大括號 (1) void main(void)

(2) void delayms(unsigned int time)


『{』與右大括號『}』
來標定。

– 程式進入點為main函數

K.H. Chang, System Control Lab. 49


4-4-1 C語言的程式架構
• 註解(comment)
– 註解可用來說明程式或是進行程式除錯(Debug)

– 單行註解符號『//』

– 多行註解以『/*』及『*/』標出註解範圍

K.H. Chang, System Control Lab. 50


4-4-1 C語言的程式架構
• 前置處理指令
– 在程式檔中用來引入定義檔或宣告檔使用

– #include <c8051f380.h>
• 優先引入定義路徑C:\Keil\C51\INC\SiLABS

– #include “c8051f380.h”
• 優先引入專案路徑

K.H. Chang, System Control Lab. 51


4-4-1 C語言的程式架構
• 前置處理指令
– 以巨集指令避免多重呼叫的問題
範例(main.h):
#ifndef __main_h
#define __main_h

void PCA_Init();
void Timer_Init();
void Port_IO_Init();
void Oscillator_Init();
void Init_Device(void);

#endif

K.H. Chang, System Control Lab. 52


4-4-2 C語言的變數與常數

• 變數名稱
– 字首需為英文字母或底線

– 第二字起可為英文字母、數字、底線

– C語言變數名稱對大寫與小寫有區別

– 變數名稱不可以為保留字

– 變數名稱的宣告儘量要有意義

K.H. Chang, System Control Lab. 53


4-4-2 C語言的變數與常數
• 資料型態
– 變數是用來存放資料,而資料型態就是變數內所存
放資料的種類。
資料型態 佔用記憶體空間 數值範圍
bit 1 Bit 0~1
char 1 Byte -128 ~ 127
unsigned char 1 Byte 0 ~ 255
int 2 Bytes -32768 ~ 32767
unsigned int 2 Bytes 0 ~ 65535

K.H. Chang, System Control Lab. 54


4-4-2 C語言的變數與常數
• 資料型態
資料型態 佔用記憶體空間 數值範圍
long 4 Bytes -2147483648 ~
2147483647
unsigned long 4 Bytes 0 ~ 4294967295
float 4 Bytes ±1.175494E-38 ~
±3.402823E38

K.H. Chang, System Control Lab. 55


4-4-2 C語言的變數與常數
• 常用資料表示法
– 字元:以單引號標示
• score = ‘A’; //會存入65在score;
– 十進制數值:不以數字0做為開頭的阿拉伯數字
• i = 65;
– 十六進制數值:以0x做為開頭的阿拉伯數字
• i = 0x41;

K.H. Chang, System Control Lab. 56


4-4-2 C語言的變數與常數

• 變數的宣告
ex1:unsigned char i=0;

ex2:char i=0;

ex3:unsigned int i=0;

ex4:int i=0;

ex5:float i=0.0;

K.H. Chang, System Control Lab. 57


4-4-2 C語言的變數與常數
• 區域變數
– 在函數內宣告的變數
• 全域變數
– 在函數外宣告的變數
• 區域變數與全域變數的不同
– 範圍不同
– 生命週期不同

K.H. Chang, System Control Lab. 58


4-4-2 C語言的變數與常數
• 常數的宣告
– 在C語言中使用前置處理器在函數外部來定義常數
ex:
#define TIME 100
#define PI 3.1415925
• 變數與常數有何不同?

K.H. Chang, System Control Lab. 59


4-4-3 C語言的運算子
• 運算子
– 在程式中用來告訴電腦要執行何種運算的符號,稱
為運算子(operator)
– 被處理的資料稱為運算(operand)
ex : a = b + 2;
在上式中『=』與『+』為運算子, 『a』與『b』與
『2』為運算元。

K.H. Chang, System Control Lab. 60


4-4-3 C語言的運算子

• 指定運算子『=』
– 在程式中,『=』稱為指定運算子,會將『=』右邊
的運算結果存入『=』左邊的變數

K.H. Chang, System Control Lab. 61


4-4-3 C語言的運算子
• 算數運算子
– 算數運算子就是我們常使用的數學運算
符號 功能 範例 說明
+ 加法 c = a + b;
- 減法 c = a - b;
* 乘法 c = a * b;
/ 除法 c = a / b;
% 求餘數 c = a % b;

K.H. Chang, System Control Lab. 62


4-4-3 C語言的運算子
• 算數運算子
– 算數運算子就是我們常使用的數學運算
符號 功能 範例 說明
++ 遞加1 c++; c = c+1;
-- 遞減1 c--; c = c-1;
- 求負值 c=-a; c = -1 * a;

K.H. Chang, System Control Lab. 63


4-4-3 C語言的運算子

• 關係運算子
– 主要用在程式中的流程控制,用來判斷某個條件是
否為真,若『條件成立』則運算結果為真(1),
若『條件不成立』則運算結果為假(0)。

K.H. Chang, System Control Lab. 64


4-4-3 C語言的運算子
• 關係運算子
符號 功能 範例 說明
> 是否大於 a>b
>= 是否大於或等於 a>=b
< 是否小於 a<b
<= 是否小於或等於 a<=b
== 是否等於 a==b
!= 是否不等於 a!=b

K.H. Chang, System Control Lab. 65


4-4-3 C語言的運算子
• 關係運算子
ex:
if (a>b)
{
//成立所執行的敘述
}
else
{
//不成立所執行的敘述
}

K.H. Chang, System Control Lab. 66


4-4-3 C語言的運算子
• 邏輯運算子
– 以判斷式的邏輯關係來判斷是否成立。
符號 功能 範例 說明
&& AND if (a<b && c>d) 若a<b且c>d,則條件成立
|| OR if(a<b || c>d) 若a<b或c>d,則條件成立
ex1:
if (a<60 && a>40)
a=60; // 若a滿足小於60且大於40則條件成立
ex2 :
if (a==0 || b=0)
c=0; //若a等於0或者b等於0則條件成立

K.H. Chang, System Control Lab. 67


4-4-3 C語言的運算子
• 邏輯運算子
ex1:
if (a<60 && a>40)
a=60; // 若a滿足小於60且大於40則條件成立

ex2 :
if (a==0 || b=0)
c=0; //若a等於0或者b等於0則條件成立

K.H. Chang, System Control Lab. 68


4-4-3 C語言的運算子
• 位元運算子
– 用來把變數內含值的每個位元做二進位運算
符號 功能
& 把相對應的每個位元做AND運算
| 把相對應的每個位元做OR運算
^ 把相對應的每個位元做XOR運算
~ 把每個位元反相
>> 把變數的內容值右移若干位元,左邊補0
<< 把變數的內容值右移若干位元,右邊補0

K.H. Chang, System Control Lab. 69


4-4-3 C語言的運算子
• 位元運算子
– ex1:
a = 0x56;
b = 0x0F;
c = a&b; // c = 0x06;
– ex2:
a = 0x56;
b = 0x0F;
c = a|b; // c = 0x5F;

K.H. Chang, System Control Lab. 70


4-4-3 C語言的運算子
• 位元運算子
– ex3:
a = 0xB5;
b = a>>2; // b = 0x2D
– ex4:
a = 0xB5;
b = a<<2; // b = 0xD4

K.H. Chang, System Control Lab. 71


4-4-3 C語言的運算子
• 複合型指定運算子
– C語言允許把指定運算子『=』和其他的運算子組成
複合型指定運算子
符號 範例 相同功能的運算式
+= a+=b; a=a+b;
-= a-=b; a=a-b;
*= a*=b; a=a*b;
/= a/=b; a=a/b;
%= a%=b; a=a%b;

K.H. Chang, System Control Lab. 72


4-4-3 C語言的運算子
• 複合型指定運算子
– C語言允許把指定運算子『=』和其他的運算子組成
複合型指定運算子
符號 範例 相同功能的運算式
&= a&=b; a=a&b;
|= a|=b; a=a|b;
^= a^=b; a=a^b;
<<= a<<=2; a=a<<2;
>>= a>>=2; a=a>>2;

K.H. Chang, System Control Lab. 73


4-4-3 C語言的運算子
• 位元設定
– 在1Byte的資料中,設定其中某幾個位元為1,且不
變動到其它位元內的資料。
ex:a=0x13,要將a中的Bit7與Bit3設定為1
a|=0x88; //將要設定的位元與1做OR
// a = 0x13|0x88;
// a = 0x9b;

K.H. Chang, System Control Lab. 74


4-4-3 C語言的運算子
• 位元清除
– 在1Byte的資料中,清除其中某幾個位元為0,且不
變動到其它位元內的資料。
ex:a = 0x13,要將a中的Bit4與Bit0清除為0
a &= ~0x11; //與要清除的位元反相做AND
// a = 0x13 & 0xEE;
// a = 0x02;

K.H. Chang, System Control Lab. 75


4-4-3 C語言的運算子
• 運算子的優先順序
– 以括號『()』來定義先後次序,優先權由內而外
ex:
a = ((3+b)*c)/d;

K.H. Chang, System Control Lab. 76


4-4-4 程式流程的控制
• 程式有四種執行方式:
– 循序
• 由上而下依序執行程式碼
– 條件
• 以條件式成立與否執行對應的程式碼
– 迴圈
• 以條件式成立與否,判斷是否重複執行程式碼
– 中斷
• 以中斷事件成立與否執行中斷副程式

K.H. Chang, System Control Lab. 77


4-4-4 程式流程的控制
• 條件判斷指令if
格式:
if (條件式)
{
敘述1; // 條件式成立時執行
敘述2;
:
}

K.H. Chang, System Control Lab. 78


4-4-4 程式流程的控制
• 分支指令if-else
格式:
if (條件式)
{
敘述T1; // 條件式成立時執行
敘述T2;
:
}
else
{
敘述F1; // 條件式不成立時執行
敘述F2;
:
}

K.H. Chang, System Control Lab. 79


4-4-4 程式流程的控制
• 階梯分支指令if-else if-else
格式:
if (條件式1)
{
敘述1; // 條件式1成立時執行
}
else if (條件式2)
{
敘述2; // 條件式1不成立與條件式2成立時執行
}
else
{
敘述3; //條件式1不成立與條件式2不成立時執行
}

K.H. Chang, System Control Lab. 80


4-4-4 程式流程的控制
• 多重分支指令 switch-case
格式:
switch(變數)
{
case 常數1:
敘述1;
break;
case常數2:
敘述2;
break;
default:
敘述n;
break;
}

K.H. Chang, System Control Lab. 81


4-4-4 程式流程的控制
• 迴圈指令 for
格式:
for(初值;條式式;運算式)
{
敘述1;
}

K.H. Chang, System Control Lab. 82


4-4-4 程式流程的控制
• 迴圈指令 while
格式:
while(條式式)
{
敘述1; //條件式成立時執行
}

K.H. Chang, System Control Lab. 83


4-4-4 程式流程的控制
• 迴圈指令 do-while
格式:
do
{
敘述1; //執行一次後,以後條件式成立時才執行
}
while(條式式)

K.H. Chang, System Control Lab. 84


4-4-4 程式流程的控制
• 跳躍指令 goto
– 容易會破壞程式執行結構,不要使用

K.H. Chang, System Control Lab. 85


4-4-5 陣列
• 陣列等於是一堆相同資料型態的變數整合在
一起,用相同的變數名稱以索引來管理資料
ex:
unsigned char a[5]; // 變數陣列
a[0] = 11; a[1] = 22; a[2] = 33; a[3] = 44; a[4] = 55;

code unsigned char a[5]={11,22,33,44,55}; //常數陣列

K.H. Chang, System Control Lab. 86


4-4-6 函數(副程式)
• 一個C語言的程式就是由一個或多個函數所
組合而成
• 在C語言中,CPU開始執行的第一個函數(
程式進入點)為main()函數,因此又稱為主
函數或主程式
• 除了主函數外的其它函數通稱為副函數或副
程式,需被主函數呼叫才會執行

K.H. Chang, System Control Lab. 87


4-4-6 函數(副程式)

• 有一種特殊的副程式,並非由主程式呼叫所
執行,而是當產生中斷事件時才會被執行,
這種特殊的副程式被稱為中斷副程式

K.H. Chang, System Control Lab. 88


4-4-6 函數(副程式)
• 函數格式
返回值的資料型態 函數名稱(引數1, 引數2....引數n)
{
敘述1;
敘述2;
敘述3;
:
}

K.H. Chang, System Control Lab. 89


4-4-6 函數(副程式)
• 沒有引數也沒有返回值的函數
void delay(void);
void main(void)
{
while(1)
{
P3 = ~P3;
delay();
}
}
void delay(void)
{
unsigned int K=0;
for(K=0;K<1000;K++);
}

K.H. Chang, System Control Lab. 90


4-4-6 函數(副程式)
• 有引數沒有返回值的函數
void delay(unsigned int);
void main(void)
{
while(1)
{
P3 = ~P3;
delay(1000);
}
}
void delay(unsigned int K)
{
while(K>0)
K--;
}

K.H. Chang, System Control Lab. 91


4-4-6 函數(副程式)
• 有引數也有返回值的函數
int sum(int, int);
void main(void)
{
int answer = 0;
answer = sum(25,33);
while(1);
}

int sum(int x, int y)


{
int z=0;
z = x+y;
return(z);
}

K.H. Chang, System Control Lab. 92


4-4-7 中斷函數
• 何謂中斷?
• 中斷事件
– 外部中斷、計時/計數中斷、串列埠中斷...etc
• 撰寫程式有兩種寫作方法
– 輪詢法(Polling)
– 中斷法(Interrupt)

K.H. Chang, System Control Lab. 93


4-4-7 中斷函數
• 中斷副程式格式
void 中斷副程式名稱(void) interrupt 中斷編號
{
敘述1;
敘述2;
:
}

K.H. Chang, System Control Lab. 94


4-4-7 中斷函數
• c8051f380.h
//-----------------------------------------------------------------------------
// Interrupt Priorities
//-----------------------------------------------------------------------------

#define INTERRUPT_INT0 0 // External Interrupt 0


#define INTERRUPT_TIMER0 1 // Timer0 Overflow
#define INTERRUPT_INT1 2 // External Interrupt 1
#define INTERRUPT_TIMER1 3 // Timer1 Overflow
#define INTERRUPT_UART0 4 // Serial Port 0
#define INTERRUPT_TIMER2 5 // Timer2 Overflow
#define INTERRUPT_SPI0 6 // Serial Peripheral Interface 0
:

K.H. Chang, System Control Lab. 95


Chapter 5
輸出入埠

授課教師:張凱雄

1
5-1 Port Input / Output
• Port I/O Cell Block Diagram

K.H. Chang, System Control Lab. 2


5-1 Port Input / Output
• 推挽式(Push-Pull)與開汲極(Open-Drain)輸出

K.H. Chang, System Control Lab. 3


5-1 Port Input / Output

K.H. Chang, System Control Lab. 4


5-1 Port Input / Output
• 在本章所有實驗,請將
Lab1_IO_Port_Programming PCB Board做如下
連接
Lab1_IO_Port_Programming C8051F340/F380DK
PortA <> PORT_3
PortB <> PORT_0

K.H. Chang, System Control Lab. 5


5-1 Port Input / Output
• ex5_1:請使用MCU的Port3依序顯示下列燈

循環方向

K.H. Chang, System Control Lab. 6


5-1 Port Input / Output
• ex5_1
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:
– Enable Crossbar
– P3 Open Drain Mode

K.H. Chang, System Control Lab. 7


5-1 Port Input / Output

• ex5_2:同上例,請使用P0_5與P0_4切換四
種不同的速度,再以P0_7與P0_6切換四種不
同的閃爍樣式。

K.H. Chang, System Control Lab. 8


5-1 Port Input / Output
• ex5_2
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:
– Enable Crossbar
– P3 Open Drain Mode

K.H. Chang, System Control Lab. 9


Chapter 6
Timer與Interrupt

授課教師:張凱雄

1
6-1 Timer/Counter
• 在本章所有實驗,請將
Lab1_IO_Port_Programming PCB Board做如下
連接
Lab1_IO_Port_Programming C8051F380DK
PortA <> PORT_3
PortB <> PORT_0

K.H. Chang, System Control Lab. 2


6-1 Timer/Counter
• Each MCU includes six counter/timers:
– two are 16-bit counter/timers compatible with those
found in the standard 8051
– Four are 16-bit auto-reload timer for use with the
SMBus or for general purpose use.
– These timers can be used to measure time intervals,
count external events and generate periodic interrupt
requests.

K.H. Chang, System Control Lab. 3


6-1 Timer/Counter
• Timer模式

K.H. Chang, System Control Lab. 4


6-2 Timer Mode0/Mode1

K.H. Chang, System Control Lab. 5


6-2 Timer Mode0/Mode1
• 13位元的數值其範圍若以
– 二進制表示:0000000000000~1111111111111
– 十六進制表示:0x0000~0x1FFF
– 十進制表示:0 ~8191

K.H. Chang, System Control Lab. 6


6-2 Timer Mode0/Mode1
• 如何將一個13位元的數分成8位元(高位元
組)與5位元(低位元組)的組合?
– 8190以二進制表示為1111111111110
– 8190若以8位元與5位元數來表示需分割
• 高位元組:8190/32=255
• 低位元組:8190%32=30
• 255以二進制表示為11111111
• 30以二進制表示為11110

K.H. Chang, System Control Lab. 7


6-2 Timer Mode0/Mode1
Timer0 Timer1
TF0 Timer0溢位旗號,Timer0計數器 TF1 Timer1溢位旗號,Timer1計數器
溢位時會被設定為1 溢位時會被設定為1
TR0 Timer0的啟動計數設定旗號 TR1 Timer1的啟動計數設定旗號
TH0 Timer0的高位元組計數值 TH1 Timer1的高位元組計數值
TL0 Timer0的低位元組計數值 TL1 Timer1的低位元組計數值

K.H. Chang, System Control Lab. 8


6-2 Timer Mode0/Mode1
• ex6_1:請以Timer0 Mode0設計基本單位為
1ms的延遲副程式,並使用此延遲副程式,
讓Port3的LED燈每0.5s轉態一次。

K.H. Chang, System Control Lab. 9


6-2 Timer Mode0/Mode1
• ex6_1
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:Enable Crossbar
• Timer0 :
– 13 Bit Counter/Timer
– SYSCLK/12、Prescaled Clock Input

K.H. Chang, System Control Lab. 10


6-2 Timer Mode0/Mode1
• ex6_1

K.H. Chang, System Control Lab. 11


6-2 Timer Mode0/Mode1
• ex6_2:請以Timer0 Mode1設計基本單位為
10ms的延遲副程式,並使用此延遲副程式
,讓Port3的LED燈每0.5s轉態一次。

K.H. Chang, System Control Lab. 12


6-2 Timer Mode0/Mode1
• ex6_2
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:Enable Crossbar
• Timer0 :
– 16 Bit Counter/Timer
– SYSCLK/12、Prescaled Clock Input

K.H. Chang, System Control Lab. 13


6-2 Timer Mode0/Mode1
• ex6_2

K.H. Chang, System Control Lab. 14


6-3 Interrupt
P.119

K.H. Chang, System Control Lab. 15


6-3 Interrupt

P.120

K.H. Chang, System Control Lab. 16


6-3 Interrupt

K.H. Chang, System Control Lab. 17


6-3 Interrupt
• ex6_3:請以Timer0 Mode1與中斷法設計一
程式,使Port3的LED燈每0.5s轉態一次。

K.H. Chang, System Control Lab. 18


6-3 Interrupt
• ex6_3
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:Enable Crossbar
• Timer0 :
– 16 Bit Counter/Timer
– SYSCLK/12、Prescaled Clock Input
• Interrupt:Enable Timer0 Interrupt

K.H. Chang, System Control Lab. 19


6-3 Interrupt
• ex6_3

K.H. Chang, System Control Lab. 20


6-3 Interrupt
• ex6_4:請以Timer0 Mode0與中斷法設計一
程式,使Port3的LED燈每0.1s移動一次的左
移右移霹靂燈。

K.H. Chang, System Control Lab. 21


6-3 Interrupt
• ex6_4
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:Enable Crossbar
• Timer0 :
– 13 Bit Counter/Timer
– SYSCLK/12、Prescaled Clock Input
• Interrupt:Enable Timer0 Interrupt

K.H. Chang, System Control Lab. 22


6-3 Interrupt
• ex6_4

K.H. Chang, System Control Lab. 23


6-4 Timer Mode2

K.H. Chang, System Control Lab. 24


6-4 Timer Mode2
• ex6_5:請以Timer0 Mode2 搭配中斷法與使
用SW101做如下頻率設定,並以P3做輸出
SW101 P3 SW101 P3
0000 0 Hz 1000 8 Hz
0001 1 Hz 1001 9 Hz
0010 2 Hz 1010 10 Hz
0011 3 Hz 1011 11 Hz
0100 4 Hz 1100 12 Hz
0101 5 Hz 1101 13 Hz
0110 6 Hz 1110 14 Hz
0111 7 Hz 1111 15 Hz

K.H. Chang, System Control Lab. 25


6-4 Timer Mode2
• ex6_5
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:Enable Crossbar
• Timer0 :
– 8 Bit Counter/Timer Auto-Reload
– SYSCLK/12、Prescaled Clock Input
• Interrupt:Enable Timer0 Interrupt

K.H. Chang, System Control Lab. 26


6-5 Timer Mode3

K.H. Chang, System Control Lab. 27


6-5 Timer Mode3

• Timer0 Mode3雖然將內部的TH0與TL0獨立成
兩個8位元的計數/計時器,但會佔用到
Timer 1的旗號TR1與TF1,所以等於還是用到
兩個Timer。

K.H. Chang, System Control Lab. 28


6-6 Counter

K.H. Chang, System Control Lab. 29


6-6 Counter

• ex6_6:設定PortB_2的光遮斷器做為Timer0
的計數器輸入源T0,將TL0計數值顯示在P3

K.H. Chang, System Control Lab. 30


6-6 Counter
• ex6_6
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:
– Set P0_2 is T0
– Enable Crossbar

K.H. Chang, System Control Lab. 31


6-6 Counter
• ex6_6
– Configuration Setting
• Timer0 :
– 16 Bit Counter/Timer Mode
– External T0

K.H. Chang, System Control Lab. 32


6-7 External Interrupt

K.H. Chang, System Control Lab. 33


6-7 External Interrupt
• A:正緣觸發
B
• B:正準位觸發
• C :負緣觸發
A C
• D :負準位觸發
D

K.H. Chang, System Control Lab. 34


6-7 External Interrupt
• ex6_7:請使用PortB.0與PortB.1做外部I/O中
斷,當按PortB.0時則加1,當按PortB.1時則
減1,並將結果顯示在PortA的LED燈。

K.H. Chang, System Control Lab. 35


6-7 External Interrupt
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:Enable Crossbar
• Interrupt:
– INT0/INT1 Configuration
» INT0: P0.0
» INT1: P0.1

K.H. Chang, System Control Lab. 36


6-7 External Interrupt
• Configuration Setting
– Timers
• Select “/INT0 is Edge Triggered”
• Select “/INT1 is Edge Triggered”

K.H. Chang, System Control Lab. 37


6-8 SPI Interface
• MCU 常用的串列介面:
– SPI Bus
– I2C Bus
– UART Interface
• 串列介面的優點:
– 減少繞線面積
– 避免串音現象(Crosstalk)

K.H. Chang, System Control Lab. 38


6-8 SPI Interface
• 74164,串入並出移位暫存器

K.H. Chang, System Control Lab. 39


6-8 SPI Interface

K.H. Chang, System Control Lab. 40


6-8 SPI Interface
• Function Table

K.H. Chang, System Control Lab. 41


6-8 SPI Interface
• ex6_8:請使用PortB.0與PortB.1做外部I/O中
斷,當按PortB.0時則加1,當按PortB.1時則
減1,計數範圍從0~99 ,並將結果顯示在
DS201與DS202的七段顯示器上。

K.H. Chang, System Control Lab. 42


6-8 SPI Interface
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:Enable Crossbar
– Set P4.3 P4.4 P4.5 are push-pull mode.
• Interrupt:
– INT0/INT1 Configuration
» INT0: P0.0
» INT1: P0.1

K.H. Chang, System Control Lab. 43


Chapter 7
串列埠

授課教師:張凱雄

1
7-1 串列埠介面與相關SFR介紹
• 在本章所有實驗,請將
Lab1_IO_Port_Programming PCB Board做如下
連接
Lab1_IO_Port_Programming C8051F380DK
PortA <> PORT_3
PortB <> PORT_0

K.H. Chang, System Control Lab. 2


7-1 串列埠介面與相關SFR介紹
• C8051F380DK RS232 Interface

K.H. Chang, System Control Lab. 3


7-1 串列埠介面與相關SFR介紹
• C8051F380DK RS232 Interface

K.H. Chang, System Control Lab. 4


7-1 串列埠介面與相關SFR介紹
• Operational Mode

K.H. Chang, System Control Lab. 5


7-1 串列埠介面與相關SFR介紹
• 8-Bit UART

K.H. Chang, System Control Lab. 6


7-1 串列埠介面與相關SFR介紹
• 9-Bit UART

K.H. Chang, System Control Lab. 7


7-1 串列埠介面與相關SFR介紹

K.H. Chang, System Control Lab. 8


7-1 串列埠介面與相關SFR介紹

K.H. Chang, System Control Lab. 9


7-1 串列埠介面與相關SFR介紹

K.H. Chang, System Control Lab. 10


7-1 串列埠介面與相關SFR介紹

K.H. Chang, System Control Lab. 11


7-1 串列埠介面與相關SFR介紹

K.H. Chang, System Control Lab. 12


7-2 串列埠字元傳送

• ex7_1:請設計一程式,使用C8051F380的
UART0串列埠,以SBUF0每秒傳送”Hello
World!\n”字串到電腦。

K.H. Chang, System Control Lab. 13


7-2 串列埠字元傳送
• ex7_1
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:
– Enable Crossbar
– Enable UART0, Set TX0 is P0.4 and RX0 is P0.5

K.H. Chang, System Control Lab. 14


7-2 串列埠字元傳送
• ex7_1
– Configuration Setting
• Timers\Timer 0:
– 16 Bit Counter/Timer
– SYSCLK/12、Prescaled Clock Input
• UART\UART0 :
– Select “8 Bit UART”
– Enable UART0 Reception
– Enable UART0 Interrupt
– Set UART Baud Rate is 115200 bps (Use Timer1)

K.H. Chang, System Control Lab. 15


7-2 串列埠字元傳送
• ex7_2:請設計一程式,使用C8051F380的
UART0串列埠,以每秒傳送HH:MM:SS字串
到電腦。
– HH:小時
– MM:分鐘
– SS:秒數

K.H. Chang, System Control Lab. 16


7-2 串列埠字元傳送
• 最大值23:59:59
(23*60+59)*60+59=86399

K.H. Chang, System Control Lab. 17


7-2 串列埠字元傳送
• ex7_2
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:
– Enable Crossbar
– Enable UART0, Set TX0 is P0.4 and RX0 is P0.5

K.H. Chang, System Control Lab. 18


7-2 串列埠字元傳送
• ex7_2
– Configuration Setting
• Timers\Timer 0:
– 16 Bit Counter/Timer
– SYSCLK/12、Prescaled Clock Input
• UART\UART0 :
– Select “8 Bit UART”
– Enable UART0 Reception
– Enable UART0 Interrupt
– Set UART Baud Rate is 115200 bps (Use Timer1)

K.H. Chang, System Control Lab. 19


7-3 串列埠字元接收
• ex7_3:請以輪詢法設計一程式,可接收由
電腦傳送到C8051F380的字串,當按下enter
後,C8051F380會將剛由電腦輸入的字串回
傳。

K.H. Chang, System Control Lab. 20


7-3 串列埠字元接收
• ex7_3
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:
– Enable Crossbar
– Enable UART0, Set TX0 is P0.4 and RX0 is P0.5

K.H. Chang, System Control Lab. 21


7-3 串列埠字元接收
• ex7_3
– Configuration Setting
• Timers\Timer 0:
– 16 Bit Counter/Timer
– SYSCLK/12、Prescaled Clock Input
• UART\UART0 :
– Select “8 Bit UART”
– Enable UART0 Reception
– Enable UART0 Interrupt
– Set UART Baud Rate is 115200 bps (Use Timer1)

K.H. Chang, System Control Lab. 22


7-3 串列埠字元接收
• ex7_4:請以中斷法接收由電腦傳送到
C8051F380的字串,當按下enter後,
C8051F380會將剛由電腦輸入的字串回傳。

K.H. Chang, System Control Lab. 23


7-3 串列埠字元接收
• ex7_4
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:
– Enable Crossbar
– Enable UART0, Set TX0 is P0.4 and RX0 is P0.5

K.H. Chang, System Control Lab. 24


7-3 串列埠字元接收
• ex7_4
– Configuration Setting
• Timers\Timer 0:
– 16 Bit Counter/Timer
– SYSCLK/12、Prescaled Clock Input
• UART\UART0 :
– Select “8 Bit UART”
– Enable UART0 Reception
– Enable UART0 Interrupt
– Set UART Baud Rate is 115200 bps (Use Timer1)

K.H. Chang, System Control Lab. 25


Chapter 8
液晶顯示模組與鍵盤

授課教師:張凱雄

1
8-1 LCD模組
• 在本章所有實驗,請將
Lab2_LCD_and_Keyboard_Interfacing PCB
Board做如下連接
Lab1_IO_Port_Programming C8051F380DK
PortC <> PORT_4
PortD <> PORT_3
PortE <> PORT_1

K.H. Chang, System Control Lab. 2


8-1 LCD模組
• Mechanical Diagram

K.H. Chang, System Control Lab. 3


8-2 LCD模組功能方塊

K.H. Chang, System Control Lab. 4


8-3 LCD模組接腳定義

NO SYM. MCU LEVEL Function


1 VSS -- -- GND (0V)
2 VDD -- -- DC 5V
3 Vo -- -- Contrast Adjust
4 RS P4.0 H/L Register Select
5 R/Wn P4.1 H/L Read/Write
6 E P4.2 H, HL Enable Signal
7 DB0 P3.0 H/L Data Bit 0
8 DB1 P3.1 H/L Data Bit 1

K.H. Chang, System Control Lab. 5


8-3 LCD模組接腳定義

NO SYM. MCU LEVEL Function


9 DB2 P3.2 H/L Data Bit 2
10 DB3 P3.3 H/L Data Bit 3
11 DB4 P3.4 H/L Data Bit 4
12 DB5 P3.5 H/L Data Bit 5
13 DB6 P3.6 H/L Data Bit 6
14 DB7 P3.7 H/L Data Bit 7
15 A -- DC 5V LED Backlight +
16 K -- GND LED Backlight -

K.H. Chang, System Control Lab. 6


8-4 控制時序圖
• 資料或命令讀出
RS=0, Read from instruction register
RS=1, Read form data register

K.H. Chang, System Control Lab. 7


8-4 控制時序圖
• 資料或命令寫入 void LCD_Command_Write(unsigned char Command)
void LCD_Data_Write(unsigned char Data_Temp)
RS=0, Write to instruction register
RS=1, Write to data register

K.H. Chang, System Control Lab. 8


8-5 控制命令

K.H. Chang, System Control Lab. 9


8-5 控制命令

K.H. Chang, System Control Lab. 10


8-5 控制命令

K.H. Chang, System Control Lab. 11


8-5 控制命令

K.H. Chang, System Control Lab. 12


8-5 控制命令

K.H. Chang, System Control Lab. 13


8-5 控制命令

K.H. Chang, System Control Lab. 14


8-5 控制命令

K.H. Chang, System Control Lab. 15


8-5 控制命令

K.H. Chang, System Control Lab. 16


8-5 控制命令

K.H. Chang, System Control Lab. 17


8-6 LCD顯示位址
• Display Address

K.H. Chang, System Control Lab. 18


8-7 內建字元唯讀記憶體

K.H. Chang, System Control Lab. 19


8-8 初始化設定

K.H. Chang, System Control Lab. 20


8-9 LCD實驗範例
• ex8-1:依據LCD控制程序,撰寫LCM驅動程
式,使其能於左上角依序顯示「Hello
World!」。

K.H. Chang, System Control Lab. 21


8-9 LCD實驗範例
• ex8-1:
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:
– Enable Crossbar

K.H. Chang, System Control Lab. 22


8-9 LCD實驗範例
• ex8-2:應用陣列指標撰寫副程式,將常數
字串陣列的文字(ASCII)顯示在LCM上.

K.H. Chang, System Control Lab. 23


8-9 LCD實驗範例
• ex8-2:
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:
– Enable Crossbar

K.H. Chang, System Control Lab. 24


8-9 LCD實驗範例
• ex8-3:設計一個如下的副程式做LCM字串顯
示。
void LCD_Str_Printf(unsigned char Start_Addr,unsigned char *DispChar,unsigned char DispNum)

– Start_Addr: 要顯示在LCM上的位址(顯示記憶體)
– *DispChar: 要輸出的字串指標
– DispNum: 要顯示輸出的字元數目

K.H. Chang, System Control Lab. 25


8-9 LCD實驗範例
• ex8-3:
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:
– Enable Crossbar

K.H. Chang, System Control Lab. 26


8-9 LCD實驗範例
• ex8-4:設計一程式,將C8051F380內的溫度
感測器的溫度值每0.5秒在LCM上顯示更新一
次。

K.H. Chang, System Control Lab. 27


8-9 LCD實驗範例
• ex8-4:
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:
– Enable Crossbar
• Timers\Timer 0:
– 16 Bit Counter/Timer
– SYSCLK/12、Prescaled Clock Input

K.H. Chang, System Control Lab. 28


8-9 LCD實驗範例
• ex8-4:
– Configuration Setting
• ADC0
– Enable ADC0
– Write of ‘1’ to AD0BUSY
– Positive Input: Temp Sensor
– Negative Input: GND
• Voltage Reference
– Voltage Reference Select: VDD
– Enable Temperature Sensor

System Control Laboratory, NFU 29


8-10 鍵盤掃瞄

K.H. Chang, System Control Lab. 30


8-10 鍵盤掃瞄
• ex8-5:請設計一程式可將4x4鍵盤的輸入字
元顯示在LCD的第二行,當滿16個字時要清
除第二行並可重新重頭顯示。

K.H. Chang, System Control Lab. 31


8-10 鍵盤掃瞄
• ex8-5:
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:
– Enable Crossbar , Set P4.7 is Push-Pull Mode
• Timers\Timer 0:
– 16 Bit Counter/Timer, Enable Timer0 Interrupt
– SYSCLK/12、Prescaled Clock Input

K.H. Chang, System Control Lab. 32


Chapter 9
Analog-to-Digital Converter
ADC 轉換器

授課教師:張凱雄

1
9-1 Analog-to-Digital Converter

System Control Laboratory, NFU 2


9-1 Analog-to-Digital Converter
• ADC0 on the C8051F380/1/2/3 is a 500 ksps,
10-bit successive-approximation-register (SAR)
ADC with integrated track-and-hold, and a
programmable window detector.

System Control Laboratory, NFU 3


9-1 Analog-to-Digital Converter
• AMUX :類比多工器
S2 S1 S0 Y
0 0 0 X0
0 0 1 X1
0 1 0 X2
0 1 1 X3
1 0 0 X4
1 0 1 X5
1 1 0 X6
1 1 1 X7

System Control Laboratory, NFU 4


9-1 Analog-to-Digital Converter
• AMUX :類比多工器
– 單端輸入模式(Single-ended Mode )
• AMUX(AIN+):連接量測訊號。
• AMUX (AIN-):接地。
– 差動輸入(AIN+-AIN-)模式(Differential Mode)
• AMUX(AIN+):連接量測訊號。
• AMUX (AIN-):連接量測訊號。

System Control Laboratory, NFU 5


9-1 Analog-to-Digital Converter
• AMUX :類比多工器
– 溫度量測模式(Temperature Mode )
• AMUX(AIN+):連接Temp Sensor。
• AMUX (AIN-):接地。

System Control Laboratory, NFU 6


9-1 Analog-to-Digital Converter
• 轉換模式可分成
– 單端輸入轉換模式
– 差動輸入轉換模式
• ADC0H與ADC0L分別用來儲存10位元ADC轉
換器的轉換後資料。
– 10位元的ADC轉換器的轉換後資料,必需由ADC0H
與ADC0L兩個8位元的暫存器分別來存放。

System Control Laboratory, NFU 7


9-1 Analog-to-Digital Converter
• 轉換後的資料存放模式可分為靠右對齊(right-justified
)與靠左對齊(left-justified)兩種方式。
• 靠右對齊(存放0x3FF為例)
ADC0H ADC0L
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1

• 靠左對齊(存放0x3FF為例)
ADC0H ADC0L
1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0

System Control Laboratory, NFU 8


9-1 Analog-to-Digital Converter
– 在單端模式下(Single-ended Mode),轉換資料為
10位元的無符號數

轉換後的10位元無符號數
實際量測電壓( AIN + ) = 參考電壓(VREF )×
1024

System Control Laboratory, NFU 9


9-1 Analog-to-Digital Converter
• 在差動模式下(Differential Mode),轉換
資料為10位有符號數(2補數系統)

轉換後的10位元有符號數
實際量測電壓( AIN + − AIN − ) = 參考電壓(VREF )×
512

System Control Laboratory, NFU 10


9-2 Starting a Conversion
• 啟動ADC進行轉換的方式
– Writing a ‘1’ to the AD0BUSY bit of register ADC0CN.
– A rising edge on the CNVSTR input signal.
– A Timer 0 overflow (i.e., timed continuous
conversions).
– Timer 1, 2, 3, 4, 5 overflow.

System Control Laboratory, NFU 11


9-2 Starting a Conversion
• 以外部訊號CNVSTR觸發啟動ADC轉換

System Control Laboratory, NFU 12


9-2 Starting a Conversion
• 以寫入AD0BUSY觸發啟動ADC轉換

System Control Laboratory, NFU 13


9-2 Starting a Conversion
• 如何知道轉換已經完成,可以讀取轉換後資
料?
– 讀取AD0BUSY特殊功能暫存器進行判斷
– 0: ADC0 conversion is complete or a conversion is not
currently in progress. AD0INT is set to logic 1 on the
falling edge of AD0BUSY.
– 1: ADC0 conversion is in progress.

System Control Laboratory, NFU 14


9-2 Starting a Conversion
• ex9_1:請使用寫入AD0BUSY進行ADC轉換的
方式,將R10的電位端(P2.5) 進行單端模式
Analog-to-Digital轉換,並將轉換值與量測值
顯示在LCM與UART上。

System Control Laboratory, NFU 15


9-2 Starting a Conversion
• ex9_1
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:
– Enable Crossbar
– Enable UART0, Set TX0 is P0.4 and RX0 is P0.5

K.H. Chang, System Control Lab. 16


9-2 Starting a Conversion
• ex9_1
– Configuration Setting
• Timers\Timer 0:
– 16 Bit Counter/Timer
– SYSCLK/12、Prescaled Clock Input
• UART\UART0 :
– Select “8 Bit UART”
– Enable UART0 Reception
– Enable UART0 Interrupt
– Set UART Baud Rate is 115200 bps (Use Timer1)

K.H. Chang, System Control Lab. 17


9-2 Starting a Conversion
• ex9_1
– Configuration Setting
• ADC0
– Enable ADC0
– write of ‘1’ to AD0BUSY
– Positive Input: P2.5
– Negative Input: GND
• Voltage Reference
– Voltage Reference Select: VDD

System Control Laboratory, NFU 18


9-3 Temperature Sensor

System Control Laboratory, NFU 19


9-3 Temperature Sensor

System Control Laboratory, NFU 20


9-3 Temperature Sensor
• ex9_2:請使用Timer0 overflow的方式進行
ADC轉換的方式,將Temperature Sensor的值
每0.5秒進行ADC轉換,並將轉換值與量測值
顯示在LCM與UART上。

System Control Laboratory, NFU 21


9-3 Temperature Sensor
• ex9_2
– Configuration Setting
• Oscillators:12MHz
• PCA:Disable Watch Dog Timer
• Port I/O:
– Enable Crossbar
– Enable UART0, Set TX0 is P0.4 and RX0 is P0.5

K.H. Chang, System Control Lab. 22


9-3 Temperature Sensor
• ex9_2
– Configuration Setting
• Timers\Timer 0:
– 16 Bit Counter/Timer
– SYSCLK/12、Prescaled Clock Input
• UART\UART0 :
– Select “8 Bit UART”
– Enable UART0 Reception
– Enable UART0 Interrupt
– Set UART Baud Rate is 115200 bps (Use Timer1)

K.H. Chang, System Control Lab. 23


9-3 Temperature Sensor
• ex9_2
– Configuration Setting
• ADC0
– Enable ADC0
– overflow of Timer 0
– Positive Input: Temp Sensor
– Negative Input: GND
• Voltage Reference
– Voltage Reference Select: VDD
– Enable Temperature Sensor

System Control Laboratory, NFU 24

You might also like