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

Tóm tắt Bài giảng Vi xử lý - Chương 5: Thiết kế hệ vi xử lý - Bùi Minh Thành: ...fied • 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 In... 76 8255 77 8255 78 8255 79 Mode 0 • Provides simple input and output operations for each of the three ports. – No “handshaking” is required, data is simply written to or read from a specified port. – Two 8-bit ports and two 4-bit ports. – Any port can be input or output. – Outputs ...1?. 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 ...

pdf140 trang | Chia sẻ: havih72 | Lượt xem: 73 | Lượt tải: 0download
Nội dung tài liệu Bài giảng Vi xử lý - Chương 5: Thiết kế hệ vi xử lý - Bùi Minh Thành, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
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 as INT0, 
then the MCS-51 will think that there 
were several “events” and respond 
several times.
• The bouncing of the switch may last for 
several milliseconds.
– Given that the MCS-51 operates at 
microsecond speed, a short ISR may 
execute several times in response to the 
above described bounciness
60
Hardware Solution
• The simplest hardware solution uses an RC time constant to 
suppress the bounce. The time constant has to be larger than 
the switch bounce and is typically 0.1 seconds.
• As long as capacitor voltage does not exceed a threshold 
value, the output signal will be continued to be recognized as a
logic 1. 
• The buffer after the switch produces a sharp high-to-low 
transition. Vcc
OUT
61
Hardware Solution
62
Software Solution
• It is also possible to counter the bouncing 
problem using software.
• The easies way is the wait-and-see 
technique
– When the input drops, an “appropriate” delay 
is executed (10 ms), then the value of the line 
is checked again to make sure the line has 
stopped bouncing
63
Mở rộng xuất nhập song song với
8255 PPI
64
65
8255
• 8051 has limited number of I/O ports
• one solution is to add parallel interface 
chip(s)
• 8255 is a Programmable Peripheral 
Interface PPI
• Add it to 8051 to expand number of 
parallel ports
• 8051 I/O port does not have 
handshaking capability
• 8255 can add handshaking capability to 
8051
66
8255
• Programmable Peripheral Interface (PPI)
– Has 3 8_bit ports A, B and C
– Port C can be used as two 4 bit ports CL and Ch
– Two address lines A0, A1 and a Chip select CS
– 8255 can be configured by writing a control-word in 
CR register
67
68
69
8255
70
8255 – Logic điều khiển đọc/ghi
71
72
73
8255
74
75
76
8255
77
8255
78
8255
79
Mode 0
• Provides simple input and output 
operations for each of the three ports. 
– No “handshaking” is required, data is simply 
written to or read from a specified port.
– Two 8-bit ports and two 4-bit ports.
– Any port can be input or output.
– Outputs are latched.
– Inputs are not latched
80
Mode 1
• Mode 1 Basic functional Definitions:
– Two Groups (Group A and Group B).
– Each group has one 8-bit data port and one 
4-bit control/data port.
– The 8-bit data port can be either input or 
output. Both inputs and outputs are latched.
– The 4-bit port is used for control and status 
of the 8-bit data port.
81
8255 mode 1 (output)
82
Mode 1 – Control Signals
• Output Control Signal Definition
– OBF (Output Buffer Full F/F). (C7 for A, C1 for B)
• The OBF output will go “low” to indicate that the CPU has 
written data out to the specified port.
– A signal to the device that there is data to be read.
– ACK (Acknowledge Input). (C6 for A, C2 for B)
• A “low” on this input informs the 8255 that the data from Port 
A or Port B has been accepted. 
– A response from the peripheral device indicating that it has 
read the data.
– INTR (Interrupt Request). (C3 for A, C0 for B)
• A “high” on this output can be used to interrupt the CPU 
when an output device has accepted data transmitted by the 
CPU.
83
Timing diagram for 
mode1(output)
84
8255 mode 1 (input)
85
Mode 1 – Control Signals
• Input Control Signal Definition
– STB (Strobe Input). (C4 for A, C2 for B)
• A “low” on this input loads data into the input latch.
– IBF (Input Buffer Full F/F) (C5 for A, C1 for B)
• A “high” on this output indicates that the data has been 
loaded into the input latch; in essence, an acknowledgement 
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_bui_minh_th.pdf