Click here to load reader

bai giang pic.pdf

  • View
    547

  • Download
    11

Embed Size (px)

Text of bai giang pic.pdf

  • Trng i hc Cng nghip Tp. H Ch Minh Khoa Cng ngh in t

    B mn in t Cng nghip

    Bi ging Vi x l

    Lu hnh ni b

  • 2

    Mc lc Chng 1: Gii thiu chung v h vi x l. Gii thiu chung v vi iu khin PIC Chng 2: Gii thiu phn cng ca PIC 16F84 v PIC 16F877A Chng 3: Tp lnh ca PIC 16F877A Chng 4: Vo ra. Hot ng ca b nh thi . Lp trnh vi iu khin PIC 16F84 v PIC 16F877A. Chng 5: Truyn thng ni tip USART Chng 6: Ngt Chng 7: B chuyn i tng t sang s (ADC). Chng 8: B nh d liu EEPROM, PWM, SPI/I2C. Chng 9: Thit k h vi x l v h pht trin vi iu khin.

  • 3

    Bi ging s 1: Gii thiu chung v h vi x l . Gii thiu chung v vi iu khin PIC ( S tit: 5)

    I. n nh lp: . II. Kim tra bi c: .. .. .. III. Tn bi ging: Gii thiu chung v h vi x l. Gii thiu chung v vi

    iu khin PIC III.1. Mc tiu: -Gii thiu h m. Lch s pht trin ca cc b vi x l. Cu trc v chc nng hot ng ca h vi x l. Chc nng v cc thnh phn bn trong ca h vi x l. Gii thiu vi iu khin. Cc h vi x l thong dng. -Gii thiu vi iu khin PIC: b nh chng trnh, xung ng h vi iu khin, h thng vi iu khin. c tnh ca vi iu khin. Cc loi vi iu khin. S dng vi iu khin: phn cng vi iu khin v lp trnh vi iu khin. III.2. dung v phng tin dy hc: -Phn trng, khn, bng, bt long, micro c dy(hay khng dy), my tnh, v n chiu (hay my chiu). III.3. Gio trnh v ti liu tham kho: Gio trnh Vi x l ca trng i hc cng nghip Tp. HCM. D.W. Smith, PIC in practice: a project-based approach, Elsevier, 2nd edition, 2006. Trng Trc, Chip n 16C84 v ng dng ca chng. H Trung M, Vi x l, NXB HQG Tp. HCM, 2003. Ti liu v vi iu khin PIC ca b mn in t cng nghip. Website: http://www.microchip.com/ http://www.alldatasheet.com/ Myke Predko, Programming and customizing the PIC microcontroller, 3rd edition, Tab Electronics, McGrawHill, 2008(Ebook). Douglass V.Hall, Microprocessors and interfacing: Programming and Hardware, 2nd ed., Macmillan/McGraw-Hill, 1992. III.4.Ni dung bi ging: Ni dung chi tit : xem bi ging chi tit. Phng php ging dy: thuyt trnh, nu vn v m thoi trao i vi sinh vin.

    Chng 1: Gii thiu chung v h vi x l. Gii thiu chung v vi iu khin PIC. 1.1. H nh phn, h thp phn v h thp lc phn. H nh phn dung c s 2, dung hai s 0 v 1 biu din. Th d: 01001010=0x27+1x26+0x25+0x24+1x23+0x22+1x21+0x20=64+8+2=74 H thp phn, c s l 10 dng cc s 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 biu din. Th d: 125=1x102+2x101+5x100=100+20+5. H thp lc phn, c s l 16 dng cc s 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. Th d: 32H=3x161+2x160=48+2=50.

  • 4

    Bng chuyn i gia s nh phn, thp phn v thp lc phn: H nh phn (8 ch s) H thp phn (3 ch s) H thp lc phn (2 ch s)00000000 000 00h 00000001 001 01h 00000010 002 02h 00000011 003 03h 00000100 004 04h 00000101 005 05h 00000110 006 06h 00000111 007 07h 00001000 008 08h 00001001 009 09h 00001010 010 0Ah 00001011 011 0Bh 00001100 012 0Ch 00001101 013 0Dh 00001110 014 0Eh 00001111 015 0Fh 1.2. Lch s pht trin ca k thut vi x l. 1.2.1.Ho vi mach so va cong nghe : IC da theo so transistor hoac cac cong thng c chia lam : -SSI : tch hp c nho -MSI: tch hp c trung bnh -LSI: tch hp c ln -VLSI: tch hp c rat ln -ULSI: tch hp c cc ln V du : MSI nh counter, multiplexer, LSI nh cac vi x ly 8 bit : 8085, Z80 VLSI nh cac vi x ly 16 bit , 32 bit: 80386 Gii thieu cong nghe san xuat chip : Ve cong nghe co cac cong nghe che tao vi mach sau : MOS : - NMOS HMOS - PMOS - CMOS HCMOS Vi H : high-performance TTL, IIL (integrated injection logic) thuoc Bipolar Trong o cong nghe MOS thng c a chuong do tch hp c nhieu, tiet kiem nang lng

  • 5

    1.2.2. T MAY TNH LN EN MAY VI TNH: a.May tnh ln :

    La loai may tnh c thiet ke e giai cac bai toan ln vi toc o rat nhanh -No lam viec vi so lieu co o dai t 64 bit hoac hn va c trang b bo nh rat ln, v vay kch thc ln. -Chung thng c dung e ieu khien cac he thong thiet b dung trong quan s hoac cac he thong may moc cua chng trnh nghien cu vu tru, e x ly thong tin trong nganh ngan hang, vv V du : IBM 4381, Honeywell DSP8

    Loai manh nhat trong cac may tnh ln goi la sieu may tnh (supercomputer). V du : Y-MP/832 cua Gray. b.May tnh con :

    La dang thu nho ve kch thc cung nh tnh nang cua may tnh ln. - Lam viec vi d lieu co o dai d lieu 32 bit vi toc o cham hn va bo nh han che - May tnh con thng dung cho cac tnh toan khoa hoc ky thuat, gia cong d lieu qui mo nho hay e ieu khien qua trnh cong nghe. V du : Vax 6360 cua DEC, MV/8000II cua Data general c.May vi tnh :

    May vi tnh la may thong dung hien nay. Mot may vi tnh co the la 1 bo vi ieu khien (micro controller) hoac la mot vo vi mach (one-chip microcomputer). V du : vi ieu khien 68HC11 cua Motorola, MCS-8051 -Co kha nang lam viec vi o dai d lieu la 4, 8, 16, 32, 64 bit -CPU cua may eu c che tao bang cong nghe mach vi ien t vi mc o to hp ln VLSI. -Tren th trng hien nay co cac ho vi x ly noi bat nh :

    Intel dung vi x ly 80x86 : 8086 (16 bit) 8088( 8bit) 80286 (16bit) 80386(32 bit) 80486(32 bit)

    80586(32 bit) Motorola dung vi x ly 680x0 Zilog : Z80 (8bit), Z8000 (16 bit)

    1.2.3. Lch s phat trien cua cac bo vi x ly : a.The he 1 : (1971-1973) -Vi x ly Intel 4004 (4 bit) dung trong may tnh xach tay. Sau o xuat hien 4040 (4 bit) 8008 (8bit)

    ac iem : -o dai t thng la 4 bit. -Cong nghe che tao PMOS vi ac iem mat o phan t nho, toc o thap, gia re va ch co kha nang a ra dong tai nho. -Toc o thc hien lenh la 10-60 micro-sec/lenh vi tan so ong ho (xung clock)fclk= 0,1-0,8 Mhz -Tap lenh n gian va phai can 1 so mach phu tr e tao nen 1 he vi x ly hoan chnh b.The he 2 : (1974-1977)

    Z80 cua hang Zilog (8 bit)

  • 6

    6800 cua hang Motorola (8bit) 6502 cua hang Mos technology (8bit) 8080 va 8085 cua hang Intel

    -Cong nghe che tao la NMOS (mat o tch hp ln hoac CMOS (tiet kiem ien nang tieu thu) -Tan so xung clock t 1-5 Mhz va toc o thc hien lenh la t 1-8 microsec/lenh c.The he 3 : (1978-1982)

    Vi x ly Intel 8086, 8088, 80186, 80286, (16 bit), MP 68000/68010 cua Motorola (16 bit)

    ay la bo vi x ly dung trong may tnh IBM PC, PC/XT, PC/AT va cac may Macintosh cua hang Apple. -Tan so xung clock t 5-10 Mhz, toc o thc hien lenh la 0,1-1 microsec/lenh -Cong nghe che tao la HMOS d.The he 4 : (1983-?)

    -Cac bo vi x ly ai dien trong the he nay la cac vi x ky 32 bit cua Intel 80386, 80486, va 32 bit Pentium 80586, MP 32 bit 68020/68030/68040.68060 cua Motorola.

    ac iem : -Bus a ch eu la 32 bit (phan biet 4GB bo nh) va co kha nang lam viec bo nh ao -Ap dung c che x ly xen ke lien tuc dong ma lenh (pipe line), bo nh cache(bo nh an), bo nh ao. Cac bo vi x ly nay eu co bo quan ly bo nh (MMU) va ca bo ong x ly toan hoc.

    Ben canh cac bo vi x ly c dung e xay dng may tnh vi tap lenh au u (CISC-complete instruction set computer), ngi ta con che tao ra cac bo vi x ly cai tien dung cho may tnh vi tap lenh rut gon (RISC- reduced instruction set computer). Xem bang : cac bo vi x ly 16/32 bit cua Intel, Motorola. e. May tnh dung bo vi x ly Pentium II, PIII, PIV: -o dai d lieu 32-64 bit

    - Toc o 1.8 Ghz-4.0 Ghz Cac bo vi x ly 16 bit cua Intel Cac bo vi x ly 32 bit cua Intel Cac bo vi x ly 16/32 bit cua Motorola T thap nien 1990 tr lai ay

    1990 Microsoft WIndows 3.0 ra i Motorola 68040 c trien khai.

    1991 Apple va IBM hp tac e khao sat RISC 1992 Microsoft WIndows 3.1 a tr thanh chuan cho cac PC. 1993 Intel Pentium (80586) ra i, cong nghe MMX c cung cap sau. 1995 Microsoft Indows 95 1995 Intel Pentium pro (P6) 1997 Intel Pentium II 1998 Intel Pentium II Xeon 1999 Intel Pentium III 2001Intel Pentium IV

  • 7

    1.3. Cu trc v hot ng ca h vi x l. 1.3.1. S o khoi S O KHOI MOT HE VI X LY C BAN

    Address bus Data bus

    T Thietb vao Thiet b ra Control bus

    Hnh 1.1 :S o khoi cua he vi x ly

    1.3.2. Nguyen ly hoat ong: -CPU (central processing unit) n v x ly trung tam. MP (mocroprocessor) :

    ay la bo nao cua may tnh, ieu khien toan bo hoat ong cua he. MP se lay lenh, phan tch va thi hanh lenh.

    -Bo nh : (memory) la ni lu tr d lieu va chng trnh can cho qua trnh thc hien lenh. Bo nh trong : rom, ram. Bo nh ngoai : bang t, a t

    -Vao ra (input/output) : la mach giao tiep gia CPU vi thiet b vao (ban phm, chuot), thiet b ra(man hnh, may in)

    -Ba bo phan nay c lien lac vi nhau thong qua bus he thong (system bus). Bus he thong gom co : +Bus a ch : (address bus) cho phep xac nh a ch cua o nh hoac ngoai vi can truy xuat (oc /ghi). Bus a ch co the la 16, 20, 24 bit +Bus d lieu (data bus) : cho phep trao oi thong tin gia Cpu va bo nh hay ngoai vi. Bus d lieu co the la 8, 16, 32 bit +Bus ieu khien (control bus) :la cac ng tn hieu do CPU a ra e ieu khien bo nh hay ngoai vi hoac la tn hieu a vao CPU. V du : RD (read-oc ), WR (write-ghi) , INTR (interrupt-ngat)

    Bus a ch ch co 1 chieu t CPU a ra, bus d lieu co tnh chat 2

    chieu (vao/ra), bus ieu khien ch co 1 chieu vao, 1 chieu ra.

    1.4. Chc nng ca cc thnh phn trong h vi x l. 1.4.1. B x l CPU. 1.4.1.1.Nhiem vu cua CPU :

    -ieu hanh hoat ong cua he thong theo y nh cua ngi s dung. Thi hanh chng trnh theo vong kn goi la chu k lay lenh.

    CPU Bo nh Vao ra I/O

  • 8

    Chu ky lenh

    Hnh 1.2: Nhiem vu cua CPU

    1.4.1.2.Cau truc CPU : Cac thanh phan chnh cua CPU gom co : . Cac thanh phan lu tr: cac thanh ghi, cac c. . Cac thanh phan thc thi (x ly) : ALU thc hien cac tnh toan so hoc , logic, dch/xoay (cac) bit. .Cac thanh phan chuyen [tn hieu]: bus . Cac thanh phan ieu khien : n v ieu khien. CPU

    Hnh 1.3 Cau truc ben trong cua CPU

    CPU (central processing unit) : n v x ly trung tam ALU (arithmetic logic unit) : n v so hoc logic Register : thanh ghi CU : control unit, n v ieu khien +ALU : thc hien cac phep tnh so hoc va logic bao gom +, -, *, /, tang , giam, and, or, not, xor, dch , quay, vv ALU co hai ngo vao va 1 ngo ra. Hai ngo vao lay d lieu t bus noi can thiet e thc hien phep toan va 1 ngo ra tra ket qua thc hien phep toan ve bus noi. e tranh d lieu chong nhau bus noi, hai ngo vao cua ALU co hai thanh ghi tam, co chc nang cai d lieu trc khi ALU thc hien phep toan.

    Lay lenh Thi hanh lenh

    IR PC Bo ieu khien CU

    n v so hoc logic

    ALU

    Cac thanh ghi

  • 9

    IN 1 IN 2 ALU OUT

    Hnh 1.4 : ALU +Thanh ghi : la cac o nh co ten toc o rat cao nam ben trong CPU. So thanh ghi ben trong CPU la rat t. Mot so thanh ghi a c nh san chc nang, mot so thanh ghi khac la thanh ghi a dung.

    -Thanh ghi PC -Thanh ghi tch luy A -Thanh ghi c F -Thanh ghi lenh IR -Thanh ghi a ch bo nh MAR -Thanh ghi a dung -Thanh ghi con tro stack SP

    +Bo ieu khien : - ieu khien s hoat ong cua cac khoi khac trong CPU ong bo vi nhau. - Xuat cac tn hieu ieu khien oc ghi bo nh va ben ngoai theo 1 trnh t nhat

    nh e am bao cho viec oc ghi bo nh c thc hien ung

    1.4.1.3. Thc hien lenh : a.Lay lenh t bo nh vao thanh ghi lenh IR (instruction register) b.Thay oi thanh ghi PC (program counter: bo em chng trnh) e chuyen en lenh ke tiep (thanh ghi PC luon gi a ch cua lenh ke tiep) c.Xac nh kieu lenh va lay ra d.Xac nh kieu d lieu ma lenh yeu cau va xac nh v tr d lieu trong bo nh. e.Neu lenh can d lieu trong bo nh, nap no vao thanh ghi cua CPU f.Thc hien lenh g.Lu ket qua ni thch hp h.Tr ve bc 1 e thc hien lenh ke 1.4.1.4.Dang lenh : Cac vung trong lenh : -Vung ma lenh : cho biet tac vu nao se c thc hien. - Vung a ch : ch a ch bo nh hoac thanh ghi cua CPU - Vung cach nh a ch : ch cach xac nh toan hang hoac a ch that. 1.4.1.5. Cac cach nh a ch : -Cach nh a ch hieu ngam. - Cach nh a ch tc thi.

  • 10

    - Cach nh a ch thanh ghi. - Cach nh a ch gian tiep qua thanh ghi. - Cach nh a ch trc tiep. - Cach nh a ch gian tiep. - Cach nh a ch tng oi. 1.4.2. B nh

    a ch 0 o nh 1 2 n-1 Hnh 1.5: Bo nh a.Bit : 0/1 bieu dien hai trang thai , la n v c ban cua bo nh. b.nh v bo nh : (memory addressing)

    Bo nh se c anh so bat au t 0 cho ti n-1, vi n la so o nh trong bo nh. Tat ca cac o nh eu co so lng bit nh nhau, neu 1 o nh co k bit th no co the co t 1 2k to hp bit khac nhau V du : vi bo nh 96 bit ta co 3 kieu to chc bo nh nh sau : 12x8 bit, 8x12 bit, 6x16 bit

    Thng 1 o nh co 8 bit=1byte. Neu bo nh co m ng a ch th se co 2m byte (o nh) V du : bo nh co 10 bit a ch dung lng bo nh la 210 byte=1KB bo nh co 11 bit a ch dung lng bo nh la 211 byte=2KB

    bo nh co 12 bit a ch dung lng bo nh la 212 byte=4KB V du : EPROM 2716 2732 2764 2KB 4KB 8KB SRAM 6116 6264 2KB 8KB c. Bo nh chnh : ROM (read only memory): bo nh ch oc RAM(random access memory) : bo nh oc ghi c Bo nh ngoai : bang t, a t (a mem, a cng)

    *Chc nang bo nh : -Co nhiem vu lu tr thong tin gom co : +Chng trnh :- khi ong -giao tiep c ban BIOS -he ieu hanh (he thong)

    3A

    0F 5

  • 11

    -ng dung +em d lieu : dung e cha d lieu vao va lay d lieu ra

    *ROM : -Cha d lieu luc che tao -Cha d lieu chet : -cac chng trnh khi ong -thong so he thong -giao tiep bios

    *Ram : thong tin trong Ram co the thay oi c -oc th thong tin cu van con -Ghi th thong tin mi se e len -Khi mat ien th se mat thong tin

    *ROM gom co cac loai nh sau : -EPROM : (erasable programmable ROM) : rom lap trnh va xoa c . Lap trnh bang cach a xung ien ap cao vao chan Vpp, xoa bang tia cc tm -PROM (programmable ROM) rom lap trnh c -EEPROM (electrical erasableprogrammable rom) rom lap trnh va xoa bang ien. V du : eeprom 2832 -MROM (maskable ROM)rom mat na

    *RAM co cac loai sau : SRAM (static ram ) ram tnh DRAM (dynamic ram ) ram ong 1.4.3. Cc thit b ngoi vi. 1.4.3.1 Phan loai ngoai vi : Cac thiet b I/O co ten goi khac nhau la cac thiet b ngoai vi (peripheral devices). Bang sau cho thay mot so thiet b ngoai vi tieu bieu theo nhom nhap (I) va xuat (O). -Thiet b lu tr ngoai: a t (FDD, HDD), USB, CD-ROM, DVD. -Thiet b giao tiep vi cong ngi: Man hnh, may in. -Thiet b ieu khien: cam bien va truyen ong. 1.4.3.2 Giao tiep I/O: V du : giao tiep I/O lap trnh c 8255, 8250 Vao Ra Cong (port) + Ban phm +Man hnh + Chuot + May in

    Hnh 1.6 V du : cong may in (LPT1) co a ch 378h

    CPU I/O Thiet b ngoai vi

  • 12

    Cong COM1(RS232) co a ch 3F8h - Thiet b ngoai vi gom co : man hnh, ban phm, chuot, may in, may quet, may ve, CD Rom,vv - Moi thiet b ngoai vi co 1 IO rieng nam trong phan xuat nhap IO - Moi IO co a ch cong rieng - IO khong phai la cho cha d lieu, ma ch la cong e d lieu qua lai - Xuat nhap tuy theo thiet b ngoai vi 1.4.4. H thng bus. Bus Cac he thong con cua MCU va CPU lien lac vi nhau thong qua bus (tuyen), bus la tap hp cac ng tn hieu ma qua o co the truyen i thong tin ve a ch, d lieu va ieu khien. Thong thng bus co o rong la 8 hay 16 bit( hoac ln hn na tuy theo CPU hoac thiet b I/O). He thong bus gom bus a ch, bus d lieu va bus ieu khien. Cai at bus chung : -Bus don kenh - Cac bo em bus 3 trang thai - Chu k oc bus. - Chu k ghi bus 1.5. Cc h vi x l thong dng: Intel 80x86. Motorola 680x0. MCS-51. 68HC11, 68HC12. Zilog Z80. Microchip PIC 16F84, PIC 16F877A. 1.6. Vi iu khin. 1.6.1. Cac gii han cua vi x ly -Can bo nh ngoai e thc thi chng trnh. -Khong the giao tiep trc tiep vi cac thiet b I/O. 1.6.2.Vi x ly va vi ieu khien: So sanh vi x ly (MPU) va vi ieu khien (MCU) MPU: -c thiet ke e thc hien chc nang CPU trong he may vi tnh. - Tap lenh c sap xep e cho phep ma va mot lng ln d lieu c chuyen i

    gia vi x ly vi bo nh va thanh ghi ngoai. - Cac tac vu tac ong vi nhom bt khong nho hn 4 bit. MCU : -c thiet ke e lam viec vi mach ngoai toi thieu. -Tap lenh n gian (khoang 255 lenh). -Cac tac vu co the tac ong len tng bt. MCU la may tnh vi tat ca trong mot chip :

  • 13

    MCU = CPU + Bo nh + Giao tiep I/O S o khoi cua mot MCU Microcontroller

    Hnh 1.7 : S o khoi mot MCU So vi MPU th MCU co them mot so mach lam cho MCU hoat ong nh mot thiet b duy nhat. MCU lam viec vi cac d lieu c a vao qua cac cong nhap noi tiep hoac song song c ieu khien bi phan mem c cat trong bo nh chng trnh. Cac MCU tieu bieu : . 8051 (intel va cac hang khac) : la MCU the he th hai cua Intel. . 68HC11 (Motorola va Toshiba) : co kien truc bo nh chung trong o cac lenh , d lieu , I/O va cac mach nh th tat ca chia xe cung vung nh. .PIC (Microchip) : Ho MCU RISC au tien ( 35 lenh).

    Memory Register

    ALU

    I/O port

    Counter Timing & control

    Interrupt chip

  • 14

    1.7.Gii thiu chung v vi iu khin PIC. 1.7.1. Mc ch ca sch Mc ch ca sch l dy bn lm sao xy dng mch iu khin s dng thit b nh l nt nhn, bn phm, cm bin, led n, loa, led 7 on, iu ny c thc hin bng cc th d. Chng trnh c vit bng hp ng. Chng ta cn mch np (loi JDM qua cng COM, hay loi qua cng USB) vit cc lnh vo chip. Phn mm bin dch MPASM hay MPLAB chuyn vn bn m ngun thnh m my. 1.7.2. B nh chng trnh Bn trong vi iu khin chng trnh m chng ta vit c lu trong vng nh EPROM (Electrically Programmable Read Only Memory), b nh ny khng bay hi v c nh khi ngun b tt. Cc lnh m chng ta lp trnh vo vi iu khin lm vic bng cc di chuyn v vn hnh d liu trong nh c bit nh l nh (file) v thanh ghi (register). B nh ny gi RAM (Random Access Memory). Th d trong iu khin nhit phng, chng ta o nhit phng bng cch bng vi iu khin thng qua thanh ghi iu khin tng t sang s (ADCON0). S o lng th c so snh vi d liu ca chng ta lu trong mt nh ngi dng (user file). Thanh ghi trng thi s ch bo nu nhit l ln hn hay nh hn gi tr yu cu v thanh ghi cng (port) s bt/tt l nhit tng ng. Bn b nh ca 16F877A c cho chung sau. Vi iu khin PIC l 8 bit, ngha l nh ngi dung v thanh ghi l 8 bit nh phn nh hnh 2.1. Bit 7 6 5 4 3 2 1 bit 0

    1 0 1 1 0 0 1 0 Hnh 1.7.1: Dng thanh ghi v nh ngi dung. Trong LSB l bit c trng s thp nht (bit 0), MSB l bit c trng s cao nht (bit 7). 1.7.3. Xung ng h vi iu khin chy tng bc qua cc lnh, vi iu khin cn xung clock ng b vic di chuyn d liu vng quanh mch in t. iu ny c cung cp bi hai t in v thch anh hay bi mch dao ng bn trong. Trong 16F84 c 4 la chn dao ng: . Mch RC dao ng m cung cp gii php gi thnh thp. . Dao ng LP, ngha l thch anh 32 KHz, m ti thiu ha tiu th nng lng. . XT: cho cu hnh thch anh chun. . HS l ty chn dao ng tc cao. Cc tn s thch anh thng dng l: 32 Khz, 1 Mhz, 4 Mhz, 10 Mhz v 20 Mhz. PIC 16F877A cn c them 4 la chn sau (tng cng l 8): . EXTRC: in tr/t in ngoi. . EXTRC in tr t in ngoi vi CLKOUT. . INTRC in tr/t in 4 Mhz ni. . INTRC in tr t in ni 4 Mhz vi CLKOUT. 1.7.4. H thng vi iu khin

  • 15

    S khi h thng vi iu khin c cho hnh 1.7.2.

    Hnh 1.7.2: H vi iu khin c bn. . Thnh phn ng vo gm c thit b s nh l cng tc, nt nhn, bn phm,.. v cm bin nh l in tr ph thuc nh sang, thermixtor, cm bin gas, cm bin p sut, . n v iu khin hin nhin l vi iu khin, vi iu khin s gim st ng vo v nh l kt qu chng trnh s bt (turn on) ng ra hay tt (turn off) ng ra. Vi iu khin lu chng trnh trong b nh v thc thi lnh di s iu khin ca mch ng h (clock). . Thit b ng ra c lm t led n, loa, ng c, hin th s, led 7 on, l nhit, qut, Chn la hin nhin nhiu nht cho vi iu khin l c bao nhiu ng vo s, ng vo analog, v ng ra m h thng yu cu. iu ny ch ra s chn ti thiu vo ra (I/O) m vi iu khin phi c. Nu ng vo analog c s dng th vi iu khin phi c b chuyn i tng t -s (A/D).

    Xem xt k tip l kch c b nh chng trnh l g. iu ny khng l vn ln khi bt u . Cc chng trnh trong sch ph hp khng gian b nh chng trnh 1 KByte (16f84) ti 8 KByte (16f877a).

    Tn s xung clock xc nh tc ti lnh c thc thi. iu ny l quan trng nu bt k tnh ton di l c xem xt. Tn s xung clock cng cao th vi iu khin hon thnh him v cng nhanh v chuyn sang nhim v khc.

    Mt xem xt na l s ngt v s mch timer (nh th) c yu cu, b nh d liu EEPROM yu cu l bao nhiu. Cc hot ng phc tp hn s c xem xt sau.

    Trong sch ny, chng trnh yu cu ng vo analog c hin thc trn PIC 16F84 hay PIC16F 877A. 1.7.5. Cc loi vi iu khin Danh sch cc loi vi iu khin PIC th c pht trin hang ngy. Chng bao gm cc thit b cho tt c cc ng dng. Th d: 18F8722 c 64K b nh EPROM, 3938 byte RAM ( nh ngi dung), 1024 byte EEPROM, 16 knh A/D 10 bit, in p tham chiu, 72 ng vo v ra (I/O), 3 timer 16 bit v 2 timer 8 bit. V c bn c 2 loi vi iu khin: thit b Flash v thit b lp trnh mt ln (one time programmable devices -OTP). Thit b Flash c th lp trnh li trong mch np trong khi thit b lp trnh mt ln khng th lp trnh li. Tt c thit b OTP tuy nhin c s phn loi ng khung (windowed) m cho php chng b xa di nh sang tia cc tm trong khong 15 pht m chng c th lp trnh li. Thit b OTP dung cu hnh dao ng RC, LP, XT hay HS. Trong sch ny chng ta dung thit b Flash l PIC 16F84 hay PIC 16F877A. 1.7.6. c tnh ca vi iu khin Bn ch ra mt thit b vi m nhn dng sn phm: . S sn phm. . Nu n l loi ng khung, OTP hay thit b Flash. Loi ng khung c ch ra bi JW. Thit b OTP c ch ra bi tn s dao ng v thit b Flash c ch ra bi ch F nh l 16F84.

    Ng vo

    iu khin

    Ng ra

  • 16

    . Tn s dao ng, thng l 04 cho cc thit b lm vic ln ti 4 Mhz, 10 cho thit b lm vic ln ti 10 Mhz, hay 20 cho thit b ln ti 20 Mhz. Thit b 20 Mhz th t tin hn thit b 4Mhz. . Tm nhit , cho ng dng tng qut 0 oC70 oC l c ch ra.

    H thng nhn dng sn phm cho vi iu khin PIC c minh ha hnh 1.7.3.

    Part No. XX X /XX ng gi L= PLCC P=PDIP (ng gi Plastic chun) So=SOIC small outline IC PQ=MQFP JW=Windowed device (CERDIP) Tm nhit - =0oC 70 oC I = -40 oC+85 oC E = -40 oC+125 oC Phm vi tn s: 04= 4Mhz 10 = 10 Mhz 20 = 20 Mhz Thit b, th d 16C711 Hnh 1.7.3. H thng nhn dng sn phm.

    1.7.7. S dng vi iu khin s dng vi iu khin trong mch, c hai lnh vc v c bn chng ta cn hiu

    1. Cch kt ni vi iu khin vi phn cng. 2. Cch vit chng trnh v np m vo vi iu khin

    1.7.1. Phn cng vi iu khin: Phn cng m vi iu khin cn hot ng th c minh ha hnh 1.7.4. Thch anh v t in c ni vi chn 15 v 16 ca 16F84 to ra xung clock m c yu cu vi iu khin i qua chng trnh v cung cp xung nh th.

  • 17

    16F84 V+ 16 MCLR 15 0V

    +5V 14 68 pF 4 32 Khz 68pF 5

    Hnh 1.7.4: Mch vi iu khin

    U1

    PIC16F84

    1234

    678910111213

    15

    16

    1718

    RA2RA3

    RA4/TOCKIMCLR

    RB0/INTRB1RB2RB3RB4RB5RB6RB7

    OSC1/CLKIN

    OSC2/CLKOUT

    RA0RA1

    C2

    68 pF

    C30.1 uF

    +5V

    GND

    Y1

    32 Khz

    C1

    68 pF

    V+

    Mch vi iu khin PIC 16F84

  • 18

    Mch vi iu khin PIC 16F877A

    Ngun cung cp cho vi iu khin: Ngun cung cp cho vi iu khin trong khong t 2v ti 6v. iu ny c th c cung cp d dng t chn 6v nh hnh 1.7.5. + -

    Hnh 1.7.5: Ngun cung cp cho vi iu khin. Diode trong mch to ra p 0,7v trn n nn lm gim in p cung cp cn 5,3v. N cung cp bo v cho vi iu khin nu ngun c ni sai tnh c. Trong trng hp ny diode s phn cc ngc v khng c dng chy qua. 7805, mch n p:

    V+ 16F84 0v

  • 19

    Kt ni ngun cung cp thong dng nht cho vi iu khin l dung IC n p 7805 c 3 chn. Kt ni c minh ha hnh 1.7.6. +5V Vin Vout 0v

    Hnh 1.7.6: Mch n p 7805 in p vo Vin cho 7805 l t 7v n 30v. Cng sut tiu tn trong 7805: S cn trng phi c xem xt khi s dng in p Vin cao a vo 7805. Th d nu Vin=24v, ng ra 7805 l +5v, th th 7805 c 24v-5v=19v ri trn n. Nu n cung cp dng 0,5A cho mch th cng sut tiu tn (volt x ampe) l: 19v x 0,5A=9,5 watt. B n p s rt nng. Do ta phi c ming tn nhit (bng nhm). Kt ni nt nhn vi vi iu khin: Cch thong dng nht v mt nt nhn ti vi iu khin l thong qua in tr ko ln ti +5V nh minh ha hnh 1.7.7. +5V 0v

    Hnh 1.7.7: Kt ni nt nhn vi vi iu khin. Khi nt nhn l h, logic 1 c ni vi vi iu khin. Khi nhn nt, logic 0 c ni vi vi iu khin.

    Mt s vi iu khin nh l 16F84, 16F818, v 16F877a c in tr ko ln bn trong ni vi mt s chn I/O. Port B trong thit b trn. Hnh 1.7.8 minh ha nt nhn c ni dung ko ln bn trong. 0V

    Hnh 1.7.8: Kt ni nt nhn s dng in tr ko ln bn trong. Kt ni ng ra vi vi iu khin:

    7805

    Vi iu khin

    Vi iu khin

  • 20

    Vi iu khin c kh nng cung cp xp x 20-25 mA ti chn ng ra. Nh vy ti nh l led n, hay rle nh c th c li trc tip. Ti ln hn yu cu giao tip thong qua transistor i vi dc, hay triac i vi ac. Thit b cch ly quang (opto-coupled device) l giao tip cch li gia vi iu khin v ti. Kt ni led n vi vi iu khin c minh ha hnh 1.7.9. 680 Ohm 0V

    Hnh 1.7.9: Kt ni 1 led vi vi iu khin. Logic 1: led sang Logic 0: led tt. 470 Ohmx4 0V

    Hnh 1.7.10: Kt ni 4 led vi PIC 16F877A 1.7.2.Lp trnh vi iu khin:

    vi iu khin thc hin mt s hnh ng iu khin, bn cn giao tip (truyn thong) vi n v yu cu vi iu khin nhng lnh phi lm g. Khi giao tip vi ngi khc, chng ta dung ngn ng ni, khi giao tip vi vi iu khin chng ta s dng ngn ng lp trnh (hp ng). Hp ng cho PIC s dng 35 lnh trong tp lnh. Thm vi lnh c dung trong vi iu khin ln. giao tip vi vi iu khin chng ta cn bit 35 lnh ny l g v cch s dng chng. Khng phi tt c 35 lnh c dung trong sch ny. Thc t bn c th vit chng trnh c ngha ch dung 5 hay 6 lnh.

    IV. Tng kt bi: -Tm tt cc chnh trong bi. -Chun b bi mi.

    V. Cu hi v bi tp v nh: Chng 1: Gii thiu chung v h vi x l. Gii thiu chung v PIC.

    1.Cc h m dung trong my tnh. M BCD l g? M ASCII l g? 2.So snh my vi tnh vi cc th h my trc y.

    Vi iu khin

    16F877a RB0 RB1 RB2 RB3

  • 21

    3. Trnh by lch s pht trin ca cc b vi x l. 4. Trnh by cu trc v hot ng ca mt h vi x l. 5. Trnh by cu trc v hot ng ca mt CPU 8 bit. 6. B nh l g? Phn bit ROM v RAM. Phn bit MROM, PROM, EPROM, EEPROM. Phn bit RAM tnh v RAM ng. 7. Thit b ngoi vi gm nhng g? 8. Gii thch cc t SSI, MSI, LSI v VLSI. 9. Trnh by s khi mt MCU (vi iu khin). So snh MCU v CPU (vi x l). 10. K tn cc h vi x l thong dng. 11. Trnh by h thng vi iu khin . 12. Cch s dng vi iu khin PIC: phn cng v vit chng trnh. 13.Cc thit b/dng c 3 trng thi l g? V ti sao chng li cn thit trong cc h thng dung bus? 14.Cn bao nhiu byte lm thnh 1 word 32 bit? 15. Mt chip b nh c dung lng 2K (2048 byte). Hi s chn a ch ca chip nh ny. 16. Hy nh v b nh 96 bit theo 3 cch. 17. Trnh by s khi ca Z80 CPU. 18. Nu cc phng php a ch ha ca Z80 CPU. 19. Neu mot chip bo nh co kch thc la 1024x 4 bits, ta phai can bao nhieu chip nh vay e tao 2K (2048) byte bo nh ? VI. Rt kinh nghim: (v thi gian, ni dung, phng php, chun b) Ngy .thng..nm 2011 Ngy .thng..nm 2011 B mn Ging vin son

  • 22

    Bi ging s 2: : Phn cng vi iu khin PIC 16F84 v PIC 16F877A( S tit: 5) I.n nh lp: . II.Kim tra bi c: .. .. .. III.Tn bi ging: : Phn cng vi iu khin PIC 16F84 v PIC 16F877A III.1. Mc tiu:

    - Vi iu khin 16F84. - Vi iu khin 16F877A : s khi PIC 16F877A, b nh chng trnh, b m chng trnh (PC) v ngn xp (stack), bn b nh d liu, cc thanh ghi chc nng c bit (SFR),v thanh ghi trng thi.

    III.2. dung v phng tin dy hc: -Phn trng, khn, bng, bt long, micro c dy(hay khng dy), my tnh, v n chiu (hay my chiu). III.3. Gio trnh v ti liu tham kho: Gio trnh Vi x l ca trng i hc cng nghip Tp. HCM. D.W. Smith, PIC in practice: a project-based approach, Elsevier, 2nd edition, 2006. Trng Trc, Chip n 16C84 v ng dng ca chng. H Trung M, Vi x l, NXB HQG Tp. HCM, 2003. Ti liu v vi iu khin PIC ca b mn in t cng nghip. Website: http://www.microchip.com/ http://www.alldatasheet.com/ Myke Predko, Programming and customizing the PIC microcontroller, 3rd edition, Tab Electronics, McGrawHill, 2008(Ebook). Douglass V.Hall, Microprocessors and interfacing: Programming and Hardware, 2nd ed., Macmillan/McGraw-Hill, 1992. III.4.Ni dung bi ging: Ni dung chi tit : xem bi ging chi tit. Phng php ging dy: thuyt trnh, nu vn v m thoi trao i vi sinh vin.

    Chng 2: Phn cng vi iu khin PIC 16F84 v PIC 16F877A. 2.1.Vi iu khin PIC 16F84 PIC 16F84A c cc c tnh: -c 35 lnh, cu lnh ch cn 1 chu k my, cu lnh nhy cn 2 chu k my. Tn s: ti a 20 Mhz v chu k my l 200 ns. B nh chng trnh 1k x 14 words, b nh RAM l 68 byte, b nh d liu dng EEPROM l 64 byte. Chiu di cu lnh l 1 word 14 bit (1 t =14 bit). X l d liu dng 8 bit (1 byte). C 15 thanh ghi chuyn dng SFR t trong b nh RAM. -dng ngn xp ct gi a ch lnh, ngn xp su n 8 lp. -c th truy cp b nh dung a ch trc tip, gin tip v tng i. -C 4 dng ngt. -13 chn xut nhp d liu (PORT A 5 chn, PORT B 8 chn). -Mt ng h timer 0, dung hanh m 8 bit nn m c ti a 256 nhp.

  • 23

    S d chn: Hnh 2.1 U3

    PIC16F84

    1234

    678910111213

    15

    16

    1718

    RA2RA3

    RA4/TOCKIMCLR

    RB0/INTRB1RB2RB3RB4RB5RB6RB7

    OSC1/CLKIN

    OSC2/CLKOUT

    RA0RA1

    Hnh 2.1: S chn ca PIC 16F84 ngha cc chn: - OSC1/CLKIN: l chn ng vo ca mch dao ng thch anh dung nh tn s xung nhp v chn ny cng l ng vo ca xung nhp (clock input). . OSC2/CLKOUT: l chn ng ra ca mch dao ng thch anh v l ng ra ca xung nhp (clock output), y tn hiu xung nhp c khuch i. . /MCLR: (Master clear) l chn reset. Tc dng ca lnh reset l tr chng trnh v a ch u 0000h, v xc lp li trng thi cho cc thanh ghi c dng. Lnh c tc dng mc thp. . RA0, RA1, RA2: l cc chn xut nhp ca port A. . RA4/T0CKI: l chn a nhim , va lm chn xut nhp ca portA v li l chn ly xung cho thanh m ca ng h timer 0 (Timer 0 clock input). Chn ny c cc Drain ca Fet h. . RB0/INT: l chn a nhim, va lm chn xut nhp d liu ca portB v l chn pht ng theo ngt ngoi. Cc chn ca portB c th c lp trnh c tr khng ln dung lm ng vo, dung nhp trng thi ngoi PIC. . RB1, RB2, RB3: l cc chn xut nhp d liu ca portB. .RB4, RB5, RB6 v RB7: l cc chn xut nhp ca port B v s pht ng ngt khi s thay i trng thi trn cc chn ny. . RB6 l 1 chn xut nhp d liu ca portB v c tnh pht ng ngt theo s thay i trng thi trn chn ny. N cn c th lp trnh dung chn ny pht xung nhp (serial clock) dung cho truyn bit dng ni tip. . RB7 l 1 chn xut nhp d liu ca portB v pht ng ngt theo s thay i trng thi trn chn ny. N cn c th lp trnh dung chn ny trao i d liu (serial data) dng cho truyn bit dng ni tip. . Vss l chn ni mass (GND, 0V). . VDD l chn ni vi VCC (PIC c th dung VCC t 2v n 5,5v). S khi bn trong ca PIC 16F84A

  • 24

    Hnh 2.2: Cu trc bn trong ca PIC 16F84A.

    2.2. Vi iu khin PIC 16F877A: 2.2.1. S khi PIC 16F877A v s cc chn v ngha Gii thiu PIC 16F877A:

    PIC l vit tt ca Programmable Intelligent Computer, c th dch l my tnh thong minh lp trnh c, l mt sn phm do hang General Instruments t cho dng sn phm u tin ca h l PIC 1650. PIC l mt h vi iu khin RISC c sn xut bi cng ty Microchip Technology. Dng PIC u tin l PIC 1650 c pht trin bi MicroElectronics Division thuc General Instruments. Lc ny PIC 1650 c dung giao tip vi cc thit b ngoi vi cho my ch CP 1600. Hin nay PIC pht trin mnh, c rt nhiu loi PIC ra i. PIC 12 ( di m lnh 12 bit) nh l dng PIC 12Cxxx (th d : PIC 12F50x, PIC 12F675), PIC 5x ( 16F54), PIC 14 nh dng PIC 16Fxxx (16F84, 16F818, 16F877A, 16F872), PIC 16 nh dng PIC 18Fxxx (PIC 18F4520, 18F2550). PIC 16F877A l vi iu khin PIC thng dng, loi trung (mid- range) c c im: -y l vi iu khin thuc h PIC 16Fxxx vi tp lnh gm 35 lnh c di 14 bit. Mi lnh u c thc thi trong mt chu k xung clock. Tc hot ng ti a cho php l 20 Mhz vi mt chu k lnh 20 ms. B nh chng trnh Flash l 8k x 14 words, b nh d liu (RAM) l 368x8 byte, v b nh EEPROM vi dung lng 256 x 8 byte. S port I/O l 5 vi 33 chn I/O. Cc c tnh ngai vi bao gm cc khi chc nng sau: -Timer 0: b m 8 bit b m vi h s t l trc. -Timer 1: b m 16 bit, b m vi h s t l trc. C th c tng m thng qua xung clock/ xung clock thch anh ngoi ch phng thc ct gi sleep.

  • 25

    -Timer 2: b m 8 bit vi thanh ghi chu k 8 bit, b m 8 bit ca h s t l trc, h s t l sau. -C hai b bt gi/so snh/iu rng xung. -Cc cng giao tip ni tip ng b (SSP) vi SPI phng thc ch v I2C (ch/t). -B truyn nhn ni tip ng b , khng ng b (UASRT/SCL) c kh nng pht hin 9 bit a ch. -Cng ph song song vi 8 bit m rng, vi cc chn iu khin RD, WR, CS. Cc c tnh analog: -B chuyn i tng t-s 10 bit trn chip vi 8 knh vo. -Hai b so snh, Bn cnh l mt vi c tnh ca vi iu khin nh : -B nh Flash vi kh nng ghi xa c 100000 ln. -B nh d liu EEPROM vi kh nng ghi xa c 1000000 ln. -D liu b nh EEPROM c th lu tr c 40 nm. -Kh nng t np chng trnh vi s iu khin ca phn mm. -Np c chng trnh ngay trn mch in ISP (in circuit programming) thong qua 2 chn. -B m xung thi gian (WDT-Watch dog timer) vi dao ng RC bn trong. -C m chng trnh bo v (chc nng bo mt m chng trnh). -C th hot ng hiu dng dao ng khc nhau. Ch sleep (phng thc ct gi) tit kim nng lng. -Cng ngh CMOS Flash/ eeprom vi ngun mc thp, tc cao. -Di in th hot ng rng : 2V 5,5 V. -Cng sut tiu th thp:

  • 26

    Hnh 2.3: Cu trc bn trong ca PIC 16F877A.

  • 27

    S chn PIC 16F877A : hnh 2.4.

    Hnh 2.4: S chn PIC 16F877A ngha cc chn: . OSC1/CLKI: chn nga vo mch dao ng, chn 13. . OSC2/CLKO: chn ng ra ca mch dao ng, chn 14. . /MCLR/Vpp: chn s 1, chn reset, tch cc mc thp v cn l xung lp trnh cho ROM. . RA0, RA1, RA2, RA3, RA4, RA5: chn 2-7, l cc chn xut nhp ca PORTA. . RE0, RE1, RE2: chn 8-10, l cc chn xut nhp ca PORTE. . Chn 2-7, 8-10 cn l ng vo analog AN0, AN1, AN2, AN3, AN4, AN5, AN6, AN7. .RA4/T0CKI: va l chn xut nhp d liu ca port A, va l chn ly xung cho thanh m ca timer 0 (timer 0 clock input). . RE0/RD: cn l xung cho php c b nh (read), tch cc logic 0. . RE1/WR: cn l xung cho php ghi b nh (write), tch cc logic 0. . RE2/CS: cn l xung chn chip (chip select) , tch cc logic 0. . RC0, RC1, RC2, RC3: chn 15-18, RC4, RC5, RC6, RC7:chn 23-26: l cc chn xut nhp ca port C. . RC0/T1OSO/T1KCI: cn l chn ly xung cho thanh m ca timer 1, v l chn T1OSO. . RC1/T1OSI: cn l chn T1OSI. . RC2/CCP1, RC1/T1OSI/CCP2: cn l chn bt (capture), so snh (compare) v iu rng xung (PWM). Moun giao tip vi timer 1 v timer 2.

    NTHSHighlight

    NTHSHighlight

    NTHSHighlight

  • 28

    Tn hiu bt: nhn khong thi gian ca s kin bn ngoi s dng mt chn ng vo, dung vi timer 1. Tn hiu so snh: thay i mt chn ng ra hay to ra mt ngt khi lng thi gian c th tri qua , dung vi timer 1. Tn hiu iu rng xung: to ra ng ra song vung, chu k bn phn xc lp v c th cu hnh li ti mt tn s xc nh, dung vi timer 2. Ton Toff

    , TTPWM on=

    T . RB0, RB1, RB2, RB3, RB4, RB5, RB6, RB7: chn 33-40 l cc chn xut nhp ca PORTB. . RB0/INT: cn l chn pht ng theo ngt ngoi. . RC6/TX/CK: n c th c lp trnh dung chn ny pht xung nhp (serial clock) dung cho truyn bit dng ni tip. y cn l chn pht TX (transmit data). Hai chn ny dung cho thu pht bt ng b v ng b (USART) ,c th nh a ch. . RC7/RX/DT: n c th c lp trnh dung chn ny trao i d liu (serial data). y cn l chn thu d liu (receive data). . RA3/AN3/Vref+, RA2/AN2/Vref-/Cref: l cc chn in p tham chiu cho b chuyn i tng t-s (ADC) 10 bit. . RC3/SCK/SCL: chn 18, cn l xung clock ni tip (Serial clock-SCK) cho SPI, v l chn clock ni tip cho I2C. RC4/SDI/SDA: chn 23, cn l chn d liu ni tip (serial data). RC5/SDO: chn 24, cn l chn xut d liu ni tip (serial data out). Mun ni tip ng b ch (Master synchronous serial port-MSSP) c th hot ng hai ch : -SPI (serial peripheral interface): giao tip ngoi vi ni tip. SPI dng 3 chn SDO, SDI, SCK. -I2C: (inter-integrated circuit): mch tch hp lien kt, dung 2 chn : SCL, SDA, c th ch ch hon ton (full master mode) hay ch t (slave mode) (vi gi a ch tng qut). .RD0, RD1, RD2, RD3: chn 19-22, RD4, RD5, RD6, RD7: chn 27-30: l cc chn xut nhp ca PORT D. . VDD: chn 11, 32: l ngun dng Vcc t 2V n 6V (5,5 V). . VSS: chn 12, 31, l chn t (mass, GND, 0V). 2.2.2. B nh chng trnh: . T 8K cc i -(8k x 14 bit/word)/1 byte= 14 Kbyte b nh. . Vect reset 0000h -B m chng trnh PC s n a ch ny khi reset. . Vect ngt 0004h. -B m chng trnh PC s n a ch ny da trn bt k ngt no.

  • 29

    Status register Pages of proram memory Bank of data memory Mux ADC ALU 14 bit working register peripherals Hnh 2.5: S khi Mid-range PIC ( PIC 16F877A). 0000h 0004h 0005h 07FFh 0800h 0FFFh 1000h 17FFh 1800h 1FFFh Hnh 2.6: B nh chng trnh. 2.2.3. B m chng trnh v ngn xp:

    Program counter

    Instruction register

    Timer 0

    USART

    MSSP

    Reset vect Interrupt vect Page 0 Page 1 Page 2 Page 3

  • 30

    . rng 13 bit PC: - byte thp nm thanh ghi PCL ALU result (8 bit) hay Opcode (11 bit). PCL l thanh ghi c th c hay ghi c. -PCH bit trang . cp nht t PCLATH . ch ra trong b nh chng trnh. Bit cao PC l khng c c , nhng c th ghi gin tip thong qua thanh ghi PCLATH. . 8 level deep stack ( stack 8 mc) Ngn xp l mt vng nh lien tc c tnh cht vo sau ra trc (last in first out-LIFO). Con tr ngn xp s gi a ch nh ngn xp. -lu tr ni dung PC . lnh push (ct ni dung vo nh ngn xp) call/interrupt . lnh pop(ly ni dung nh ngn xp ra ) return/retfie/retlw PC Call, return, Retfie, retlw Program memory Hnh 2.7 2.2.4. Bn b nh d liu:

    PCLATH

    PCH PCL

    Stack level 1 Stack level 8

  • 31

    000h 080h 100h 180h 10Fh 18Fh 01Fh 09Fh 110h 190h 020h 0A0h 128 byte 0EFh 16Fh 1EFh 07Fh 0FFh 17Fh 1FFh Bank 0 Bank 1 Bank 2 Bank 3 Hnh 2.8 2.2.5. Cc thanh ghi chc nng c bit: . Khi nim nh thanh ghi (register file ). -T t c b nh d li u l ph n c a file thanh ghi, v v y b t k nh trong b nh d li u c th ho t ng tr c ti p. -T t c ngo i vi c nh x v o b nh d li u nh l chu i c c thanh ghi. -T p l nh tr c giao: t t c c c l nh c th ho t ng tr n b t k v tr b nh d li u. -Khu n d ng l nh t d i cho ph p file thanh ghi c th nh a ch tr c ti p. . c truy xut nh bt k thanh ghi khc. . Mt s thanh ghi mang qua tt c cc bank (v d PCLATH, INTCON,). Thanh ghi PORTB Thanh ghi PORTC Thanh ghi PORTD Thanh ghi PORTE Thanh ghi PORTA Thanh ghi TRISA Thanh ghi TRISB Thanh ghi TRISC Thanh ghi TRISD Thanh ghi TRISE Thanh ghi PCLATH Thanh ghi INTCON Thanh ghi PIR1 Thanh ghi PIR2 Thanh ghi PIE1 Thanh ghi PIE2

    Thanh ghi chc nng c bit (SFR) Thanh ghi a nng

    SFR Thanh ghi a nng 70h-7Fh truy xut

    SFR Thanh ghi a nng 70h-7Fh truy xut

    SFR Thanh ghi a nng 70h-7Fh truy xut

  • 32

    05h 85h 06h 86h 07h 87h 08h 88h 09h 89h 0Ah 8Ah 0Bh 8Bh 0Ch 8Ch 0Dh 8Dh Bank 0 Bank 1 Hnh 2.9 2.2.6. Thanh ghi trng thi : STATUS register Bit 7 6 5 4 3 2 1 Bit 0 Hnh 2.10: Thanh ghi STATUS Thanh ghi trng thi dung phn nh hot ng ca ALU trong CPU (nh php ton c nh, trn, zero..) thong qua cc bit c (flags bit) .Thanh ghi trng thi ca: -trng thi s hc ca ALU (c nh, trn, zero..). -trng thi reset. -bit chn bank cho b nh d liu. .Bit 6 v 5, RP1 v RP0 l bit chn bank ti a ch bank 0,1,2, v 3 chn thanh ghi khc v nh ngi dung . RP1 RP0 Chn 0 0 Bank 0 0 1 Bank 1 1 0 Bank 2 1 1 Bank 3 .IRP, bit 7: bit chn bank thanh ghi gin tip (dung cho nh a ch gin tip). 1= bank 2,3 0=bank 0,1

    PORTA PORTB PORTC PORTD PORTE PCLATH INTCON PIR1 PIR2

    TRISA TRISB TRISC TRISD TRISE PCLATH INTCON PIE1 PIE2

    IRP RP1 RP0 /T0 /PD Z DC C

  • 33

    Thanh ghi trng thi cha kt qu ca php ton s hc hay logic ca chng trnh . 8 bit ca thanh ghi trng thi c minh ha hnh 2.10. . C C (carry flag bit), bit 0: c nh. Bit ny l 1 nu c nh t lnh cng hay tr (ADDWF, ADDLW, SUBLW, SUBWF). Th d: nu 1 s 8 bit c cng vi s khc 1 0 1 1 0 0 1 1 + 0 0 1 0 1 0 0 1 1 1 0 1 1 1 0 0 C=0: khng c nh ct ny. Th d: Cng hai s 8 bit 1 0 1 1 0 0 1 1 + 1 0 1 1 0 1 0 1 1 0 1 1 0 1 0 0 0 C=1 c nh ct ny ( bit 9 c a vo c C). Nu kt qu ca php tr l +ve hay zero th c nh C=1 (bt). Nu kt qu ca php tr l l ve th c nh C=0 (xa). . Bit 2, c Z (zero flag bit): c zero. Bit ny c bt ln 1 nu kt qu ca php ton logic hay s hc l 0, ngha l m xung v 0. Th d: 0 1 0 0 1 1 0 0 + 1 0 0 0 1 1 0 1 1 1 0 1 1 0 0 1 (khc 0) C Z=0 Th d: 10100001 - 10100001 00000000 (l 0) c Z=1 Mt s dng quan trng ca bit ny l kim tra nu mt bin trong b nh l bng vi gi tr c nh, th d l nh CARS cha 150. MOVLW .150; t 150 vo thanh ghi lm vic W SUB CARS,W ; tr W t CARS, ngha l CARS-150 BTFSS STATUS,ZEROBIT; bit zero Z bt ln 1 nu CARS=150. . Bit 1, c DC: bit nh/mn s cho lnh ADDWF, ADDLW, SUBLW, SUBWF. i vi mng, cc tnh c gi. 1= c nh t bit 4 ca kt qu. 0=khng c nh t bit 4 ca kt qu. Th d: 28 BCD + 38 BCD tng ng 1 0 0 1 0 1 0 0 0 + 0 0 1 1 1 0 0 0

    0 1 1 0 0 0 0 0

    c nh t bit 3 sang bit 4 nn c DC=1. . Bit 4, /T0: time-out bit

  • 34

    1= sau khi bt in, lnh CLRWDT hay SLEEP 0=mt WDT time out din ra. . Bit 3,/PD: power-down bit 1=sau khi bt in hay lnh CLRWDT 0= bng cch thc thi lnh SLEEP.

  • 35

    Bn b nh ca 16F84: a ch Tn nh (file name) Tn nh (File name) 00 a ch gin tip Ind. Add a ch gin tip Ind. Add 01 TMR0 OPTION 02 PCL PCL 03 STATUS STATUS 04 FSR FSR 05 PORTA TRISA 06 PORTB TRISB 07 - - 08 EEDATA EECON1 09 EEADR EECON2 0Ah PCLATH PCLATH 0Bh INTCON INTCON 0Ch 4Fh

    68 nh ngi dung (user files)

    Bank 0 Bank 1 Bn b nh vi iu khin 16F877A a ch Tn nh bank 0 Tn nh bank

    1 Tn nh bank 2

    Tn nh bank 3

    00h IND. Add a ch gin tip

    Ind. Add Ind. Add Ind. Add

    01h TMR0 OPTION TMR0 OPTION 02h PCL PCL PCL PCL 03h STATUS STATUS STATUS STATUS 04h FSR FSR FSR FSR 05H PORTA TRISA 06H PORTB TRISB PORTB TRISB 07H PORTC TRISC 08H PORTD TRISD 09H PORTE TRISE 0AH PCLATH PCLATH PCLATH PCLATH 0BH INTCON INTCON INTCON INTCON 0CH PIR1 PIE1 EEDATA EECON1 0DH PIR2 PIE2 EEADR EECON2 0EH TMR1L PCON EEDATH 0FH TMR1H EEDARH 10H T1CON Thanh ghi a

    nng 96 byte Thanh ghi a nng 96 byte 11H TMR2 SSPCON2

    12H T2CON PR2 13H SSPBUF SSPADD 14H SSPCON SSPSTAT 15H CCPR1L 16H CCPR1H 17H CCP1CON 18H RCSTA TXSTA

  • 36

    19H TXREG SPBRG 1AH RCREG 1BH CCPR2L 1CH CCPR2H 1DH CCP2CON 1EH ADRESH ADRESL 1FH ADCON0 ADCON1 6FH 7FH

    Thanh ghi a nng 96 byte

    Thanh ghi a nng 80 byte

  • 37

    *Thanh ghi PIE1 (a c 8CH) Bit 7 6 5 4 3 2 1 0 Thanh ghi PIE1 cha cc bit cho php ring r cho cc ngt ngoi vi. Bit 7 PSPIE: bit cho php ngt c /ghi port t song song. 1=cho php ngt c/ghi PSP. 0=cm ngt c ghi PSP. Ch : PSPIE l dnh d tr trn thit b PIC 16F873A/876A; lun gi bit ny b xa. Bit 6: ADIE: bit cho php ngt chuyn i A/D. 1=cho php ngt chuyn i A/D. 0= cm ngt chuyn i A/D. Bit 5 RCIE: bit cho php ngt nhn (thu) USART. 1=cho php ngt thu USART. 0=cm ngt thu USART. Bit 4: TXIE: bit cho php ngt pht USART. 1=cho php ngt pht USART. 0=cm ngt pht USART. Bit 3 SSPIE: bit cho php ngt port ni tip ng b. 1=cho php ngt SSP. 0=cm ngt SSP. Bit 2 CCP1IE: bit cho php ngt CCP1. 1=cho php ngt CCP1. 0=cm ngt CCP1. Bit 1 TMR2IE: bit cho php ngt khp TMR2 ti PR2. 1= cho php ngt khp TMR2 ti PR2. 0=cm ngt khp TMR2 ti PR2. Bit 0 TMR1IE: bit cho php ngt trn (overflow) TMR1. 1=cho php ngt trn TMR1. 0=cm ngt trn TMR1. *Thanh ghi PIR1 (a ch 0CH) Thanh ghi PIR1 cha cc bit c ring r cho cc ngt ngoi vi. Bit 7 6 5 4 3 2 1 0 Bit 7 PSPIF: bit c ngt c /ghi port t song song. 1= mt tc v c hay ghi din ra (phi c xa bng phn mm). 0=khng c c hay ghi din ra. Ch : PSPIF l dnh gi ch trn thit b PIC 16F873A/876A; lun gi bit ny b xa. Bit 6 ADIF: bit c ngt chuyn i A/D. 1=mt chuyn i A/D hon thnh.

    PSPIE ADIE RCIE TXIE SSPIE CCP1IE TMR2IE TMR1IE

    PSPIF ADIF RCIF TXIF SSPIF CCP1IF TMR2IF TMR1IF

  • 38

    0=chuyn i A/D khng hon thnh. Bit 5 RCIF: bit c ngt thu USART. 1=b m nhn USART y. 0=b m nhn USART trng. Bit 4 TXIT: bit c ngt pht USART. 1=b m pht USART trng. 0= b m pht USART y. Bit 3 SSPIF: bit c ngt port ni tip ng b (SSP). 1=iu kin ngt SSP din ra v phi c xa bng phn mm trc khi tr v t chng trnh phc v ngt (ISR). iu kin m thit lp bit ny(=1) l: .SPI- mt s nhn/pht din ra. .T I2C -mt s nhn/pht din ra. .Ch I2C -Mt s nhn/pht din ra. -iu kin Start khi ng c hon thnh bi mun SSP. -iu kin Stop khi ng c hon thnh bi mun SSP. -iu kin Restart khi ng c hon thnh bi nun SSP. -iu kin chp nhn khi ng c hon thnh bi mun SSP. -iu kin Start din ra trong khi mun SSP l rnh ri (h thng a(nhiu) ch). -iu kin Stop din ra khi mun SSP l rnh ri (h thng a ch). 0=khng c iu kin ngt SSP din ra. Bit 2 CCP1IF: bit c ngt CCP1. Ch bt gi: 1= Bt gi thanh ghi TMR1 din ra (phi c xa bng phn mm). 0= khng din ra bt gi thanh ghi TMR1. Ch so snh: 1=Mt s khp so snh thanh ghi TMR1 din ra (phi c xa bng phn mm). 0=khng c din ra s khp so snh TMR1. Ch PWM: Khng c dung trong ch ny. Bit 1 TMR2IF: bit c ngt khp TMR2 ti PR2. 1=khp TMR2 ti PR2 din ra. 0=khng din ra khp TMR2 ti PR2. Bit 0 TMR1IF: bit c ngt trn TMR1. 1=thanh ghi TMR1 b trn (phi c xa bng phn mm). 0=Thanh ghi TMR1 khng c trn. *Thanh ghi PIE2 (a ch 8DH) Thanh ghi PIE2 cha cc bit cho php ring r cho ngt ngoi vi CCP2, ngt xung t bus SSP, ngt tc v ghi EEPROM v ngt so snh. Bit 7 6 5 4 3 2 1 0 Bit 7 Khng hin thc, c l 0. Bit 6 CMIE: bit cho php ngt b so snh. 1=cho php ngt b so snh. 0=cm ngt b so snh.

    - CMIE - EEIE BCLIE - - CCP2IE

  • 39

    Bit 5 khng hin thc, c l 0. Bit 4 EEIE: bit cho php ngt hot ng ghi EEPROM. 1=cho php ngt ghi EEPROM. 0= cm ngt ghi EEPROM. Bit 3 BCLIE: bit cho php ngt xung t bus. 1=cho php ngt xung t bus. 0=cm ngt xung t bus. Bit 2-1 khng hin thc, c l 0. Bit 0 CCP2IE: bit cho php ngt CCP2. 1=cho php ngt CCP2. 0=cm ngt CCP2. *Thanh ghi PIR2 (a ch 0Dh) Thanh ghi PIR2 cha cc bit c cho ngt CCP2, ngt xung t bus SSP, ngt tc v ghi EEPROm v ngt b so snh. Bit 7 6 5 4 3 2 1 0 Bit 7 Khng hin thc, c l 0. Bit 6 CMIF: bit c ngt b so snh. 1=ng vo b so snh thay i (phi c xa bng phn mm). 0=ng vo b so snh khng i. Bit 5 Khng hin thc, c l 0. Bit 4 EEIF: bit c ngt tc v ghi EEPROM. 1=tc v ghi hon thnh (phi c xa bng phn mm). 0=tc v ghi khng hon thnh. Bit 3 BCLIF: bit c ngt xung t bus. 1=Mt xung t bus din ra trong SSP khi cu hnh ch ch I2C. 0=Khng c xung t bus din ra. Bit 2-1 Khng hin thc, c l 0. Bit 0 CCP2IF: bit c ngt CCP2. Ch bt gi: 1=Bt gi thanh ghi TMR1 din ra (phi c xa bng phn mm) 0=khng c din ra bt gi thanh ghi TMR1. Ch So snh: 1=Mt s khp so snh thanh ghi TMR1 din ra. 0=khng c din ra khp so snh thanh ghi TMR1. Ch PWM: Khng s dng . *Thanh ghi PCON: (a ch 8Eh) Thanh ghi PCON cha cc bit c cho php s khc bit gia Reset lc bt in v Reset lc mt ngt ngun. Bit 7-2: khng hin thc, c l 0. Bit 1 POR : bit trng thi reset lc bt in. 1=Khng c reset lc bt in din ra.

    - CMIF - EEIF BCLIF - - CCP2IF

  • 40

    0=mt reset lc bt in din ra (phi c bt (=1) bng phn mm sau khi reset lc bt in din ra). Bit 0 BOR: bit trng thi reset lc mt ngt ngun. 1=Khng c reset Brown-out din ra. 0= Mt reset brown-out din ra (phi c bt bng phn mm sua khi reset brown-out din ra).

    IV.Tng kt bi: -Tm tt cc chnh trong bi. -Chun b bi mi.

    V. Cu hi v bi tp v nh: Chng 2: Phn cng vi iu khin PIC 16F84 v 16F877A 1.Trnh by cu trc v hot ng ca vi iu khin PIC 16F84. 2. Trnh by cu trc v hot ng ca vi iu khin PIC 16F877A. 3. Trnh by b nh chng trnh. T chc b nh ca PIC 16F877A/16F84 c c im g? (lai Harvard hay Von Neumann). 4. B m chng trnh PC l g? 5. Ngn xp l g? Cng dng ca con tr nn xp. c im b nh ngn xp ca PIC 16F877A/16F84 (c my mc ). 6. Trnh by ngha cc chn ra ca PIC 16F84, 16F877A. 7. K tn v nu chc nng ca cc thanh ghi chc nng c bit (SFR). 8. Thanh ghi STATUS (trng thi) l g. Nu c in cc bit ca n. 9. Trnh by bn b nh d liu ca PIC 16F877A. 10. Trnh by 6 ng dng thc t ca b vi x l. 11. Cho bit c b nh gi a ch c ca b vi x l vi 20 ng a ch. 12. Minh ha lm th no chuyn ni dung a ch 20H vo thanh ghi tch ly W. 13. Sau y l mt lnh ca PIC 16F877A: MOVWF 06h a)M lnh 14 bit ca lnh ny l g? b)Gii thch mc ch cc bit ca lnh ny. c)Lnh ny tn bao nhiu chu k my thc thi? d)Gi s PIC 16F877A dng thch anh 4 Mhz th my bao lu thc thi lnh ny? 14.Bus vi x l no l 2 chiu. 15. Nu nghi ng CPU b hng th qui trnh v nhng iu cn ch cn phi theo sa cha l nh th no? VI. Rt kinh nghim: (v thi gian, ni dung, phng php, chun b) Ngy .thng..nm 2011 Ngy .thng..nm 2011 B mn Ging vin son

  • 41

    Bi ging s 3: : Tp lnh ca vi iu khin PIC( S tit: 10)

    III.1. Mc tiu: -Gii thiu. -Cc phng php a ch ha. - Tp lnh ca vi iu khin PIC : tp lnh vi iu khin PIC, cc thanh ghi, tm tt tp lnh. -Cc ch th assembler. -Phng php lp trnh: phng php lp trnh, chng trnh con.

    III.2. dung v phng tin dy hc: -Phn trng, bng, khn, bt long, micro c dy(hay khng dy), my tnh, v n chiu (hay my chiu). III.3. Gio trnh v ti liu tham kho: Gio trnh Vi x l ca trng i hc cng nghip Tp. HCM. D.W. Smith, PIC in practice: a project-based approach, Elsevier, 2nd edition, 2006. Trng Trc, Chip n 16C84 v ng dng ca chng. H Trung M, Vi x l, NXB HQG Tp. HCM, 2003. Ti liu v vi iu khin PIC ca b mn in t cng nghip. Datasheet c a PIC 16F84 v 16F877A. Website: http://www.microchip.com/ http://www.alldatasheet.com/ Myke Predko, Programming and customizing the PIC microcontroller, 3rd edition, Tab Electronics, McGrawHill, 2008(Ebook). Douglass V.Hall, Microprocessors and interfacing: Programming and Hardware, 2nd ed., Macmillan/McGraw-Hill, 1992. III.4.Ni dung bi ging: Ni dung chi tit : xem bi ging chi tit. Phng php ging dy: thuyt trnh, nu vn v m thoi trao i vi sinh vin.

    Chng 3: Tp lnh ca vi iu khin PIC 3.1.Gii thiu chung v lp trnh hp ng 3.1.1. Chng trnh :

    Hnh 3.1

    Ma nguon

    Thong dch/Bien dch

    Ngon ng may CPU

    Hanh ong

  • 42

    -Chng trnh : la chuoi cac cau lenh bao cho may phai lam g, lam nh the nao vakhi nao phai lam. -Ngon ng may : la chuoi cac so 0/1 ma CPU thc hien. Ngon ng may c giai quyet trc tiep bi mach ien t -Ngon ng cap cao : gom co cac ngon ng sau . Ngon ng lap trnh hng oi tng : C++ .Ngon ng lap trnh cap cao : C, Pascal, QBasic .Ngon ng lap trnh logic : prolog . Ngon ng lap trnh ham : lisp . Thiet ke giao dien : Visual Basic, Borland Delphi va C++ Builder/Visual C++ - Bo phan bien dch : la nhng chng trnh dch ma nguon sang ma may - Ngon ng assembly : dung hoa ngon ng cap cao va ngon ng may, ta dung ngon

    ng assembly. Chng trnh se dung ma gi nh va dung cac lenh cua CPU Assembly language Assembler Object program Program Hnh 3.2: Qua trnh assembling 1 chng trnh nguon V du : MOVWF 6 assembling 00 0000 1000 0110 (ma may)

    d liu trong thanh ghi W c t vo PORT B.

    3.1.2.Gii thieu qua trnh bien dch : Phn mm MPLAB MPASM.EXE : dch m ngun thnh m my , chy MS-DOS. Trong mi trng Windows, cc tp tin cn c trn a l :MPASMWIN.EXE: , MPLINK.exe, v MPLIB.exe. To ra d n dung MPLAB:

    Ngon ng assembly

    Trnh bien dch

    Chng trnh oi tng

  • 43

    MPASM (assembler) MPLIB(librarian) Hnh 3.3: File Project trong MPLAB. Trong MPLAB, project (d n) l 1 nhm cc file (tp tin) c to ra hay nhn dng bi IDE. Hnh 3.3 minh ha cu trc ca 1 d n hp ng. 3.1.3. Qui trnh xy dng chng trnh: a. Nhn v nghe: vn g cn gii quyt, ci g cho, ci g cn tm, c th v mt lu (flow chart) din t chng trnh hot ng nh th no. b. Vit chng trnh theo ngn ng assembly (hp ng) s dng cc iu cho gii quyt bi ton theo yu cu u ra. c.Nhp chng trnh vo my tnh. d. CHuyn m ng cho chng trnh (assembling-bin dch). e. Cho chy chng trnh. f. nh gi kt qu. g. Bo tr. V d: Vit chng trnh nhp mt s v in ra tr s tuyt i ca s . V lu gii thut: Bat au Nhap 1 so Tnh tr tuyet oi In ket qua Ket thuc

    Prog1.asm PxxFyy.inc

    Prog1.obj

    MPLINK (linker)

    Prog1.hex Prog1.lst Prog1.map Prog1.err

    Sub.lib Device.lkr

  • 44

    K hieu bat au/ket thuc Nhap/xuat Lenh/thao tac Lenh re nhanh Goi chng trnh con 3.1.4. Cu to lnh:

    Mot lenh trong ngon ng assembly (hp ng) trong chng trnh gom co 4 khu vc sau : Nhan Ma lenh cac toan hang ghi chu LOOPA MOVF TMR0, W ; c TMR0 vo W *Nhan (label field): la mot ten , dung cac ch cai (a-z), so (0-9) va dau gach di Nhan dung cho tham khao , lam a ch , goi chng trnh con *Ma lenh (Op-code) : la nhng ch viet tat, de gi nh c dung oi vi lenh cua PIC 16F877A. V du : movf, movlw, addwf, subwf vv *Toan hang (operand) : lenh co the la mot toan hang hoac hai toan hang. Trng hp 2 toan hang th toan hang th 2 goi la toan hang en (destination operand) va toan hang th 1 goi la toan hang nguon (source operand) V d: IORWF 7,W ; ni dung nh 7 c OR vi W, kt qu c lu W DECF 12 W : toan hang en, 7 : toan hang nguon V du : lenh 1 toan hang : COMF 6; o bit (b) nh 6. Lnh NOP ;khng c ton hng. *Ghi chu (comment) : la nhng li chu thch cho cho lenh o bat au bang dau (;). Chng trnh se bo qua 3.2.Phng php a ch ha (Phng thc tm kim a ch): Truy xut b nh d liu: a.Tm gin tip b nh Phng thc tm loi ny thc hin thong qua INDF b nh v FSR. a ch thc t trc tin t vo trong FSR thong qua vic truy cp INDF thc hin gin tip c v vit s liu b nh. Phng php tm a ch gin tip ch yu dng vit cc chng trnh cc bng tra hoc l bng tr s thc ht sc tin li. Th d: addwf INDF, b. Tm kim a ch s lp tc (tc thi). S thao tc v s lp tc, c th nhn c trc tip t trong m lnh. Th d: movlw Movlw .122 c.Tm kim a ch trc tip.

  • 45

    C th phng vn truy cp tm kim a ch trc tip vi bt k b nh no, tc l trong m lnh bao gm a ch ca b nh b truy cp. Th d: addwf , d. Tm kim a ch byte. C th trc tip truy cp tm kim mt bit bt k no trong b nh bt k tc l trong m lnh bao gm a ch ca b nh b truy cp ng thi bao gm a ch hang s trong b nh . Truy xut b nh chng trnh: e. a ch tuyt i: goto f. a ch tng i: addwf PCL,f *a ch trc tip thanh ghi:

    Th d: khi ng bit 0-3 ca port B nh ng ra. Thanh ghi W 9 bit a ch hin thc RP1 RP0 7 bit t lnh

    F0h

    0 1 0 0 0 0 0 0 0

  • 46

    Bsf STATUS, RP0 Movlw b11110000 Movwf TRISB Bcf STATUS,RP0 Clrf PORTB

    *a ch gin tip thanh ghi: 2 file ( nh) : a ch gin tip (00h) v FSR (09h)

  • 47

    Th d: Xa tt c v tr RAM t 20h ti 7Fh. Thanh ghi W 9 bit a ch hin thc IRP FSR Bcf STATUS,IRP Movlw 0x20 Movwf FSR loop clrf INDF Incf FSR,f Btfss FSR,7 Goto loop Lu s N vo thanh ghi file a nng: bn lu s N vo thanh ghi file 09h. iu ny l a ch trc tip. tuy nhin, ta bo chng trnh chuuyn s N vo vo thanh ghi file X, trong X gi gi gi tr 09h. iu ny l a ch gin tip. Thanh ghi file X thc s c gi l thanh ghi chn file (FSR) (bi v n l thanh ghi file m chn la thanh ghi file no chuyn s vo). Th d: chuyn 00h vo nh 08h movlw d08; np s 08 vo FSR Movwf FSR Clrf INDF ; xo a ch gin tip *a ch tc thi: Th d: movlw 0x20

    20h

    0 0 0 0 0 0 0 0 0

  • 48

    *PC absolute addressing (a ch tuyt i PC): Lnh call v goto: 13 12 11 10 9 8 7 6 5 4 3 2 1 0 . a ch tuyt i PC (B nh chng trnh) -Nhy ti 1 v tr b nh chng trnh khc ra khi chui tun t PC. -Gi chng trnh con. -11 bit ca a ch 13 bit yu cu c m ha trong lnh. -2 bit them vo s n t thanh ghi PCLATH. . c dung khi thc hin tc v goto tnh ton. -a ch nhy l c tnh bi chng trnh. -a ch c tnh ton l c ghi trc tip vo b m chng trnh.

    Th d:

    org 0x0000 molw HIGH chuongtrinhcon movwf PCLATH call chuongtrinhcon . org 0x1250 chuongtrinhcon .. return *a ch tng i PC:

    Opcode 0 0 0 0 0 0 0 0 0 0 0

  • 49

    ghi vo PC: 1)ghi byte cao vo PCLATH. 2)ghi byte thp vo PCL (PCH s c np vi gi tr t PCLATH). Th d: movlw HIGH 0x1250 movwf PCLATH movlw LOW 0x1250 movwf PCL Th d: S dng bng tm kim vi a ch tng 61i ly mu bit hin th con s trn led 7 on. Org 0x0020; trang 0 Movlw HIGH sevensegdecode Movwf PCLATH Movlw .5 Call sevensegdecode Movwf PORTB org 0x1800; trang 3 sevensegdecode: addwf PCL,f retlw b00111111 ;0 retlw b00000110; 1 retlw b01011011; 2 retlw b01001111;3 retlw b01100110;4 retlw b01101101;5 retlw b01111101;6 retlw b00000111;7 retlw b01111111;8 retlw b01101111;9

  • 50

    3.3. Tp lnh ca PIC 16F84A v 16F877A Tp lnh ca PIC bao gm 35 lnh. Mt lnh thc thi trong 1 chu k my, tr

    cc lnh nhy l 2 chu k my. Vi iu khin lm vic ch yu bng vn hnh d liu trong nh. Mt s trong cc nh ny l thanh ghi c bit, ci khc l nh ngi dng. Trong ng dng iu khin, d liu c th c c t port (cng) ng vo, vn hnh v chuyn ti port ng ra. s dng vi iu khin bn cn bit cch di chuyn v vn hnh d liu ny trong b nh. C 35 lnh trong PIC 16F877A/16F84A cho php bn lm iu ny. S dng vi iu khin l s dung nhng lnh ny trong chng trnh. Ging nh bt k ngn ng m bn khng s dng tt c t tt c mi lc, i khi bn khng bao gi dung ci khc ch by gi v lp li. Tp lnh ca PIC th ging iu ny, bn c th qun l tt vi 15 lnh m thi. Nhiu nht (a s) trong cc lnh bao gm vic s dng thanh ghi lm vic (working register) hay W. Thanh ghi W l trung tm (tri tim) ca vi iu khin PIC. di chuyn d liu t nh A (file A) sang nh B, bn phi di chuyn n t nh A sang W v sau t W sang nh B, kh ging h thng in thoi dn hng mt cuc gi ti ni khc thng qua tng i. Thanh ghi W cng vn hnh php ton logic v s hc trn d liu. 3.3.1. Tp lnh vi iu khin PIC lien lc (giao tip) vi vi iu khin PIC bn phi hc cch lp trnh n s dng tp lnh ca PIC. PIC 16F84 c 1k x 14 bit word b nh chng trnh EEPROM, 68x8 bit thanh ghi a nng v 35 lnh . PIC 16F877A c 8kx14 bit word b nh chng trnh Flash, 368x8 bit thanh ghi a nng v 35 lnh lm nn ba nhm lnh : bit, byte v tc v iu khin v s. Cc lnh ny c th c chia lm thnh 3 loi: . lnh bit, m tc ng ln 1 bit trong nh (file). . lnh byte, m tc ng ln 8 bit trong mt nh. . Tc v iu khin v s , m hiu chnh nh vi cc bin hay iu khin s di chuyn ca d liu t nh ny n ni khc. a.Lnh bit: Dng tng qut ca lnh: Tc v thanh ghi file hng bit: 13 10 9 7 6 0 b=3 bit a ch bit. f=7 bit a ch thanh ghi file. Lnh bit tc ng trn bit c th trong mt nh, v vy lnh c th c theo sau bi d liu m ch ra s nh v s bit. Th d BSF 6,3. M ny khng c nhiu thong tin chng ta c th s dng iu g ging nh BSF PORTB,BUZZER trong PORTB l nh 6 v BUZZER c ni vi bit 3 ca port ng ra. Trong phn tng ng chng ta s thy PORTB equ 6 v BUZZER equ 3. * BCF bit ; xa trong nh.

    Opcode b (bit#) f(file#)

  • 51

    Th d: GPIO equ 6; GPIO l nh 6 BCF GPIO,4; xo (0)bit 4 ca nh 6 * BSF bit; bt trong nh. Th d: BSF GPIO,2; bt bit 2 ln 1. * BTFSC bit; kim tra bit trong nh v b qua nu bit b xa (0). Th d: CHK_ON BTFSC GPIO,0; kim tra bo ng * BTFSS bit; kim tra bit trong tp tin ( nh) v b qua nu bit c bt (=1). Th d: zerobit equ 2; zerobit l bit 2 STATUS equ 3; STATUS l nh 3. BTFSS STATUS,zerobit b)Lnh byte: Dng tng qut ca lnh: Tc v thanh ghi file hng byte: 13 8 7 6 0 D=0 cho ch l thanh ghi W. D=1 cho ch l nh f. F= 7 bit a ch thanh ghi file. Lnh byte lm vic trn tt c 8 bit trong nh. V vy mt lnh byte s c theo sau bi s nh ph hp. TH d: DECF 0Ch; lnh ny khng c nhiu thong tin chng ta s ch ra tn ca nh nh l DECF COUNT. D nhin chng ta s cn khai bo phn tng ng m COUNT l nh 0Ch bng lnh COUNT equ 0Ch. * ADDWF cng W v F. * ANDWF and logic W v F. * CLRF xa nh . * CLRW xa thanh ghi lm vic W. * COMF ly b nh. * DECF gim nh i 1. * DECFSZ gim nh i 1, b qua nu bng khng (0). * INCF tng nh ln 1. * INCFSZ tng nh ln 1 v b qua nu bng khng (0). * IORWF: I-xor W v F (inclusive or) * MOVF di chuyn F vo W. * MOVWF di chuyn W vo F. * NOP khng lm g c. * RLF quay tri nh 1 bit ( 1 v tr). * RRF quay phi nh 1 bit. * SUBWF tr W t F , ngha l F-W. * SWAPF trao i 4 bit thp v 4 bit cao ca F. * XORWF EXOR W v F (exclusive or). c) Tc v iu khin v s: Dng tng qut ca lnh:

    Opcode d f(file#)

  • 52

    Tc v iu khin v s: 13 8 7 0 K=gi tr tc thi 8 bit. *Ch dung cho lnh CALL v GOTO: 13 11 10 0 k=11 bit gi tr tc thi. f=00h7Fh. Tc v s v iu khin vn hnh d liu v thc hin nhy (r nhnh) chng trnh. * ADDLW cng s vi W. * ANDLW AND logic s vi W. * CALL gi chng trnh con. * CLRWDT xa b m xung thi gian (b nh thi ch gi ca). * GOTO nhy ti a ch. * IORLW inclusive-or s vi W. * MOVLW chuyn s vo W. * RETFIE tr v t ngt. * RETLW tr v ni s trong W *RETURN tr v t chng trnh con. * SLEEP i vo ch ch (standby). * SUBLW tr s t W, ngha l s-W. * XORLW EXOR s v W. Nhng lnh ny lm vic ch yu trn 2 thanh ghi 8 bit-thanh ghi lm vic W v nh F m c th l mt trong 15 thanh ghi c bit ca 68 thanh ghi nh a dng m to thnh b nh ngi d ng (RAM) ca 16F84 hay ca 368 byte RAM ca 16F877A. Vi iu khin PIC l thit b 8 bit-iu ny c ngha l s cc i m c th lu tr trong bt k v tr nh l 255. Mt s PIC nh l 17C43 c 454 byte b nh d liu. nh v nh ln hn 255, tng trang hay bank c gii thiu. Bank 0 gi a ch nh ln ti 255, trong khi bank 1 c th gi hn 255 v bank 2 hn 255,vv Bn cn bit thanh ghi hay nh nm bank c th. 3.3.2. Thanh ghi: Thanh ghi c to thnh t 8 bit nh minh ha hnh sau:

    Opcode k(s)

    Opcode k(s)

  • 53

    Bit 7 6 5 4 3 2 1 0 MSB .... LSB Hnh: Cu to thanh ghi. Bit 0 l bit c trng s thp nht (LSB-Least Significant bit) v bit 7 l bit c trng s cao nht (MSB-Most Significant bit). *Thanh ghi 00: thanh ghi nh a ch d liu gin tip. Xem thanh ghi chn nh , thanh ghi 04. *Thanh ghi 01: TMR0, thanh ghi m/nh th 0. Thanh ghi ny c th c vit hay c ging nh bt k thanh ghi no khc. N c dung cho m hay nh th s kin. Ni dung ca thanh ghi c th c tng (cng 1) bi ng dng ca 1 xung bn ngoi p vo chn TOCKI, th d m xe i vo bi u xe hay bi xung clock chu k lnh bn trong m chy ca tn s thch anh ti s kin thi gian. *Thanh ghi 02, PCL, b m chng trnh . B m chng trnh t ng tng thc hin lnh chng trnh. Mt ng dng ca s dng b m chng trnh l minh ha phn bng tm kim chng cc v d lp trnh (chng 8). *Thanh ghi 03h, thanh ghi trng thi (STATUS register). *Thanh ghi 04 FSR, thanh ghi la chn nh. Thanh ghi la chn nh c s dng kt hp vi thanh ghi 00. Chng c dung trong a ch gin tip c hay ghi d liu khng phi t nh c th, nhng ti hay t nh c ch ra bi d liu trong thanh ghi chn nh. *Thanh gho 05 PORTA v thanh ghi 06 PORTB Port l cc kt ni chn m cho php vi iu khin lin lc (truyn thng) vi th gii xung quanh (bn ngai). PortA l port 5 bit 16F84 , ch c 5 bit thp c dng v l port 6 bit 16F877A, ch c 6 bit thp c dng. Bit 0 ca portA c th dung lp trnh l ng vo ng h (T0CKI). Port B l port 8 bit. thit lp port, TRIS c dung. TRIS l vit tt ca 3 trng thi. Ba trng thi c th l ng vo tr khng cao, ng ra mc cao (5V) hay ng ra mc thp (0V). *Thanh 8Fh: thanh ghi iu khin dao ng (ch c 16F818) Thanh ghi iu khin dao ng c dung chn tn s xung clock khi s dng dao ng bn trong. Bit 7 6 5 4 3 2 1 0 Hnh : Thanh ghi iu khin dao ng. Bit 6-4: IRCF2: IRCF0 : cc bit chn tn s dao ng bn trong. IRCF2 IRCF1 IRCF0 1 1 1 8 Mhz (ngun 8 Mhz li xung clock). 1 1 0 4 Mhz 1 0 1 2 Mhz 1 0 0 1 Mhz 0 1 1 500 khz 0 1 0 250 khz 0 0 1 125 khz

    1 0 1 1 0 0 1 0

    - IRCF2 IRCF1 IRCF0 - IOFS - -

  • 54

    0 0 0 31,25 khz (ngun INTRC li xung clock trc tip). bit 2: IOFS: INTOSC bit n nh tn s. *Thanh ghi W: Thanh ghi W gi kt qu ca mt tc v hay truyn d liu bn trong. N nh l trao i in thoi-d liu i vo thanh ghi W v c truyn ra ti mt nh khc. *Thanh ghi tu chn : OPTION register Thanh ghi ny c dung t l trc b m/ng h thi gian thc. Xung ng h TMR0 chy tn s thch anh nhng c th c chia tn nh xung. bi h s t l trc cho o khong thi gian di hn. *Ngn xp (stack) Ngn xp l mt vng nh lien tc c tnh cht vo sau ra trc (last in first out-LIFO). Con tr ngn xp s gi a ch nh ngn xp. Ngn xp l tn gn cho nh m gi vt ca a ch chng trnh khi lnh call c thc hin. C 8 mc ngn xp trong 16F84A/16F877A, m ngha l chng trnh c th nhy ti 1 chng trnh con v t nhy ti chng trnh con khc, lm thnh 8 ln nhy tng cng v ngn xp s (c th) tr v n ngc ti chng trnh. 16C54 c ngn xp 2 mc. 3.3.3. Tm tt tp lnh 3.3.3.1. Lnh bit a/BCF: xo bit trong nh F. Th d: BCF 6,4 ; bit 4 c xa trong nh 6. File 6 l PORTB, iu ny xa bit 4, ngha l bit 4 =0. b/BSF Bt bit trong nh F. Th d: BSF 6,4;lnh ny bt bit 4 (=1) trong file 6, ngha l bit 4=1. c/BTFSC: kim tra bit trong nh v nhy (b qua) nu b xa (=0). Th d: BTFSC 3,2 ; lnh ny kim tra bit 2 trong nh 3. Nu n b xa th lnh k tip c b qua. nh 3 l thanh ghi trng thi STATUS, bit 2 l c zero Z. Nh vy chng trnh nhy nu kt qu ca mt lnh l zero. d/BTFSS: kim tra bit trong nh v nhy (b qua) nu bit c bt ln 1. Th d: BTFSS 3,2; nu bit 2 trong nh 3 l bt ln 1 th lnh k tip b b qua. 3.3.3.2. Lnh byte: a/ADDWF: Cng ni dung ca W vo F. Th d: ADDWF 7; cng ni dung ca thanh ghi W v nh 7, ch l kt qu t vo nh 7. Gi s nh 7 cha s 5, thanh ghi W cha s 4. Kt qu lnh cng l nh 7 cha s 9 Th d: ADDWF 7,W; nh trn nhng kt qu c t W,w=9. Trng thi nh hng c C, DC , v Z. Th d: Cng 3h v 4h vi nh 20h cha 3h v nh 21h cha s 4h. Movlw .0; np 0h vo thanh ghi W Addwf 32,w; cng ni dung nh 20h (32) vi w, kt qu lu w Addwf 33,w; cng ni dung nh 21h (33) vi w, kt qu lu w. b/ANDWF: Ni dung ca W c and (v) logic vi F.

  • 55

    Th d: ANDWF 12,W; ni dung ca nh 12 c and (v) logic vi ni dung ca thanh ghi W. Ch l ni dung kt qu c t vo W. nh 12: 0 1 1 0 0 0 1 0 W: 1 0 0 1 0 1 0 0 andwf W: 1 1 1 1 0 1 1 0 Th d: ANDWF 12; nh trn nhng ni dung kt qu c t vo nh 12 (=11110110). Trng thi nh hng c Z. c/CLRF: Lnh ny xa nh F, ngha l 8 bit trong nh F b xa (0). Th d: CLRF 5; xa nh 5. Trng thi nh hng c Z. d/CLRW: Lnh ny xa thanh ghi W. Th d: CLRW Trng thi nh hng c Z. e/COMF 8 bit trong thanh ghi F c ly b, ngha l o li. Th d: COMF 6; o bit (b) nh 6. Gi s nh 6 l 1 0 1 0 0 0 0 1 Comf 0 1 0 1 1 1 1 0 Trng thi nh hng c Z. f/DECF: Tr i 1 t nh F (F-1). Lnh ny c ch cho m xung v 0. Th d: DECF 12; s gim nh 12 i 1, kt qu s c lu trong nh 12. Gi s nh 12 cha s 5, kt qu lnh decf cho nh 12 cha 4. Th d: DECF 12,W; nh trn v s lu kt qu vo W (=4), li nh 12 khng i. Trng thi nh hng c Z. g/DECFSZ: Ni dung ca nh F c gim i 1 v lnh k tip c b qua nu kt qu bng 0. Th d : DECFSZ 12 Hay DECFSZ COUNT h/INCF: Cng 1 vo nh F. Gi tr ny sau c so snh vi ci khc xem nu gi tr tng cng t c cha. Th d: INCF 14; tng nh 14 ln 1 Hay INCF COUNT Gi s nh 14 cha s 2, sau khi thc hin lnh incf nh 14 cha s 3. Trng thi nh hng c Z. i/INCFSZ: Cng 1 vo F nu kt qu bng 0 th b qua lnh k tip. Th d: INCFSZ 19 Hay INCFSZ COUNT j/IORWF: Ni dung ca thanh ghi W c OR (hoc ) logic vi (thanh ghi ) nh F. Th d: IORWF 7,W; ni dung nh 7 c OR vi W, kt qu c lu W.

  • 56

    Th d: IORWF 7; nh trn nhng kt qu c lu nh 7. Trng thi nh hng c Z. k/MOVF: Ni dung ca nh F c di chuyn vo thanh ghi W, t d liu c th c di chuyn ti port ng ra. Th d: MOVF 12,W; nh 12 c chuyn ti W. Th d MOVF 12; nh 12 c chuyn ti nh 12? c zero b nh hng. Trng thi nh hng c Z. l/MOVWF: Ni dung ca thanh ghi W c chuyn vo nh F. Th d: MOVWF 6; d liu trong thanh ghi W c t vo PORTB. W=11100010, sau lnh movwf th nh 6 cha 11100010. m/NOP: (No operation) Khng lm g c. iu ny ging nh tng khng tt nhng rt hu ch cho tr hon nh. Lnh NOP to tr ca tc xung clock. n/RLF: Ni dung ca nh F c quay i 1 bit t tri qua phi thong qua c nh. Dch 1 bit nh phn v bn tri c ngha l s c nhn vi 2. Lnh ny c ch khi nhn s nh phn. Th d: RLF 12,W; dch tri nh 12 qua c nh, kt qu c t vo W. Th d: RLF 12; nh trn nhng kt qu c t vo nh 12. S bn di minh ha nh 12 c dch tri C C nh 12 trc khi quay tri C C nh 12 sau khi quay o/RRF: Lnh ny quay phi ni dung nh F 1 bit v bn phi (Ni dung nh F c quay 1 v tr v bn phi. Th d : RRF 12,W; dch phi nh 12 qua c nh, kt qu c t vo W. Th d: RRF 12; nh trn nhng kt qu c t vo nh 12. nh 12 trc khi quay phi C C

    0 1 0 1 1 0 0 1 0

    1 0 1 1 0 0 1 0 0

    0 1 0 1 1 0 0 1 0

  • 57

    nh 12 sau khi quay C C p/SUBWF: Ni dung ca thanh ghi W c tr i t ni dung ca nh F. Th d: SUBWF 14,W; thc hin F-W, kt qu c t vo W. Th d: SUBWF 14; thc thi F-W, kt qu t vo F ( nh 14). Ch : nu W>F th c C=0 v kt qu l m (-ve). Nu W

  • 58

    Th1i d : ANDLW 12H Hay ANDLW B00010010 Hay ANDLW .18 Gi s W= 0 0 1 0 1 0 1 0 12h=0 0 0 1 0 0 1 0 lnh andlw: W= 0 0 0 0 0 0 1 0=02h Thanh ghi W c and(v) vi s 12h. Kt qu l 02h c t vo W. c)CALL:

    Lnh ny s gi mt chng trnh con trong chng trnh chnh. Th d: CALL WAIT1MIN; lnh ny s gi mt chng trnh con (bn vit) i 1 pht. C th l bt n trong mt pht v sau tr li chng trnh chnh. d)CLRWDT:

    B nh thi watchdog (b m xung thi gian) b xa. B m xung thi gian l thit b an ton trong vi iu khin nu chng trnh lm b m xung thi gian ht thi gian v sau khi ng li chng trnh. Trng thi nh hng c T0, PD. e)GOTO:

    y l lnh nhy khng iu kin ti mt v tr c th trong chng trnh. Th d: GOTO SIREN Th d: GOTO MAIN f)IORLW:

    Ni dung ca thanh ghi W c OR (hoc) logic vi mt s. Th d: IORLW 27 W= 1 0 0 1 1 0 1 1 L (s)= 0 0 0 1 1 0 0 1 iorlw L+W = 1 0 0 1 1 0 1 1 iu ny l cc rt hu ch xc nh nu bt k bit no trong nh c bt ln 1, ngha l bng cch OR mt nh vi 00000000 nu tt c cc bit l zero, kt qu OR l zero v bit c zero c bt trong thanh ghi trng thi. Trng thi nh hng c Z. g) MOVLW:

    S 8 bit c di chuyn trc tip vo W. Th d: MOVLW .127; chuyn s 127 vo W Trng thi nh hng c Z. h)RETFIE.

    Lnh ny c dung tr v t ngt. i)RETLW:

    Lnh ny c dung cui mt chng trnh con tr v chng trnh chnh theo sau lnh CALL. Gi tr s c t trong thanh ghi W. Lnh ny c th c dung vi bng tm kim. Th d: RETLW 0 j)RETURN:

    Lnh ny c dung tr v t chng trnh con. k)SLEEP

    Khi thc hin lnh ny, chip vi iu khin c t vo ch ngh (sleep). Bit trng thi gim cng sut b xa, trng thi ht hn (timeout) c bt ln 1, b m xung thi gian v b chia tn t l trc c xa v b li dao ng b tt. B m xung thi gian vn cn chy t xung clock bn trong ca n.

  • 59

    Th d: SLEEP Trng thi nh hng c T0, PD. l)SUBLW:

    Ni dung ca thanh ghi W c tr t 1 s. Th d: SUBLW 14; thc hin 14-W, kt qu c t vo W. Bi c nh C v c zero Z trong thanh ghi trng thi (STATUS) b nh hng. Ch : nu W>14 th c C=0 v kt qu l ve (m). Nu W

  • 60

    *Chng trnh nhn hai s nh phn 8 bit: Start No C=1? Yes No B m =0? yes End 20h: s b nhn. 21h: s nhn. Sauk hi chng trnh chy xong, kt qu s c gi vo 2 : 30h cha kt qu byte thp v 31h cha kt qu byte cao. Chng trnh nhn 2 s 16 bit s cho kt qu 32 bit. Cch thc hin cng ging nh php nhn 2 s 8 bit. 3.4.Cac ch th assembler : (assembler directive)

    Cac ch th assembler la cac lenh hng vao chng trnh assembly ch khong phai do CPU thc hien. Chng trnh ta viet se bao gom cac ch th assembler, tuy nhien cac ch th assembler nay se khong c dch ra ma may. V du : ORG, EQU, vv

    Moi ch th assembler gom co 4 khu vc : Ten Ch th Argument Ghi chu Pi equ 3.14 ;xac nh gia tr so pi * Ten : dung cac k hieu nh ch, so giong nh nhan, bat au bang ch cai * Ch th : ten viet tat ch th assembler, giong nh op-code * Khu vc oi (argument) : cha 1 a ch nh hoac 1 so e s dung cung vi d lieu va do d lieu xac nh V du : DT=pi*R^2

    Xo ACC v 2 nh cha kt qu Np b m vi 8.

    Dch tri cha s nhn.

    Xo c carry (A)=(A)+(20h)

    Quay phi ACC v nh 30h

    Gim b m (X=X-1)

  • 61

    CV=2*pi*R * Ghi chu : comment Mot so ch th assembler :

    1.Ch dan ieu khien trang thai assembler - ORG (originate) : khi au

    Ch th org dung e at con tro lenh vao v tr khi au cua chng trnh trong bo nh. V du : org 100h ; chng trnh bat au v tr nh 0100h - END : phai la phat bieu cuoi cung trong tap tin nguon. Ta khong c s dung

    nhan trc ch dan END. Nhng g sau ch dan END se khong c x ly. 2. Ch dan nh ngha ky hieu - EQU(equate) , can bang hay SET

    c dung e lien ket 1 ten vi mot so hoac mot k hieu tng trng khac. V du : pi equ 3.14

    DT=2*pi*R, trong chng trnh assembler se thay pi=3,14 Th du : N38 SET 30 TMR0 EQU 1; TMR0 l file 1 STATUS EQU 3; STATUS l file 3 3. Ch dan khi tao tr trong bo nh 4. Ch dan danh cho trong bo nh 5. Ch dan lien ket chng trnh 3.5. Phng phap lap trnh : 3.5.1. Phng phap lap trnh -Phan tch he thong -Lu o giai thuat -Viet chng trnh

    Khi phai viet nhng chng trnh dai va hoan chnh th van e to chc cau truc tr nen rat quan trong. Mot phng phap to chc la xay dng e cng . Co the dung e cng e chia bai toan thanh nhng phan nho. Nhng phan nay phai theo th t e giai quyet bai toan 1 cach chnh xac. Mot phng phap lam e cng cho bai toan la ve lu o.

    Bai toan : xay dng 1 lu o cho chng trnh ngon ng assembly e ieu khien bao ong mot tram can xe ch hang. Neu xe ch qua 1 trong lng nao o th se co am thanh bao ong, con neu gap c xe di hang qui nh th khong co bao ong va cho phep xe i qua

    Data_in : trong lng xe vao Data_out : gia tr xuat e ieu khien , 00h :ngat bao ong, FFh : bao ong Data_in max_wt data_out=FFh=11111111b V trong lng cc ai la 10 tan nen max_wt=10=Ah

  • 62

    Lu o : Bat au STATUS equ 3 nh ngha bien CARRY equ 0 Data_in equ 20h Data_out equ 21h at gii han trong lng Max_wt equ 0Ah Ngat bao ong Alarm_off : movlw .0 Movwf data_out oc trong lng data_in NEXT : movf data_in,w; nh 20h vo w Sublw Max_wt; 10-w So sanh

    goto Alarm_off NEXT Xuat bao ong Alarm_on : movlw b11111111 Movwf Data_out Goto NEXT

    Hnh 3.4:Bang lu o chng trnh cua tram kiem soat trong lng xe 3.5.2.Chng trnh con : (subroutine) -Chng trnh con la mot phan nho cua chng trnh , dung e thc hien mot nhiem vu rieng biet. Thng nhiem vu chng trnh con thc hien la loai lap lai trong chng trnh -Cu phap : Call ni en

    Lenh e chung ta chuyen sang chng trnh con la lenh call. Khi ket thuc chng trnh con, muon quay ve chng trnh chnh th dung lenh RETLW/RETURN. Ni en cua chng trnh con co the la 1 nhan, 1 a ch . Lenh RETURN thng khong co toan hang kem theo, RETLW c tr v s.

    Khi CPU thc hien lenh call th a ch ke tiep cua chng trnh c gi cat vao ngan xep (stack). Neu khong lam nh vay th CPU khong biet quay ve au trong chng trnh chnh. Lenh call se ay noi dung ang hien dien trong con tro lenh PC vao ngan xep, tiep theo CPU se a a ch nh cua chng trnh con vao con tro lenh PC.

    Qua trnh xay ra nh sau : - Lenh call c CPU oc va giai ma - Con tro lenh c tang len e ch vao lenh ke tiep trong chng trnh chnh - Tiep theo noi dung cua con tro lenh c ay vao trong ngan xep - a ch xuat phat cua chng trnh con c nap vao con tro lenh

  • 63

    - Chng trnh con c thc hien - Lenh retlw/RETURN la lenh cuoi trong chng trnh con se ay bat a ch quay

    ve cua chng trnh chnh ra khoi ngan xep - a ch quay ve c nap vao con tro lenh va chng trnh chnh c tiep tuc V du : Call Delay ; goi chng trnh con delay

    nh ngha chng trnh con Delay :

    Delay: clrf tmr0; bt u timer 0

    Loopa movf tmr0,w; c tmr0 vo w

    Sublw .32;time-32

    Btfss 3,2; kim tra time-32=0

    Goto loopa; time khng bng 32

    Retlw 0; time l 32 , tr v

    Chng trnh con weight:

    Chng trnh tram can xe 3 thang o : hnh 3.5

    start nh ngha d lieu at max_wt call nap data_in1 ngat bao ong weight call weight call ret bao ong nap data_in2 ret call weight call ret nap data_in3 call weight call jump ret

    Hnh 3.5 IV.Tng kt bi: -Tm tt cc chnh trong bi. -Chun b bi mi.

    V. Cu hi v bi tp v nh: Chng 3: Tp lnh ca PIC 16F877A v 16F84

  • 64

    1.Chng trnh l g? Nu c im ca ngn ng my, hp ng v ngn ng cp cao. 2. Qu trnh bin dch : cch to d n dung MPLAB. 3. Trnh by cc bc (qui trnh) xy dng chng trnh. 4. Mt lnh hp ng y gm cc b phn no? 5. Phng php xc nh v tr ca mt ton hng c gi l g? 6.Trnh by cc phng thc a ch ha. Cho v d. 7.Tp lnh ca PIC c chia lm my nhm. K tn. 8. Gii thch ngha ca lnh MOVF 12,W. 9. Ch th EQU dung lm g. 10. Cho mt th d v ngn ng lp trnh my tnh bc cao. 11. Ngn ng lp trnh my tnh bc cao dung. dch cc lnh ra m my. 12. Assember l g? Phn mm MPLAB ( Windows) hay MPASM ( DOS) dung lm g? 13. Nu cc lnh bit v cho v d. 14. Nu c lnh v byte v cho v d. 15. Nu cc lnh v iu khin v s. Cho v d. 16. Vit cng trnh cng 4H vo 3H dng thanh ghi W, nh 20H (gi gi tr 4H) v 21H (gi gi tr 3H). Np kt qu vo a ch nh 22H mang nhn STORE (HD: STORE EQU 22H). 17.Vit chng trnh tnh 1+2+3+4 dng lnh ADDWF v INCF dung thanh ghi W v nh 20H. Kt qu lu W. 18. Vit chng trnh tnh Z=A.B+(not)C trong dung nh 20H cho lu gi tr A, nh 21H lu gi tr B, nh 22H lu gi tr C. Lu kt qu ca Z trong v tr nh 23H mang nhn STORE. 19. Vit chng trnh nhn hai s nh phn 8 bit. nh 20H cha s b nhn, nh 21H cha s nhn. Kt qu tch s 16 bit cha trong 2 nh 22H (byte thp) v 23H (byte cao). 20. Lnh COMF 12 cho kt qu nh th no nu nh 12 cha ni dung FFH. 21. Trnh by cc ch th assembler. 22. Vit chng trnh pht m thanh bo ng khi nhit ca mt t lnh vt qu 50 F. V lu i vi chng trnh v gii thch chng trnh ang lm g bng cc ch gii. Bo m c c cc ch th assembler cn thit. 23. Vit chng trnh cho bi ton trm cn xe. 24. S khc nhau gia lnh v ch th assembler. 25. nh ngha chng trnh con. 26. Mot cong tac DIP 4 bit va 1 LED 7 oan anode chung c noi vi PIC 16F877A nh hnh ve. Viet 1 chng trnh oc lien tuc ma 4 bit t cong tac DIP va cap nhat cac led e hien th ky t hexa thch hp. Th du, neu ma 1100B c oc vao, ky t hexa C se xuat hien. Nh vay, cac segment (oan) a en g, tng ng se la ON, OFF, OFF, ON, ON, ON, va OFF . Chu y rang at 1 chan port pic 16f877a len 1 lam LED tng ng ON. (xem hnh ve)

  • 65

    PIC 16F877A 74LS244 LED 7 oan +5V a b c d e f g h 8x220 ohm

    Hnh : Giao tiep vi cong tac DIP va LED 7 oan 27.Thit k mt mch m m v hin th trn led 7 on (loi catt chung), s ln mt nt nhn c n ti 10. M nh phn li led 7 on nh sau: S PORTB B7 B6 B5 B4 B3 B2 B1 B0 0 0 1 1 1 0 1 1 1 1 0 1 0 0 0 0 0 1 2 0 0 1 1 1 0 1 1 3 0 1 1 0 1 0 1 1 4 0 1 0 0 1 1 0 1 5 0 1 1 0 1 1 1 0 6 0 1 1 1 1 1 0 0 7 0 1 0 0 0 0 1 1 8 0 1 1 1 1 1 1 1 9 0 1 0 0 1 1 1 1 Xt mch in nh sau:

    Hnh: S mch ca iu khin led 7 on.

    RA0 RB0 RA1 RB1 RA2 RB2 RA3 RB3 RB4 RB5