Bài giảng Vi xử lý - Chương 5: Thiết kế hệ vi xử lý (Phần 1) - Bùi Minh Thành

Nội dung

• Giải mã địa chỉ

• Giao tiếp bộ nhớ

• Giao tiếp với khóa (switch) và bàn phím

• Giao tiếp bộ hiển thị (Display)

– Giao tiếp với LED

– Giao tiếp với LCD

• Giao tiếp A/D-D/

pdf 140 trang yennguyen 2340
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Vi xử lý - Chương 5: Thiết kế hệ vi xử lý (Phần 1) - Bùi Minh Thành", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

Tóm tắt nội dung tài liệu: Bài giảng Vi xử lý - Chương 5: Thiết kế hệ vi xử lý (Phần 1) - Bùi Minh Thành

Bài giảng Vi xử lý - Chương 5: Thiết kế hệ vi xử lý (Phần 1) - Bùi Minh Thành
1Chương 5
Thiết kế hệ vi xử lý
2Nội dung
• Giải mã địa chỉ
• Giao tiếp bộ nhớ
• Giao tiếp với khóa (switch) va ̀ bàn phím
• Giao tiếp bộ hiển thi ̣ (Display)
– Giao tiếp với LED
– Giao tiếp với LCD
• Giao tiếp A/D-D/A
35.1 Giải mã địa chỉ
• Khi vi xử lý gửi một địa chỉ ra bus địa chỉ, thì thông tin này phải được chuyển
thành lệnh cụ thể cho thiết bị cụ thể. Giải mã địa chỉ thực hiện tác vụ này. Nó sử
dụng thông tin bus địa chỉ để xác định thiết bị nào sẽ được truy cập.
4Phương pháp giải mã địa chỉ
Có 2 phương pháp giải mã địa chỉ:
1. Giải mã toàn phần (Full address decoding): Mỗi
ngoại vi được gán cho một địa chỉ duy nhất. Tất cả
các bit địa chỉ được dùng để định nghĩa vị trí được
tham chiếu.
2. Giải mã một phần (Partial address decoding): Không
phải tất cả các bit được dùng cho việc giải mã địa chỉ. 
Các ngoại vi có thể đáp ứng cho trên một địa chỉ. 
Phương pháp làm giảm độ phức tạp trong mạch giải
mã địa chỉ. Thông thường các hệ thống nhỏ sử dụng
giải mã một phần.
5Thí dụ: TK mạch giải mã địa chỉ 
• Với bảng bộ nhớ sau, hãy thiết kế mạch giải mã toàn phần và
mạch giải mã một phần cho các chip bộ nhớ (RAM 4KB và
ROM 16KB) và I/O
6Bảng bô ̣ nhớ/IO của TD
7Thi ́ du ̣ giải mã địa chỉ toàn phần
8Ky ̃ thuật giải mã một phần
9Thi ́ du ̣ giải mã địa chỉ một phần
10
kỹ thuật giải mã một phần dẫn đến:
1. Mạch giải mã đơn giản hơn.
2. Nhiều vùng địa chỉ hơn được cấp phát với tầm địa 
chỉ một phần. Thi ́ du ̣ mạch giải mã cho RAM chỉ 
kiểm tra A15 va ̀ kết quả là toàn bô ̣ tầm địa chỉ từ 
0000H đến 7FFFH được cấp phát cho RAM.
3. Sự bất lợi của giải mã địa chỉ một phần là bất cứ 
khai triển nào thêm vê ̀ bô ̣ nhớ hoặc thiết bị I/O thì 
cần thiết kê ́ lại mạch giải mã địa chỉ. Trái lại, với
mạch giải mã toàn phần thì bất cứ sự thêm vào bô ̣ 
nhớ hoặc thiết bị I/O, ta không cần sửa đổi mạch
giải mã địa chỉ.
11
Hardware dùng cho mạch giải mã địa chỉ
• Cổng logic Æ mạch phức tạp
• Mạch giải mã (Decoder) – TD:74LS138,...
• Mạch so sánh (Comparator)
• ROM Æ thừa chức năng
• PLD: PLA, PAL,... Æ cho đáp ứng nhanh
12
5.2 Giao tiếp bộ nhớ
• Vi xử lý sử dụng bộ nhớ để lưu trữ các lệnh và dữ liệu trong khi thực thi
chương trình. Do đó vi xử lý thực hiện nhiều tác vụ ghi/đọc với bộ nhớ trong
khi thực thi chương trình.
• Mỗi chip bộ nhớ RAM hay ROM sẽ có một ngõ vào có tên là /CE (Chip 
Enable=cho phép chip [hoạt động]) hoặc /CS (Chip Select=Chọn chip), thông
thường các chân này hoạt động logic tích cực thấp, nghĩa là chân này bằng mức
0 thì chip này được chọn. 
• Vì trong mạch có nhiều thiết bị I/O và bộ nhớ, do đó cần phải có mạch giải mã
địa chỉ để tạo ra các tín hiệu chọn chip.
• Bus điều khiển có các tín hiệu định thì (do vi xử lý cung cấp) để đồng bộ
chuyển thông tin giữa vi xử lý và bộ nhớ hay thiết bị I/O. Tổng quát thì có 2 tín
hiệu RD (Read) và WR (Write), hai tín hiệu này thông thường cũng hoạt động
logic tích cực thấp. Ngoài ra tùy theo vi xử lý còn có thêm các tín hiệu khác như
ALE, PSEN,...
13
Một số chân điều khiển bộ nhớ
Bộ nhớ (và hầu hết các thiết bị ngoại vi) có các đường điều khiển đặc biệt để
giao tiếp với vi xử lý
• /CS hoặc /CE (Chip Select hoặc Chip Enable)
– được lái bởi mạch giải mã địa chỉ từ vi xử lý.
– thường là tích cực thấp
– khi được xác định, thì chip/ngoại vi được chọn.
• /OE (Output Enable=cho phép xuất) hay /RD (với RAM)
– thường thấy trong các bộ nhớ
– khi nó tích cực (thường là tích cực thấp) thì ngõ ra ở trạng thái hi-Z.
– đôi khi nó được xem như /RD trong RAM.
• /WR (Write Enable=cho phép ghi)
– được lái bởi /WR của vi xử lý
14
Qui tắc chung về thiết kế mạch giao
tiếp bộ nhớ với 8051
1. Lập bảng bộ nhớ
– Hệ thống và ứng dụng phụ thuộc
– Giải pháp đơn giản là tách riêng 64KB bộ nhớ dữ liệu và 64KB bộ nhớ mã.
– Để sử dụng các địa chỉ trên 64K thì sử dụng thêm các bit từ những cổng I/O 
không sử dụng làm các đường địa chỉ.
2. Chọn linh kiện bộ nhớ thích hợp (nếu không bị áp đặt sử dụng).
3. Sử dụng mạch giải mã địa chỉ (nếu cần) để tạo ra các tín hiệu /CE hay /CS 
cho các chip bộ nhớ.
4. Sử dụng đường /PSEN (từ 8051) cho bộ nhớ mã.
5. Sử dụng các đường /RD, /WR cho bộ nhớ dữ liệu.
6. Chân /EA=VCC (+5V) để sử dụng ROM trong 8051, cho /EA=GND (0V) 
để truy cập ROM ngoài.
15
TD: Giao tiếp 8051 với RAM HM 6264 và
ROM 27C256 
16
TD: 8031/8051 giao tiếp với RAM ngoài và I/O
17
Giao tiếp bô ̣ nhớ với mạch giải mã 
địa chỉ là PLD
18
5.5 Giao tiếp với khóa (switch) va ̀ 
bàn phím
19
Công tắc/khóa (hay phím đơn) ở các 
chân cổng I/O
20
Sử dụng DIP switch ở cổng I/O
Đoạn chương trình đọc dữ liệu vào:
mov P1,#0FFH ; đặt cấu hình nhập cho
cổng P1
mov A, P1
21
Kết nối tối thiểu cho các
hê ̣ thống dùng 89C51/52
22
SWITCH ON I/O PORTS (1/2)
23
SWITCH ON I/O PORTS (2/2)
•Good Circuit
It is always best connecting the switch to ground with a pull-up resistor as shown in 
the "Good" circuit. When the switch is open, the 10k resistor supplies very small 
current needed for logic 1. When it is closed, the port pin is short to ground. The 
voltage is 0V and all the sinking current requirement is met, so it is logic 0. The 10k 
resistor will pass 0.5 mA (5 Volt/10k ohm). Thus the circuits waste very little current in 
either state. The drawback is that the closure of switch gives logic 0 and people like to 
think of a switch closure gives logic 1. But this is not a matter because it is easy to 
handle in software.
•Fair circuit
The "Fair" circuit requires that the pull-down resistor be very small. Otherwise, the pin 
will rise above 0.9V when the resistor passes the 1.6mA sinking current. When the 
switch is closed, the circuit waste a large current since virtually no current flows into 
the pin. The only advantage is that a switch closure gives logic 1.
•Poor circuit
In the "Poor" circuit, the logic 1 is stable when the switch is closed. But when the 
switch is open, the input floats to a noise-sensitive high rather than a low. An open 
TTL pin is usually read as logic 1 but the pin may picks up noise like an antenna.
To conclude, driving a TTL input should always consider current sinking (pulling input 
to 0V).
24
Keypad controller
25
Giao tiếp với bàn phím hex
26
Giao tiếp với bàn phím hex 
Để đọc toàn bộ bàn phím, mỗi hàng lần lượt được đọc vào như sau:
1. Dữ liệu 0111 được ghi ra các bit cổng xuất (P1.0 đến P1.3).
2. Các bit cổng nhập (P1.4 đến P1.7) được đọc vào. Nếu không có phím
nào được nhấn trên hàng đó thì giá trị đọc vào sẽ là 1111. Nếu có bất
cứ phím nào được nhận trên hàng đó thì sẽ có 0 ở bit tương ứng.
3. Dữ liệu 1011 được ghi vào cổng xuất bằng cách dịch 0 vào cột kế; và
cổng nhập được đọc vào.
4. Dữ liệu 1101 được ghi vào cổng xuất; và cổng nhập được đọc vào.
5. Dữ liệu 1110 được ghi vào cổng xuất; và cổng nhập được đọc vào.
6. Chu trình này được lặp đi lặp lại vô tận bằng cách quay về bước 1 ở
trên.
27
Giao tiếp với bàn phím hex
Dạng sóng ở các bit cổng xuất P1.0 đến P1.3
28
Chương trình quét bàn phím
; Chương trình quét bàn phím
org 2000H
start: LJMP init ; nhảy đến chương trình chính
scan: MOV P1, #0F0H ; kiểm tra với các phím nhấn
MOV A, P1 ; bằng cách quan sát P1
CJNE A, #0F0H, scan
scanner:
MOV A, #0FEH ; khuôn mẫu bit bắt đầu (4 bit thấp là 1110)
1up: MOV R0, A ; cất khuôn mẫu xuất
MOV P1, A ; mẫu xuất
MOV A, P1 ; đọc cổng
MOV R1, A ; cất phím đọc được
ORL A, #0FH ; đặt các bit ở cột lên 1
CJNE A, #0FFH, cnvrt ; nhảy đến cnvrt nếu phím được nhấn
MOV A, R0 ; lấy lại các bit lái cột
RL A ; xoay bit lái cột sang trái
CJNE A, #0EFH, 1up ; nhảy đến quét cột kế
SJMP scanner ; bắt đầu đợt quét mới
29
Chương trình quét bàn phím
cnvrt:
MOV A, R1 ; khôi phục phím đọc được
MOV R3, #0 ; xóa bộ đếm bảng
clup: JNB ACC.0, cnvrt2 ; xong với số đếm chính
RR A ; xoay giá trị
MOV R2, A ; cất giá trị được xoay
MOV A, R3 ; tăng bộ đếm thêm 1
ADD A, #4 ; cộng 4 cho mỗi cột
MOV R3, A ; cất số đếm
MOV A, R2 ; lấy lại giá trị xoay
SJMP c1up
cnvrt2: MOV A, R1 ; khôi phục phím đọc được.
SWAP A ; hoán đổi 2 nửa byte
c1up2: JNB ACC.0, xlat; nhảy đến dịch phím đọc được
RR A ; xoay để tìm bit 0
INC R3 ; tăng số đếm thêm 1
SJMP c1up2 ; nhảy đến kiểm tra bit kế
xlat: MOV A, R3 ; lấy số đếm
MOV DPTR, #keytab ; chỉ đến bảng dịch
MOVC A, @A+DPTR ; lấy ký tự được dịch
RET
30
DSPLY: RET ; trình con display giả (cụ thể sẽ đựoc viết đầy đủ)
init: ACALL scan ; lấy phím nhấn
MOV R0, A ; cất ký tự
CLR C ; xóa cờ nhớ để trừ
SUBB A, #0AH ; kiểm tra xem có > 10 ?
JNC letter ; nhảy nếu > 10
MOV A, R0 ; lấy lại ký tự
ORL A, #30H; đổi thành số ASCII
SJMP dply ; nhảy đến hiển thị ký tự
letter: MOV A, R0 ; lấy lại ký tự
ADD A, #55 ; điều chỉnh cho ASCII
dply: LCALL DSPLY; hiển thị ký tự
SJMP init ; làm lại 
Chú ý là 3 dòng tại vòng lặp “scan” kiểm tra xem có phải tất cả các phím 
nhấn đã được nhả ra trước khi bắt đầu chu trình quét.
31
Một dạng bàn phím
(keypad hay keyboard)
32
Interfacing to the Keyboard
– keyboard is organized in a matrix of rows and columns
– the key press is scanned and identified by microcontroller
33
Interfacing to the Keyboard
• It is the function of the microcontroller to scan the keyboard 
continuously to detect and identify the key pressed 
• To detect a pressed key, the microcontroller grounds all rows 
by providing 0 to the output latch, then it reads the columns 
– If the data read from columns is D3 –D0 = 1111, no key 
has been pressed and the process continues till key press 
is detected 
– If one of the column bits has a zero, this means that a key 
press has occurred 
• For example, if D3 –D0 = 1101, this means that a key 
in the D1 column has been pressed 
• After detecting a key press, microcontroller will go 
through the process of identifying the key
34
Interfacing to the Keyboard
• Starting with the top row, the microcontroller 
grounds it by providing a low to row D0 only
– It reads the columns, if the data read is all 1s, no key 
in that row is activated and the process is moved to 
the next row
• It grounds the next row, reads the columns, and 
checks for any zero
– This process continues until the row is identified
• After identification of the row in which the key has 
been pressed
– Find out which column the pressed key belongs to
35
Interfacing to the Keyboard
36
Interfacing to the Keyboard
37
Interfacing to the Keyboard
38
Interfacing to the Keyboard
39
Interfacing to the Keyboard
40
Interfacing to the Keyboard
41
Interfacing to the Keyboard
Last program for detection and identification 
of key activation goes through the following 
stages:
1.To make sure that the preceding key has 
been released, 0s are output to all rows at 
once, and the columns are read and checked 
repeatedly until all the columns are high 
When all columns are found to be high, 
the program waits for a short amount of 
time before it goes to the next stage of 
waiting for a key to be pressed
42
Interfacing to the Keyboard
2. To see if any key is pressed, the columns are 
scanned over and over in an infinite loop until one 
of them has a 0 on it 
 Remember that the output latches connected to 
rows still have their initial zeros (provided in stage 
1), making them grounded
 After the key press detection, it waits 20 ms for 
the bounce and then scans the columns again
(a) it ensures that the first key press detection 
was not an erroneous one due a spike noise
(b) the key press. If after the 20-ms delay the 
key is still pressed, it goes back into the loop to 
detect a real key press
43
Interfacing to the Keyboard
3.To detect which row key press belongs to, it grounds one 
row at a time, reading the columns each time
 If it finds that all columns are high, this means that the 
key press cannot belong to that row
–Therefore, it grounds the next row and continues 
until it finds the row the key press belongs to 
 Upon finding the row that the key press belongs to, it sets 
up the starting address for the look-up table holding the 
scan codes (or ASCII) for that row
4.To identify the key press, it rotates the column bits, one bit 
at a time, into the carry flag and checks to see if it is low 
 Upon finding the zero, it pulls out the ASCII code for that 
key from the look-up table
 otherwise, it increments the pointer to point to the next 
element of the look-up table
44
Switch Matrix
• Switches organized as Row/Column
• Switch Shorts row line to column line
• Walking zero on columns to activate one 
column at a time
• Check for low level on row inputs to 
determine which key in this column is 
pressed
45
Simple Switch Matrix
46
Switch Matrix
• Switches organized in Rows/Columns
• Switch Shorts row line to column line
• Walking zero on columns to activate one 
column at a time
• Check for low level on row inputs to 
determine which key is pressed
• Multiplexed row and column
47
Multiplexing a Switch Matrix
48
Switch Matrix Key Pressed
49
Phần xuất/nhập số với VXL
50
Giao tiếp với các thiết bị I/O
• Truyền dữ liệu được đồng bô ̣ hóa bằng các mạch
giao tiếp đgl các cổng IO (nhập-xuất).
– Một cổng IO có thê ̉ được dùng cho nhiều thiết bị khác
nhau.
– Tất cả các cổng IO có thể dùng chung bus.
• Mạch giao tiếp có thê ̉ bao gồm
– Các thanh ghi dữ liệu: dữ liệu được gửi đi hay nhận vê ̀
– Các thanh ghid điều khiển: đê ̉ chọn kiểu tác vụ IO
– Các thanh ghi trạng thái: trạng thái của tác vụ IO
• Với cách nhìn VXL thì các thuật ngữ “cổng IO” và 
“thiết bị IO” đôi khi được dùng cho nhau.
51
Chip giao tiếp IO
Bus địa chỉ IO có thê ̉ trùng hoặc khác bus địa 
chỉ dùng cho bô ̣ nhớ chính.
52
Nhắc lại: Có 2 kiểu ánh xa ̣ IO 
(a) IO-mapped input/output (b) Memory-mapped input/output
53
Các kiểu cổng IO
• Một cổng IO có thê ̉ có nhiều chân IO; mỗi
chân tương ứng với 1 bit dữ liệu.
• Có 3 kiểu chân IO:
– Các chân chỉ nhập
– Các chân chỉ xuất
– Các chân 2 chiều
• Một cổng IO có thê ̉ chứa nhiều chân có 
các kiểu hỗn hợp.
54
Các chân nhập (dùng đệm 3 trạng thái)
55
Các chân xuất (dùng mạch chốt)
56
Các chân 2 chiều
Đặt cấu hình:
• Nhập: ghi D=1 để M1 OFF
• Xuất: ghi D=0 để M1 ON
57
Bouncing Contacts
• Push-button switches, toggle switches, and 
electromechanical relays all have one thing in 
common: contacts.
• Metal contacts make and break the circuit and 
carry the current in switches and relays. 
Because they are metal, contacts have mass.
• Since at least one of the contacts is movable, it 
has springiness.
• Since contacts are designed to open and close 
quickly, there is little resistance (damping) to 
their movement
58
Bouncing
• Because the moving contacts have mass and 
springiness with low damping they will be "bouncy" 
as they make and break.
• That is, when a normally open (N.O.) pair of contacts 
is closed, the contacts will come together and bounce 
off each other several times before finally coming to 
rest in a closed position.
• The effect is called "contact bounce" or, in a switch, 
"switch bounce”.
59
Why is it a problem?
• If such a switch is used as a source to 
an edge-triggered input such  ... cknowledgement 
from the 8255 to the device.
– INTR (Interrupt Request) (C3 for A, C0 for B)
• A “high” on this output can be used to interrupt the CPU 
when an input device is requesting service.
86
Timing diagram for 
mode1(input)
87
Mode 2 - Strobed Bidirectional 
Bus I/O
• MODE 2 Basic Functional Definitions:
– Used in Group A only.
– One 8-bit, bi-directional bus port (Port A) and 
a 5-bit control port (Port C).
– Both inputs and outputs are latched.
– The 5-bit control port (Port C) is used for 
control and status for the 8-bit, bi-directional 
bus port (Port A).
88
Mode 2
• Output Operations
– OBF (Output Buffer Full). The OBF 
output will go low to indicate that the 
CPU has written data out to port A.
– ACK (Acknowledge). A low on this 
input enables the tri-state output 
buffer of Port A to send out the data. 
Otherwise, the output buffer will be in 
the high impedance state.
• Input Operations
– STB (Strobe Input). A low on this 
input loads data into the input latch.
– IBF (Input Buffer Full F/F). A high on 
this output indicates that data has 
been loaded into the input latch.
Pin Functio
n
PC7 /OBF
PC6 /ACK
PC5 IBF
PC4 /STB
PC3 INTR
PC2 I/O
PC1 I/O
PC0 I/O
89
90
BSR Mode
• If used in BSR mode, then the bits of 
port C can be set or reset individually
91
BSR Mode example
Move dptr, 0093h
Up: Move a, 09h ;set pc4
Movx @dptr,a
Acall delay
Mov a,08h ;clr pc4
Movx @dptr,a
Acall delay
Sjmp up
92
Interfacing 8255 with 8051
• CS is used to interface 8255 with 8051
• If CS is generated from lets say Address lines 
A15:A12 as follows,
A15:A13 = 110
• Address of 8255 is 110 xxxxx xxxx xx00b
• Base address of 8255 is
– 1100 0000 0000 0000b=C000H
• Address of the registers
– A = C000H 
– B = C001H 
– C = C002H 
– CR = C003H
93
Interfacing 8255 with 8051
8255
8051
74138
3×8 decoder
74373 
P0.7-P0.0
(AD7-AD0)
D7-D0
D7-D0
/CS
A0
A1
O0
O1
O7
A2
A1
A0
P2.7(A15)
P2.6(A14)
P2.5(A13)
ALE
/RD
/WR
/RD
/WR
94
8255 Usage: Simple Example
• 8255 memory mapped to 8051 at address C000H base
– A = C000H, B = C001H, C = C002H, CR = C003H
• Control word for all ports as outputs in mode0
– CR : 1000 0000b = 80H
test: mov A, #80H ; control word
mov DPTR, #C003H ; address of CR
movx @DPTR, A ; write control word
mov A, #55h ; will try to write 55 and AA 
; alternatively
repeat:mov DPTR,#C000H ; address of PA
movx @DPTR, A ; write 55H to PA
inc DPTR ; now DPTR points to PB
movx @DPTR, A ; write 55H to PB
inc DPTR ; now DPTR points to PC
movx @DPTR, A ; write 55H to PC
cpl A ; toggle A (55→AA, AA→55)
acall MY_DELAY ; small delay subroutine
sjmp repeat ; for (1)
95
5.6 Giao tiếp bộ hiển thi ̣ (Display)
5.6.1 Giao tiếp với LED
96
Giao tiếp với LED
97
Giao tiếp với LED đơn
98
TD: Mạch cho nhập từ phím nhấn va ̀ xuất ra LED đơn
99
Mạch nhập từ phím nhấn
100
Mạch xuất ra LED đơn
101
LED ON I/O PORTS (1/2)
102
LED ON I/O PORTS (2/2)
Since TTL outputs is designed to feed multiple TTL inputs, they are good at 
current sinking but poor at current sourcing. The Standard TTL can sink up to 
16mA and source 250uA. The LS logic family can sink 8mA and source 100uA. 
The 8051 port pin can sink 1.6mA (3.2mA for port 0) and source 60uA. 
Therefore, if you drive significant current, try to arrange your circuits to use 
current sinking. Unlike diodes, Light-emitting diodes have a forward voltage 
drop from 1.7 to 2.5 volts and most of them flow a forward current 20mA.
Poor circuit
since the TTL output can't source above 1mA so the LED will be very dim.
Fair circuit
The LED will conduct heavily at about 2V and the extra 3V has to be dropped in 
the TTL circuitry. This causes high power dissipation in the TTL or the LED 
fails.
Good circuit
The resistor limits the current. The resistance can be calculated by assuming its 
voltage is about 2.5V and the TTL output is 0.9V. For 2.2V LED, 1.9V is across 
the resistor so the 220ohm would limit the current to 8.6mA (1.9/220). For 1.7V 
LED, 2.4V is across the resistor so it would limit the current to 10.9mA 
(2.4/220). The resistor should not less than 100 Ohm or the LED would fail.
103
Example 1
• Connection -Switch -P1.0, LED - P2.0
• Condition - Turn on LED when switch is pressed.
• Solution:
SETB P2.0 ; LED OFF
SETB P1.0 ; input pin.
LOOP: JB P1.0, LOOP ; not grounded then 
; stay in loop
CLR P2.0 ; To clear pin P0.0 when 
; P1.0 is at 0 v
• Schematic Circuit?
104
Ex: RELAY ON I/O PORT 
(2CO Relay)
In A, NPN transistor (say a BC337 or BC338) is being used to control a relay 
with a 5 V coil. Series base resistor R1 is used to set the base current for Q1, 
so that the transistor is driven into saturation (fully turned on) when the relay is 
to be energized. That way, the transistor will have minimal voltage drop, and 
hence dissipate very little power as well as delivering most of the 5V to
the relay coil.
How do work out the value of R1?.
Let us say RLY1 needs 50mA of coil current to pull in and hold reliably, and has 
a resistance of 24 Ohms so it draws this current from 5V. Our BC337/338 
transistor will need enough base current to make sure it remains saturated at 
this collector current level. To work this out, we simply make sure that the base 
current is greater than this collector current divided by the transistors minimum
DC current gain hFE. So as the BC337/338 has a minimum hFE of 100 (at 
100mA), we'll need to provide it with at least 50mA/100 = 0.5mA of base 
current.
In practice, you give it roughly double this value, say 1mA of base current, just 
to make sure it does saturate. So if your resistance will be TTL Logic High 
Voltage (Min) /1ma ( 1K approx)
105
Example 2 – Switch and LED
Connection 
Port 0 is connected to eight 
LEDs, each of them is 
connected to 5V through a 
330ohm resistor. Port 1 is 
connected to a DIP switch and a 
10Kohm resistor
Condition 
Corresponding LED should light 
up when switch pressed , i.e. if 
Switch at 1.0 is pressed -> LED 
at P0.0 should light up.
Solution:
; To configure port 1 for input.
MOV P1, #0FFH
LOOP: MOV A, P1
MOV P0, A
SJMP LOOP ; Stay in 
;infinite loop
106
Using ULN (Driver IC)
Another option for driving relays would be to use a high-voltage, high-current, 
Darlington array driver IC such as the ULN2803. The ULN2803 can directly 
interface to the data outputs of the 8051 pins, and provides much higher 
drive-current. The ULN2803 also has internal diode protection that eliminates 
the need for the fly-back diode as shown in the above relay driver schematics. 
You can connect 8 relay using this IC.
So ULN is better choice if you have more than 3 relay. ( Simple design of 
circuit & PCB as well ! )
107
7 Segment Display
INTRODUCTION
For the seven segment display you can use the LT-541 or LSD5061-
11 chip (etc). Each of the segments of the display is connected to 
a pin on the 8051 (the schematic shows how to do this). In order to 
light up a segment on the the pin must be set to 0V. To turn a 
segment off the corresponding pin must be set to 5V. This is simply 
done by setting the pins on the 8051 to '1' or '0'.
LED displays are
– Power-hungry (10ma per LED)
– Pin-hungry (8 pins per 7-seg display)
But they are cheaper than LCD display
7-SEG Display are available in two types: Common anode (CA) & 
common cathode (CC), but command anode display are most 
suitable for interfacing with 8051 since 8051 port pins can sink
current better than sourcing it.
108
7 Segment Display
dp = decimal point
109
The 7-segment Display (Cont.)
• 7-segment displays come in 2 configurations:
Common Anode (CA) Common Cathode (CC)
• As we have seen, it would be preferable to connect 
the cathode of each diode to the output pin.
• Therefore, the common anode variety would be 
better for our interfacing needs.
110
Interfacing a 7-segment display 
• Also, as seen with interfacing the LED, a resistor will be 
needed to control the current flowing through the diode.
– This leaves two possibilities:
– Case 2 would be more appropriate as case 1 will produce 
different brightness depending on the number of LEDs turned 
on.
111
Use of current buffer
• Interfacing to a DIP switch and 7-segment display
• Output a ‘1’ to ON a segment
• We can use 74244 to common cathode 7_seg 
112
BCD to 7-Seg lookup table
mov p3,#0fh
mov a,p3
anl a,0fh
get_code: mov DPTR, #7s_tab
movc A, @A+DPRT
mov p1,a
7s_tab: db 3fh,30h,5bh,4fh,66h
db 6dh,7dh,07h,7fh,6fh
END
a
b
ce
f
d
f
e
a
b
e
g
d
a
b
c
g
d
b
c
g
f
a
c
g
f
d
a
ce
g
f
d
a
b
c
a
b
ce
g
f
d
a
b
c
f
g
d
BCD p g f e d c b a
7_seg
he
x
0000
0001 0 0 1 1 0 0 0 0 30
0010 0 1 0 1 1 0 1 1 5b
0011 0 1 0 0 1 1 1 1 4f
0100 0 1 1 0 0 1 1 0 66
0101 0 1 1 0 1 1 0 1 6d
0110 0 1 1 1 1 1 0 1 7d
0111 0 0 0 0 0 1 1 1 07
1000 0 1 1 1 1 1 1 1 7f
1001 0 1 1 0 1 1 1 1 6f
0 0 1 1 1 1 1 1 3f
113
Creating Digit Pattern
with 7-segment LED Display
For displaying Digit say 7 we need to light segments: a ,b, c. Since 
we are using Common anode display , to do so we have to to provide 
Logic 0 (0 v) at anode of these segments.
So need to clear pins: P1.0 ,P1.1,P1.2. that is 1 1 1 1 1 0 0 0 Î F8h .
Connection
Hex Code
You can also do this for some characters like A ,E .. but not for D or B because it will be 
same as that of 0 & 8 . So this is one of limitation of 7-seg display.
Since we can Enable only one 7-seg display at a time ,we need to scan these display at 
fast rate .The scanning frequency should be high enough to be flicker-free. At least 30HZ 
.Therefore – time one digit is ON is 1/30 seconds
114
INTERFACING TO LED DISPLAY (1/2)
Note that we are using Common Anode display. so the common Anode pin 
is tied to 5v .The cathode pins are connected to port 1 through 330 Ohm 
resistance (current limiting).
115
INTERFACING TO LED DISPLAY (2/2)
Connection: a:h to port p1.0:p1.7 , D0:D1 to p3.0:p3.1.
To Display: Consider example of vending machine where we want to 
display number of soft drink bottles on display entered by customer. 
Suppose he enter 3 (03) bottles then we will use lookup table to
see DIGIT PATTERN of these keys.
So DIGI[1]=C0 (hex code for '0') &
DIGI[2]=B0(hex code for '3').
Algorithm
start : Disable [D0:D1]
again : Enable D0
[a:h] - pattern for Digit1
Delay
Disable D0. 
Enable D1
[a:h] - pattern for Digit2
Delay
Goto again
116
Hiển thị quét LED với 4 LED 7 đoạn
117
Chu ́ ý với hiển thi ̣ dồn kênh
Với hiển thị LED dồn kênh:
• Ở mỗi thời điểm chỉ có một hiển thị LED 7 đoạn được cho
phép (qua các khóa điện tử BJT).
• Các ngõ vào a-h nối chung với nhau cho tất cả các LED 7 
đoạn.
• Tổng số chân cổng cần sử dụng là 8 + số ký số (digit), với thí
dụ hình 5.80 thì tổng số chân là 8 + 4 = 12.
• Tần số quét phải đủ cao để tránh tình trạng thấy LED nhấp
nháy:
• tối thiểu 40Hz
• thời gian cho 1 digit sáng là 1/40 giây.
• tần số quét cao hơn thì sẽ giảm sự nhấp nháy
118
TD: Mạch hiển thị LED 7 đoạn của
www.MightyMicons.com
119
Mạch này dùng LED 7 đoạn loại
nào? (CA hay CC)
120
Một sô ́ thí dụ với LED và phím nhấn
121
Basic Output Techniques with LEDs
P1.7
P1.6
P1.5
P1.4
P1.3
P1.2
P1.1
P1.0
+5V
8051
EA VCC
Reset
XTAL1
XTAL2
VSS
122
Example 3: Light-up LEDs
All LEDs ON
All LEDs OFF
1st: LEDs
123
Program Listing for Example 3
ORG 0000H
CLR A
LOOP: MOV P1, A
CPL A
ACALL DELAY
AJMP LOOP
DELAY: MOV R6, #250
DL1: MOV R7, #200
DL2: DJNZ R7, DL2
DJNZ R6, DL1
RET
END
Start
Set A = 00
Move the content 
of A to P1
Delay for 0.1s
Invert the 
content of A
Assume 12MHz clock, determine the delay time.
Time delay, Tex1 = 1 + [ ( 1+ 200 * 2 ) + 2 ] * 250 + 2 = 100,753 MachineCycle
124
Example 4: Lighting Sequence
ORG 0000H
START: MOV R1, #07H
MOV A, #11111110B
LEFT: MOV P1, A
ACALL DELAY
RL A
DJNZ R1, LEFT
;
MOV R1, #07H
MOV A, #01111111B
RIGHT: MOV P1, A
ACALL DELAY
RR A
DJNZ R1, RIGHT
AJMP START
;
DELAY: .
125
Example 5: Use a Look-up Table
126
Program Listing of Example 5
ORG 0000H
START: MOV R0, #OK−LUT+1 ; length of table
MOV DPTR, #LUT ; code start address
MOV R1, #00H
LOOP: MOV A, R1
MOVC A, @A+DPTR
MOV P1, A
ACALL DELAY
INC R1 ; point to next data of table
DJNZ R0, LOOP ; finish ?
AJMP START
;
DELAY:
; do not modify the value in register R0, & R1
; DELAY = 1 + (100751 + 2 ) * 2 + 2 = 201,509 Machine Cycle !
MOV R5, #2
DL1: MOV R6, #250
DL2: MOV R7, #200
DL3: DJNZ R7, DL3
DJNZ R6, DL2
DJNZ R5, DL1
RET
;
LUT: DB 01111110B
DB 00111100B
DB 00011000B
DB 00000000B
DB 00011000B
DB 00111100B
DB 01111110B
DB 11111111B
;
DB 01111110B 
DB 00111100B
DB 00011000B
DB 00000000B
DB 00011000B
DB 00111100B
DB 01111110B
DB 11111111B
;
DB 00000000B
DB 11111111B
DB 00000000B
OK: DB 11111111B
END
127
Example 6: Basic Input Technique
P1.7
P1.6
P1.5
P1.4
P1.3
P1.2
P1.1
P1.0
+5V
8051
EA VCC
Reset
XTAL1
XTAL2
VSS
P3.7
P3.6
P3.5
P3.4
SW1
SW2
SW3
SW4
128
Example 6
When SW1 Closed
When SW3 Closed When SW4 Closed
When SW2 Closed
Priority: SW1 SW2 SW3 SW4
129
Flow Chart of Example 6
Start
Initialization
Set P3 as input port
Read SW1∼SW4 status
SW1 closed?
SW2 closed?
SW3closed?
SW4 closed?
SW1 Handler
Y
SW2 Handler
Y
SW3 Handler
Y
SW4 Handler
Y
2
1
21
N
N
N
N
130
Program Listing of Example 6
ORG 0000H
MOV R1, #00000000B
MOV R2, #01010101B
MOV R3, #00001111B
MOV R4, #11110000B
;
TEST: ORL P3, #0FFH 
; P3 is configured as input port!
JNB P3.7, CASE1
JNB P3.6, CASE2
JNB P3.5, CASE3
JNB P3.4, CASE4
AJMP TEST
;
CASE1: MOV A, R1
MOV P1, A
ACALL DELAY
XRL A, #11111111B
MOV P1, A
AJMP TEST
;
CASE2: MOV A, R2
MOV P1, A
ACALL DELAY
XRL A, #10101010B
MOV P1, A
AJMP TEST
CASE3: MOV A, R3
MOV P1, A
ACALL DELAY
XRL A, #11110000B
MOV P1, A
AJMP TEST
CASE4: MOV A, R4
MOV P1, A
ACALL DELAY
XRL A, #00001111B
MOV P1, A
AJMP TEST
DELAY: ..
;inside DELAY, don’t modify A, R1, R2, R3 & R4.
RET
END
131
Ex7: 7-Segment LED Numeric Display
R3 is used as a counter, write a 8051 assembly language program using 
look-up table method, to display the value in R3 to a 7-segment display
Eprom Version
Of 8051
132
Program Listing of Example 7
ORG 0000H
MOV R3, #00H
LOOP: MOV DPTR, #TABLE
MOV A, R3
MOVC A, @A+DPTR
;
; Display numbers on 7-segment display
MOV P1, A
ACALL DELAY
;
; Increase R3 by 1 and loop back
MOV A, R3
ADD A, #1
DA A
ANL A, #0FH 
; take the lower nibble only
; now A has value between 0 to 9
MOV R3, A ;update R3
AJMP LOOP
;
DELAY: ..
RET
TABLE: DB 11000000B ; 0
DB 11111001B ; 1
DB 10100100B ; 2
DB 10110000B ; 3
DB 10011001B ; 4
DB 10010010B ; 5
DB 10000010B ; 6
DB 11111000B ; 7
DB 10000000B ; 8
DB 10010000B ; 9
;
END
133
Một số thí dụ các mạch giao tiếp IO
134
8031/8051 với LED và loa
135
Giao tiếp với động cơ DC
136
Giao tiếp với động cơ bước
137
Giao tiếp với DAC
138
Mạch loa đi kèm với DAC
139
Mạch điều khiển động cơ DC đi 
kèm DAC
140
Giao tiếp với ADC

File đính kèm:

  • pdfbai_giang_vi_xu_ly_chuong_5_thiet_ke_he_vi_xu_ly_phan_1_bui.pdf