Dựng ngắt ngoàị

Một phần của tài liệu TRƯ NG BI H C K THU T CÔNG NGHI PKHOA I N TMÔN K THU T MÁY TÍNHBÀI GI NG PHÁT CHO SINH (Trang 151)

Dưới đõy là một vớ dụ khỏc của sự thực thi ngắt. Một ngắt ngoài được tạo ra khi một logic khụng (0) là xảy ra trờn chõn P3.2 hoặc chõn P3.3. Tựy thuộc vào đú là đầu vào hoạt động nào, một trong hai cụng việc sẽ được thực thi:

Hỡnh 4-4

Một logic số mức khụng (0) trờn P3.2 khởi tạo sự thực thi ngắt Isr_Int0, vỡ thế số tăng dần trong R0 được sao chộp ra cổng P0. Logic số mức khụng trờn P3.3 khởi tạo sự thực thi chương trỡnh con ngắt Isr_Int1, số tăng dần trong R1 được sao chộp sang P1.

Trong ngắn hạn, mỗi lần bấm vào cỏc nỳt nhấn INT0 và INT1 sẽ được tớnh và ngay lập tức được hiển thị ở định dạng nhị phõn trờn cổng thớch hợp (LED mà chung dương). ;************************************************************************ ;* PROGRAM NAME : Int.ASM

;* DESCRIPTION : Program counts interrupts INT0 generated by appearance of high-to-low

;* transition signal on pin P3.2 Result appears on port P0. Interrupts INT1 are also

;* counted up at the same timẹ They are generated byappearing high-to- low transition

;* signal on pin P3. The result appears on port P1.

;************************************************************************ ;BASIC DIRECTIVES $MOD53 $TITLE(INT.ASM) $PAGEWIDTH(132) $DEBUG $OBJECT $NOPAGING ;RESET VECTORS CSEG AT 0

JMP XRESET ; Reset vector

ORG 003H ; Interrupt routine ađress for INT0

JMP Isr_Int0

ORG 013H ; Interrupt routine ađress for INT1

JMP Isr_Int1 ORG 100H XRESET:

MOV TCON,#00000101B ; Interrupt INT0 is generated ;by appearing

; high-to-low transition signal on pin P3.2 ; Interrupt INT0 is generated by appearing ; high-to-low transition signal on pin P3.3 MOV IE,#10000101B ; Interrupt enabled

MOV R0,#00H ; Counter starting value MOV R1,#00H

MOV P0,#00H ; Reset port P0 MOV P1,#00H ; Reset port P1 LOOP: SJMP LOOP ; Remain here Isr_Int0:

INC R0 ; Increment value of interrupt INT0 counter MOV P0,R0

RETI Isr_Int1:

INC R1 ; Increment value of interrupt INT1 counter MOV P1,R1

RETI

END ; End of program 4.2.5 Lập trỡnh ngắt ngoài theo sườn xuống.

Phỏt hiện nếu cú sườn xuống thỡ sinh ngắt, bật loạ ;Int1_Edge_Trigger

; Chõn 1.3 nối với loa

; Khi cú cạnh xuống ở INT1 -> bật ;loa 1 lỳc rồi tắt

ORG 0000H LJMP MAIN ;ISR của INT1

153 ORG 0013H ;INT1 ISR

SETB P1.3 ;bật loa MOV R3,#255

BACK: DJNZ R3,HERE ;delay 1 chỳt CLR P1.3 ;tắt loa RETI ;

;MAIN program for initialization ORG 30H (adsbygoogle = window.adsbygoogle || []).push({});

MAIN: SETB TCON.2 ;INT1: cạnh MOV IE,#10000100B ;EN ngắt ngoài 1 HERE: SJMP HERE ;chờ ngắt END

4.2.6 Sử dụng LED 7 thanh

Cỏc vớ dụ sau đõy mụ tả việc sử dụng đốn LED hiển thị 7 thanh. LED loại õm chung. Kể từ khi phổ biến cỏch tư duy logic là một (1) bật cỏi gỡ đú lờn và khụng logic (0) tắt cỏi gỡ đú, đốn hiển thị tiờu thụ dũng điện nhỏ (mức tiờu thụ điện năng thấp) và cỏc cực được nối với điện trở khỏ caọ

Để tiết kiệm chõn I/O, bốn LED hiển thị được kết nối để hoạt động ở chế độ multiplex. Nú cú nghĩa là tất cả cỏc đoạn cú cựng tờn được kết nối với một cổng ra, và chỉ cú một màn hỡnh LED hoạt động tại một thời điểm, ta gọi là quột LED.

Tranzistors và segmenats trờn màn hỡnh này nhanh chúng được kớch hoạt, do đú làm cho ta tưởng rằng tất cả cỏc chữ số đang hoạt động đồng thờị

4.2.7 Viết chữ số trờn LED 7 thanh

Chương trỡnh này là một loại bài tập làm "núng lờn" trước khi làm việc thực tế. Mục đớch của vớ dụ này là để hiển thị trờn màn hỡnh bất cứ điều gỡ đú. Chế độ Multiplex khụng được sử dụng thời gian nàỵ Thay vào đú, 3 chữ số được hiển thị trờn duy nhất một trong số đú (đầu tiờn bờn phải).

Kể từ khi với vi điều khiển, "khụng biết" làm thế nào để viết số 3, một chương trỡnh con nhỏ gọi là Disp được sử dụng (vi điều khiển viết số này là 0000 0011). Điều này cho phộp chương trỡnh con hiển thị tất cả cỏc chữ số thập phõn (0-9). Nguyờn tắc hoạt động rất đơn giản. Một số muốn hiển thị, được cộng vào địa chỉ hiện tại và lệnh nhảy được thực thi, số khỏc muốn hiển thị, sẽ được nhảy đến địa chỉ khỏc.

;************************************************************************ ;* PROGRAM NAME : 7Seg1.ASM

;* DESCRIPTION: Program displays number "3" on 7-segment LED display ;************************************************************************ ;BASIC DIRECTIVES $MOD53 $TITLE(7SEG1.ASM) $PAGEWIDTH(132) $DEBUG $OBJECT $NOPAGING ;STACK

Hỡnh 4-5 DSEG AT 03FH

STACK_START: DS 040H ;RESET VECTORS

CSEG AT 0

JMP XRESET ; Reset vector ORG 100H

XRESET: MOV SP,#STACK_START ; Define Stack pointer

MOV P1,#0 ; Turn off all segments on displays

MOV P3,#20h ; Activate display D4 LOOP:

MOV A,#03 ; Send number “3” to display LCALL Disp ; Perform appropriate masking for the number MOV P1,A

SJMP LOOP

Disp: ; Subroutine for displaying digits INC A MOVC A,@A+PC RET DB 3FH ; Digit 0 mask DB 06H ; Digit 1 mask DB 5BH ; Digit 2 mask DB 4FH ; Digit 3 mask

155 DB 66H ; Digit 4 mask DB 6DH ; Digit 5 mask DB 7DH ; Digit 6 mask DB 07H ; Digit 7 mask DB 7FH ; Digit 8 mask DB 6FH ; Digit 9 mask END ; End of program

4.2.8 Thụng bỏo bằng văn bản trờn màn hỡnh LCD

Vớ dụ này sử dụng loại LCD phổ biến nhất để hiển thị văn bản trong hai dũng với 16 ký tự mỗi dũng. Để tiết kiệm chõn IO của vi điều khiển, chỉ cú 4 chõn được sử dụng cho giao tiếp dữ liệụ Bằng cỏch này, mỗi byte được truyền đi theo hai bước: đầu tiờn là 4 bit cao sau đú là 4 bit thấp.

LCD cần phải được khởi tạo tại đầu chương trỡnh (trước khi sử dụng cỏc tớnh năng ghi đọc LCD). Bờn cạnh đú, cỏc phần của chương trỡnh lặp đi lặp lại trong chương trỡnh tạo ra một chương trỡnh con đặc biệt. Tất cả điều này cú vẻ rất phức tạp, nhưng toàn bộ chương trỡnh về cơ bản thực hiện một số hoạt động đơn giản và hiển thị dũng chữ “LCD display”.

Hỡnh 4-6

************************************************************************* ;* PROGRAM NAME : Lcd.ASM

;* DESCRIPRTION : Program for testing LCD displaỵ 4-bit communication ;* is used. Program does not check BUSY flag but uses program delay ;* between 2 commands. PORT1 is used for connection

;* to the microcontroller. ;************************************************************************ ;BASIC DIRECTIVES $MOD53 $TITLE(LCD.ASM) $PAGEWIDTH(132) $DEBUG $OBJECT $NOPAGING ;Stack DSEG AT 0E0h

Stack_Start: DS 020h Start_ađress EQU 0000h ;Reset vectors CSEG AT 0 ORG Start_ađress JMP Inic ORG Start_ađress+100h

MOV IE,#00 ; All interrupts are disabled (adsbygoogle = window.adsbygoogle || []).push({});

MOV SP,#Stack_Start

Inic: CALL LCD_inic ; Initialize LCD ;*************************************************

;* MAIN PROGRAM

;*************************************************

START: MOV A,#80h ; Next character will appear on the first CALL LCD_status ; location in the first line

;of LCD displaỵ

MOV A,#' ' ; Display character ‘ ’. CALL LCD_putc ; Call subroutine for

;character transmission. MOV A,#' ' ; Display character ‘ ’. CALL LCD_putc

MOV A,#' ' ; Display character ‘ ’. CALL LCD_putc

MOV A,#' ' ; Display character ‘ ’. CALL LCD_putc

MOV A,#' ' ; Display character ‘ ’. CALL LCD_putc

MOV A,#' ' ; Display character ‘ ’. CALL LCD_putc

MOV A,#'L' ; Display character ‘L’. CALL LCD_putc

MOV A,#'C' ; Display character ‘C’. CALL LCD_putc

MOV A,#'D' ; Display character ‘D’. CALL LCD_putc

MOV A,#0c0h ; Next character will appear; ; on the first

CALL LCD_status ; location in the second line ;of LCD displaỵ

MOV A,#' ' ; Display character ‘ ’. CALL LCD_putc ; Call subroutine for

;character transmission. MOV A,#' ' ; Display character ‘ ’. CALL LCD_putc

MOV A,#' ' ; Display character ‘ ’. CALL LCD_putc

MOV A,#' ' ; Display character ‘ ’. CALL LCD_putc

MOV A,#'D' ; Display character ‘D’. CALL LCD_putc

MOV A,#'ớ ; Display character ‘i’. CALL LCD_putc

MOV A,#'s' ; Display character ‘s’. CALL LCD_putc

157 MOV A,#'p' ; Display character ‘p’.

CALL LCD_putc

MOV A,#'l' ; Display character ‘l’. CALL LCD_putc

MOV A,#'ỏ ; Display character ‘a’. CALL LCD_putc

MOV A,#'ý ; Display character ‘y’. CALL LCD_putc

MOV R0,#20d ; Wait time (20x10ms) CALL Delay_10ms (adsbygoogle = window.adsbygoogle || []).push({});

MOV DPTR,#LCD_DB ; Clear display MOV A,#6d

CALL LCD_inic_status

MOV R0,#10d ; Wait time(10x10ms) CALL Delay_10ms

JMP START

;********************************************* ;* Subroutine for wait time (T= r0 x 10ms) ;*********************************************

Delay_10ms: MOV R5,00h ; 1+(1+(1+2*r7+2)*r6+2)*r5 approximately MOV R6,#100d ; (if r7>10)

MOV R7,#100d ; 2*r5*r6*r7

DJNZ R7,$ ; $ indicates current instruction. DJNZ R6,$-4 DJNZ R5,$-6 RET ;************************************************************************ * ;* SUBROUTINE: LCD_inic

;* DESCRIPTION: Subroutine for LCD initialization. ;*

;* (is used with 4-bit interface, under condition that pins DB4-7 on LCD ;* are connected to pins PX.4-7 on microcontroller’s ports,

; ịẹ four higher

;* bits on the port are used). ;*

;* NOTE: It is necessary to define port pins for controlling LCD operation:

;* LCD_enable, LCD_read_write,

; LCD_reg_select,similar to port for connection to LCD.

;* It is also necessary to define ađresses for the first character in each

;* linẹ

;************************************************************************ *

LCD_enable BIT P1.3 ; Bit for activating pin E on LCD. LCD_read_write BIT P1.1 ; Bit for activating pin RW on LCD. LCD_reg_select BIT P1.2 ; Bit for activating pin RS on LCD. LCD_port SET P1 ; Port for connection to LCD.

Busy BIT P1.7 ; Port pin on which Busy flag appears. LCD_Start_I_red EQU 00h ; Ađress of the first message character

; in the first line of LCD displaỵ LCD_Start_II_red EQU 40h ; Ađress of the first message character

; in the second line of LCD displaỵ LCD_DB: DB 00111100b ; 0 -8b, 2/1 lines, 5x10/5x7 format

DB 00011000b ; 2 -Display/cursor shift, right/left DB 00001100b ; 3 -Display ON, cursor OFF,

;cursor blink off

DB 00000110b ; 4 -Increment mode, display shift off

DB 00000010b ; 5 -Display/cursor home DB 00000001b ; 6 -Clear display

DB 00001000b ; 7 -Display OFF, cursor OFF, ; cursor blink off

LCD_inic:

;***************************************** MOV DPTR,#LCD_DB

MOV A,#00d ; Triple initialization in 8-bit CALL LCD_inic_status_8 ; mode is performed at the beginning MOV A,#00d ; (in case of slow increment of (adsbygoogle = window.adsbygoogle || []).push({});

CALL LCD_inic_status_8 ; power supply when the power supply is on

MOV A,#00d

lcall LCD_inic_status_8

MOV A,#1d ; Change from 8-bit into CALL LCD_inic_status_8 ; 4-bit mode MOV A,#1d

CALL LCD_inic_status

MOV A,#3d ; As from this point the program executes in ;4-bit mode CALL LCD_inic_status MOV A,#6d CALL LCD_inic_status MOV A,#4d CALL LCD_inic_status RET LCD_inic_status_8: ;****************************************** PUSH B MOVC A,@A+DPTR

CLR LCD_reg_select ; RS=0 - Write command CLR LCD_read_write ; R/W=0 - Write data on LCD

MOV B,LCD_port ; Lower 4 bits from LCD port are memorized

ORL B,#11110000b ORL A,#00001111b ANL A,B

MOV LCD_port,A ; Data is moved from A to LCD port SETB LCD_enable ; high-to-low transition signal

; is generated on the LCD's EN pin CLR LCD_enable

MOV B,#255d ; Time delay in case of improper reset DJNZ B,$ ; during initialization

DJNZ B,$ DJNZ B,$ POP B RET

159 LCD_inic_status: ;************************************************************************ * MOVC A,@A+DPTR CALL LCD_status RET ;************************************************************************ * ;* SUBROUTINE: LCD_status

;* DESCRIPTION: Subroutine for defining LCD status.

;************************************************************************ * LCD_status: PUSH B MOV B,#255d DJNZ B,$ DJNZ B,$ DJNZ B,$

CLR LCD_reg_select ; RS=O: Command is sent to LCD CALL LCD_port_out

SWAP A ; Nibles are swapped in accumulator DJNZ B,$

DJNZ B,$ DJNZ B,$

CLR LCD_reg_select ; RS=0: Command is sent to LCD CALL LCD_port_out

POP B RET

;************************************************************************ ;* SUBROUTINE: LCD_putc

;* DESCRIPTION: Sending character to be displayed on LCD.

;************************************************************************ LCD_putc: PUSH B

MOV B,#255d DJNZ B,$

SETB LCD_reg_select ; RS=1: Character is sent to LCD CALL LCD_port_out

SWAP A ; Nibles are swapped in accumulator DJNZ B,$

SETB LCD_reg_select ; RS=1: Character is sent to LCD CALL LCD_port_out POP B RET ;************************************************************************ * ;* SUBROUTINE: LCD_port_out

;* DESCRIPTION: Sending commands or characters on LCD display

;************************************************************************ * (adsbygoogle = window.adsbygoogle || []).push({});

LCD_port_out: PUSH ACC PUSH B

ORL B,#11110000b ORL A,#00001111b ANL A,B

MOV LCD_port,A ; Data is copied from A to LCD port SETB LCD_enable ; high-to-low transition signal

; is generated on the LCD's EN pin CLR LCD_enable

POP B POP ACC RET

END ; End of program 4.2.9 Nhận dữ liệu qua UART

Để kớch hoạt tớnh năng giao tiếp nối tiếp UART thành cụng, điều cần thiết để đỏp ứng cỏc quy định cụ thể của chuẩn RS232. Nú chủ yếu đề cập đến cỏc cấp điện ỏp theo yờu cầu của chuẩn nàỵ Theo đú,-10V đến -3V là mức logic một (1) trong thụng điệp, trong khi 3V đến 10 V là mức logic khụng (0). Cỏc vi điều khiển chuyển đổi chớnh xỏc dữ liệu về định dạng nối tiếp, nhưng nguồn cung cấp điện ỏp chỉ 5V. Từ đú, khụng phải dễ dàng để chuyển đổi 0V thành 10V và 5V thành -10V, Hoạt động này trờn cả đường truyền và đường nhận dữ liệụ Ở đõy, MAX232 của Maxim được sử dụng bởi vỡ nú được phổ biến rộng rói, rẻ và đỏng tin cậỵ

Vớ dụ này cho thấy làm thế nào để nhận được thụng điệp gửi từ PC. Timer T1 tạo tốc độ baud. Từ khi thạch anh 11,0592 MHz được sử dụng, rất dễ dàng để cú được tiờu chuẩn tốc độ truyền mà tốc độ baud là 9600 bps. Mỗi dữ liệu nhận được ngay lập tức được chuyển ra P1.

Hỡnh 4-7

161 ;* PROGRAM NAME : UartR.ASM

;* DESCRIPTION: Each data received from PC via UART appears on the port ;* P1. ;* ;************************************************************************ ;BASIC DIRECTIVES $MOD53 $TITLE(UARTR.ASM) $PAGEWIDTH(132) $DEBUG $OBJECT $NOPAGING ;STACK DSEG AT 03FH STACK_START: DS 040H ;RESET VECTORS CSEG AT 0

JMP XRESET ; Reset vector

ORG 023H ; Starting ađress of ;UART interrupt routine JMP IR_SER

ORG 100H

XRESET: MOV IE,#00 ; All interrupts are disabled MOV SP,#STACK_START ; Initialization of Stack pointer MOV TMOD,#20H ; Timer1 in mode2

MOV TH1,#0FDH ; 9600 baud rate at the frequency of ; 11.0592MHz

MOV SCON,#50H ; Receiving enabled, 8-bit UART MOV IE,#10010000B ; UART interrupt enabled

CLR TI ; Clear transmit flag CLR RI ; Clear receive flag SETB TR1 ; Start Timer1

LOOP: SJMP LOOP ; Remain here

IR_SER: JNB RI,OUTPUT ; If any data is received, ; move it to the port MOV A,SBUF ; P1

MOV P1,A

CLR RI ; Clear receive flag OUTPUT: RETI

END ; End of program 4.2.10Truyền dữ liệu qua UART

Chương trỡnh này mụ tả cỏch sử dụng UART để truyền dữ liệụ Một dóy số (0-255) được truyền đến mỏy PC ở tốc độ truyền 9600 baud. MAX 232 được sử dụng như là một điều ỏp.

;************************************************************************ ;* PROGRAM NAME : UartS.ASM

;* DESCRIPTION: Sends values 0-255 to PC.

;************************************************************************ ;BASIC DIRECTIVES

$MOD53 $TITLE(UARTS.ASM) $PAGEWIDTH(132) $DEBUG $OBJECT $NOPAGING ;STACK DSEG AT 03FH STACK_START: DS 040H ;RESET VECTORS CSEG AT 0

JMP XRESET ; Reset vector ORG 100H

XRESET: MOV IE,#00 ; All interrupts are disabled MOV SP,#STACK_START ; Initialization of Stack pointer MOV TMOD,#20H ; Timer1 in mode 2 (adsbygoogle = window.adsbygoogle || []).push({});

MOV TH1,#0FDH ; 9600 baud rate at the frequency of ; 11.0592MHz

MOV SCON,#40H ; 8-bit UART

CLR TI ; Clear transmit bit CLR RI ; Clear receive flag MOV R3,#00H ; Reset caunter SETB TR1 ; Start Timer 1

START: MOV SBUF,R3 ; Move number from counter to a PC LOOP1: JNB TI,LOOP1 ; Wait here until byte transmission is ; complete

CLR TI ; Clear transmit bit

INC R3 ; Increment the counter value by 1

CJNE R3,#00H,START ; If 255 bytes are not sent return to the

; label START LOOP: SJMP LOOP ; Remain here END ; End of program

4.2.11Chương trỡnh con phục vụ truyền thụng nối tiếp

Serial_Init: ;Khởi tạo:

;Set timer 1 mode to 8-bit Auto-Reload

mov TMOD,#20H

;Enable reception

;Set Serial port mode to 8-bit UART

mov SCON,#50H ;Set baudrate to 9600 at 11.0592MHz mov TH1,#0FDH mov TL1,#0FDH ;Start Timer setb TR1 ret

Serial_Send: ; truyền nội dung thanh ghi A ra UART

163

;sent completely

jnb TI,Serial_Send

;clear the transmit interrupt flag

clr TI

;Then move the data to send in SBUF

mov SBUF,A

ret

Serial_Read: ; nhận từ UART về thanh ghi A

;Wait for Receive interrupt flag

jnb RI,Serial_Read

;If falg is set then clear it

clr RI (adsbygoogle = window.adsbygoogle || []).push({});

;Then read data from SBUF

mov A,SBUF

ret

4.2.12Truyền thụng UART cho 8051 bằng phần mềm

Để thực hiện thành cụng UART đầu tiờn chỳng ta cần phải biết giao thức truyền thụng UART.

Sơ đồ trờn cho thấy dạng súng của một frame truyền. Đầu tiờn là bit bắt đầu .. sau đú 8-bit dữ liệu và tại một bit dừng cuốị Cú một cụng thức bớ mật để tớnh toỏn thời gian trỡ hoón là cú baudrate chớnh xỏc giữa cỏc bit.

Dưới đõy là một phần mềm triển khai UART, trong đú cú thể được sử dụng ở chương trỡnh C cũng như ASM. Nú được viết cho phần mềm Keil. Nhưng với một vài thay đổi nhỏ bạn cú thể dựng nú trong chương trỡnh của bạn.

?SỦPUTC SEGMENT CODE

?SỦGETC SEGMENT CODE PUBLIC _putc

PUBLIC getc

txd_pin EQU P3.1 ;Transmit on this pin

rxd_pin EQU P3.0 ;Receive on this pin

;Formula to calculate the bit time delay constant

;This constant is calculated as: (((crystal/baud)/12) - 5) / 2 ;crystal is the frequency of crystal in Hz

;baud is required baudrate

;Please try to keep baudrate below 9600 ;to get best results :)

BITTIM EQU 45; (((11059200/9600)/12) - 5) / 2 ;---

Một phần của tài liệu TRƯ NG BI H C K THU T CÔNG NGHI PKHOA I N TMÔN K THU T MÁY TÍNHBÀI GI NG PHÁT CHO SINH (Trang 151)