Giáo trình bài tập & hướng dẫn sử dụng Training .Kit.8951

Chia sẻ: Nguyễn Hùng | Ngày: | Loại File: PDF | Số trang:51

0
508
lượt xem
259
download

Giáo trình bài tập & hướng dẫn sử dụng Training .Kit.8951

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo sách 'giáo trình bài tập & hướng dẫn sử dụng training .kit.8951', kỹ thuật - công nghệ, tự động hoá phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:
Lưu

Nội dung Text: Giáo trình bài tập & hướng dẫn sử dụng Training .Kit.8951

  1. O V A S T h C T .,L d T ec o tr 105B Nguyen Luong Bang St, Danang Tel: 0511.736.909. Fax: 0511.736.909 Email: novas@vnn.vn GIA O TRI BẬI T, P & NH HỚ Ơ NG D- N S̉ DU NG Training.Kit.8951 Bo tai lie u kủ m theo r a i n g.t n 9 5 1 cu a NOVAS bao gn K 8K gá m: 1. Gia o trı ly thuye t ho vi ề ìu khỉn 8051 nh 2. Gia o trı bậi tưp vậ hớ ơ ng d̃n s dung nh r a i n g.t n 9 5 1 gn K 8K 3. Ð ˜ CD cợng cu lưp trı vậ datasheet a nh NOVAS mong nhưn ề ớ óc gừp y tạ bả ề o c. n r a i n g.t n 9 5 1 gn K 8K
  2. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh Training.Kit.PLC, Training.Kit.Inverter, Training.Kit.Microcontroller Muc luc Bai 1. Cụ ng cầ lôp trı .................................................................................................................................................. 3 nh Bai 2. Phứn cư ng............................................................................................................................................................. 4 Bai 3. Hin thi LED ơự n (Thí c hanh: lam quen 8951, tễ tr̃ nh ).................................................................................... 4 Bai 4. Hin thi LED 7 ơoấn (Thí c hanh: xu t port, quỏt led)....................................................................................... 5 Bai 5. Hin thi LED ma trôn .......................................................................................................................................... 10 Bai 6. Hin thi LCD (Thí c hanh: man hı tinh th lòng 2 dếng) ............................................................................. 11 nh Bai 7. Giao tiắp ban phễ (Thí c hanh: ng˘ t ngoai, chẩng nảy khi n va nhổphễ quỏt phễ chuyn ơửi hex m m, m, sang ascii, gữi d˜ lie u lÂn m´ y tễ ........................................................................................................................... 15 nh) Bai 8. A m thanh (Thí c hanh: tấo tr̃dặng vếng lóp)................................................................................................. 19 Bai 9. DAC, Sề Sin, Motor DC (Thí c hanh: Chuyn ơửi sẩ - tể ự ng tí ơiớu khin motor dc va tấo sề sin) . 25 ng ng Bai 10. ADC (Thí c hanh: chuyn ơửi tể ự ng tí - sẩ, ng˘ t ngoai, ng˘ t timer, hin thi LED 7 ơoấn)....................... 27 Bai 11. Motor bể ờc (Thí c hanh: ơiớu khin motor bể ờc, giao tiắp ban phễ hin thi) ........................................ 30 m, Bai 12. Tấo 2 sề vuụ ng (Thí c hanh: ng˘ t ơinh thĐ i) ............................................................................................. 35 ng Bai 13. Ð iớu chắ ơo ro ng xung PWM Pulse Width Modulation (Th í c hanh: ng˘ t ơinh thĐ i)............................... 36 Bai 14. Ð á ng há sẩ (Thí c hanh: ng˘ t ơinh thĐ i, LED 7 ơoấn kiu quỏt, v n ơớ lôp trı IO va bổ ve thanh ghi nh o vời ISR) .......................................................................................................................................................................... 38 Bai 15. Encoder (Thí c hanh: Nhôp xu t port, Ng˘ t ngoai, Bo ơắm, LED 7 ơoấn, bổ ve thanh ghi vời ISR) ....... 46 o Bai 16. Giao tiắp m´ y tễ (Thí c hanh: giao tiắp nẩi tiắp m´ y tễ theo giao thư c RS232)................................... 48 nh nh ean T 2 g
  3. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh Bậi 1. Cợng cu lưp trınh 1.1. Trı soả thêo Crimson nh n Soan tho noi dung trong cư a sí chı lổ u file dổ ơ i dang .asm. nh, 1.2. Trı soả thêo vậ biị n di 8051ide nh n ch File ng˜vốo: .asm, file ng˜ra: .hex. ean T 3 g
  4. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh 1.3. Trı nả EZ Downloader nh p Ńi PC vố Kit nap b˘ng cí ng RS232, sư dung trđ EZ nh ểe nap chip 8951, file ng˜vốo dang .hex (biịn di ch dâ trđ 8051ide). ng nh Bậi 2. Phứn cựng Mo hı day hđ c vi ề ieu khỉ n (Training.Kit.8951) ề ợơ c chia thầnh nhieu module nhợ sau: nh ç Module xư lıtrung tựm: chip 8951, mach dao ểong thach anh, reset ç Module ADC, DAC (chuyen ểí i tổ õ ng tậ sang ś vố ś sang tổ õ ng tậ) nhện tı hiừu n tổ õ ng tậ tế cm biấ xuềt tı hiừu tổ õ ng tậ ểiáu khien motor DC, ểo sẩng LED n, n ç Module giao tiấ mẩy tı theo chuỏ n RS232 p nh ç Module motor bổ ơ c ç Module motor mot chiáu, ểiáu chấểo rong xung PWM ç Module hien thi ốn hđ tinh the lồ ng (LCD) m nh ç Module hien thi ED 7 ểoan L ç Module hien thi ED ma trện L ç Module hien thi ED ểõ n L ç Module bốn phı m ç Module loa ç Nguó n cềp Bậi 3. Hỉn thiLED ề ẫ n (Thíc hậnh: lậm quen 8951, tễ trấ) nh 3.1. Muc ề ễ ch Thậc hốnh mot ểoan lừnh ểõ n gin nhềt vơ i LED ểõ n, qua ểủ kiem tra sậ hoat ểong côa cễ ng cu lệp trđ bo lệp trđ nh, nh vố Kit8951. Hieu cẩch tao tr̃ ểõ n gin. (Phứn c´ ng: Xem phu luc) 3.2. Chớ ẫ ng trınh ;*************************************************************************** ;a. Blinking a Single LED ---novas---- ;************************************************************************** ; variables and constants ;************************************************************************** ; RESET ;reset routine .ORG 0H ;locate routine at 00H AJMP START ;jump to START ;************************************************************************** ; INTERRUPTS (not used) ;place interrupt routines at appropriate ;memory locations .ORG 03H ;external interrupt 0 RETI .ORG 0BH ;timer 0 interrupt RETI .ORG 13H ;external interrupt 1 RETI .ORG 1BH ;timer 1 interrupt RETI .ORG 23H ;serial port interrupt RETI ean T 4 g
  5. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh .ORG 25H ;locate beginning of rest of program ;************************************************************************** INITIALIZE: ;set up control registers MOV TCON,#00H MOV TMOD,#00H MOV PSW,#00H MOV IE,#00H ;disable interrupts RET ;************************************************************************** ; Real code starts below. The first two routines are for delays so we ; can slow down the blinking so we can see it. (Without a delay, it ; would blink so fast it would look like it was always on. ;************************************************************************** ;This delay program is quite bad compared to using DJNZ, see NOVAS lecture notes DELAYMS: ;millisecond delay routine ; ; MOV R7,#00H ;put value of 0 in register R7 LOOPA: INC R7 ;increase R7 by one (R7 = R7 +1) MOV A,R7 ;move value in R7 to Accumlator (also known as A) CJNE A,#0FFH,LOOPA ;compare A to FF hex (256). If not equal go to LOOPA RET ;return to the point that this routine was called from ;************************************************************************** DELAYHS: ;half second delay above millisecond delay ; This delay forming is bad compared to what shown in NOVAS lecture notes MOV R6,#00H ;put 0 in register R6 (R6 = 0) MOV R5,#002H ;put 2 in register R5 (R5 = 2) LOOPB: INC R6 ;increase R6 by one (R6 = R6 +1) ACALL DELAYMS ;call the routine above. It will run and return to here. MOV A,R6 ;move value in R6 to A JNZ LOOPB ;if A is not 0, go to LOOPB DEC R5 ;decrease R5 by one. (R5 = R5 -1) MOV A,R5 ;move value in R5 to A JNZ LOOPB ;if A is not 0 then go to LOOPB. RET ;************************************************************************** START: ;main program (on power up, program starts at this point) ACALL INITIALIZE ;set up control registers LOOP: CPL P1.0 ;ComPLement (invert) P1.0 (this makes LED change) ACALL DELAYHS ;go to above routine that causes a delay AJMP LOOP ;go to LOOP(always jump back to point labeled LOOP) .END ;end program Bậi 4. Hỉn thiLED 7 ề oả (Thíc hậnh: xué port, quỏt led) n t 4.1. Muc ề ễ ch ç Hien thibo ểấ lịn tế 00 ểấ 99, trịn 2 LED 7 m n ểoan theo kieu quò t (tiấ kiừm ś ng˜ ra) file: t nv7seggood.asm ç Hien thibo ểấ lịn tế 0 ểấ F (hừ ểấ cõ ś m n m 16), trịn 1 LED 7 ểoan, file: nv7segbad.asm ç (Phứn c´ ng: Xem phu luc) ean T 5 g
  6. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh 4.2. Chớ ẫ ng trınh ;----------------------------------------------------------------------- ; nv7seggood.asm ; Seven Segment LED Display --novas-- ; Scanning solution ;----------------------------------------------------------------------- ORG 0H ;locate routine at 00H LJMP MAIN ; INTERRUPTS (not used) ;place interrupt routines at appropriate ;memory locations ORG 03H ;external interrupt 0 RETI ORG 0BH ;timer 0 interrupt RETI ORG 13H ;external interrupt 1 RETI ORG 1BH ;timer 1 interrupt RETI ORG 23H ;serial port interrupt RETI ORG 30H ;initialize SFRs INIT: MOV TCON,#00H MOV TMOD,#00H MOV PSW,#00H MOV IE,#00H ;disable interrupts RET ;************************************************************************ ;Delay ;************************************************************************ DELAY_1MS: PUSH 00H ;MOV R1,#250 D1: MOV R0,#250 ;5OOUS delay w1: DJNZ R0,w1 MOV R0,#250 ;500US delay w2: DJNZ R0,w2 ;DJNZ R1,D1 POP 00H RET ;************************************************************************* ; display subroutine ;************************************************************************* DISPLAY: MOV DPTR,#TABLE MOV A,R1 MOVC A,@A+DPTR SETB P2.0 CLR P2.1 MOV P1,A LCALL DELAY_1MS MOV A,R0 MOVC A,@A+DPTR CLR P2.0 ean T 6 g
  7. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh SETB P2.1 MOV P1,A LCALL DELAY_1MS RET TABLE: DB 11000000B,11111001B,10100100B,10110000B,10011001B, DB 10010010B,10000010B,11111000B,10000000B,10010000B ;*************************************************************************** ;CONV subroutine converts Hex to Decimal in term of Unit, Tens and Hundreds ;contained in R0, R1, R2 ;*************************************************************************** CONV: MOV A,R7 MOV B,#10 DIV AB MOV R0,B ;DON VI MOV B,#10 DIV AB MOV R1,B ;CHUC MOV R2,A ;TRAM RET ;*************************************************************************** ;display numbers from 00 to 99 ;*************************************************************************** MAIN: ACALL INIT REPEAT: CJNE R7,#99,QUAYVE MOV R7,#0 QUAYVE: INC R7 LCALL CONV LCALL DISPLAY SJMP REPEAT END ;*************************************************************************** ; nv7segbad.asm ;* Seven Segment Display from 0 to F ; Bad Way ;*************************************************************************** ; RESET ;reset routine ORG 0H ;locate routine at 00H AJMP START ;jump to START ;************************************************************************** ; INTERRUPTS (not used) ;place interrupt routines at appropriate ;memory locations ORG 03H ;external interrupt 0 RETI ORG 0BH ;timer 0 interrupt RETI ORG 13H ;external interrupt 1 RETI ean T 7 g
  8. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh ORG 1BH ;timer 1 interrupt RETI ORG 23H ;serial port interrupt RETI ORG 25H ;locate beginning of rest of program ;************************************************************************** INITIALIZE: ;set up control registers MOV TCON,#00H MOV TMOD,#00H MOV PSW,#00H MOV IE,#00H ;disable interrupts RET ;************************************************************************** ; Real code starts below. ; This program is very bad compared to using DJNZ ;************************************************************************** DELAYMS: ;millisecond delay routine MOV R7,#00H ;put value of 0 in register R7 LOOPA: INC R7 ;increase R7 by one (R7 = R7 +1) MOV A,R7 ;move value in R7 to Accumlator (also known as A) CJNE A,#0FFH,LOOPA ;compare A to FF hex (256). If not equal go to LOOPA RET ;return to the point that this routine was called from ;************************************************************************** DELAYHS: ;half second delay above millisecond delay MOV R6,#00H ;put 0 in register R6 (R6 = 0) MOV R5,#002H ;put 2 in register R5 (R5 = 2) LOOPB: INC R6 ;increase R6 by one (R6 = R6 +1) ACALL DELAYMS ;call the routine above. It will run and return to here. MOV A,R6 ;move value in R6 to A JNZ LOOPB ;if A is not 0, go to LOOPB DEC R5 ;decrease R5 by one. (R5 = R5 -1) MOV A,R5 ;move value in R5 to A JNZ LOOPB ;if A is not 0 then go to LOOPB. RET ;************************************************************************** DISPLAY_0: ; Display 0 on the Seven Segment Display MOV P2, #00000001B ; P2.7 is on the left and P2.0 on the right. RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_1: ; Display 1 on the Seven Segment Display MOV P2, #01001111B ; P2.7 is on the left and P2.0 on the right. RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_2: ; Display 2 on the Seven Segment Display MOV P2, #00010010B ; P2.7 is on the left and P2.0 on the right. RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_3: ; Display 3 on the Seven Segment Display MOV P2, #00000110B ; P2.7 is on the left and P2.0 on the right. RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_4: ; Display 4 on the Seven Segment Display MOV P2, #01001100B ; P2.7 is on the left and P2.0 on the right. RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_5: ; Display 5 on the Seven Segment Display ean T 8 g
  9. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh MOV P2, #00100100B ; P2.7 is on the left and P2.0 on the right. RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_6: ; Display 6 on the Seven Segment Display MOV P2, #00100000B ; RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_7: ; Display 7 on the Seven Segment Display MOV P2, #00001111B ; RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_8: ; Display 8 on the Seven Segment Display MOV P2, #00000000B ; RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_9: ; Display 9 on the Seven Segment Display MOV P2, #00001100B ; RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_A: ; Display A on the Seven Segment Display MOV P2, #00001000B ; RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_b: ; Display b on the Seven Segment Display MOV P2, #01100000B ; RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_C: ; Display C on the Seven Segment Display MOV P2, #00110001B ; RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_d: ; Display d on the Seven Segment Display MOV P2, #01000010B ; RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_E: ; Display E on the Seven Segment Display MOV P2, #00110000B ; RET ; The B at the end means it is a binary number ;************************************************************************** DISPLAY_F: ; Display F on the Seven Segment Display MOV P2, #00111000B ; RET ; The B at the end means it is a binary number ;************************************************************************** START: ;main program (on power up, program starts at this point) ACALL INITIALIZE ;set up control registers LOOP: ACALL DISPLAY_0 ;Display 0 on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ACALL DISPLAY_1 ;Display 1 on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ACALL DISPLAY_2 ;Display 2 on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ACALL DISPLAY_3 ;Display 3 on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ACALL DISPLAY_4 ;Display 4 on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ACALL DISPLAY_5 ;Display 5 on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ean T 9 g
  10. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh ACALL DISPLAY_6 ;Display 6 on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ACALL DISPLAY_7 ;Display 7 on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ACALL DISPLAY_8 ;Display 8 on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ACALL DISPLAY_9 ;Display 9 on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ACALL DISPLAY_A ;Display A on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ACALL DISPLAY_b ;Display b on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ACALL DISPLAY_C ;Display C on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ACALL DISPLAY_d ;Display d on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ACALL DISPLAY_E ;Display E on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay ACALL DISPLAY_F ;Display F on the seven segment display ACALL DELAYHS ;go to above routine that causes a delay AJMP LOOP ;go to LOOP(always jump back to point labeled LOOP) END ;end program Bậi 5. Hỉn thiLED ma trưn (Phứn c´ ng: Xem phu luc) ;----------------------------------------------------------------------- ; Matrix LED Display --novas-- ;----------------------------------------------------------------------- h equ p0 ;low nible for red color, high nible for green color col equ p2 ;8 columns ORG 0H ;locate routine at 00H LJMP MAIN ;initialize SFRs INIT: MOV TCON,#00H MOV TMOD,#00H MOV PSW,#00H MOV IE,#00H ;disable interrupts mov col,#7fh RET ;************************************************************************ ;Delay ;************************************************************************ DELAY_1MS: PUSH 00H ;MOV R1,#250 D1: MOV R0,#250 ;5OOUS delay w1: DJNZ R0,w1 MOV R0,#250 ;500US delay w2: DJNZ R0,w2 ;DJNZ R1,D1 POP 00H ean T10 g
  11. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh RET DELAY_500MS: PUSH 00H PUSH 01H MOV R1,#250 D11: MOV R0,#230 ;500us DJNZ R0,$ MOV R0,#230 ;500us DJNZ R0,$ DJNZ R1,D11 MOV R1,#250 D22: MOV R0,#230 ;5OOus DJNZ R0,$ MOV R0,#230 ;500us DJNZ R0,$ DJNZ R1,D22 POP 01H POP 00H RET ;*************************************************************************** main: ACALL INIT REPEAT: clr a mov h,a acall delay_500ms acall delay_500ms acall delay_500ms acall delay_500ms mov a,col rr a mov col,a SJMP REPEAT END Bậi 6. Hỉn thiLCD (Thíc hậnh: mận hı tinh th̉ lòng 2 nh dắng) 6.1. Muc ề ễ ch ç Hien thimốn hđ tinh the lồ ng (Liquid nh Crystal Display - LCD), 2 dắng. ç (Phứn c´ ng: xem phu luc) ç Gần tềt c cẩc jum khi thậc thi bối tệp nốy, thẩo jum khi khễ ng dâ LCD ng 6.2. Mợ tê ç Mễ t LCD 16 chựn: ean T11 g
  12. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh Pin Description Pin Description Pin Description Pin Description 1 GND 5 RW 9 D2 13 D6 2 VDD 6 Enable 10 D3 14 D7 3 V0 7 D0 11 D4 15 Not used 4 RS 8 D1 12 D5 16 Not used ç Tệp lừnh 6.3. Chớ ẫ ng trınh ;************************************************************************** ; --novas-- display on LCD 16 pins ; display in fast or slow speeds nvlcd.asm ;************************************************************************** ; PORT USAGE ;cr equ 0dh ;carrier return enable equ p2.0 ; enable pin 6 LCD ean T12 g
  13. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh RW equ p2.2 ; read/write pin 5 LCD RS equ p2.1 ; register select pin 4 LCD slow_disp set 30h ; 0 = fast display, 1 = slow display ; P1 connected to 8bit data bus of LCD, in the normal sequence ; P1.0 to P1.7 connected to D0 to D7 ORG 0 ;locate routine at 00H LJMP MAIN ;************************************************************************** ; INTERRUPTS ; not used ORG 03H ;external interrupt 0 reti ORG 0BH ;timer 0 interrupt RETI ORG 13H ;external interrupt 1 RETI ORG 1BH ;timer 1 interrupt RETI ORG 23H ;serial port interrupt RETI ORG 30H ;************************************************************************** ;initialize SFRs here if needed ;************************************************************************** ; init lcd LCD_SETUP: mov a,#38h ; function set: 8 bits, 2 lines, 5x7 font lcall write_control mov a,#7h ; entry mode: 7/6 = scroll on/off lcall write_control mov a,#0ch ; display on, cursor off, blink off lcall write_control mov slow_disp,#1 ; slow ret ;************************************************************************** ; look at the table in novas lab note write_control: clr RS clr RW mov p1,a setb enable clr enable setb RW setb RS mov a,slow_disp jz slow_ctrl lcall long_delay slow_ctrl: lcall delay ret ;************************************************************************** ; look at the table in novas lab note write_data: setb RS clr RW ean T13 g
  14. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh mov p1,a ; write data setb enable clr enable setb RW clr RS mov a,slow_disp ; is this a fast or slow write? jz slow_data ; - fast lcall long_delay ; - slow slow_data: lcall delay ret ;************************************************************************** ; short delay - used to set timing requirments delay: mov r7,#03fh del2: djnz r7,del2 ret ; long delay - used to scroll across screen slowly long_delay: mov r5,#07h lgdel: mov r6,#070h lgdel1: mov r7,#0ffh lgdel2: djnz r7,lgdel2 djnz r6,lgdel1 djnz r5,lgdel ret ;************************************************************************** clrscr: push slow_disp mov slow_disp,#1 ; update quickly mov a,#1 lcall write_control pop slow_disp ret ;******************************************************************** ; Send a null terminated string to LCD ;******************************************************************* send_string: clr a movc a,@a+dptr ; get character jz exit ; stop if char == null lcall write_data ; else send it inc dptr ; point to next char sjmp send_string exit: ret ; number 0 put at the end to recognize the end point ; see the novas lecture notes mess1: db '_NOVAS Automation & Embedded System/',0 mess2: db '_Truong Cao dang Cong nghe/',0 ;*******************************************************************/ ;main program MAIN: ACALL LCD_SETUP ;initialize the LCD acall clrscr top: mov dptr,#mess1 ; write message 1 to LCD lcall send_string mov dptr,#mess2 ; write message 2 to LCD lcall send_string sjmp top ean T14 g
  15. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh end Bậi 7. Giao tie p bận phễ (Thíc hậnh: ng˘t ngoậi, chẩ nổy m ng khi é vậ nhê phễ quỏt phễ chuỷn ề ữi hex sang ascii, g i n m, m, dệ li u lị n ma y tễ nh) 7.1. Muc ề ễ ch ç Mach giao tiấ bốn phı hien thi ED 7 ểoan p m, L ç Ch́ng nỏ y khi ền vố nh phı b˘ng phã n m mám (keypress & keyrelease debouncing) ç Chuyen ểí i mẤ hex sang ascii, gư i lịn PC a.  n phı lịn (INC) ểe tĐ ng giẩ tri ấ File: nvkey.asm m ể m, b. Cẩch kấ ńi bốn phı nhổ trong hđ 9.2 sẩch V— K, nguyịn tầc giao tiấ bốn phı tiấ t m nh p mt kiừm ś chựn ng˜vốo (saving inputs) 16 phı ền ńi tơ i 8 ng˜vốo 8951, File: nvkey1.asm m 7.2. Chớ ẫ ng trınh ;----------------------------------------------------------------------- ; --novas-- ; a. keyboard reading and display on 7seg leds ;----------------------------------------------------------------------- inc_key bit p3.3 ORG 0H ;locate routine at 00H JMP MAIN ; INTERRUPTS (not used) ;place interrupt routines at appropriate ;memory locations ORG 03H ;external interrupt 0 reti ORG 0BH ;timer 0 interrupt RETI ORG 13H ;external interrupt 1 jmp isr_1 ORG 1BH ;timer 1 interrupt RETI ORG 23H ;serial port interrupt RETI ORG 30H ;initialize SFRs INIT: MOV TCON,#00H MOV TMOD,#00H MOV PSW,#00H SETB EA SETB EX1 ;external 1 interrupt CLR IT1 ;Negative Edge Active, IT0 = TCON.0 setb inc_key ;see lecture notes NOVAS mov r7,#0 RET ;************************************************************************ ;Delay ;************************************************************************ ean T15 g
  16. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh DELAY_1MS: PUSH 00H MOV R0,#250 ;5OOUS delay DJNZ R0,$ MOV R0,#250 ;500US delay DJNZ R0,$ POP 00H RET ;************************************************************************* ; display subroutine ;************************************************************************* DISPLAY: MOV DPTR,#TABLE mov R6,#0 display1: MOV A,R2 MOVC A,@A+DPTR SETB P2.0 SETB P2.1 CLR P2.2 ;led 3 on MOV P1,A LCALL DELAY_1MS MOV A,R1 MOVC A,@A+DPTR SETB P2.0 CLR P2.1 ;led 2 on setb p2.2 MOV P1,A LCALL DELAY_1MS MOV A,R0 MOVC A,@A+DPTR CLR P2.0 ;led 1 on SETB P2.1 SETB P2.2 MOV P1,A LCALL DELAY_1MS inc r6 cjne r6,#100,display1 RET TABLE: DB 11000000B,11111001B,10100100B,10110000B,10011001B, DB 10010010B,10000010B,11111000B,10000000B,10010000B ;*************************************************************************** ;CONV subroutine converts Hex to Decimal in term of Unit, Tens and Hundreds ;contained in R0, R1, R2 ;*************************************************************************** CONV: MOV A,R7 MOV B,#10 DIV AB MOV R0,B ;DON VI ean T16 g
  17. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh MOV B,#10 DIV AB MOV R1,B ;CHUC MOV R2,A ;TRAM RET ;*************************************************************************** ;reading keys with external interrupt 1 ;*************************************************************************** isr_1: setb inc_key INC R7 jnb inc_key,$ ;wait until key released reti ;*************************************************************************** ;main program starts here MAIN: ACALL INIT REPEAT: LCALL CONV LCALL DISPLAY SJMP REPEAT END ; ******************************************************************* ; --novasờ ; b. keypad, reads keypad & send to PC via Serial port ; low nibble for rows, high nibble for columns org 0 ; ******************************************************************* main: lcall in_hex ; get code from keypad lcall htoa ; convert to ascii lcall outchar ; send to terminal sjmp main ; ******************************************************************* ; debounce keypress and key release ; ******************************************************************* in_hex: mov r3,#50 ; debounce count back: lcall get_key ; key pressed? jnc in_hex ; no - check again djnz r3,back ; yes - repeat 50 times push acc ; save key code back2: mov r3,#50 ; wait for key release back3: lcall get_key ; key still pressed? jc back2 ; yes - keep checking djnz r3,back3 ; no - repeat 50 times pop acc ; recover key code ret ; ******************************************************************* ; get keypad status - return with C = 0 if no key pressed ; - return with C = 1, and key in acc. if pressed ean T17 g
  18. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh ; ******************************************************************* get_key: mov a,#0feh ; start with column 0 mov r6,#4 ; use r6 as a counter test_next: movx p1,a ; activate column line mov r7,a ; save a mov a,p1 ;read back from keypad anl a,#0f0h ; isolate row lines cjne a,#0f0h,key_hit ; row line active? mov a,r7 ; no - move to next column line rl a djnz r6,test_next clr c ; no key pressed sjmp exit ; return with c = 0 key_hit: mov r7,a ; save row code in r7 mov a,#4 ; prepare to calc column weighting clr c subb a,r6 ; 4-r6 = column weighting mov r6,a ; save in r6 mov a,r7 ; restore scan code in acc swap a ; put scan code in low nibble mov r5,#4 ; use r5 as counter again: rrc a ; rotate until zero bit found jnc done ; done when c = 0 inc r6 ; add 4 until active row found inc r6 inc r6 inc r6 djnz r5,again done: setb c ; c = 1 (key pressed) mov a,r6 ; hex code in acc. lcall lookup_key ; change mapping to suit your keypad exit: ret ; ******************************************************************* ; hex to ascii ; ******************************************************************* htoa: anl a,#0fh cjne a,#0ah,$+3 jc htoa2 add a,#7 htoa2: add a,#'0' ret ; ******************************************************************* ; send char out serial port ; ******************************************************************* outchar: jnb ti,outchar ; wait until ti is set clr ti ; clear it mov sbuf,a ; send acc to serial buffer ret ; ******************************************************************* ; Keypad lookup table... ; ; We're returning We want to return ; this.... this.... ; ean T18 g
  19. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh ; 3 2 1 0 -> 123A ; 7 6 5 4 -> 456B ; B A 9 8 -> 789C ; F E D C -> E0FD ; ; Hence, we need a mapping as shown: ; ; 0123 4567 89AB CDEF ; Original ; A321 B654 C987 D#0* ; Final ; ; The acc. contains the original value, so we need to add this value ; to the start of the table to get the new value. ; ******************************************************************* lookup_key: mov dptr,#key_tab movc a,@a+dptr ret key_tab: db 0Ah,3,2,1,0Bh,6,5,4,0Ch,9,8,7,0Dh,0Fh,0,0Eh ;depending on your kepad, you may change to this ;db 0,1,2,3,4,5,6,7,8,9,0AH,0Bh,0Ch,0Dh,0Eh,0Fh End Bậi 8. A m thanh (Thíc hậnh: tả trấ dặng vắng l–p) o 8.1. Muc ề ễ ch a. Tao ựm thanh tã n ś 440Hz khễ ng dâ bo ểi thỗi ng nh b. Tao 2 ựm thanh tã n ś 500Hz vố 250Hz ểó ng thỗi c. Tao chuữi ựm thanh củ tã n ś gim dã n d. Tao chuữi ựm thanh tã n ś biấ thiịn tĐ ng vố gim n 8.2. Chớ ẫ ng trınh ;*************************************************************************** ;* --novas-- ;* a. Single Sound 440 Hz ( T=2273us) ;*************************************************************************** ; RESET ;reset routine ORG 0 ;locate routine at 00H AJMP START ;jump to START ;************************************************************************** ; Real code starts below. DELAY440: ;millisecond delay routine MOV R7,#02CH ;put value of 2C (44 in decimal) in register R7 LOOPA: INC R7 ;increase R7 by one (R7 = R7 +1) MOV A,R7 ;move value in R7 to Accummulator (also known as A) CJNE A,#0FFH,LOOPA ;compare A to FF hex (255). If not equal go to LOOPA RET ;return to the point that this routine was called from ;************************************************************************** ; START: ;main program (on power up, program starts at this point) CPL P1.0 ;ComPLement (invert) P1.0 (this makes LED change) ACALL DELAY440 ;go to above routine that causes a delay AJMP START END ;end program ean T19 g
  20. Gi´ o trı bai tôp r a i n g.t n 9 5 1 gn K 8K nh ;*************************************************************************** ;* -- novas ờ b. Alternating Tones: 500Hz and 250Hz ;*************************************************************************** RESET ;reset routine ORG 0H ;locate routine at 00H AJMP START ;jump to START ; ;************************************************************************** ; INTERRUPTS (not used) ;place interrupt routines at appropriate ;memory locations ORG 03H ;external interrupt 0 RETI ORG 0BH ;timer 0 interrupt RETI ORG 13H ;external interrupt 1 RETI ORG 1BH ;timer 1 interrupt RETI ORG 23H ;serial port interrupt RETI ORG 25H ;locate beginning of rest of program ;************************************************************************** INITIALIZE: ;set up control registers MOV TCON,#00H MOV TMOD,#00H MOV PSW,#00H MOV IE,#00H ;disable interrupts RET ;************************************************************************** ; Real code starts below. ;************************************************************************** DELAYMS: ;millisecond delay routine ; ; MOV R7,#00H ;put value of 0 in register R7 LOOPA: INC R7 ;increase R7 by one (R7 = R7 +1) MOV A,R7 ;move value in R7 to Accumlator (also known as A) CJNE A,#0FFH,LOOPA ;compare A to FF hex (256). If not equal go to LOOPA RET ;return to the point that this routine was called from ;************************************************************************** DELAYHS: ;half second delay above millisecond delay ; ; MOV R6,#080H ;put 0 in register R6 (R6 = 0) MOV R5,#002H ;put 2 in register R5 (R5 = 2) LOOPB: INC R6 ;increase R6 by one (R6 = R6 +1) ACALL DELAYMS ;call the routine above. It will run and return to here. MOV A,R6 ;move value in R6 to A JNZ LOOPB ;if A is not 0, go to LOOPB DEC R5 ;decrease R5 by one. (R5 = R5 -1) MOV A,R5 ;move value in R5 to A JNZ LOOPB ;if A is not 0 then go to LOOPB. RET ;************************************************************************** TONE_ONE: ;About 500 Hz ean T20 g

CÓ THỂ BẠN MUỐN DOWNLOAD

Đồng bộ tài khoản