You are on page 1of 40

Bo co thc tp

KT K13 HVKTQS

LI M U
t nc ngy mt pht trin. Mc tiu cng nghip ha, hin i ha t nc phi da vo khoa hc k thut. Vn con ngi l vn tin quyt trong vic c t c mc tiu cng nghip ha hin i ha t nc hay khng. c i ng nhn lc c trnh , nm vng kin thc khoa hc k thut th ngoi vic nng cao cht lng o to l thuyt cn cn nhng ni dung thc hnh thc t ngi hc cng c kin thc l thuyt c trang b trn ging ng, ng thi c k nng x l nhng vn m thc tin t ra. nhm mc ch , trong chng trnh o to bc i hc ngnh T ng ha ti Hc vin KTQS c rt nhiu n c giao cho sinh vin sau khi tri qua nhng mn hc trang b kin thc trn lp nhm gip sinh vin tng hp li nhng kin thc m mnh c trang b. n x l tin l mt trong s . Vi bo co c giao: Thit k ng h thi gian thc s dng DS1307, hin th trn 6 Led 7 thanh vi cc ch : Hin th thi gian, ci t gi v ci t bo thc sau mt thi gian tm hiu v thc hin ti di s hng dn ca Thy Trnh Mnh Tuyn v Thy Nguyn Vn Xun, nhm chng em hon thnh n. Do trnh cn hn ch nn chc chn n khng trnh khi nhng sai st. Nhm chng em xin c cc thy sa cha, ch bo hon thin tt hn. H ni, ngy 10 thng 05 nm 2011 Sinh vin nhm 11 lp KT K13 HVKTQS

Bo co thc tp

KT K13 HVKTQS

CHNG I
GII THIU PHN CNG CA BORD MCH VI IU KHIN
1.1. Khi vi x l trung tm a. CPU 89C51 B vi iu khin 8051 c hng Intel cho ra mt vo nm 1981,vi 128 byte RAM, 4Kbyte ROM, 4 cng vo / ra 8bit, hai b nh thi, mt cng ni tip, tt c c tch hp trn mt chip. B vi iu khin 8051 l b x l 8 bit, tc l CPU ch lm vic c vi 8 bit d liu. D liu ln hn 8 bit s c chia thnh cc d liu 8 bit x l. 8051 tr nn ph bin sau khi Intel cho php cc nh sn xut khc sn xut v bn bt k dng bin th no ca 8051 m h mun vi iu kin h phi m chng trnh tng thch vi 8051. T dn n s ra i nhiu phin bn ca 8051 vi cc tc khc nhau v dung lng ROM trn chip khc nhau. Tuy nhin iu quan trng l mc d c nhiu bin th ca 8051 nhng tt c cc lnh u tng thch vi 8051 ban u. iu ny c ngha l nu chng trnh c vit cho mt phin bn 8051 no th cng s chy c vi mi phin bn khc m khng ph thuc vo hng sn xut. Chp AT89C51 l phin bn 8051 ca hng Atmel Corproration. b. S cu trc phn cng chip AT89C51 * CPU: B x l trung tm bao gm cc thnh phn sau: -Thanh ghi tch ly A -Thanh ghi tch ly ph B -Khi logic s hc ALU(Arithmetic Logical Unit). -Thanh ghi t trng thi chng trnh PSW(Program Status Word). -Thanh ghi b m chng trnh PC

Bo co thc tp
-Thanh ghi con tr ngn xp SP -Bn bng thanh ghi. -B gii m lnh -B iu khin thi gian v lgic
Ngt ngoi ROM On - Chip chng trnh

KT K13 HVKTQS

ETC RAM On - Chip Timer 1 Timer 2 Vo b m

iu khin ngt

CPU

B to dao ng

Bus iu khin

4 Cng vo /ra

Cng ni tip

P0 P1 P2 P3 a ch/ D liu

TXD RXD

Hnh 1.1: S khi b vi x l 8051 * B to dao ng * Khi iu khin ngt * Khi iu khin v qun l Bus * Cc b m/nh thi * Cc cng vo ra

Bo co thc tp
* Cng giao tip ni tip * B nh chng trnh ROM * B nh d liu RAM c. S chn ca Chp AT89C51

KT K13 HVKTQS

Hnh 1.2: Hnh nh v s chn 8051 + Port 0: L cc chn t 32 n 39 trn 8051, c k hiu l P0.0, P0.1 cho n P0.7. C 2 cng dng. Trong cc thit k c ti thiu thnh phn port 0 c s dng lm nhim v xut / nhp. Trong cc thit k ln hn c b nh ngoi th n tr thnh bus a ch v bus d liu a hp. y l mt port xut nhp song hng cc mng h 8 bit. Nu c s dng nh l mt ng xut th mi chn c th ko 8 ng vo TTL. Khi mc 1 c vit vo chn ca port 0, cc chn ny c th dng nh l cc ng nhp tng tr cao. Port 0 cng nhn cc byte code (byte m chng trinh) khi lp trnh Flash, v xut ra cc byte code khi kim tra chng trnh. Cn phi c cc in tr pullup bn ngoi khi thc hin vic kim tra chng trnh. + Port 1: Port 1 ch c mt cng dng l xut/nhp, cc chn s 1 n 8 trn 8051. Cc chn ca port 1 c k hiu l P1.0, P1.1,, P1.7 v c dng giao tip vi thit b bn ngoi khi c yu cu. Ch c s dng giao tip vi cc thit b

Bo co thc tp

KT K13 HVKTQS

ngoi vi m khng c chc nng no khc na. N l mt port xut/nhp song hng 8 bit c cc in tr pullup bn trong. Cc b m ng ra ca port 1 c th ko hoc cung cp 4 ng nhp TTL. Khi mc 1 c vit vo cc chn ca port1, chng c ko ln cao bi cc in tr pullup ni v c th c dng nh l cc ng nhp. Nu ng vai tr l ng nhp cc chn ca port 1 s cp dng I IL do cc in tr pullup bn trong. + Port 2: L cc chn t s 21 n 28 trn 8051, k hiu cc chn l P2.0, P2.1, P2.7. C 2 cng dng, hoc lm nhim v xut/ nhp hoc l byte a ch cao ca bus a ch 16 bit cho cc thit k c b nh chng trnh ngoi hoc cc thit k c nhiu hn 256 byte b nh d liu ngoi. Port 2 cng c in tr pullup bn trong. Cc b m ng ra ca port 2 c th ko hoc cung cp 4 ng vo TTL. Khi cc mc 1 c vit vo cc chn ca port 2 th chng c dng nh cc ng vo. Khi c dng nh cc ng vo cc chn ca port 2 cng cung cp dng I IL do cc in tr trong. + Port 3: Cc chn s 10 n s 17 trn 8051, k hiu l P3.0, P3.1P3.7 c 2 cng dng. Dng lm nhim v xut / nhp v khi khng lm nhim v ny th mi chn ca port 3 c cc chc nng ring nh sau: P3.0(RxD)- Chn nhn d liu ca port ni tip P3.1(TxD)- Chn pht d liu ca port ni tip P3.2 ( ) - Ng vo ngt ngoi 0 P3.3( )- Ng vo ngt ngoi 1 P3.4(T0)- Ng vo ca b nh thi/m 0 P3.5(T1)- Ng vo ca b nh thi/m 1 P3.6()- iu khin ghi b nh d liu ngoi P3.7()- iu khin c b nh d liu ngoi

Bo co thc tp
+ Chn PSEN :

KT K13 HVKTQS

L chn cho php b nh chng trnh.8051 cung cp cho ta 4 tn hiu iu khin bus, chn l chn s 29. y l tn hiu iu khin cho php ta truy xut b nh chng trnh ngoi, n thng c ni vi chn cho php xut OE ca ROM cho php c cc byte lnh. Cc m nh phn ca chng trnh hay opcode (m thao tc) c c t EP-ROM, qua bus d liu v c cht vo thanh ghi lnh IR ca 8051 c gii m. Tn hiu + Chn ALE: y l chn cho php cht a ch. Trn 8051 y l chn s 30, n l mt chn gii a hp bus d liu v bus a ch. Chn xut tn hiu cht a ch vo mt thanh ghi ngoi trong sut 1/ 2 chu k ca b nh. Sau khi iu ny c thc hin, cc chn ca port 0 s xut/nhp d liu. c dng lm xung clock cho h thng. Chn c tn s bng 1/6 tn s ca mch dao ng bn trong chip iu khin v c th c dng lm xung clock cho phn cn li ca h thng. Nu mch giao ng c tn s 12MHz, tn hiu c tn s 2MHz. Ngoi l duy nht l trong thi gian thc thi lnh MOVX, th mt xung ALE s b b qua. Chn cn c dng nhn xung ng vo lp trnh cho EPROM trn chip iu khin h 8051 ny. + Chn EA: Chn s 31, l chn truy xut ngoi c ni vo 5V hoc t GND (logic 0). Khi ni vo 5V th thc thi chng trnh trong ROM ni. Nu chn ny ni t th chng trnh cn thc thi cha b nh ngoi. Cc phin bn ca h 8051 sau ny cn s dng chn lm chn nhn in p cp in 21V cho vic lp trnh EPROM ni (np EPROM). + Chn RESET: logic 0 trong xut thi gian tm -np lnh. Khi thc thi mt chng trnh cha ROM ni thi mc logic 1.

Bo co thc tp

KT K13 HVKTQS

L chn s 9, ng vo RST l ng vo xa chnh ca 8051 dng thit lp li trng thi ban u cho h thng hay gi tt l reset h thng. Khi ng vo ny c treo logic 1 ti thiu hai chu k my, cc thanh ghi bn trong 8051 s c np cc gi tr thch hp cho vic khi ng li h thng.

Hnh 1.3: Mch RESET 8051 + Cc chn XTAL1 v XTAL2: Mch giao ng bn trong chip 8051 c ghp vi thch anh bn ngoi qua cc chn ny. l cc chn s 18 v 19.

Hnh 1.4: Mch to dao ng dng thch anh Cc t n nh cng c chn theo yu cu, tn s thch anh l 12MHz, 16MHzt gm c gi tr t 27 n 33pF, thng chn l 33pF n nh lm vic cho thch anh.

Bo co thc tp

KT K13 HVKTQS

Ngoi mch to dao ng bng thch anh chng ta cng c th tao mt mch dao ng dng cng logic.

Hnh 1.5: Mch to dao ng dng cng l-gic

CHNG 2
GIAO TIP IC V IC THI GIAN THC DS1307
2.1. Giao tip IC

Bo co thc tp

KT K13 HVKTQS

Giao thc u tin truyn thng ni tip c pht trin bi Philips Semiconductor v c gi l bus IC. V ngun gc n c thit k l iu khin lin thng IC (Inter-Intergrated Circuit) nn n c t tn l I2C. Tt c cc chip c tch hp v tng thch vi IC u c thm mt giao din tch hp trn Chip truyn thng trc tip vi cc thit b tng thch IC khc. Vic truyn d liu ni tip theo hai hng 8 bit c thc thi theo 3 ch sau: + Chun (Standard)100 Kbits/sec + Nhanh (Fast)400 Kbits/sec + Tc cao (High speed)3.4 Mbits/sec ng bus thc hin truyn thng ni tip IC gm hai ng l ng truyn d liu ni tip SDA v ng truyn nhp xung ng h ni tip SCL. V c ch hot ng l ng b nn n cn c mt nhp xung tn hiu ng b. Cc thit b h tr IC u c mt a ch nh ngha trc, trong mt s bit a ch l thp c th cu hnh. n v hoc thit b khi to qu trnh truyn thng l n v Ch v cng l n v to xung nhp ng b, iu khin cho php kt thc qu trnh truyn. Nu n v Ch mun truyn thng vi n v khc n s gi km thng tin a ch ca n v m n mun truyn trong d liu truyn. n v T u c gn v nh a ch thng qua n v Ch c th thit lp truyn thng v trao i d liu. Bus d liu c thit k cho php thc hin nhiu n v Ch v T trn cng Bus. Qu trnh truyn thng IC c bt u bng tn hiu start to ra bi n v Ch. Sau n v Ch s truyn i d liu 7 bit cha a ch ca n v T m n mun truyn thng, theo th t l cc bit c trng s ln nht MSB s c truyn trc. Bit th tm tip theo s cha thng tin xc nh n v T s thc hin vai tr nhn (0) hay gi (1) d liu. Tip theo s l mt bit ACK xc nhn bi n v nhn nhn c 1 byte trc hay khng. n v truyn (gi) s truyn i 1 byte d liu bt u bi MSB. Ti im cui ca byte truyn, n v nhn s to ra mt bit xc nhn ACK mi. Khun mu 9 bit ny (gm 8 bit d liu v 1 bit xc

Bo co thc tp

KT K13 HVKTQS

nhn) s c lp li nu cn truyn tip byte na. Khi n v Ch trao i xong d liu cn n s quan st bit xc nhn ACK cui cng ri sau s to ra mt tn hiu dng STOP kt thc qu trnh truyn thng. IC l mt giao din truyn thng c bit thch hp cho cc ng dng truyn thng gia cc n v trn cng mt bo mch vi khong cch ngn v tc thp. V d nh truyn thng gia CPU vi cc khi chc nng trn cng mt bo mch nh EEPROM, cm bin, ng h to thi gian thc... Hu ht cc thit b h tr IC hot ng tc 400Kbps, mt s cho php hot ng tc cao vi Mbps. IC kh n gin thc thi kt ni nhiu n v v n h tr c ch xc nh a ch 2.2. IC thi gian thc DS1307 DS1307 l chip ng h thi gian thc (RTC : Real-time clock), khi nim thi gian thc y c dng vi ngha thi gian tuyt i m con ngi ang s dng, tnh bng giy, pht, gi DS1307 l mt sn phm ca Dallas Semiconductor (mt cng ty thuc Maxim Integrated Products). Chip ny c 7 thanh ghi 8-bit cha thi gian l: giy, pht, gi, th (trong tun), ngy, thng, nm. Ngoi ra DS1307 cn c 1 thanh ghi iu khin ng ra ph v 56 thanh ghi trng c th dng nh RAM. DS1307 xut hin 2 gi SOIC v DIP c 8 chn nh trong hnh 2.1.

Hnh 2.1. S chn DS1307 Cc chn ca DS1307 c m t nh sau: X1 v X2: l 2 ng kt ni vi 1 thch anh 32.768KHz lm ngun to dao ng cho chip. VBAT: cc dng ca mt ngun pin 3V nui chip.

10

Bo co thc tp

KT K13 HVKTQS

GND: chn mass chung cho c pin 3V v Vcc. Vcc: ngun cho giao din I2C, thng l 5V v dng chung vi vi iu khin. Ch l nu Vcc khng c cp ngun nhng VBAT c cp th DS1307 vn ang hot ng (nhng khng ghi v c c). SQW/OUT: mt ng ph to xung vung (Square Wave / Output Driver), tn s ca xung c to c th c lp trnh. Nh vy chn ny hu nh khng lin quan n chc nng ca DS1307 l ng h thi gian thc. SCL v SDA l 2 ng giao xung nhp v d liu ca giao din I2C. Cu to bn trong DS1307 bao gm mt s thnh phn nh mch ngun, mch dao ng, mch iu khin logic, mch giao din I2C, con tr a ch v cc thanh ghi (hay RAM). S dng DS1307 ch yu l ghi v c cc thanh ghi ca chip ny. V th c 2 vn c bn l cu trc cc thanh ghi v cch truy xut cc thanh ghi ny thng qua giao din I2C. Nh trnh by, b nh DS1307 c tt c 64 thanh ghi 8-bit c nh a ch t 0 n 63 (t 00H n 3FH theo h HexaDecimal). Tuy nhin, thc cht ch c 8 thanh ghi u l dng cho chc nng ng h (RTC) cn li 56 thanh ghi b trng c th c dng cha bin tm nh RAM nu mun. By thanh ghi u tin cha thng tin v thi gian ca ng h bao gm: giy (SECONDS), pht (MINUETS), gi (HOURS), th (DAY), ngy (DATE), thng (MONTH) v nm (YEAR). Vic ghi gi tr vo 7 thanh ghi ny tng ng vi vic ci t thi gian khi ng cho RTC. Vic c gi tr t 7 thanh ghi l c thi gian thc m chip to ra. V d, lc khi ng chng trnh, chng ta ghi vo thanh ghi giy gi tr 42, sau 12s chng ta c thanh ghi ny, chng ta thu c gi tr 54. Thanh ghi th 8 (CONTROL) l thanh ghi iu khin xung ng ra SQW/OUT (chn 6). Tuy nhin, do chng ta khng dng chn SQW/OUT nn c th b qua thanh ghi th 8. T chc b nh ca DS1307 c trnh by trong hnh 2.2.

11

Bo co thc tp

KT K13 HVKTQS

Hnh 2.2. T chc b nh ca DS1307

Hnh 2.3. T chc cc thanh ghi thi gian ca DS1307

12

Bo co thc tp

KT K13 HVKTQS

V 7 thanh ghi u tin l quan trng nht trong hot ng ca DS1307, chng ta s kho st cc thanh ghi ny mt cch chi tit. Trc ht hy quan st t chc theo tng bit ca cc thanh ghi ny nh trong hnh 2.3 + Thanh ghi giy (SECONDS): thanh ghi ny l thanh ghi u tin trong b nh ca DS1307, a ch ca n l 0x00. Bn bit thp ca thanh ghi ny cha m BCD 4-bit ca ch s hng n v ca gi tr giy. Do gi tr cao nht ca ch s hng chc l 5 (khng c giy 60) nn ch cn 3 bit l c th m ha c (s 5 =101, 3 bit). Bit cao nht, bit 7, trong thanh ghi ny l 1 iu khin c tn CH (Clock halt treo ng h), nu bit ny c set bng 1 b dao ng trong chip b v hiu ha, ng h khng hot ng. V vy, nht thit phi reset bit ny xung 0 ngay t u. + Thanh ghi pht (MINUTES): c a ch 01H, cha gi tr pht ca ng h. Tng t thanh ghi SECONDS, ch c 7 bit ca thanh ghi ny c dng lu m BCD ca pht, bit 7 lun lun bng 0. + Thanh ghi gi (HOURS): c th ni y l thanh ghi phc tp nht trong DS1307. Thanh ghi ny c a ch 02H. Trc ht 4-bits thp ca thanh ghi ny c dng cho ch s hng n v ca gi. Do DS1307 h tr 2 loi h thng hin th gi (gi l mode) l 12h (1h n 12h) v 24h (1h n 24h) gi, bit6 xc lp h thng gi. Nu bit6=0 th h thng 24h c chn, khi 2 bit cao 5 v 4 dng m ha ch s hng chc ca gi tr gi. Do gi tr ln nht ca ch s hng chc trong trng hp ny l 2 (=10, nh phn) nn 2 bit 5 v 4 l m ha. Nu bit6=1 th h thng 12h c chn, vi trng hp ny ch c bit 4 dng m ha ch s hng chc ca gi, bit 5 ch bui trong ngy, AM hoc PM. Bit5 =0 l AM v bit5=1 l PM. Bit 7 lun bng 0. + Thanh ghi th (DAY ngy trong tun): nm a ch 03H. Thanh ghi DAY ch mang gi tr t 1 n 7 tng ng t Ch nht n th 7 trong 1 tun. V th, ch c 3 bit thp trong thanh ghi ny c ngha.

13

Bo co thc tp

KT K13 HVKTQS

Cc thanh ghi cn li c cu trc tng t, DATE cha ngy trong thng (1 n 31), MONTH cha thng (1 n 12) v YEAR cha nm (00 n 99). Ch , DS1307 ch dng cho 100 nm, nn gi tr nm ch c 2 ch s, phn u ca nm do ngi dng t thm vo (v d 20xx). Ngoi cc thanh ghi trong b nh, DS1307 cn c mt thanh ghi khc nm ring gi l con tr a ch hay thanh ghi a ch (Address Register). Gi tr cathanh ghi ny l a ch ca thanh ghi trong b nh m ngi dng mun truy cp.

14

Bo co thc tp

KT K13 HVKTQS

Hnh 2.4. Cu trc DS1307 2.3. Thut ton giao tip IC gia VK v DS1307 + iu kin START v STOP * START v STOP l nhng iu kin bt buc phi c khi mt thit b ch mun thit lp giao tip vi mt thit b no trong mng I2C. START l iu kin khi u, bo hiu bt u ca giao tip, cn STOP bo hiu kt thc mt giao tip. Hnh 11 m t iu DS1307 vi vi iu khin

kin START v iu kin STOP khi giao tip I2C gia

15

Bo co thc tp

KT K13 HVKTQS

Hnh 2.5: iu kin START v STOP * Ban u khi cha thc hin qu trnh giao tip, c hai ng SDA v SCL u mc cao (SDA = SCL = HIGH). Lc ny bus IC c coi l ri (bus free), sn sng cho mt giao tip. Hai iu kin START v STOP l khng th thiu trong vic giao tip gia cc thit b I2C, tt nhin l trong giao tip ny cng khng ngoi l. * iu kin START: mt s chuyn i trng thi t cao xung thp trn ng SDA trong khi ng SCL ang mc cao (cao = 1; thp = 0) bo hiu mt iu kin START * iu kin STOP: Mt s chuyn i trng thi t mc thp ln cao trn ng SDA trong khi ng SCL ang mc cao. * C hai iu kin START v STOP u c to ra bi thit b ch. Sau tn hiu START, bus IC coi nh ang trong trng thi lm vic (busy). Bus IC s ri, sn sng cho mt giao tip mi sau tn hiu STOP t pha thit b ch. * Sau khi c mt iu kin START, trong qua trnh giao tip, khi c mt tn hiu START c lp li thay v mt tn hiu STOP th bus IC vn tip tc

16

Bo co thc tp

KT K13 HVKTQS

trong trng thi bn. Tn hiu START v lp li START u c chc nng ging nhau l khi to mt giao tip. + Ch hot ng

Hnh 2.6: Ch hot ng ca IC DS1307 + DS1307c th hot ng 2 ch sau: * ch slave nhn (ch DS1307 ghi ): chui d liu v chui xung clock s c nhn thng qua SDA v SCL. Sau mi byte c nhn th 1 bit ACKnowledge s c truyn. Cc iu kin START v STOP s c nhn dng khi bt u v kt thc 1 truyn 1 chui, nhn dng a ch c thc hin bi phn cng sau khi chp nhn a ch ca slave v bit mt chiu. * Ch slave pht ( ch DS1307 c ): byte u tin slave nhn c tng t nh ch slave ghi. Tuy nhin trong ch ny th bit chiu li ch chiu chuyn ngc li. Chui d liu c phat i trn SDA bi DS1307 trong khi chui xung clock vo chn SCL + lm vic vi DS1307, ta thc hin cc bc nh sau: * START IC * Ghi: 0DxH (y l a ch ca DS1307 do nh sn xut quy nh trong giao tip I2C) vi: x=0: Ghi d liu vo DS1307 x=1: c d liu vo DS1307

17

Bo co thc tp

KT K13 HVKTQS

* Ghi tham s x ny vo, c ngha l vic tip theo l chng ta ghi hay c d liu t con DS1307 ty vo gi tr x=0 (ghi d liu) hay x=1 (c d liu). * Ghi vo a ch thanh ghi cn ghi hoc cn c (bng thanh ghi ca DS1307 ny c gi thiu hnh 3 & hnh 4). * Ghi hoc c d liu. * STOP I2C + Mt v d minh ha cho vic c ghi * Thanh ghi c a ch 01H cha Data v pht, mun set pht vo DS1307 chng ta lm theo quy trnh: STARTGhi: 0D0HGhi tip: 01HGhi tip: <thng s cn ci t (BCD)> Ghi tip hoc STOP nu ch mun ci t thi gian cho pht.

Hnh 2.7: Ch Ghi ca DS1307 * Nu mun Ghi vo a ch 01H ri k tip Ghi vo a ch 04H chng hn th chng ta phi START li t uGhi vo 0D0H ( xc nh s Ghi vo DS1307 _ hng giao tip l Ghi vo) Ghi tip 04HGhi d liu ca thanh ghi cn ci tSTOP I2C. * Tng t, nu chng ta mun c th trc ht chng ta phi ghi vo a ch cn c: tc l vn tip tc tin hnh 3 th tc STARTGhi 0D0HGhi vo a ch (a ch ca thanh ghi m ta mun c d liu). Sau , mi START li

18

Bo co thc tp

KT K13 HVKTQS

ri ghi li 0D1H (lc ny mi thng bo l ta s c t DS1307), tip theo c c bnh thng (thanh ghi c c s l thanh ghi c a ch ta mi va ghi vo), tip tc c th a ch cn c s t ng tng ln cho n khi STOP I2C.

Hnh 2.8: Ch c ca DS1307

19

Bo co thc tp

KT K13 HVKTQS

CHNG 3
THIT K PHN CNG V LU THUT TON
3.1. S nguyn l v mch in S nguyn l c thit k trn phn mm OrCad 16.3 3.2. Mch in Sau khi thit k s nguyn l ta chuyn sang lm s mch in.

Hnh 3.1: S b tri linh kin trn mch in

20

Bo co thc tp

KT K13 HVKTQS

Hnh 3.2: S i dy mch in lp TOP

Hnh 3.3: S i dy lp BOTTOM

21

Bo co thc tp
3.2. Lu thut ton + Lu thut ton chng trnh.

KT K13 HVKTQS

START

c DS1307

Sai Set Flag=0? ng Sai Alram Flag =1? ng Sai Time=Alarm? ng Bt Alarm

Hin Th

Quyt phm

22

Bo co thc tp
Chng trnh:
$mod51 TEMP DATA 37H XUNG_NHAY DATA 38H ; XUNG 100ms

KT K13 HVKTQS

BIEN_NHAY DATA 39H ;0 = SANG TAT CA DEN , 1 = NHAY led TUONG UNG KHI set GIAY DATA 40H PHUT DATA 41H GIO DATA 42H DONVI_GIAY DATA 47H CHUC_GIAY DATA 48H DONVI_PHUT DATA 49H CHUC_PHUT DATA 4AH DONVI_GIO DATA 4BH CHUC_GIO DATA 4CH PHAN_TRAM_GIAY DATA 4DH FLAG_SET DATA 4EH ;0 = KHONG SET , 1 = SET PHUT , 2 = SET GIO LED_GIAY BIT P2.0 LED_C_GIAY BIT P2.1 LED_PHUT BIT P2.2 LED_C_PHUT BIT P2.3 LED_GIO BIT P2.4 LED_C_GIO BIT P2.5 ;--------I2C------SCL BIT P3.0 SDA BIT P3.1 SW_1 BIT P3.2 SW_2 BIT P3.3 SW_3 BIT P3.4 LED_DATA EQU P0 BYTE_W EQU 11010000B BYTE_R EQU 11010001B

23

Bo co thc tp
ADD_LOW EQU 62H DATA_DS EQU 63H ;===================================== ;===================================== ORG 00H LJMP MAIN ;=========================== ;=========================== ORG 0BH LJMP NGAT_TIME ;=========================== ;=========================== ORG 030H MAIN: ;reset tat ca cac bien MOV GIAY,#0 MOV PHUT,#0 MOV GIO,#0 MOV BIEN_NHAY,#0 MOV XUNG_NHAY,#0 MOV FLAG_SET,#0 MOV R0,#0 MOV IE,#10001010B MOV TMOD,#11H MOV TL0,#LOW(-9216) MOV TH0,#HIGH(-9216) SETB TR0 MOV A,#0FFH MOV LED_DATA,A MOV DPTR,#BANGSO CLR SCL CLR SDA

KT K13 HVKTQS

24

Bo co thc tp
NOP SETB SCL SETB SDA NOP MOV ADD_LOW,#00H MOV DATA_DS,#00H LCALL WRITE_BYTE ;========== ;========================================== LOOP_HIEN_THI: ; chuong trinh chinh chay tai day ;========================================== MOV A,FLAG_SET CJNE A,#0,L_HT CALL INIT_PORT L_HT: LCALL HIEN_THI LCALL SCAN_KEY SJMP LOOP_HIEN_THI ;==========================================

KT K13 HVKTQS

INIT_PORT: ;================================================= =READS SECONDS ;================================================= READ_SEC: MOV ADD_LOW,#00h LCALL READ_BYTE MOV A,DATA_DS CALL BCD_HEX MOV GIAY,A

25

Bo co thc tp
LCALL I2C_STOP

KT K13 HVKTQS

;================================================= =READS MINUTES MOV ADD_LOW,#01h LCALL READ_BYTE MOV A,DATA_DS CALL BCD_HEX MOV PHUT,A LCALL I2C_STOP ;================================================= =READS HOURS MOV ADD_LOW,#02h LCALL READ_BYTE MOV A,DATA_DS CALL BCD_HEX MOV GIO,A LCALL I2C_STOP RET ;================================================= ================================= ;=====stop I2C communication I2C_Stop: CLR SDA SETB SCL NOP SETB SDA RET ;================================================= ================================= ;************************************************* **** ;* WRITE DATA_DS TO DS1307 1 BYTE * ;* INPUT : ADD_LOW * ;* : DATA_DS *

26

Bo co thc tp
WRITE_BYTE: CLR SDA ;start bit CLR SCL MOV A,#BYTE_W ;send control byte LCALL LOOP_BYTE SETB SDA SETB SCL JB SDA,WRITE_BYTE ;loop until busy CLR SCL MOV A,ADD_LOW ;send address low LCALL LOOP_BYTE SETB SDA SETB SCL JB SDA,WRITE_BYTE ;loop until busy CLR SCL MOV A,DATA_DS ;send DATA LCALL LOOP_BYTE SETB SDA SETB SCL JB SDA,WRITE_BYTE ;loop until busy CLR SDA CLR SCL SETB SCL ;stop bit SETB SDA RET ;========================================== BCD_HEX: ;========================================== MOV B,#10H DIV AB

KT K13 HVKTQS

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

27

Bo co thc tp
MOV TEMP,B ;CAT HANG DON VI MOV B,#10 MUL AB ADD A,TEMP ret ;========================================== HEX_BCD: ;========================================== MOV B,#10 DIV AB MOV TEMP,B ;CAT HANG DON VI MOV B,#10H MUL AB ADD A,TEMP ret ;==========================================

KT K13 HVKTQS

;************************************************* ***** ;* READ DATA FROM DS1307 1 BYTE * ;* INPUT : ADD_HIGH * ;* : ADD_LOW * ;* OUTPUT : DATA_DS * ;************************************************* ***** READ_BYTE: CLR SDA ;start bit CLR SCL MOV A,#BYTE_W ;send control byte LCALL LOOP_BYTE SETB SDA SETB SCL JB SDA,READ_BYTE ;loop until busy CLR SCL

28

Bo co thc tp
MOV A,ADD_LOW ;send address low LCALL LOOP_BYTE SETB SDA SETB SCL JB SDA,READ_BYTE ;loop until busy CLR SCL SETB SCL SETB SDA CLR SDA ;start bit CLR SCL MOV A,#BYTE_R ;send control byte LCALL LOOP_BYTE SETB SDA SETB SCL JB SDA,READ_BYTE ;loop until busy CLR SCL LCALL LOOP_READ SETB SDA SETB SCL CLR SCL SETB SCL ;stop bit SETB SDA RET

KT K13 HVKTQS

;************************************************* *** ;* WRITE * ;* INPUT: ACC * ;************************************************* *** LOOP_BYTE: PUSH 02H MOV R2,#08H

29

Bo co thc tp
LOOP_SEND: RLC A MOV SDA,C SETB SCL CLR SCL DJNZ R2,LOOP_SEND POP 02H RET

KT K13 HVKTQS

;************************************************* **** ;* READ * ;* OUTPUT: ACC * ;************************************************* **** LOOP_READ: PUSH 02H MOV R2,#08H LOOP_READ1: SETB SCL MOV C,SDA CLR SCL RLC A DJNZ R2,LOOP_READ1 MOV DATA_DS,A POP 02H RET ;========================================== TACHSO: ; tach rieng hang chuc va hang don vi bang cach chia cho 10 ;========================================== MOV A,GIAY ;Lan luot chia cac Bien: Giay, Phut, Gio cho 10 MOV B,#10 ;de tach phan Don Vi va Hang Chuc ra, de cat rieng vao cac Bien tuong ung. DIV AB ;PHAN NGUYEN trong A, PHAN DU trong B MOV CHUC_GIAY,A ;Luu lai HANG CHUC Giay

30

Bo co thc tp
MOV DONVI_GIAY,B ;luu lai DON VI Giay ;========== MOV A,PHUT MOV B,#10 DIV AB MOV CHUC_PHUT,A MOV DONVI_PHUT,B ;========== MOV A,GIO MOV B,#10 DIV AB MOV CHUC_GIO,A MOV DONVI_GIO,B RET ;======================================== HIEN_THI: ; HIEN THI LED 7 DOAN ;======================================== MOV A,FLAG_SET CJNE A,#0,CHOP_NHAY LCALL HIENTHI AJMP THOAT_HIENTHI ;========================================

KT K13 HVKTQS

CHOP_NHAY: ; KIEM TRA BIEN NHAY VA FLAG_SET DE TAO HIEU UNG NHAY LED DANG SETING ;================================= MOV A,BIEN_NHAY CJNE A,#0,CHOP_NHAY1 LCALL HIENTHI AJMP THOAT_HIENTHI CHOP_NHAY1: LCALL NHAY

31

Bo co thc tp
JMP CHOP_NHAY THOAT_HIENTHI: RET ;================================= HIENTHI: ;================================= LCALL HIENTHI_S LCALL HIENTHI_P LCALL HIENTHI_G RET ;================================= NHAY: ;================================= MOV A,FLAG_SET CJNE A,#1,KT1 LCALL HIENTHI_S LCALL HIENTHI_G KT1: MOV A,FLAG_SET CJNE A,#2,THOAT_N LCALL HIENTHI_S LCALL HIENTHI_P THOAT_N: RET ;================================= HIENTHI_S: ;================================= ;hien thi hang don vi cua Giay MOV A,DONVI_GIAY MOVC A,@A+DPTR MOV LED_DATA,A

KT K13 HVKTQS

32

Bo co thc tp
CLR LED_GIAY LCALL DL SETB LED_GIAY ;========== MOV A,CHUC_GIAY ;hien thi hang chuc cua Giay MOVC A,@A+DPTR MOV LED_DATA,A CLR LED_C_GIAY LCALL DL SETB LED_C_GIAY RET ;================================= HIENTHI_P: ;================================= MOV A,DONVI_PHUT ;hien thi hang don vi cua Phut MOVC A,@A+DPTR MOV LED_DATA,A CLR LED_PHUT LCALL DL SETB LED_PHUT ;============ MOV A,CHUC_PHUT ;hien thi hang chuc cua Phut MOVC A,@A+DPTR MOV LED_DATA,A CLR LED_C_PHUT LCALL DL SETB LED_C_PHUT RET ;================================= HIENTHI_G: ;=================================

KT K13 HVKTQS

33

Bo co thc tp
MOV A,DONVI_GIO ;hien thi hang don vi cua gio MOVC A,@A+DPTR MOV LED_DATA,A CLR LED_GIO LCALL DL SETB LED_GIO ;============ MOV A,CHUC_GIO ;hien thi hang chuc cua Gio MOVC A,@A+DPTR MOV LED_DATA,A CLR LED_C_GIO LCALL DL SETB LED_C_GIO RET ;================================= NGAT_TIME: ;================================= INC XUNG_NHAY

KT K13 HVKTQS

INC PHAN_TRAM_GIAY ;DAT TIMER CHAY 1/100 GIAY MOV TL0,#LOW(-9216) MOV TH0,#HIGH(-9216) SETB TR0 ;=============== PUSH ACC PUSH PSW ;Thanh ghi trang thi chuong trnh ;========== MOV A,XUNG_NHAY ;TAO XUNG NHAP NHAY = 1/4 GIAY CJNE A,#25,TIME1 MOV XUNG_NHAY,#0 INC BIEN_NHAY MOV A,BIEN_NHAY

34

Bo co thc tp
CJNE A,#3,TIME1 MOV BIEN_NHAY,#0 ;========== TIME1:

KT K13 HVKTQS

MOV A,PHAN_TRAM_GIAY ;Kiem tra bien PHAN_TRAM_GIAY - Thoat khoi ngat Time0 neu khong = CJNE A,#100,THOAT_NGAT_TIME MOV PHAN_TRAM_GIAY,#0 ;Neu = 100 th set bien nay = 0 ;=============== THOAT_NGAT_TIME: LCALL TACHSO POP PSW POP ACC RETI ;================================== SCAN_KEY: ;KIEM TRA PHIM NHAN ;================================== SW1: ;SET TIME JB SW_1,SW2 INC FLAG_SET MOV A,FLAG_SET CJNE A,#3,L_SW1 ;========== MOV A,PHUT CALL HEX_BCD MOV DATA_DS,A MOV ADD_LOW,#01H LCALL WRITE_BYTE ;========== MOV A,GIO CALL HEX_BCD

35

Bo co thc tp
MOV DATA_DS,A MOV ADD_LOW,#02H LCALL WRITE_BYTE MOV FLAG_SET,#0 L_SW1: LCALL DL1 LCALL DL1 LCALL DL1 LCALL DL1 LJMP NOKEY ;=============== SW2: ;SET_MIN JB SW_2,SW3 MOV A,FLAG_SET CJNE A,#0,SW20 LJMP NOKEY SW20: MOV A,FLAG_SET CJNE A,#1,TANG_GIO ; JB SW_2,SW3 ;=============== TANG_PHUT: INC PHUT ;Roi tang Bien phut them 1 MOV A,PHUT CJNE A,#60,L_SW2 ; MOV PHUT,#0 ;Neu = 60 th set bien nay = 0 L_SW2: LCALL DL1 LCALL DL1 LJMP SW2

KT K13 HVKTQS

36

Bo co thc tp
;=============== TANG_GIO: ;SET HOUR JB SW_2,SW3 MOV A,FLAG_SET CJNE A,#2,SW3 JB SW_2,SW3 INC GIO ;Roi tang Bien Gio them 1 MOV A,GIO

KT K13 HVKTQS

CJNE A,#24,L_TANG_GIO ;Bien gio = 60? - Thoat khoi ngat Time0 neu khong = MOV GIO,#0 L_TANG_GIO: LCALL DL1 LCALL DL1 LJMP TANG_GIO ;============================== SW3: ;DANG NHAN SW3? JB SW_3,NOKEY ;KHONG NHAN SW2? KIEM TRA SW3 MOV A,FLAG_SET CJNE A,#0,SW30 LJMP NOKEY SW30: MOV A,FLAG_SET ;DANG NHAN SW2. KIEM TRA CHE DO CHINH GIO HAY CHINH PHUT. CJNE A,#1,GIAM_GIO ; JB SW_3,NOKEY ;=============== GIAM_PHUT: DEC PHUT ;Roi tang Bien phut them 1 MOV A,PHUT CJNE A,#-1,L_SW3 ;Bien Phut = -1? - Thoat khoi ngat Time0 neu khong = MOV PHUT,#59 ;Neu = -1 th set bien nay = 60

37

Bo co thc tp
L_SW3: LCALL DL1 LCALL DL1 LJMP SW3 ;=============== GIAM_GIO: ;=============== JB SW_3,NOKEY MOV A,FLAG_SET CJNE A,#2,NOKEY LCALL HIENTHI JB SW_3,NOKEY DEC GIO ;Roi Giam Bien Gio them 1 MOV A,GIO

KT K13 HVKTQS

CJNE A,#-1,L_GIAM_GIO ;Bien gio = -1? - Thoat khoi ngat Time0 neu khong = MOV GIO,#23 L_GIAM_GIO: LCALL DL1 LCALL DL1 LJMP GIAM_GIO ;=============== NOKEY: RET ;================================= DL: ;================================= MOV R7,#200 DJNZ R7,$ RET ;==================

38

Bo co thc tp
DL1: ;================== PUSH 00H PUSH 01H MOV R1,#20O DEL: LCALL HIEN_THI LCALL HIEN_THI LCALL HIEN_THI MOV R0,#250 DJNZ R0,$ DJNZ R1,DEL POP 01H POP 00H RET ;=================== DELAY_1MS: ;=================== MOV R7,#200 DL_1MS_1: MOV R6,#200 DJNZ R6,$ DJNZ R7,DL_1MS_1 RET ;==================== DELAY: ;==================== NOP NOP NOP NOP

KT K13 HVKTQS

39

Bo co thc tp
NOP NOP NOP NOP RET ;================================= BANGSO: ;=================================

KT K13 HVKTQS

DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H RET END

40