Giáo trình môn học Vi điều khiển

Vi điều khiển AT89C51

AT89C51 là vi điều khiển do Atmel sản xuất, chế tạo theo công nghệ CMOS

có các đặc tính như sau:

- 4 KB PEROM (Flash Programmable and Erasable Read Only Memory), có khả

năng tới 1000 chu kỳ ghi xoá

- Tần số hoạt động từ: 0Hz đến 24 MHz

- 3 mức khóa bộ nhớ lập trình

- 128 Byte RAM nội.

- 4 Port xuất /nhập I/O 8 bit.

- 2 bộ Timer/counter 16 Bit.

- 6 nguồn ngắt.

- Giao tiếp nối tiếp điều khiển bằng phần cứng.

- 64 KB vùng nhớ mã ngoài

- 64 KB vùng nhớ dữ liệu ngoài.

- Cho phép xử lý bit.

- 210 vị trí nhớ có thể định vị bit.

- 4 chu kỳ máy (4 µs đối với thạch anh 12MHz) cho hoạt động nhân hoặc chia.

- Có các chế độ nghỉ (Low-power Idle) và chế độ nguồn giảm (Power-down).

Ngoải ra, một số IC khác của họ MCS-51 có thêm bộ định thời thứ 3 và 256

byte RAM nội.

pdf 194 trang kimcuc 3040
Bạn đang xem 20 trang mẫu của tài liệu "Giáo trình môn học Vi điều khiển", để 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: Giáo trình môn học Vi điều khiển

Giáo trình môn học Vi điều khiển
Sưu tầm bởi: www.daihoc.com.vn 
BỘ GIÁO DỤC & ĐÀO TẠO 
TRƯỜNG ĐẠI HỌC KỸ THUẬT CÔNG NGHỆ 
THÀNH PHỐ HỒ CHÍ MINH 
KHOA ĐIỆN – ĐIỆN TỬ 
--- oOo --- 
GIÁO TRÌNH 
VI ĐIỀU KHIỂN 
Tác giả: ThS. PHẠM HÙNG KIM KHÁNH 
03/2008 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình vi xử lý 
 i 
LỜI NÓI ĐẦU 
Giáo trình Vi điều khiển được biên soạn nhằm cung cấp cho sinh viên kiến thức 
về họ vi điều khiển MCS-51, cách thức lập trình điều khiển, nạp chương trình 
và thiết kế phần cứng điều khiển thiết bị. 
Giáo trình được sử dụng cho khóa học 45 tiết dành cho sinh viên hệ đại học 
Khoa Điện Điện tử trường Đại học Kỹ thuật Công nghệ TPHCM. 
Bố cục giáo trình gồm 4 chương dựa theo đề cương môn học Kỹ thuật Vi điều 
khiển dành cho sinh viên ngành Điện Tử Viễn Thông: 
Chương 1. Tổng quan về họ vi điều khiển MCS-51 
Chương 2. Lập trình hợp ngữ 
Chương 3. Các hoạt động của họ vi điều khiển MCS-51 
Chương 4. Các ứng dụng 
Phụ lục 1: Tóm tắt tập lệnh 
Phụ lục 2: Mô tả tập lệnh 
PHẠM HÙNG KIM KHÁNH 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình vi xử lý 
 ii 
MỤC LỤC 
Chương 1: Tổng quan về họ MCS-51 .......................................................................... 1 
1. Giới thiệu ..................................................................................................................... 1 
2. Vi điều khiển AT89C51 .............................................................................................. 1 
 2.1. Sơ đồ .............................................................................................................. 2 
 2.2. Định thì chu kỳ máy ...................................................................................... 6 
 2.3. Tổ chức bộ nhớ .............................................................................................. 8 
 2.4. Các thanh ghi chức năng đặc biệt (SFR – Special Function Registers) ...... 17 
 2.5. Cấu trúc port ................................................................................................ 21 
 2.6. Hoạt động Reset .......................................................................................... 22 
 2.7. Các vấn đề khác ........................................................................................... 23 
Bài tập chương 1 ............................................................................................................ 34 
Chương 2: Lập trình hợp ngữ .................................................................................... 35 
1. Các phương pháp định địa chỉ ................................................................................... 35 
2. Các vấn đề liên quan khi lập trình hợp ngữ ............................................................... 36 
 2.1. Cú pháp lệnh ................................................................................................ 36 
 2.2. Khai báo dữ liệu .......................................................................................... 37 
 2.3. Các toán tử ................................................................................................... 38 
 2.4. Cấu trúc chương trình .................................................................................. 39 
3. Tập lệnh ..................................................................................................................... 41 
 3.1. Nhóm lệnh chuyển dữ liệu .......................................................................... 41 
 3.2. Nhóm lệnh xử lý bit ..................................................................................... 46 
 3.3. Nhóm lệnh chuyển điều khiển ..................................................................... 47 
 3.4. Nhóm lệnh logic .......................................................................................... 51 
 3.5. Nhóm lệnh số học ........................................................................................ 53 
Bài tập chương 2 ............................................................................................................ 56 
Chương 3: Các hoạt động ........................................................................................... 57 
1. Hoạt động định thời (Timer / Counter) ..................................................................... 57 
 1.1. Giới thiệu ..................................................................................................... 57 
 1.2. Hoạt động Timer / Counter ......................................................................... 57 
 1.3. Các thanh ghi điều khiển hoạt động ............................................................ 58 
 1.3.1. Thanh ghi điều khiển timer (Timer/Counter Control Register) ...... 58 
 1.3.2. Thanh ghi chế độ timer (TMOD – Timer/Counter Mode) ............. 59 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình vi xử lý 
 iii 
 1.4. Các chế độ hoạt động .................................................................................. 59 
 1.4.1. Chế độ 0 .......................................................................................... 60 
 1.4.2. Chế độ 1 .......................................................................................... 60 
 1.4.3. Chế độ 2 .......................................................................................... 61 
 1.4.4. Chế độ 3 .......................................................................................... 61 
 1.5. Timer 2 ........................................................................................................ 62 
 1.5.1. Các thanh ghi điều khiển Timer 2 .................................................. 62 
 1.5.2. Chế độ capture ................................................................................ 64 
 1.5.3. Chế độ tự động nạp lại .................................................................... 64 
 1.5.4. Chế độ tạo xung clock .................................................................... 65 
 1.5.5. Chế độ tạo tốc độ baud ................................................................... 66 
 1.6. Các ví dụ ...................................................................................................... 67 
2. Cổng nối tiếp (Serial port) ......................................................................................... 71 
 2.1. Các thanh ghi điều khiển hoạt động ............................................................ 72 
 2.1.1. Thanh ghi SCON (Serial port controller) ....................................... 72 
 2.1.2. Thanh ghi BDRCON (Baud Rate Control Register) ...................... 73 
 2.2. Tạo tốc độ baud ........................................................................................... 73 
 2.2.1. Tạo tốc độ baud bằng Timer 1 ........................................................ 74 
 2.2.2. Tạo tốc độ baud bằng Timer 2 ........................................................ 76 
 2.2.3. Bộ tạo tốc độ baud nội (Internal Baud Rate Generator) ................. 77 
 2.3. Truyền thông đa xử lý ................................................................................. 77 
 2.4. Nhận dạng địa chỉ tự động .......................................................................... 78 
 2.5. Kiểm tra lỗi khung ....................................................................................... 79 
 2.6. Các ví dụ ...................................................................................................... 79 
3. Ngắt (Interrupt) .......................................................................................................... 81 
 3.1. Các thanh ghi điều khiển hoạt động ............................................................ 82 
 3.1.1. Thanh ghi IE (Interrupt Enable) ..................................................... 82 
 3.1.2. Thanh ghi IP (Interrupt Priority) .................................................... 82 
 3.1.3. Thanh ghi TCON (Timer/Counter Control) ................................... 83 
 3.2. Xử lý ngắt .................................................................................................... 84 
 3.3. Ngắt do bộ định thời .................................................................................... 86 
 3.4. Ngắt do cổng nối tiếp .................................................................................. 89 
 3.5. Ngắt ngoài ................................................................................................... 91 
Bài tập chương 3 ............................................................................................................ 94 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình vi xử lý 
 iv 
Chương 4: Các ứng dụng dựa trên họ vi điều khiển MCS-51................................. 95 
1. Điều khiển Led đơn ................................................................................................... 95 
2. Điều khiển Led 7 đoạn .............................................................................................. 98 
 2.1. Cấu trúc và bảng mã hiển thị dữ liệu trên Led 7 đoạn ................................ 98 
 2.2. Các phương pháp hiển thị dữ liệu ............................................................. 100 
 2.2.1. Phương pháp quét ......................................................................... 100 
 2.2.2. Phương pháp chốt ......................................................................... 104 
3. Điều khiển ma trận Led ........................................................................................... 107 
4. Điều khiển động cơ bước......................................................................................... 112 
5. Điều khiển LCD (Liquid Crystal Display) .............................................................. 115 
6. Giao tiếp với PPI8255 ............................................................................................. 129 
Bài tập chương 4 .......................................................................................................... 135 
Phụ lục 1: Soạn thảo và nạp chương trình .............................................................. 136 
Phụ lục 2: Mô phỏng bằng Proteus .......................................................................... 181 
Phụ lục 3: Tóm tắt tập lệnh ...................................................................................... 191 
Phụ lục 4: Mô tả tập lệnh .......................................................................................... 195 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình Vi điều khiển Tổng quan về vi điều khiển MCS-51 
Phạm Hùng Kim Khánh Trang 1 
Chương 1: TỔNG QUAN VỀ VI ĐIỀU 
KHIỂN MCS-51 
Chương này giới thiệu tổng quan về họ vi điều khiển MCS-51(chủ yếu trên 
AT89C51): cấu trúc phần cứng, sơ đồ chân, các thanh ghi, đặc tính lập trình và các 
đặc tính về điện. 
1. Giới thiệu 
Họ vi điều khiển MCS-51 do Intel sản xuất đầu tiên vào năm 1980 là các IC 
thiết kế cho các ứng dụng hướng điều khiển. Các IC này chính là một hệ thống vi xử 
lý hoàn chỉnh bao gồm các các thành phần của hệ vi xử lý: CPU, bộ nhớ, các mạch 
giao tiếp, điều khiển ngắt. 
MCS-51 là họ vi điều khiển sử dụng cơ chế CISC (Complex Instruction Set 
Computer), có độ dài và thời gian thực thi của các lệnh khác nhau. Tập lệnh cung cấp 
cho MCS-51 có các lệnh dùng cho điều khiển xuất / nhập tác động đến từng bit. 
MCS-51 bao gồm nhiều vi điều khiển khác nhau, bộ vi điều khiển đầu tiên là 
8051 có 4KB ROM, 128 byte RAM và 8031, không có ROM nội, phải sử dụng bộ 
nhớ ngoài. Sau này, các nhà sản xuất khác như Siemens, Fujitsu,  cũng được cấp 
phép làm nhà cung cấp thứ hai. 
MCS-51 bao gồm nhiều phiên bản khác nhau, mỗi phiên bản sau tăng thêm 
một số thanh ghi điều khiển hoạt động của MCS-51. 
2. Vi điều khiển AT89C51 
AT89C51 là vi điều khiển do Atmel sản xuất, chế tạo theo công nghệ CMOS 
có các đặc tính như sau: 
- 4 KB PEROM (Flash Programmable and Erasable Read Only Memory), có khả 
năng tới 1000 chu kỳ ghi xoá 
- Tần số hoạt động từ: 0Hz đến 24 MHz 
- 3 mức khóa bộ nhớ lập trình 
- 128 Byte RAM nội. 
- 4 Port xuất /nhập I/O 8 bit. 
- 2 bộ Timer/counter 16 Bit. 
- 6 nguồn ngắt. 
- Giao tiếp nối tiếp điều khiển bằng phần cứng. 
- 64 KB vùng nhớ mã ngoài 
- 64 KB vùng nhớ dữ liệu ngoài. 
- Cho phép xử lý bit. 
- 210 vị trí nhớ có thể định vị bit. 
- 4 chu kỳ máy (4 µs đối với thạch anh 12MHz) cho hoạt động nhân hoặc chia. 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình Vi điều khiển Tổng quan về vi điều khiển MCS-51 
Phạm Hùng Kim Khánh Trang 2 
- Có các chế độ nghỉ (Low-power Idle) và chế độ nguồn giảm (Power-down). 
Ngoải ra, một số IC khác của họ MCS-51 có thêm bộ định thời thứ 3 và 256 
byte RAM nội. 
2.1. Sơ đồ 
Hình 1.1 – Sơ đồ khối của AT89C51 
P0.0 – P0.7 P2.0 – P2.7 
PORT 0 DRIVERS PORT 2 DRIVERS
RAM 
RA
M
 A
D
D
R 
RE
G
IS
TE
R 
PORT O 
LATCH 
PORT 2 
LATCH 
ROM 
ACC 
B 
REGISTER 
TMP2 
ALU
PSW
STACK 
POINTER
PROGRAM 
ADDR REGISTER
BUFFER 
PC 
INCREAMENTER
PROGRAM
COUNTER 
DPTR 
PCON
TMP1 
SCON TMOD TCON
T2CON* TH0 TL0
TL1
TH1
TH2* TL2*  RCAP2H*
RCAP2L* SBUF IE  IP 
IINTERRUPT SERIAL PORT AND 
TIMER BLOCKS 
VCC 
VSS 
IN
ST
RU
CT
IO
N
RE
G
IS
TE
R 
TIMING AND 
CONTROL 
PSEN 
ALE 
EA 
RST 
PORT 1 LATCH PORT 3 LATCH 
OSC 
PORT 1 
DRIVER
PORT 3 
DRIVER
XTAL 2 XTAL 1 
P1.0 – P1.7 P3.0 – P3.7 
Note: * for Timer 2 only 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình Vi điều khiển Tổng quan về vi điều khiển MCS-51 
Phạm Hùng Kim Khánh Trang 3 
AT89C51 gồm có 40 chân, mô tả như sau: 
Hình 1.2 – Sơ đồ chân của AT89C51 
™ Port 0: 
Port 0 là port có 2 chức năng ở các chân 32 – 39 của AT89C51: 
- Chức năng IO (xuất / nhập): dùng cho các thiết kế nhỏ. Tuy nhiên, khi dùng 
chức năng này thì Port 0 phải dùng thêm các điện trở kéo lên (pull-up), giá trị 
của điện trở phụ thuộc vào thành phần kết nối với Port. 
Khi dùng làm ngõ ra, Port 0 có thể kéo được 8 ngõ TTL. 
Khi dùng làm ngõ vào, Port 0 phải được set mức logic 1 trước đó. 
- Chức năng địa chỉ / dữ liệu đa hợp: khi dùng các thiết kế lớn, đòi hỏi phải sử 
dụng bộ nhớ ngoài thì Port 0 vừa là bus dữ liệu (8 bit) vừa là bus địa chỉ (8 bit 
thấp). 
Ngoài ra khi lập trình cho AT89C51, Port 0 còn dùng để nhận mã khi lập trình 
và xuất mà khi kiểm tra (quá trình kiểm tra đòi hỏi phải có điện trở kéo lên). 
RST
9
XTAL2
18 XTAL1
19
G
N
D
20
PSEN
29ALE/PROG
30
EA/VPP
31
V
C
C
40
P1.0
1
P1.1
2
P1.2
3
P1.34
P1.4
5
P1.5
6
P1.6
7
P1.7
8
P2.0/A8
21
P2.1/A9
22
P2.2/A10
23
P2.3/A11
24
P2.4/A12
25
P2.5/A13
26
P2.6/A14
27
P2.7/A15
28
P3.0/RXD
10
P3.1/TXD
11
P3.2/INT0
12
P3.3/INT1 13
P3.4/T0
14
P3.5/T1
15
P3.6/WR
16
P3.7/RD
17
P0.0/AD0
39
P0.1/AD1
38
P0.2/AD2
37
P0.3/AD3
36
P0.4/AD4
35
P0.5/AD5
34
P0.6/AD6
33
P0.7/AD7
32
AT89C51
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình Vi điều khiển Tổng quan về vi điều khiển MCS-51 
Phạm Hùng Kim Khánh Trang 4 
™ Port 1: 
Port1 (chân 1 – 8) chỉ có một chức năng là IO, không dùng cho mục đích khác 
(chỉ trong 8032/8052/8952 thì dùng thêm P1.0 và P1.1 cho bộ định thời thứ 3). Tại 
Port 1 đã có điện trở kéo lên nên không cần thêm điện trở ngoài. 
Port 1 có khả năng kéo được 4 ngõ TTL  ... RY WITH THE INVERSE OF OV. 
36.1. ORL C,bit 
Bytes: 2 
Cycles: 2 
Encoding: 
0 1 1 1 0 0 1 0 bit address
Operation: ORL 
(C) ← (C) ∨ (bit) 
36.2. ORL C,/bit 
Bytes: 2 
Cycles: 2 
Encoding: 
1 0 1 0 0 0 0 0 bit address
Operation: ORL 
(C) ← (C) ∨ (bit) 
37. POP direct 
Function: Pop from stack. 
Description: The contents of the internal RAM location addressed by the Stack 
Pointer is read, and the Stack Pointer is decremented by one. The value read is then 
transferred to the directly addressed byte indicated. No flags are affected. 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình Vi điều khiển Phụ lục 4 – Mô tả tập lệnh 
Phạm Hùng Kim Khánh Trang 225 
Example: The Stack Pointer originally contains the value 32H, and internal RAM 
locations 30H through 32H contain the values 20H, 23H, and 01H, respectively. The 
following instruction sequence, 
POP DPH 
POP DPL 
leaves the Stack Pointer equal to the value 30H and sets the Data Pointer to 0123H. At 
this point, the following instruction, 
POP SP 
leaves the Stack Pointer set to 20H. In this special case, the Stack Pointer was 
decremented to 2FH before being loaded with the value popped (20H). 
Bytes: 2 
Cycles: 2 
Encoding: 
1 1 0 1 0 0 0 0 direct address
Operation: POP 
(direct) ← ((SP)) 
(SP) ← (SP) - 1 
38. PUSH direct 
Function: Push onto stack 
Description: The Stack Pointer is incremented by one. The contents of the indicated 
variable is then copied into the internal RAM location addressed by the Stack Pointer. 
Otherwise no flags are affected. 
Example: On entering an interrupt routine, the Stack Pointer contains 09H. The Data 
Pointer holds the value 0123H. The following instruction sequence, 
PUSH DPL 
PUSH DPH 
leaves the Stack Pointer set to 0BH and stores 23H and 01H in internal RAM 
locations 0AH and 0BH, respectively. 
Bytes: 2 
Cycles: 2 
Encoding: 
1 1 0 0 0 0 0 0 direct address
Operation: PUSH 
(SP) ← (SP) + 1 
((SP)) ← (direct) 
39. RET 
Function: Return from subroutine 
Description: RET pops the high- and low-order bytes of the PC successively from the 
stack, decrementing the Stack Pointer by two. Program execution continues at the 
resulting address, generally the instruction immediately following an ACALL or 
LCALL. No flags are affected. 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình Vi điều khiển Phụ lục 4 – Mô tả tập lệnh 
Phạm Hùng Kim Khánh Trang 226 
Example: The Stack Pointer originally contains the value 0BH. Internal RAM 
locations 0AH and 0BH contain the values 23H and 01H, respectively. The following 
instruction, 
RET 
leaves the Stack Pointer equal to the value 09H. Program execution continues at 
location 0123H. 
Bytes: 1 
Cycles: 2 
Encoding: 
0 0 1 0 0 0 1 0
Operation: RET 
(PC15-8) ← ((SP)) 
(SP) ← (SP) - 1 
(PC7-0) ← ((SP)) 
(SP) ← (SP) - 1 
40. RETI 
Function: Return from interrupt 
Description: RETI pops the high- and low-order bytes of the PC successively from 
the stack and restores the interrupt logic to accept additional interrupts at the same 
priority level as the one just processed. The Stack Pointer is left decremented by two. 
No other registers are affected; the PSW is not automatically restored to its pre-
interrupt status. Program execution continues at the resulting address, which is 
generally the instruction immediately after the point at which the interrupt request was 
detected. If a lower- or same-level interrupt was pending when the RETI instruction is 
executed, that one instruction is executed before the pending interrupt is processed. 
Example: The Stack Pointer originally contains the value 0BH. An interrupt was 
detected during the instruction ending at location 0122H. Internal RAM locations 
0AH and 0BH contain the values 23H and 01H, respectively. The following 
instruction, 
RETI 
leaves the Stack Pointer equal to 09H and returns program execution to location 
0123H. 
Bytes: 1 
Cycles: 2 
Encoding: 
0 0 1 1 0 0 1 0
Operation: RETI 
(PC15-8) ← ((SP)) 
(SP) ← (SP) - 1 
(PC7-0) ← ((SP)) 
(SP) ← (SP) - 1 
41. RL A 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình Vi điều khiển Phụ lục 4 – Mô tả tập lệnh 
Phạm Hùng Kim Khánh Trang 227 
Function: Rotate Accumulator Left 
Description: The eight bits in the Accumulator are rotated one bit to the left. Bit 7 is 
rotated into the bit 0 position. No flags are affected. 
Example: The Accumulator holds the value 0C5H (11000101B). The following 
instruction, 
RL A 
leaves the Accumulator holding the value 8BH (10001011B) with the carry 
unaffected. 
Bytes: 1 
Cycles: 1 
Encoding: 
0 0 1 0 0 0 1 1
Operation: RL 
(An + 1) ← (An) n = 0 - 6 
(A0) ← (A7) 
42. RLC A 
Function: Rotate Accumulator Left through the Carry flag 
Description: The eight bits in the Accumulator and the carry flag are together rotated 
one bit to the left. Bit 7 moves into the carry flag; the original state of the carry flag 
moves into the bit 0 position. No other flags are affected. 
Example: The Accumulator holds the value 0C5H(11000101B), and the carry is zero. 
The following instruction, 
RLC A 
leaves the Accumulator holding the value 8BH (10001010B) with the carry set. 
Bytes: 1 
Cycles: 1 
Encoding: 
0 0 1 1 0 0 1 1
Operation: RLC 
(An + 1) ← (An) n = 0 - 6 
(A0) ← (C) 
(C) ← (A7) 
43. RR A 
Function: Rotate Accumulator Right 
Description: The eight bits in the Accumulator are rotated one bit to the right. Bit 0 is 
rotated into the bit 7 position. No flags are affected. 
Example: The Accumulator holds the value 0C5H (11000101B). The following 
instruction, 
RR A 
leaves the Accumulator holding the value 0E2H (11100010B) with the carry 
unaffected. 
Bytes: 1 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình Vi điều khiển Phụ lục 4 – Mô tả tập lệnh 
Phạm Hùng Kim Khánh Trang 228 
Cycles: 1 
Encoding: 
0 0 0 0 0 0 1 1
Operation: RR 
(An) ← (An + 1) n = 0 - 6 
(A7) ← (A0) 
44. RRC A 
Function: Rotate Accumulator Right through Carry flag 
Description: The eight bits in the Accumulator and the carry flag are together rotated 
one bit to the right. Bit 0 moves into the carry flag; the original value of the carry flag 
moves into the bit 7 position. No other flags are affected. 
Example: The Accumulator holds the value 0C5H (11000101B), the carry is zero. 
The following instruction, 
RRC A 
leaves the Accumulator holding the value 62 (01100010B) with the carry set. 
Bytes: 1 
Cycles: 1 
Encoding: 
0 0 0 1 0 0 1 1
Operation: RRC 
(An) ← (An + 1) n = 0 - 6 
(A7) ← (C) 
(C) ← (A0) 
45. SETB 
Function: Set Bit 
Description: SETB sets the indicated bit to one. SETB can operate on the carry flag 
or any directly addressable bit. No other flags are affected. 
Example: The carry flag is cleared. Output Port 1 has been written with the value 34H 
(00110100B). The following instructions, 
SETB C 
SETB P1.0 
sets the carry flag to 1 and changes the data output on Port 1 to 35H (00110101B). 
45.1. SETB C 
Bytes: 1 
Cycles: 1 
Encoding: 
1 1 0 1 0 0 1 1
Operation: SETB 
(C) ← 1 
45.2. SETB bit 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình Vi điều khiển Phụ lục 4 – Mô tả tập lệnh 
Phạm Hùng Kim Khánh Trang 229 
Bytes: 2 
Cycles: 1 
Encoding: 
1 1 0 1 0 0 1 0 bit address
Operation: SETB 
(bit) ← 1 
46. SJMP rel 
Function: Short Jump 
Description: Program control branches unconditionally to the address indicated. The 
branch destination is computed by adding the signed displacement in the second 
instruction byte to the PC, after incrementing the PC twice. Therefore, the range of 
destinations allowed is from 128 bytes preceding this instruction 127 bytes following 
it. 
Example: The label RELADR is assigned to an instruction at program memory 
location 0123H. The following instruction, 
SJMP RELADR 
assembles into location 0100H. After the instruction is executed, the PC contains the 
value 0123H. 
Note: Under the above conditions the instruction following SJMP is at 102H. 
Therefore, the displacement byte of the instruction is the relative offset (0123H-
0102H) = 21H. Put another way, an SJMP with a displacement of 0FEH is a one-
instruction infinite loop. 
Bytes: 2 
Cycles: 2 
Encoding: 
1 0 0 0 0 0 0 0 relative address
Operation: SJMP 
(PC) ← (PC) + 2 
(PC) ← (PC) + rel 
47. SUBB A, 
Function: Subtract with borrow 
Description: SUBB subtracts the indicated variable and the carry flag together from 
the Accumulator, leaving the result in the Accumulator. SUBB sets the carry (borrow) 
flag if a borrow is needed for bit 7 and clears C otherwise. (If C was set before 
executing a SUBB instruction, this indicates that a borrow was needed for the 
previous step in a multiple-precision subtraction, so the carry is subtracted from the 
Accumulator along with the source operand.) AC is set if a borrow is needed for bit 3 
and cleared otherwise. OV is set if a borrow is needed into bit 6, but not into bit 7, or 
into bit 7, but not bit 6. When subtracting signed integers, OV indicates a negative 
number produced when a negative value is subtracted from a positive value, or a 
positive result when a positive number is subtracted from a negative number. The 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình Vi điều khiển Phụ lục 4 – Mô tả tập lệnh 
Phạm Hùng Kim Khánh Trang 230 
source operand allows four addressing modes: register, direct, register-indirect, or 
immediate. 
Example: The Accumulator holds 0C9H (11001001B), register 2 holds 54H 
(01010100B), and the carry flag is set. The instruction, 
SUBB A,R2 
will leave the value 74H (01110100B) in the accumulator, with the carry flag and AC 
cleared but OV set. 
Notice that 0C9H minus 54H is 75H. The difference between this and the above result 
is due to the carry (borrow) flag being set before the operation. If the state of the carry 
is not known before starting a single or multiple-precision subtraction, it should be 
explicitly cleared by CLR C instruction. 
47.1. SUBB A,Rn 
Bytes: 1 
Cycles: 1 
Encoding: 
1 0 0 1 1 r r r
Operation: SUBB 
(A) ← (A) - (C) - (Rn) 
47.2. SUBB A,direct 
Bytes: 2 
Cycles: 1 
Encoding: 
1 0 0 1 0 1 0 1 direct address
Operation: SUBB 
(A) ← (A) - (C) - (direct) 
47.3. SUBB A,@Ri 
Bytes: 1 
Cycles: 1 
Encoding: 
1 0 0 1 0 1 1 i
Operation: SUBB 
(A) ← (A) - (C) - ((Ri)) 
47.4. SUBB A,#data 
Bytes: 2 
Cycles: 1 
Encoding: 
1 0 0 1 0 1 0 0 immediate data
Operation: SUBB 
(A) ← (A) - (C) - #data 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình Vi điều khiển Phụ lục 4 – Mô tả tập lệnh 
Phạm Hùng Kim Khánh Trang 231 
48. SWAP A 
Function: Swap nibbles within the Accumulator 
Description: SWAP A interchanges the low- and high-order nibbles (four-bit fields) 
of the Accumulator (bits 3 through 0 and bits 7 through 4). The operation can also be 
thought of as a 4-bit rotate instruction. No flags are affected. 
Example: The Accumulator holds the value 0C5H (11000101B). The instruction, 
SWAP A 
leaves the Accumulator holding the value 5CH (01011100B). 
Bytes: 1 
Cycles: 1 
Encoding: 
1 1 0 0 0 1 0 0
Operation: SWAP 
(A3-0) ↔ (A7-4) 
49. XCH A, 
Function: Exchange Accumulator with byte variable 
Description: XCH loads the Accumulator with the contents of the indicated variable, 
at the same time writing the original Accumulator contents to the indicated variable. 
The source/destination operand can use register, direct, or register-indirect addressing. 
Example: R0 contains the address 20H. The Accumulator holds the value 3FH 
(0011111lB). Internal RAM location 20H holds the value 75H (01110101B). The 
following instruction, 
XCH A,@R0 
leaves RAM location 20H holding the values 3FH (00111111B) and 75H 
(01110101B) in the accumulator. 
49.1. XCH A,Rn 
Bytes: 1 
Cycles: 1 
Encoding: 
1 1 0 0 1 r r r
Operation: XCH 
(A) ↔ ((Rn) 
49.2. XCH A,direct 
Bytes: 2 
Cycles: 1 
Encoding: 
1 1 0 0 0 1 0 1 direct address
Operation: XCH 
(A) ↔ (direct) 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình Vi điều khiển Phụ lục 4 – Mô tả tập lệnh 
Phạm Hùng Kim Khánh Trang 232 
49.3. XCH A,@Ri 
Bytes: 1 
Cycles: 1 
Encoding: 
1 1 0 0 0 1 1 i
Operation: XCH 
(A) ↔ ((Ri)) 
50. XCHD A,@Ri 
Function: Exchange Digit 
Description: XCHD exchanges the low-order nibble of the Accumulator (bits 3 
through 0), generally representing a hexadecimal or BCD digit, with that of the 
internal RAM location indirectly addressed by the specified register. The high-order 
nibbles (bits 7-4) of each register are not affected. No flags are affected. 
Example: R0 contains the address 20H. The Accumulator holds the value 36H 
(00110110B). Internal RAM location 20H holds the value 75H (01110101B). The 
following instruction, 
XCHD A,@R0 
leaves RAM location 20H holding the value 76H (01110110B) and 35H (00110101B) 
in the Accumulator. 
Bytes: 1 
Cycles: 1 
Encoding: 
1 1 0 1 0 1 1 i
Operation: XCHD 
(A3-0) ↔ ((Ri3-0)) 
51. XRL , 
Function: Logical Exclusive-OR for byte variables 
Description: XRL performs the bitwise logical Exclusive-OR operation between the 
indicated variables, storing the results in the destination. No flags are affected. 
The two operands allow six addressing mode combinations. When the destination is 
the Accumulator, the source can use register, direct, register-indirect, or immediate 
addressing; when the destination is a direct address, the source can be the 
Accumulator or immediate data. 
Note: When this instruction is used to modify an output port, the value used as the 
original port data is read from the output data latch, not the input pins. 
Example: If the Accumulator holds 0C3H (1100001lB) and register 0 holds 0AAH 
(10101010B) then the instruction, 
XRL A,R0 
leaves the Accumulator holding the value 69H (01101001B). 
When the destination is a directly addressed byte, this instruction can complement 
combinations of bits in any RAM location or hardware register. The pattern of bits to 
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình Vi điều khiển Phụ lục 4 – Mô tả tập lệnh 
Phạm Hùng Kim Khánh Trang 233 
be complemented is then determined by a mask byte, either a constant contained in the 
instruction or a variable computed in the Accumulator at run-time. The following 
instruction, 
XRL P1,#00110001B 
complements bits 5, 4, and 0 of output Port 1. 
51.1. XRL A,Rn 
Bytes: 1 
Cycles: 1 
Encoding: 
0 1 1 0 1 r r r
Operation: XRL 
(A) ¬ (A) XOR (Rn) 
51.2. XRL A,direct 
Bytes: 2 
Cycles: 1 
Encoding: 
0 1 1 0 0 1 0 1 direct address
Operation: XRL 
(A) ¬ (A) XOR (direct) 
51.3. XRL A,@Ri 
Bytes: 1 
Cycles: 1 
Encoding: 
0 1 1 0 0 1 1 i
Operation: XRL 
(A) ¬ (A) XOR (Ri) 
51.4. XRL A,@#data 
Bytes: 2 
Cycles: 1 
Encoding: 
0 1 1 0 0 1 0 0 immediate data
Operation: XRL 
(A) ¬ (A) XOR #data 
51.5. XRL direct,A 
Bytes: 2 
Cycles: 1 
Encoding: 
0 1 1 0 0 0 1 0 direct address
Sưu tầm bởi: www.daihoc.com.vn 
Giáo trình Vi điều khiển Phụ lục 4 – Mô tả tập lệnh 
Phạm Hùng Kim Khánh Trang 234 
Operation: XRL 
(direct) ¬ (direct) XOR (A) 
51.6. XRL direct,#data 
Bytes: 3 
Cycles: 2 
Encoding: 
0 1 1 0 0 0 1 1 direct address immediate data 
Operation: XRL 
(direct) ¬ (direct) XOR #data 

File đính kèm:

  • pdfgiao_trinh_mon_hoc_vi_dieu_khien.pdf