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

pdf 140 trang phuongnguyen 3840
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)", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfbai_giang_vi_xu_ly_chuong_5_thiet_ke_he_vi_xu_ly_phan_1.pdf

Nội dung text: Bài giảng Vi xử lý - Chương 5 Thiết kế hệ vi xử lý (Phần 1)

  1. Chương 5 Thiếtkế hệ vi xử lý 1
  2. Nội dung •Giảimãđịachỉ •Giaotiếpbộ nhớ •Giaotiếpvới khóa (switch) và bàn phím •Giaotiếpbộ hiểnthị (Display) –GiaotiếpvớiLED –GiaotiếpvớiLCD •Giaotiếp A/D-D/A 2
  3. 5.1 Giảimãđịachỉ • Khi vi xử lý gửimột địachỉ ra bus địachỉ, thì thông tin này phải được chuyển thành lệnh cụ thể cho thiếtbị cụ thể. Giảimãđịachỉ thựchiệntácvụ này. Nó sử dụng thông tin bus địachỉđểxác định thiếtbị nào sẽđượctruycập. 3
  4. Phương pháp giảimãđịachỉ Có 2 phương pháp giảimãđịachỉ: 1. Giảimãtoànphần (Full address decoding): Mỗi ngoạivi được gán cho một địachỉ duy nhất. Tấtcả các bit địachỉđượcdùngđể định nghĩavị trí được tham chiếu. 2. Giảimãmộtphần (Partial address decoding): Không phảitấtcả các bit được dùng cho việcgiảimãđịachỉ. Các ngoại vi có thểđáp ứngchotrênmột địachỉ. Phương pháp làm giảm độ phứctạptrongmạch giải mã địachỉ. Thông thường các hệ thống nhỏ sử dụng giảimãmộtphần. 4
  5. Thí dụ: TK mạch giải mã địa chỉ •Vớibảng bộ nhớ sau, hãy thiếtkế mạch giảimãtoànphầnvà mạch giảimãmộtphần cho các chip bộ nhớ (RAM 4KB và ROM 16KB) và I/O 5
  6. Bảng bộ nhớ/IO củaTD 6
  7. Thí dụ giải mã địa chỉ toàn phần 7
  8. Kỹ thuậtgiải mã mộtphần 8
  9. Thí dụ giải mã địa chỉ mộtphần 9
  10. kỹ thuậtgiải mã mộtphầndẫn đến: 1. Mạch giải mã đơngiảnhơn. 2. Nhiều vùng địa chỉ hơn đượccấp phát vớitầm địa chỉ mộtphần. Thí dụ mạch giải mã cho RAM chỉ kiểm tra A15 và kết quả là toàn bộ tầm địa chỉ từ 0000H đến 7FFFH đượccấp phát cho RAM. 3. Sự bấtlợicủagiải mã địa chỉ mộtphần là bấtcứ khai triển nào thêm về bộ nhớ hoặcthiết bị I/O thì cầnthiếtkế lạimạch giải mã địa chỉ. Trái lại, với mạch giải mã toàn phầnthì bấtcứ sự thêm vào bộ nhớ hoặcthiết bị I/O, ta không cầnsửa đổimạch giải mã địa chỉ. 10
  11. Hardware dùng cho mạch giải mã địa chỉ •Cổng logic Æ mạch phứctạp •Mạch giải mã (Decoder) – TD:74LS138, •Mạch so sánh (Comparator) •ROM Æ thừachứcnăng • PLD: PLA, PAL, Æ cho đáp ứng nhanh 11
  12. 5.2 Giao tiếpbộ nhớ •Vi xử lý sử dụng bộ nhớđểlưutrữ các lệnh và dữ liệu trong khi thựcthi chương trình. Do đóvi xử lý thựchiệnnhiềutácvụ ghi/đọcvớibộ nhớ trong khi thựcthichương trình. •Mỗichip 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ựcthấp, nghĩalàchânnàybằng mức 0 thì chip này đượcchọn. •Vìtrongmạch có nhiềuthiếtbị I/O và bộ nhớ, do đócầnphảicómạch giảimã địachỉđểtạo ra các tín hiệuchọnchip. •Bus điềukhiể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ữavi xử lý và bộ nhớ hay thiếtbị 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ựcthấ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, 12
  13. Mộtsố chân điều khiểnbộ nhớ Bộ nhớ (và hầuhết các thiếtbị ngoại vi) có các đường điềukhiển đặcbiệt để giao tiếpvớivi xử lý • /CS hoặc/CE(Chip Select hoặc Chip Enable) – đượcláibởimạch giảimãđịachỉ từ vi xử lý. –thường là tích cựcthấp –khiđượcxácđịnh, thì chip/ngoạivi đượcchọn. • /OE (Output Enable=cho phép xuất) hay /RD (vớiRAM) –thường thấy trong các bộ nhớ – khi nó tích cực(thường là tích cựcthấp) thì ngõ ra ở trạng thái hi-Z. – đôi khi nó đượcxemnhư /RD trong RAM. • /WR (Write Enable=cho phép ghi) – đượcláibởi/WR củavi xử lý 13
  14. Qui tắc chung về thiếtkế mạch giao tiếpbộ nhớ với8051 1. Lậpbảng bộ nhớ –Hệ thống và ứng dụng phụ thuộc –Giải pháp đơngiản là tách riêng 64KB bộ nhớ dữ liệu và 64KB bộ nhớ mã. – Để sử dụng các địachỉ 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 địachỉ. 2. Chọn linh kiệnbộ nhớ thích hợp(nếu không bị áp đặtsử dụng). 3. Sử dụng mạch giảimãđịachỉ (nếucầ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. 14
  15. TD: Giao tiếp 8051 với RAM HM 6264 và ROM 27C256 15
  16. TD: 8031/8051 giao tiếpvới RAM ngoài và I/O 16
  17. Giao tiếpbộ nhớ vớimạch giải mã địa chỉ là PLD 17
  18. 5.5 Giao tiếpvới khóa (switch) và bàn phím 18
  19. Công tắc/khóa (hay phím đơn) ở các chân cổng I/O 19
  20. Sử dụng DIP switch ở cổng I/O Đoạnchương trình đọcdữ liệuvào: mov P1,#0FFH ; đặtcấuhìnhnhậpcho cổng P1 20 mov A, P1
  21. Kếtnốitốithiểuchocác hệ thống dùng 89C51/52 21
  22. SWITCH ON I/O PORTS (1/2) 22
  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). 23
  24. Keypad controller 24
  25. Giao tiếpvới bàn phím hex 25
  26. Giao tiếpvới bàn phím hex Để đọctoànbộ bàn phím, mỗihànglầnlượt được đọcvàonhư 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 đượcnhấntrênhàngđó thì giá trịđọcvàosẽ là 1111. Nếucóbất cứ phím nào đượcnhậntrênhàngđó thì sẽ có 0 ở bit tương ứng. 3. Dữ liệu 1011 được ghi vào cổng xuấtbằng cách dịch 0 vào cộtkế; và cổng nhập được đọcvào. 4. Dữ liệu 1101 được ghi vào cổng xuất; và cổng nhập được đọcvào. 5. Dữ liệu 1110 được ghi vào cổng xuất; và cổng nhập được đọcvào. 6. Chu trình này đượclặp đilặplạivôtậnbằng cách quay về bước1 ở trên. 26
  27. Giao tiếpvới bàn phím hex Dạng sóng ở các bit cổng xuấtP1.0 đến P1.3 27
  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 đếnchương trình chính scan: MOV P1, #0F0H ; kiểmtravớ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ẫuxuất MOV P1, A ; mẫuxuất MOV A, P1 ; đọccổng MOV R1, A ; cất phím đọc được ORL A, #0FH ; đặt các bit ở cộtlên1 CJNE A, #0FFH, cnvrt ; nhảy đến cnvrt nếu phím đượcnhấn MOV A, R0 ; lấylạ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ộtkế SJMP scanner ; bắt đầu đợt quét mới 28
  29. Chương trình quét bàn phím cnvrt: MOV A, R1 ; khôi phụcphímđọc được MOV R3, #0 ; xóa bộ đếmbảng clup: JNB ACC.0, cnvrt2 ; xong vớisố đếmchính RR A ; xoay giá trị MOV R2, A ; cấtgiátrị đượcxoay MOV A, R3 ; tăng bộ đếm thêm 1 ADD A, #4 ; cộng 4 cho mỗicộ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 29
  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ấylạiký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. 30
  31. Mộtdạng bàn phím (keypad hay keyboard) 31
  32. Interfacing to the Keyboard – keyboard is organized in a matrix of rows and columns – the key press is scanned and identified by microcontroller 32
  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 33
  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 34
  35. Interfacing to the Keyboard 35
  36. Interfacing to the Keyboard 36
  37. Interfacing to the Keyboard 37
  38. Interfacing to the Keyboard 38
  39. Interfacing to the Keyboard 39
  40. Interfacing to the Keyboard 40
  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 41
  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 42
  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 43 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 44
  45. Simple Switch Matrix 45
  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 46
  47. Multiplexing a Switch Matrix 47
  48. Switch Matrix Key Pressed 48
  49. Phầnxuất/nhậpsố vớiVXL 49
  50. Giao tiếpvới các thiết bị I/O •Truyềndữ 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ộtcổng IO có thể được dùng cho nhiềuthiế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 đượcgửi đi hay nhậnvề – Các thanh ghid điềukhiển: để chọnkiểutác vụ IO – Các thanh ghi trạng thái: trạng thái củatác vụ IO •Với cách nhìn VXL thì các thuậtngữ “cổng IO” và “thiết bị IO” đôi khi được dùng cho nhau. 50
  51. Chip giao tiếpIO Bus địa chỉ IO có thể trùng hoặc khác bus địa chỉ dùng cho bộ nhớ chính. 51
  52. Nhắclại: Có 2 kiểuánhxạ IO (a) IO-mapped input/output (b) Memory-mapped input/output 52
  53. Các kiểucổng IO •Mộtcổng IO có thể có nhiềuchânIO; 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ộtcổng IO có thể chứa nhiều chân có các kiểuhỗnhợp. 53
  54. Các chân nhập (dùng đệm 3 trạng thái) 54
  55. Các chân xuất (dùng mạch chốt) 55
  56. Các chân 2 chiều Đặtcấu hình: •Nhập: ghi D=1 để M1 OFF •Xuất: ghi D=0 để M1 ON 56
  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 57
  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”. 58
  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 59
  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 60
  61. Hardware Solution 61
  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 62
  63. Mở rộng xuấtnhập song song với 8255 PPI 63
  64. 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 65
  65. 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 66
  66. 8255 69
  67. 8255 – Logic điều khiển đọc/ghi 70
  68. 8255 73
  69. 8255 76
  70. 8255 77
  71. 8255 78
  72. 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 79
  73. 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. 80
  74. 8255 mode 1 (output) 81
  75. 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. 82
  76. Timing diagram for mode1(output) 83
  77. 8255 mode 1 (input) 84
  78. 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. 85
  79. Timing diagram for mode1(input) 86
  80. 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). 87
  81. Mode 2 • Output Operations – OBF (Output Buffer Full). The OBF Pin Functio output will go low to indicate that the n CPU has written data out to port A. PC7 /OBF – ACK (Acknowledge). A low on this PC6 /ACK input enables the tri-state output buffer of Port A to send out the data. PC5 IBF Otherwise, the output buffer will be in PC4 /STB the high impedance state. PC3 INTR • Input Operations – STB (Strobe Input). A low on this PC2 I/O input loads data into the input latch. PC1 I/O – IBF (Input Buffer Full F/F). A high on this output indicates that data has PC0 I/O been loaded into the input latch. 88
  82. BSR Mode • If used in BSR mode, then the bits of port C can be set or reset individually 90
  83. 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 91
  84. 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 92
  85. Interfacing 8255 with 8051 P2.7(A15) A2 P2.6(A14) A1 P2.5(A13) A0 74138 8051 3×8 decoder /CS 8255 ALE O0 A0 74373 O1 A1 P0.7-P0.0 D7-D0 O7 (AD7-AD0) D7-D0 /RD /RD /WR /WR 93
  86. 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 subroutine94 sjmp repeat ; for (1)
  87. 5.6 Giao tiếpbộ hiểnthị (Display) 5.6.1 Giao tiếpvớiLED 95
  88. Giao tiếpvớiLED 96
  89. Giao tiếpvớiLED đơn 97
  90. TD: Mạch cho nhậptừ phím nhấnvà xuấtraLED đơn 98
  91. Mạch nhậptừ phím nhấn 99
  92. Mạch xuấtraLED đơn 100
  93. LED ON I/O PORTS (1/2) 101
  94. 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. 102
  95. 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? 103
  96. 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) 104
  97. 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 105
  98. 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 ! ) 106
  99. 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. 107
  100. 7 Segment Display dp = decimal point 108
  101. 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. 109
  102. 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 110 on.
  103. 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 111
  104. BCD to 7-Seg lookup table mov p3,#0fh BCD p g f e d c b a he mov a,p3 7_seg x 0000 0 0 1 1 1 1 1 1 3f anl a,0fh 0001 0 0 1 1 0 0 0 0 30 get_code: mov DPTR, #7s_tab 0010 0 1 0 1 1 0 1 1 5b movc A, @A+DPRT 0011 0 1 0 0 1 1 1 1 4f mov p1,a 0100 0 1 1 0 0 1 1 0 66 0101 0 1 1 0 1 1 0 1 6d 7s_tab: db 3fh,30h,5bh,4fh,66h 0110 0 1 1 1 1 1 0 1 7d 0111 0 0 0 0 0 1 1 1 07 db 6dh,7dh,07h,7fh,6fh 1000 0 1 1 1 1 1 1 1 7f END 1001 0 1 1 0 1 1 1 1 6f a a a a a a a a f b f b b f b f f b f b f b g g g g g g g e c e e c c c e c c e c c d d d d d d d 112
  105. 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 113
  106. 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). 114
  107. 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 115 Goto again
  108. Hiểnthị quét LED với4 LED 7 đoạn 116
  109. Chú ý vớihiểnthị dồnkênh Vớihiểnthị LED dồn kênh: • Ở mỗithời điểmchỉ có mộthiểnthị LED 7 đoạn đượccho phép (qua các khóa điệntử BJT). • Các ngõ vào a-h nối chung với nhau cho tấtcả các LED 7 đoạn. •Tổng số chân cổng cầnsử dụng là 8 + số ký số (digit), vớithí dụ hình 5.80 thì tổng số chân là 8 + 4 = 12. •Tầnsố quét phải đủ cao để tránh tình trạng thấy LED nhấp nháy: •tốithiểu40Hz •thời gian cho 1 digit sáng là 1/40 giây. •tầnsố quét cao hơn thì sẽ giảmsự nhấp nháy 117
  110. TD: Mạch hiển thị LED 7 đoạncủa www.MightyMicons.com 118
  111. Mạch này dùng LED 7 đoạnloại nào? (CA hay CC) 119
  112. Mộtsố thí dụ vớiLED và phím nhấn 120
  113. BasicBasic OutputOutput TechniquesTechniques withwith LEDsLEDs +5V EA VCC Reset P1.7 P1.6 P1.5 8051 P1.4 P1.3 XTAL1 P1.2 XTAL2 P1.1 P1.0 VSS 121
  114. ExampleExample 3:3: LightLight upup LEDsLEDs All LEDs ON All LEDs OFF 1st: LEDs 122
  115. ProgramProgram ListingListing forfor ExampleExample 33 Start ORG 0000H CLR A Set A = 00 LOOP: MOV P1, A CPL A ACALL DELAY Move the content AJMP LOOP of A to P1 DELAY: MOV R6, #250 DL1: MOV R7, #200 DL2: DJNZ R7, DL2 Delay for 0.1s DJNZ R6, DL1 RET Invert the END content of A Assume 12MHz clock, determine the delay time. Time delay, Tex1 = 1 + [ ( 1+ 200 * 2 ) + 2 ] * 250 + 2 = 100,753 MachineCycle123
  116. ExampleExample 4:4: LightingLighting SequenceSequence 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: . 124
  117. ExampleExample 5:5: Use a Look-up Table 125
  118. ProgramProgram ListingListing ofof ExampleExample 55 LUT: DB 01111110B ORG 0000H DB 00111100B START: MOV R0, #OK−LUT+1 ; length of table DB 00011000B MOV DPTR, #LUT ; code start address DB 00000000B MOV R1, #00H DB 00011000B LOOP: MOV A, R1 DB 00111100B MOVC A, @A+DPTR DB 01111110B MOV P1, A DB 11111111B ACALL DELAY ; INC R1 ; point to next data of table DB 01111110B DJNZ R0, LOOP ; finish ? DB 00111100B AJMP START DB 00011000B ; DB 00000000B DELAY: DB 00011000B ; do not modify the value in register R0, & R1 DB 00111100B ; DELAY = 1 + (100751 + 2 ) * 2 + 2 = 201,509 Machine Cycle ! DB 01111110B MOV R5, #2 DB 11111111B DL1: MOV R6, #250 ; DL2: MOV R7, #200 DB 00000000B DL3: DJNZ R7, DL3 DB 11111111B DJNZ R6, DL2 DB 00000000B DJNZ R5, DL1 OK: DB 11111111B RET END ; 126
  119. ExampleExample 6:6: BasicBasic InputInput TechniqueTechnique +5V EA VCC P1.7 Reset P1.6 P1.5 P1.4 P1.3 8051 P1.2 P1.1 P1.0 XTAL1 P3.7 SW1 XTAL2 P3.6 SW2 P3.5 SW3 P3.4 SW4 VSS 127
  120. ExampleExample 66 When SW1 Closed When SW2 Closed When SW3 Closed When SW4 Closed Priority: SW1 SW2 SW3 SW4 128
  121. FlowFlow ChartChart ofof ExampleExample 66 2 Start Y SW1 closed? SW1 Handler Initialization N Y 1 2 SW2 closed? SW2 Handler Set P3 as input port N Y SW3closed? SW3 Handler Read SW1∼SW4 status N Y SW4 closed? SW4 Handler N 1 129
  122. ProgramProgram ListingListing ofof ExampleExample 66 ORG 0000H CASE2: MOV A, R2 MOV P1, A MOV R1, #00000000B ACALL DELAY MOV R2, #01010101B XRL A, #10101010B MOV R3, #00001111B MOV P1, A MOV R4, #11110000B AJMP TEST ; CASE3: MOV A, R3 TEST: ORL P3, #0FFH MOV P1, A ; P3 is configured as input port! ACALL DELAY JNB P3.7, CASE1 XRL A, #11110000B JNB P3.6, CASE2 MOV P1, A JNB P3.5, CASE3 AJMP TEST JNB P3.4, CASE4 CASE4: MOV A, R4 AJMP TEST MOV P1, A ; ACALL DELAY CASE1: MOV A, R1 XRL A, #00001111B MOV P1, A MOV P1, A ACALL DELAY AJMP TEST XRL A, #11111111B DELAY: MOV P1, A ;inside DELAY, don’t modify A, R1, R2, R3 & R4. AJMP TEST RET ; END 130
  123. Ex7:Ex7: 77 SegmentSegment LEDLED NumericNumeric DisplayDisplay Eprom Version Of 8051 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 131
  124. ProgramProgram ListingListing ofof ExampleExample 77 ORG 0000H TABLE: DB 11000000B ; 0 DB 11111001B ; 1 MOV R3, #00H DB 10100100B ; 2 LOOP: MOV DPTR, #TABLE DB 10110000B ; 3 MOV A, R3 DB 10011001B ; 4 MOVC A, @A+DPTR DB 10010010B ; 5 ; DB 10000010B ; 6 ; Display numbers on 7-segment display DB 11111000B ; 7 MOV P1, A DB 10000000B ; 8 ACALL DELAY DB 10010000B ; 9 ; ; ; Increase R3 by 1 and loop back END 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: 132 RET
  125. Một số thí dụ các mạch giao tiếp IO 133
  126. 8031/8051 với LED và loa 134
  127. Giao tiếp với động cơ DC 135
  128. Giao tiếp với động cơ bước 136
  129. Giao tiếp với DAC 137
  130. Mạch loa đi kèm với DAC 138
  131. Mạch điều khiển động cơ DC đi kèm DAC 139
  132. Giao tiếp với ADC 140