Bài giảng Vi xử lý - Chương 5: Thiết kế hệ vi xử lý
• 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/A
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) 4/4/2011 53 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 ! ) 4/4/2011 54 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 4/4/2011 55 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. 4/4/2011 56 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 c f e d f e a b e g d a b c g d b c f g a c f g d a c f e g d a b c a b c f e g d a b c f g d BCD p g f e d c b a 7_seg he x 0000 0 0 1 1 1 1 1 1 3f 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 4/4/2011 57 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). 4/4/2011 58 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 4/4/2011 59 117 Chú ý với hiển thị 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 4/4/2011 60 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 4/4/2011 61 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 4/4/2011 62 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: . 4/4/2011 63 125 Example 5: Use a Look-up Table 126 Program Listing of Example 5 ORG 0000H START: MOV R0, #OKLUT+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 4/4/2011 64 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 4/4/2011 65 129 Flow Chart of Example 6 Start Initialization Set P3 as input port Read SW1SW4 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 4/4/2011 66 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 4/4/2011 67 133 Một số thí dụ các mạch giao tiếp IO 134 8031/8051 với LED và loa 4/4/2011 68 135 Giao tiếp với động cơ DC 136 Giao tiếp với động cơ bước 4/4/2011 69 137 Giao tiếp với DAC 138 Mạch loa đi kèm với DAC 4/4/2011 70 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:
- bai_giang_vi_xu_ly_chuong_5_thiet_ke_he_vi_xu_ly.pdf