Tim Hieu Pic 16f84a

Click here to load reader

  • date post

    05-Jul-2015
  • Category

    Documents

  • view

    588
  • download

    3

Embed Size (px)

Transcript of Tim Hieu Pic 16f84a

CHI TIT CH

Bn t tm hiu v PIC: Vit chng trnh cho PIC16F84A

Bi ang son: VKH

PIC (Peripheral Interface Controller) l IC lp trnh dng iu khin cc thit b t ng, vi cc b nh chng trnh (Flash ROM) v b nh m (SRAM). C th xem PIC l mt my tnh nh, do trong PIC c b phn x l cc m lnh, quen gi l cc chng trnh c Bn cho ci t vo b nh Flash ROM, c b nh SRAM, nh vy c th xem PIC l loi IC n phin, ngha l mt mnh n c th cu to thnh mt thit b vi iu khin hon chnh. Chng ta bit b nh trong PIC thng c dung lng nh (dung lng ln nh cn ty theo loi PIC). Tn s xung nhp ca PIC, ti a thng l 20MHz v dung lng b nh thng t 1K n 4K.

Trong bi vit ny ti s vit v PIC16F84A, bi vit gm c 3 phn:

Phn 1: Gii thiu khi qut v PIC16F84A Phn 2: Trnh by cc ng dng ca PIC16F84A qua mt ng dng c th. Phn 3: Ni v trnh vit chng trnh v gii thiu v hp np chng trnh chi cc IC h PIC.

Phn 1: Gii thiu khi qut v PIC16F84APIC16F84A c cc c tnh sau:

* Ch dng 35 cu lnh vit tt c cc chng trnh ngun cho PIC16Fxx. * Tt c cc cu lnh ch dng 1 chu k my, cc cu lnh nhy dng 2 chu k my. * Tc vn hnh: dng xung nhp l 20MHz v chu k my l 200ns. * C b nh chng trnh (Flash Program Memory) 1024 Word (1K x 14).

* B nh RAM (RAM File Rigister) 68 byte. * B nh d liu EEPROM 64 byte. * Chiu rng cu lnh l 1 Word 14 bit. * X l d liu dng 8 bit (1 byte). * C 15 thanh ghi chuyn dng SFR (Special Function Register) trong RAM. * Dng ngn xp c chiu xu 8 lp. * C mode truy cp theo a ch trc tip, gin tip v a ch tng i. * C 4 dng ngt: Ngt ngoi trn tn hiu trn chn RB0/INT. Ngt theo bit bo trn ca Timer 0. Ngt theo s thay i trn cc chn RB4, RB5, RB6, RB7. Ngt khi ghi xong d liu vo b nh EEPROM.

* C 13 chn dng xut nhp d liu ( cng Port A c 5 chn v Port B c 8 chn). * C kh nng cp dng chy vo/chy ra ln: 25mA, cp cho cc Led chiu sng. * C 1 ng h Timer 0 (TMR0), dng thanh m xung 8 bit, nn m c ti a 256 nhp. * Vi b nh chng trnh Flash Program Memory, c th xa ghi c 1000 ln. * Vi b nh EEPROM c th xa ghi d liu n 1000000 ln. * D liu ct gi trong b nh EEPROM c th an ton tn 40 nm.

* C th lp trnh v np ngay trn bo (ICSP, In-Circuit Serial Programming) ch dng 2 chn. * C chc nng POR (Power On Reset), PWRT (Power-up Timer), OST (Oscillator Start-up Timer). * Chc nng Watch-dog (WDT) lm vic vi mch dao ng RC ring trong IC. * C mode bo v m. (Code Protection). * C mode Sleep dng tit kim in nng khi trng thi ch. * C nhiu ty chn cho mch dao ng to xung nhp chnh.

* Lm vic vi mc ngun nui trong khong 2V n 5.5V.

Hnh 1: Cho thy cng dng ca cc chn trn IC.

Trong hnh ny:

* OSC1/CLKIN l chn ng vo ca mch dao ng thch anh. nh tn cho xung nhp v cng l mt ng vo ca mch to xung nhp.

* OSC2/CLKOUT l chn ng ra ca mch dao ng thch anh v l ng ra ca xung nhp.

* /MCLR (Master Clear) l chn Reset, tc dng ca lnh Reset l tr chng trnh v a ch 0000h. Lnh tc dng mc p thp.

* RA0, RA1, RA2, RA3, l cc chn xut nhp ca bn cng Port A.

* RA4/TOCKI l chn a nhim, va lm chn xut nhp ca Port A v li l chn ly xung cho thanh m ca ng h Timer 0 ( chn ny c cc Drain h).

* RB0/INT l chn a nhim, va lm chn xut nhp ca Port B v li l chn pht ng theo ngt ngoi. Cc chn ca Port B c th c lp trnh c tr khng ln dng lm ng vo, nhp trng thi ngoi vo PIC.

* RB1, RB2, RB3, l cc chn xut nhp ca bn cng Port B.

* RB4, RB5, l cc chn xut nhp ca Port B v pht ng ngt theo s thay i trn cc chn ny.

* RB6, l 1 chn xut nhp ca Port B v pht ng ngt theo s thay i trn chn ny. N cn c th lp trnh dng chn ny pht xung nhp dng cho cng nng truyn d liu ni tip.

* RB7, l 1 chn xut nhp ca Port B v pht ng ngt theo s thay i trn chn ny. N cn c th lp trnh dng chn ny trao i d liu dng cho cng nng truyn d liu ni tip.

* VSS l chn ni masse ly dng ( y l dng in t).

* VDD l chn ni vo ng ngun dng (t 2V n 5.5V).

Hnh 2: Hnh v cho thy cc khi chc nng trong PIC16F84A

Flash Program Memory c dung lng 1024 thanh nh, loi rng 14 bit (1Kx14). y Bn ct gi cc m lnh ca chng trnh ngun. Cc m lnh c truy cp theo m a ch c trong thanh ghi PC (Program Counter). M lnh xut ra trn thanh ghi Instruction Register.

Thanh ghi Program Counter dng ghi cc a ch ca m lnh ca b nh Flash ROM. Trong hot ng, khi dng lnh nhy n cc chng trnh con, th a ch hin ti s c tm thi cho ct gi trong cc thanh ghi ngn xp, y chiu xu ngn xp ch c 8 lp (8 Level Stack).

Ngn xp 8 Level Stack dng lu gi cc m a ch ca chng trnh chnh khi trong chng trnh c dng lnh nhy. a ch ct vo ngn xp c th hiu nh dng lnh Push v a ch ly ra t ngn xp c th hiu nh dng lnh Pop.

Instruction Register l thanh ghi m lnh, ng ra, m lnh c th chuyn n khi gii m Instruction Decode & Control to ra lnh iu khin. Hay chuyn n khi x l a ch a knh Address Multiplex truy cp cc thanh nh trong b nh RAM (File Register).

Instruction Decode & Control l khi gii m, xc ?nh tnh nng iu khin trong cu lnh, n tc dng vo khi ?nh trng thi cho IC. Khi ny gm c cc chc nng: Power-up Timer, Oscillator Start-up Timer, Power-on Reset, Watchdog Timer.

Power-up Timer dng kch hot IC theo ng h Timer.

Oscillator Start-up Timer dng kch hot mch dao ng theo ng h Timer.

Power-on Reset dng phc nguyn cc thanh ghi trong PIC khi n mi c cp ngun.

Watchdog Timer dng vo ra mode Ch gi nh theo ng h Timer, mt tnh nng dng tit kim in.

Timing Generation l khi to ra xung nhp chnh, tn s ca xung nhp thng ?nh theo thch anh. PIC16F84A c th hot ng vi xung nhp 20MHz.

ng h Timer 0 (TMR0) dng mt thanh m 8 bit to ra chc nng iu khin theo thi gian. N c th m ti a 256 xung nhp, khi thanh m y, bit bo trn s chuyn ln mc 1.

Cc bn cng I/O Port (gm Port A c 5 chn v Port B c 8 chn) dng xut nhp d liu. Nhiu chn cn c tnh a nhim, nn ngoi cng nng xut nhp d liu n cn c cc cng nng khc. Nh cng nng nhp xung m trn chn RA4/TOCKI, cng nng ngt trn chn RB0/INT

B nh d liu EEPROM Data Memory, y l b nh xa ghi c trn 1 triu ln, EEPROM c 64 thanh nh, vi rng 8 bit (1 byte). truy cp d liu trong cc thanh nh, m a ch s chuyn vo thanh ghi EEADR v d liu xut nhp trn thanh ghi EEDATA.

B nh RAM File Register, y l b nh RAM c 68 thanh nh, vi rng 8 bit, trong c 12 thanh nh chuyn dng (SFR, Special Function Register), cc thanh nh cn li c dng lm thanh nh ph dng (GPR, General Purpose Register). Cc thanh nh chuyn dng xc ?nh hot ng ca IC, cc thanh nh ph dng dng lm thanh nh tm. Khi IC mt ngun, cc d liu trong cc thanh nh RAM u b xa sch. Ngi ta truy cp d liu trong cc thanh nh ca RAM vi b ni m a ch RAM Addr (7 ?ng) v xut nhp d liu trn b ni Data Bus (8 ng)

Thanh ghi FSR (File Select Register) dng truy tm d liu theo m a ch gin tip (Indirect Address), dng b ni Indirect Addr (7 ? ng).

Thanh ghi STATUS (Status Register) dng chn ?nh cc iu kin lvievj cho IC.

MUX (Multiplex) l khi x l a nhim, n cung cp m cho khi tnh ton ALU.

ALU (Arithmetic & Logic Unit) l khi thc hin cc php ton s hc v logic. Khng c b phn ny, IC khng c gi l IC my tnh.

Thanh ghi W reg (Work Register) l thanh ghi cng tc, n ct gi cc kt qu ca khi ton ALU.

Hnh 3: Tm hiu b nh Flash Program Memory (tc b nh EEPROM).

Sau y chng ta tm hiu cu trc cc thanh ghi, b nh trong PIC16F84A:

Flash Memory c dng lu gi cc m lnh ca chng trnh ngun. y 1 Word l 14 bit. N c 1024 thanh nh (QUEN GI L 1KWORD). KHI IC B MT NGUN, CC D LIU CT TRONG CC THANH NH S KHNG B XA MT. S LN XA GHI CA LOI B NH NY KHNG QU 1000 LN. CHNG TA BIT MI THANH NH S C XC ?NH THEO MT M A CH. A CH KHI U L 0000H.

Ngi ta t chc cc thanh nh trong b nh Flash ROM nh sau:

Thanh nh c a ch 0000h (Reset Vector) l v tr nhy n ca chc nng Reset. Khi IC va c cp in, hay do tc dng ca cng nng Watch-dog v bt c l do no khc, tc dng Reset s cho chng trnh khi u tr li t a ch 0000h.

Thanh nh 0004h (Interrupt Vector) dng cho chc nng ngt. Khi dng ngt theo Timer 0, khi thanh m trn, bit bo trn chuyn ln mc 1, chng trnh s nhy v a ch ny. Tc dng ca cc ngt ngoi cng khin chng trnh nhy n a ch 0004h ny.

Thanh ghi 2007h (Configuration word) dng xc ?nh hot ng c bn ca PIC, trong vng ny c cc bit dng xc ?nh cho mode Power-up Timer, xc ?nh cho mode Watch-dog Timer cng dng chn ?nh iu kin lm vic ca mch dao ng. Cc thanh nh bn trn vng ny (Test/Configuration Memory Space) c dng cho cng nng CI T CHNG TRNH VO B NH. BN KHNG TH TRUY CP C CC THANH NH TRONG VNG NH NY.

B nh EEPROM (Electrically Erasable Programmable Read Only Memory). y l loi b nh ROM xa ghi bng mc p cao Vpp, nn ni dung ghi trong cc thanh nh s khng b xa dng IC khng c cp in. Cc thanh nh ny c th cho xa ghi li nhiu ln, dung lng ca b nh l 64 byte (tc c 64 thanh nh), s ln xa ghi b hn ch khong 1 triu ln. V l do ny, b nh EEPROM khng c dng lm b nh tm thi nh RAM (cn s ln xa ghi khng gii hn), b nh EEPROM ch c dng ghi cc d liu t phi thay i. D liu ghi trong b nh ny c th tn ti n trn 40 nm.

Hnh 4: Tm hiu b nh SRAM

Cc thanh nh ca b nh File Register ca RAM (Random Access Memory). y, ngi ta dng cch chuyn di nh ( chuyn bank) cho b nh ny. Mi di nh c dung lng l 80 byte (80 thanh nh vi m a ch l 00h n 4fh). Trong IC vi iu khin ?n phin PIC16F84A c 2 di nh (C HAI BANK, BANK 0 V BANK 1). B NH NY CHIA RA LM 2 PHN:

PHN U, NGI TA LY 12 BYTE (TC 12 THANH NH C A CH T 00H N 0BH) CA MI DI DNG LM CC THANH NH C DNG (SFR, SPECIAL FUNCTION REGISTER) V DNG N T BIT 0/1 XC ?NH CC TRNG THI VN HNH CA PIC. ?NH IU KIN CHO VIC XUT NHP TRN CC BN CNG V CN ?NH CC IU KIN KHC NA.

C 16

THANH GHI KHC NHAU TRONG VNG CHA CC THANH GHI C DNG

(11

THANH TRONG BANK GHI LU TR BANK.

0

V

5

THANH TRONG BANK

GHI NY DNG QUN L S VN HNH CA

1). NI DUNG TRONG MI THANH PIC. MC D C TNG CNG 24 THANH

(FILE

REGISTER), NHNG

7

TRONG CHNG NM CHUNG TRONG C HAI

CNTH

LI

68

BYTE

(TC 68

THANH NH, C A CH T

0CH

N

13

TR LN, C GI L CC THANH GHI PH DNG

4FH), T BYTE (GPR, GENERAL