MCS-51 Nguyn Quc Cng B mn 3i HBK HN MCS-51 & Assembly language 2 Plan Tp lnh MCS-51 Ngn ng Assembly s dng phn mm Keil Gii thiu Phn mm m phng mch in Proteus V d MCS-51 & Assembly language 3 Tp lnh Vi x l u c mt tp lnh Cc lnh c c ct trong b nh chng trnh di dng cc m lnh nh phn thun tin cho vic lp trnh: Cc m lnh c quy c vit dng gi nh (mnemonic) Cn chng trnh dch cc mnemonic qua m my (Assembler): Hp ng MCS-51 & Assembly language 4 Chng trnh s dng hp ng EDITOR PROGRAM ASSEMBLER PROGRAM LINKER PROGRAM OH PROGRAM Myfile.asm Myfile.obj Other obj file Myfile.lst Myfile.abs Myfile.hex MCS-51 & Assembly language 5 Mt s thut ng Assembly: l mt cch th hin cc code lnh ca mt CPU di dng cc k hiu m con ngi c th d dng c Assembler: phn mm chy trn my tnh c kh nng dch cc file vit bng Assembly sang dng object file Object code: Cha cc m lnh dng binary Object file: Object code Thng tin v relocation phc v cho linhker MCS-51 & Assembly language 6 Linhker Chng trnh c kh nng tp hp cc object file to thnh mt chng trnh duy nht c th executable Segment L mt n v b nh code hoc data. C nhiu kiu segment trong mt chng trnh Assembly Absolute segment: c nh khng th thay i Relocatable segment: c cc thuc tnh i km cho php linker ghp nhiu segments li vi nhau trc khi to ra mt chng trnh executable MCS-51 & Assembly language 7 Tp lnh MCS-51 Gm 255 lnh: 139 lnh 1 byte 92 lnh 2 byte 24 lnh 3 byte c chia thnh cc nhm lnh S hc Logic Chuyn data Kim tra iu kin (Boolean Instructions) iu khin chng trnh MCS-51 & Assembly language 8 Assembly cho MCS-51 (Standard) Keil cung cp 4 tools A51 Macro Assembly (\Keil\C51\Bin\A51.EXE) BL51 Linker/Locator (\Keil\C51\Bin\BL51.EXE) LIB51 Library Manager (\Keil\C51\Bin\LIB51.EXE) OH51 Object-Hex Converter (\Keil\C51\Bin\OH51.EXE) MCS-51 & Assembly language 9 Cc bc to mt chng trnh s dng Keil ASM PL/M 51: l ngn ng bc cao c Intel pht trin vo nhng nm 1980 MCS-51 & Assembly language 10 Cu trc mt chng trnh ASM Cc lnh (statements) ASM gm Cc t kha iu khin chng trnh assembler (assembler control) Cc t kha nh hng cho chng trnh dch (assembler directive) Cc lnh my (instruction) Mi dng ch c th cha duy nht mt lnh assembler control hoc assembler directive hoc mnemonic Mi lnh phi vit trn mt dng Cc tham bin hoc cc ton hng ca cc lnh c phn bit vi cc lnh t nht bi mt space Chng trnh assembly phi c kt thc bi asm directive END, cc statements sau t kha END s b b qua MCS-51 & Assembly language 11 $TITLE (Demo Program #1) CSEG AT 0000h JMP $ END TITLE assembler control JMP instruction mnemonic CSEG, END assembler derective MCS-51 & Assembly language 12 Lnh my [;comment] [, operand] [, operand] [operand] mnemonic [label:] [ ] c ngha rng trng l optional label: nhn c dng nh du a ch m ti lnh c t mnemonic dng gi nh ca lnh my operand ton hng comment li ch thch MCS-51 & Assembly language 13 Directive nh hng cho chng trnh dch ASM Cch x l cc lnh mnemonic trong chng trnh nh ngha cc k t, t trc cc nh,... Khng sinh ra code lnh executable (ngoi tr mt s directive DB v DW), bao gm cc nhm: Segment control Symbol definition Memory Initialization Reserving Memory Program linkage Address control Other MCS-51 & Assembly language 14 Segment control Generic Segments C tn v mt lp cc thuc tnh i km Cc Generic Segment c cng tn cc object file khc nhau s c ghp li thnh mt segment khi linhker Cc t kha: SEGMENT, RSEG Absolute Segments Dng nh ngha cc vng nh xc nh Cc t kha: CSEG, BSEG, DSEG, ISEG, XSEG MCS-51 & Assembly language 15 SEGMENT segment SEGMENT class reloctype alloctype segment tn ca segment SEGMENT t kha class ch ra khng gian cc lp b nh (code, data, idata, xdata...) reloctype xc nh hot ng relocation cho linker allotype xc nh hot ng allocation cho linker MCS-51 & Assembly language 16 RSEG RSEG segment RSEG t kha segment tn ca generic segment c nh ngha trc bi t kha SEGMENT RSEG c dng La chn mt generic segment c nh ngha bi SEGMENT V d: MYPROG SEGMENT CODE ; declare a segment RSEG MYPROG ; select the segment MOV A, #0 MOV P0, A MCS-51 & Assembly language 17 BSEG, CSEG, DSEG, ISEG, XSEG BSEG AT address defines an absolute BIT segment. CSEG AT address defines an absolute CODE segment. DSEG AT address defines an absolute DATA segment. ISEG AT address defines an absolute IDATA segment. XSEG AT address defines an absolute XDATA segment. Thng bo vi chng trnh linker mt segment ti mt a ch nh trc MCS-51 & Assembly language 18 inh ngha cc k hiu (Symbol directive) Cho php s dng cc k hiu biu din thanh ghi. a ch, cc s Cc k hiu c nh ngha s khng c nh ngha li, ngoi tr lnh SET MCS-51 & Assembly language 19 EQU, SET symbol EQU expression symbol EQU register symbol SET expression symbol SET register symbol tn ca k hiu expression biu thc s, c th cha cc k hiu nhng phi c khai bo trc register thanh ghi: A, R0-R7 symbol c nh ngha vi EQU s khng c nh ngha li Khng th s dng EQU nh ngha li cc symbol nh ngha bi SET v ngc li MCS-51 & Assembly language 20 V d: LIMIT EQU 1200 VALUE EQU LIMIT 200 + 'A' SERIAL EQU SBUF ACCU EQU A COUNT EQU R5 VALUE SET 100 VALUE SET VALUE / 2 COUNTER SET R1 TEMP SET COUNTER TEMP SET VALUE * VALUE MCS-51 & Assembly language 21 BIT, DATA, CODE, IDATA, XDATA symbol BIT bit_address defines a BIT symbol symbol CODE code_address defines a CODE symbol symbol DATA data_address defines a DATA symbol symbol IDATA idata_address defines an IDATA symbol symbol XDATA xdata_address defines a XDATA symbol symbol is the name of the symbol to define. The symbol name can be used anywhere an address of this memory class is valid. bit_address is the address of a bit in internal data memory in the area 20H .. 2FH or a bit address of an 8051 bit-addressable SFR. code_address is a code address in the range 0000H .. 0FFFFH. data_address is a data memory address in the range 0 to 127 or a special function register (SFR) address in the range 128 .. 255. idata_address is an idata memory address in the range 0 to 255. xdata_address is an xdata memory address in the range 0 to 65535. MCS-51 & Assembly language 22 V d: DATA_SEG SEGMENT BITADDRESSABLE RSEG DATA_SEG ; a bit-addressable rel_seg ENABLE_FLAG BIT 60H ; an absolute bit DONE_FLAG BIT 24H.2 ; an absolute bit P1_BIT2 EQU 90H.2 ; a SFR bit RESTART CODE 00H INTVEC_0 CODE RESTART + 3 INTVEC_1 CODE RESTART + 0BH INTVEC_2 CODE RESTART + 1BH SERBUF DATA SBUF ; redfinition of SBUF RESULT DATA 40H RESULT2 DATA RESULT + 2 PORT1 DATA 90H ; a SFR symbol BUFFER IDATA 60H BUF_LEN EQU 20H BUF_END IDATA BUFFER + BUF_LEN 1 MCS-51 & Assembly language 23 sfr, sbit sfr sfr_symbol = address; sbit sfr_symbol = bit-address; sfr_symbol is the name of the special function register (SFR) symbol to define. address is an SFR address in the range 0x80 0xFF. bit-address is address of an SFR bit in the format address ^ bitpos or sfr_symbol ^ bitpos. address or sfr_symbol refers to an bitaddressable SFR and bitpos specifies the bit position of the SFR bit in the range 0 7. V d: sfr P0 = 0x80; sfr P1 = 0x90; sbit P0_0 = P0^0; sbit P1_1 = 0x90^1; MCS-51 & Assembly language 24 Khi to b nh (Memory initialization) Khi to cc gi tr trong b nh code T kha DB define byte (8 bits) DW define word (16 bits) MCS-51 & Assembly language 25 label: DB expression , expression ... label: DW expression , expression label: is the symbol that is given the address of the initialized memory. expression is a byte value. Each expression may be a symbol, a character string, or an expression. V d: REQUEST: DB 'PRESS ANY KEY TO CONTINUE', 0 TABLE: DW TABLE, TABLE + 10, ZERO ZERO: DW 0 MCS-51 & Assembly language 26 Reserving memory Khai bo dung lng vng b nh s c s dng T kha DBIT khai bo vng b nh bit DS khai bo vng b nh byte MCS-51 & Assembly language 27 DBIT, DS label: DBIT expression label: DS expression label is the symbol that is given the address of the reserved memory. The label is a typeless number and gets the current address value and the memory class of the active segment. The label can only be used where a symbol of this type is allowed. expression is the number of bit (DBIT) or bytes (DS) to reserve. The expression cannot contain forward references, relocatable symbols, or external symbols. ON_FLAG: DBIT 1 ; reserve 1 bit OFF_FLAG: DBIT 1 GAP: DS (($ + 16) AND 0FFF0H) $ DS 20 TIME: DS 8 V d: MCS-51 & Assembly language 28 Lin kt chng trnh (Program linkage) S dng cc inh hng lin kt chng trnh cho php Phn chia mt chng trnh thnh cc module (file) ring bit Cc module trao i vi nhau thng qua tn v cc tham chiu C 3 kiu nh hng PUBLICa ra danh sch cc k hiu (symbol) c dng bn ngoi module. Symbol phi c nh ngha trong module co t kha PUBLIC EXTRN a ra danh sch cc symbol m module c th s dng nhng cc symbol ny c nh ngha trong module khc NAME ch ra tn ca mudule MCS-51 & Assembly language 29 PUBLIC PUBLIC symbol , symbol symbol must be a symbol that was defined somewhere within the source file. Forward references to symbol names are permitted. All symbol names, with the exception of register symbols and segment symbols, may be specified with the PUBLIC directive. Multiple symbols must be separated with a comma (,). V d: PUBLIC PUT_CRLF, PUT_STRING, PUT_EOS PUBLIC ASCBIN, BINASC PUBLIC GETTOKEN, GETNUMBER MCS-51 & Assembly language 30 EXTRN EXTRN class (symbol , symbol ) class is the memory class where the symbol has been defined and may be one of the following: BIT, CODE, DATA, IDATA, XDATA, or NUMBER (to specify atypeless symbol). symbol is an external symbol name. V d: EXTRN CODE (PUT_CRLF), DATA (BUFFER) EXTERN CODE (BINASC, ASCBIN) MCS-51 & Assembly language 31 NAME NAME modulename modulename is the name to use for the object module and can be up to 40 characters long. The modulename must adhere to the rules for symbol names. Nu tn ca module khng c ch ra bi directive NAME th tn file (khng c phn m rng) s c s dng Ch c mt NAME c s dng trong mt file MCS-51 & Assembly language 32 Address control nh hng cho chng trnh dch bit a ch ni s t code lnh hoc a ch ca cc thanh ghi s dng C 2 kiu ORG thay i ni dung ca b m a ch n mt gi tr mi. Cc statements tip theo sau s c t bt u t a ch mi USING ch ra bank thanh ghi no c s dng cho R0 n R7 MCS-51 & Assembly language 33 ORG ORG expression expression must be an absolute or simple relocatable expression without any forward references. Only absolute addresses or symbol values in the current segment may be used. Nu ORG xut hin trong mt absolute segment th gi tr expression l gi tr tuyt i Nu ORG xut hin trong mt relocatable segment th gi tr ca expression c coi l gi tr offset V d: ORG 100H ORG RESTART MCS-51 & Assembly language 34 USING USING expression expression is the register bank number which must be a value between 0 and 3. USING khng to ra bt k mt code lnh thay i register bank, n ch n gin l s dng a ch tuyt i ca cc thanh ghi USING 3 PUSH AR2 ; Push register 2 in bank 3 USING 1 PUSH AR2 ; Push register 2 in bank 1 MCS-51 & Assembly language 35 Phn mm Proteus Phn mm ca hng Labcenter Electronics Ltd (http://www.labcenter.co.uk) Gi phn mm V mch nguyn l, mch in M phng s dng SPICE VSM (Virtrual System Modelling) cho php m phng ch mixed gia tng t v s (bao gm c cc h thng c s dng cc chip vi iu khin) Gi phn mm ty thuc vo tnh nng m khch hng la chn Ti liu tham kho c rt t, ch yu l cc file help i km MCS-51 & Assembly language 36 MCS-51 & Assembly language 37 V d 1 Vit chng trnh assembler kim tra cng P1.5, nu bng 0 th thit lp P1.6 = 1 bng 1 th thit lp P1.6 = 0 Qu trnh kim tra clp li cho n khi tt ngun MCS-51 & Assembly language 38 V d 2 Vit chng trnh assembler pht ra xung vung c chu k 20ms vi duty cycle 50% ti cng P1.0 (s dng timer). Bit tn s clock ca MCU l 12MHz