You are on page 1of 93

TRNG I HC GIAO THNG VN TI

KHOA IN IN T
B MN K THUT IN T

N TT NGHIP
TI
NG DNG GPS/GSM TRONG QUN L GIM
ST PHNG TIN C NHN

Gio vin hng dn: ThS. Trn Mnh Cng


Sinh vin thc hin : V nh Chung
M sinh vin
: 0801023
Lp: K thut in t & tin hc CN- K49

H Ni, Thng 05 nm 2013

N TT NGHIP

GVHD: Th.S Trn Mnh Cng

LI M U
Ngy nay cng vi s pht trin mnh m ca cc ngnh khoa hc k thut,
cng ngh k thut in t m trong l k thut t ng iu khin ng vai tr
quan trng trong mi lnh vc khoa hc k thut, qun l, cng nghip, cung cp
thng tin ... Vi nhu cu thng tin ngy cng tng ca con ngi, mng vin thng
ra i p ng nhu cu . Hin nay, mng vin thng tr nn ph bin trn ton
cu, n khng ch p ng nhu cu trao i thng tin n thun (nghe, gi, nhn tin)
m chng ngy cng c ng dng rng ri vo nhiu lnh vc. Vi s pht trin
nhanh chng cc dch v v ng dng vic thng tin qua mng vin thng ngy tin
li, tit kim thi gian, m bo an ton v tit kim c chi ph s dng. Vi cht
lng mng vin thng nh hin nay to iu kin cho cc ng dng v iu khin
t xa ra i v cng ngy cng c ng dng rng ri.
X hi loi ngi pht trin i hi con ngi phi hot ng trong cc mi
trng phc tp v nguy him hn, khi iu khin t xa tr nn mt yu cu tt
yu. T nhng robot lm vic trong cc hm m, nh my in ht nhn, di y i
dng, robot chin trng hay cc robot khm ph cc hnh tinh xa xi u l cc
thnh tu cng ngh v iu khin t xa.
Vi iu kin c s vt cht hin c nc ta v nhu cu thc t. Th ng dng
iu khin cc thit b t xa qua mng GPRS/GPS v mng in thoi di ng GSM l
rt kh thi. ng dng ny s gip ngi iu khin c th qun l v iu khin cc
thit b mt cch nhanh chng, chnh xc, hiu qu m khng b gii hn v khong
cch ch cn ni c mng GPRS hoc mng di ng ph sng.
Xut pht t thc tin , em quyt nh nghin cu v thc hin ti:
NG DNG GPS/GSM TRONG QUN L GIM ST PHNG TIN C
NHN di s hng dn ca thy Trn Mnh Cng ging vin b mn K thut
in trng i hc Giao thng vn ti.
n c hon thnh nh s hng dn tn tnh v tm huyt ca thy Trn
Mnh Cng, cng nhng kin ng gp qu bu ca cc thy c trong b mn K
thut in t v s n lc c gng ca bn thn. Tuy nhin, do kin thc hiu bit v
thi gian hn hp nn n khng trnh khi nhng thiu st. Knh mong s ng gp
kin ca thy c v cc bn n c hon thin hn trong thi gian cn li.
Qua y, em xin gi li cm n n cc thy, c gio Trng i hc Giao
Thng Vn Ti H Ni, b mn K Thut in T nhit tnh ging dy v truyn
t nhng kin thc, nhng kinh nghim qu gi trong sut 5 nm hc i hc.
SINH VIN V NH CHUNG

LP: T & THCN K49

N TT NGHIP

GVHD: Th.S Trn Mnh Cng

c bit em xin gi li cm n su sc n thy gio Trn Mnh Cng tn


tnh hng dn, ch bo em trong sut qu trnh hc tp cng nh nghin cu khoa hc
v qu trnh thc tp tt nghip, lm n tt nghip. Chc thy c lun mnh khe
v cng tc tt, vng tay cho a cc th h sinh vin n vi nhng b tri thc mi.
H Ni, Ngy 02 Thng 05 Nm 2013
Sinh Vin
V nh Chung

SINH VIN V NH CHUNG

LP: T & THCN K49

N TT NGHIP

GVHD: Th.S Trn Mnh Cng

MC LC
LI M U ................................................................................................................ 1
CHNG I: TNG QUAN V CC H THNG GIM ST ......................................... 1
1.1.H thng gim st an ninh t xa. ........................................................................ 1
1.1.1.Nguyn tc hot ng c bn ca h thng gim st an ninh t xa. ........... 1
1.1.2.ng dng ca h thng gim st an ninh t xa. .......................................... 2
1.2H thng gim st giao thng. ............................................................................. 2
1.2.1.Gii thiu v h thng gim st giao thng. ................................................ 2
1.2.2.Mc ch ca h thng. ................................................................................ 3
1.2.3.Nguyn l ca h thng. ............................................................................... 3
1.2.4.c im ca h thng. ................................................................................ 3
1.2.5.Giao din phn mm. .................................................................................... 4
1.3.Gii thiu v ti. ............................................................................................. 4
1.3.1.Tnh cp thit ca ti. .............................................................................. 4
1.3.2.Mc ch v phng php nghin cu. ........................................................ 5
1.3.2.1.Mc ch. ............................................................................................... 5
1.3.2.2.Phng php nghin cu. ..................................................................... 5
1.3.3.Gii hn ti. ............................................................................................. 5
CHNG II GII THIU MODULE SIM900, SIM908 TP LNH AT COMMAND,
CM BIN CHUYN NG ................................................................................................ 6
2.1.Tng quan v h thng thng tin di ng GSM. .................................................. 6
2.1.1.Gii thiu v cng ngh GSM. ..................................................................... 6
2.1.2.c im cng ngh GSM. ........................................................................... 6
2.1.3.Cu trc mng GSM. .................................................................................... 6
2.2.Tng quan v h thng nh v ton cu GPS. .................................................... 8
2.2.1.Gii thiu v h thng nh v ton cu GPS. .............................................. 8
2.2.2.S hot ng ca h thng nh v GPS. ...................................................... 8
2.2.3. chnh xc ca h thng nh v GPS. ...................................................... 8
2.2.4.Cc thnh phn ca h thng nh v GPS. ................................................. 9
2.2.4.1.Phn khng gian. ................................................................................... 9
2.2.4.2.Phn kim sot. ..................................................................................... 9
2.2.4.3.Phn s dng. ....................................................................................... 10
2.3.Gii thiu phn cng SIM900. ........................................................................... 10
2.3.1.c im ca Sim900. ................................................................................ 10
2.3.2.Mch phn cng sim900. ............................................................................ 11
2.3.2.1.Khi ngun. .......................................................................................... 11
2.3.2.2.Tt\bt SIM900 s dng chn PWKEY. ............................................... 12
2.3.2.3.Ngun nui RTC. .................................................................................. 12
2.3.2.4.Giao tip ni tip. ................................................................................. 13
2.3.2.5.Giao tip m thanh. .............................................................................. 14
2.3.2.6.Giao tip Sim card. ............................................................................... 14
2.3.2.7.S ch th trng thi mng. ................................................................... 15
2.4.Gii thiu phn cng Sim908. ............................................................................ 16
2.4.1.c im ca Sim908. ................................................................................. 16
2.4.2.ng dng Sim908. ....................................................................................... 17
SINH VIN V NH CHUNG

LP: T & THCN K49

N TT NGHIP

GVHD: Th.S Trn Mnh Cng

2.5.Cm bin chuyn ng PIR. ............................................................................... 17


2.5.1.Nguyn l pht hin. .................................................................................... 17
2.5.2.Cu trc cm bin PIR. ............................................................................... 18
2.5.3.Nguyn l lm vic ca loi u d PIR. .................................................... 18
2.6.Kho st tp lnh AT Command. ........................................................................ 20
2.6.1.Cc lnh kim tra ban u........................................................................... 21
2.6.2.Cc lnh khi to......................................................................................... 22
2.6.3.Cc lnh x l cuc gi. .............................................................................. 23
2.6.4.Cc lnh v SMS. ......................................................................................... 23
2.6.5.Cc lnh v GPRS. ...................................................................................... 26
2.6.6.Cc lnh v GPS. ......................................................................................... 27
2.6.6.1.Tng quan cc tp lnh c bn. ........................................................... 27
2.6.6.2.Cu hnh cc tp lnh v GPS. ............................................................. 27
2.6.7.Cc lnh khc. ............................................................................................. 31
CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307 ........................... 32
3.1.Tng quan vi iu khin AVR. ............................................................................ 32
3.1.1.c tnh vi iu khin ATmeg128. .............................................................. 32
3.1.1.Cu trc vi iu khin AVR. ........................................................................ 33
3.1.2.Cu trc b nh v cng vo - ra................................................................ 33
3.1.2.1.Cu trc b nh. ................................................................................... 33
3.1.2.2.Cng vo/ra Atmega128. ...................................................................... 35
3.1.2.3.Cch thc hot ng. ........................................................................... 36
3.1.3.B nh thi Atmega128. ............................................................................. 38
3.1.3.1.Cc nh ngha. .................................................................................... 38
3.1.3.2.Cc thanh ghi b nh thi 1. ............................................................... 39
3.1.4.Cu trc ngt ca ATmega128. ................................................................... 43
3.1.4.1.Khi nim v ngt. ................................................................................ 43
3.1.4.2.Trnh phc v ngt v bng vector ngt............................................... 44
3.1.4.3.Th t u tin ngt. .............................................................................. 46
3.1.4.4.Ngt lng nhau. .................................................................................... 46
3.1.4.5.Ngt ngoi Atmega128. ........................................................................ 46
3.1.5.B truyn nhn d liu ni tip USART. ..................................................... 47
3.1.5.1.B truyn nhn ni tip ATmega128. ................................................... 48
3.2.Chip thi gian thc DS1307. .............................................................................. 50
3.2.1.Cc tnh nng. ............................................................................................. 50
3.2.2.M t. ........................................................................................................... 50
CHNG IV: THIT K V THI CNG .................................................................... 55
4.1.Phng n thit k. ............................................................................................. 55
4.2.S khi tng qut ca ton h thng v chc nng tng khi. ..................... 55
4.2.1.S tng qut h thng. ............................................................................ 55
4.2.2.Chc nng tng khi. .................................................................................. 56
4.3.Thit k phn cng. ............................................................................................ 57
4.3.1.S nguyn l............................................................................................ 57
4.3.2.Thit k mch in........................................................................................... 59
4.4.Thit k chng trnh phn mm. ....................................................................... 61
4.4.1.Lu thut ton chng trnh. ................................................................. 61
SINH VIN V NH CHUNG

LP: T & THCN K49

N TT NGHIP

GVHD: Th.S Trn Mnh Cng

4.5.Phn mm giao din qun l. ............................................................................. 66


4.5.1.Gii thiu phn mm Visual Studio ca Microsoft. .................................... 66
4.5.2.Xy dng giao din truyn thng ni tip RS232 dng Visual Studio. ....... 66
CHNG V:KT QU THC NGHIM V HNG PHT TRIN .......................... 76
5.1.Kt qu thc hin n...................................................................................... 76
5.2.Hnh nh mch thc nghim............................................................................... 77

SINH VIN V NH CHUNG

LP: T & THCN K49

N TT NGHIP

GVHD: Th.S Trn Mnh Cng

Danh mc cc hnh v:
Hnh 1: S h thng gim st an ninh t xa. ............................................................... 1
Hnh 2: M hnh h thng gim st giao thng. .............................................................. 2
Hnh 3: S nguyn l h thng gim st giao thng. .................................................. 3
Hnh 4: Hnh nh xe vi phm vt qu tc cho php. ................................................. 4
Hnh 5: Hnh nh xe vi phm vt n . ...................................................................... 4
Hnh 6: Cu trc mng GSM............................................................................................ 7
Hnh7: S chc nng SIM900 .................................................................................... 11
Hnh 8: Mch tham chiu cho ngun cp SIM900 .......................................................... 11
Hnh 9: Mch ng dng tt/bt SIM900. ........................................................................ 12
Hnh 10: Ngun cp RTC t pin sc li. ......................................................................... 12
Hnh 11: Ngun cp RTC t pin khng sc li. .............................................................. 12
Hnh 12: Ngun cp RTC t t in. ............................................................................... 12
Hnh 13 : Kt ni ca giao tip ni tip. ......................................................................... 13
Hnh 14 : Mch chuyn mc RS232 ................................................................................ 13
Hnh 15: Cu hnh giao tip Speaker .............................................................................. 14
Hnh 16 : Cu hnh giao tip tai nghe ............................................................................. 14
Hnh 17: Mch tham chiu cho simcard 8 chn. ............................................................ 15
Hnh 18: Mch tham chiu cho simcard 6 chn ............................................................. 15
Hnh 19: Mch tham chiu ca NETLIGHT. .................................................................. 16
Hnh 20: M hnh gim st hot ng phng tin giao thng. ..................................... 17
Hnh 21: Cu trc cm bin chuyn ng PIR. .............................................................. 18
Hnh 22: Nguyn l pht hin chuyn ng ngang ca cc ngun thn nhit. ............... 19
Hnh 23: Mch ng dng cm bin PIR ng ngt thit b qua rle.............................. 19
Hnh 24: Cu trc vi iu khin AVR. ............................................................................. 33
Hnh 25: B nh chng trnh c v khng s dng Bootloader. .................................. 34
Hnh 26: Cu trc b nh d liu. .................................................................................. 35
Hnh 27: S khi b nh thi 16 bit. ......................................................................... 38
Hnh 28: Cc ngt lng nhau. ......................................................................................... 46
Hnh 29: S khi b USART. ...................................................................................... 48
Hnh 30: S khi v cc mc logic trong khi pht xung ng h. ........................... 49
Hnh 31: Cu to bn ngoi DS1307. ............................................................................. 50
Hnh 32: S khi DS1307. .......................................................................................... 52
Hnh 33: Mch kt ni DS1307. ...................................................................................... 52
Hnh 34: Cu trc b nh DS1307. ................................................................................. 53
Hnh 35: Cu trc thanh ghi DS1307.............................................................................. 53
Hnh 36: S khi tng qut h thng qun l gim st phng tin c nhn. ........... 55
Hnh 37: S nguyn l khi ngun h thng. ............................................................. 57
Hnh 38: S nguyn l mch x l trung tm. ............................................................ 58
Hnh 39: S nguyn l mch Sim908. ........................................................................ 58
Hnh 40: S nguyn l mch iu khin chnh. .......................................................... 59
Hnh 41: Mch x l trung tm. ...................................................................................... 59
Hnh 42: Mch Sim908. ................................................................................................... 60
Hnh 43: Mch iu khin chnh. .................................................................................... 60
Hnh 44: Lu thut ton chng trnh chnh. ............................................................ 61
SINH VIN V NH CHUNG

LP: T & THCN K49

N TT NGHIP

GVHD: Th.S Trn Mnh Cng

Hnh 45: Lu thut ton x l tin nhn. ..................................................................... 62


Hnh 46: Lu thut ton nhn d liu. ....................................................................... 63
Hnh 47: Lu thut ton gi d liu........................................................................... 63
Hnh 48: Lu chng trnh ly d liu. ..................................................................... 64
Hnh 49: Lu chng trnh con iu khin m thit b. ............................................. 64
Hnh 50: Lu chng trnh con iu khin tt thit b............................................... 65
Hnh 51: Lu thut ton gi tin nhn. ....................................................................... 65
Hnh 52: To project mi trn C#. .................................................................................. 67
Hnh 53: La chn khun mu cho project. .................................................................... 68
Hnh 54: Thit k giao din. ............................................................................................ 68
Hnh 55: Giao din qun l, gim st. ............................................................................ 73
Hnh 56: Form bo nhn d liu. .................................................................................... 75
Hnh 57: Mch x l trung tm Sim900 .......................................................................... 77
Hnh 58: Module Sim908................................................................................................. 78
Hnh 59: Mch x l gn trn phng tin. .................................................................... 78
Hnh 60: Mch x l gn trn phng tin. .................................................................... 79
Hnh 61: Form cnh bo c d liu gi v trung tm..................................................... 79
Hnh 62: V tr v cc thng tin v thit b trn mn hnh qun l.................................. 80
Hnh 63: Module gi v tr phng tin ti in thoi ch phng tin ......................... 86

SINH VIN V NH CHUNG

LP: T & THCN K49

N TT NGHIP

GVHD: Th.S Trn Mnh Cng

Danh mc cc bng:
Bng1: nh ngha cc chn giao tip sim. .............................................................. 14
Bng 2: Bng trng thi lm vic ca NETLIGHT. ..................................................15
Bng 3: Chc nng cc lnh GPRS. .........................................................................26
Bng 4: Chc nng cc lnh GPS. ............................................................................27
Bng 5: Cu hnh cho cc chn cng. .......................................................................38
Bng 6: u ra so snh ch non-PWM. ............................................................... 40
Bng 7: u ra so snh ch fast-PWM. ............................................................... 40
Bng 8: u ra so snh ch PWM hiu chnh pha v tn s. .............................. 40
Bng 9: La chn cc ch thc thi ca b nh thi 1. .......................................41
Bng 10: La chn tc xung clock. ......................................................................42
Bng 12: iu khin kiu bt mu ngt. ...................................................................47
Bng 13: Phng trnh tnh ton ci t thanh ghi tc truyn. ........................... 49

SINH VIN V NH CHUNG

LP: T & THCN K49

N TT NGHIP

GVHD: Th.S Trn Mnh Cng

Danh mc cc t vit tt:


STT
1

CH VIT
TT
GSM

GPRS

GPS

PDA

AT

THUT NG
TING ANH
Global System for
MobileCommunications
General Packet Radio
Service
Global Positioning
System
Personal Digital
Assistant
AT Command

THUT NG TING VIT

CR

Carriage return

LF

Line Feed

DTE

DCE

10

RTC

Data Terminal
Equipment
Data Communications
Equipment
Real Time Clock

11

TxD

Transmit data

ng truyn d liu.

12

RxD

Receive data

ng nhn d liu.

13

MT

Mobile Terminal

14

TE

Terminal Equipment

15

BSS

16

TRAU

17

BSC

18

BTS

H thng thng tin di ng ton


cu.
Dch v v tuyn gi tng hp.
H thng nh v ton cu.
Thit b k thut s h tr c
nhn.
Tp lnh iu khin ca SIMCOM.
M iu khin ca ng tc v u
dng.
M iu khin ca ng tc xung
dng.
Thit b cui x l s liu.
Thit b kt cui knh s liu.
ng h thi gian thc.

Thit b u cui mng.


Thit b u cui (my tinh, h vi
iu khin).
H thng trm gc.

Base Station
Subsystem.
Transcoding Rate and B chuyn i m v phi hp tc
Adaption Unit
.
Base Station Controller B iu khin trm gc.
Base Transceiver
Station

SINH VIN V NH CHUNG

Trm thu pht gc.

LP: T & THCN K49

CHNG I: TNG QUAN V CC H THNG GIM ST

CHNG I
TNG QUAN V CC H THNG GIM ST
1.1. H thng gim st an ninh t xa.

Hnh 1: S h thng gim st an ninh t xa.

Dch v gim st an ninh t xa l m hnh bo v hot ng trn nguyn tc kt


hp gia cc thit b bo v cng ngh cao hin nay nh: Camera k thut s, bo
ng, bo chy, bo kh gascng vi s tip ng kp thi ca lc lng phn ng
nhanh pht hin v ngn chn cc ri ro do trm cp t nhp hoc ha hon gy ra
v c s h tr kp thi khi c cc yu cu khn cp v y t, sc khe
1.1.1. Nguyn tc hot ng c bn ca h thng gim st an ninh t xa.
Vi cc thit b bo ng hin i, nhanh v chnh xc nh: Camera k thut s,
u bo khi, bo nhit, u r chuyn ng, cng tc t, cng tc khnmi hnh vi
t nhp, trm cp v cc du hiu gy ra chy n hay tn hiu khn cp s c pht
hin ngay t khi mi xy ra v pht tn hiu cnh bo v trung tm gim st qua ng
truyn internet hoc in thoi hu tuyn. Khi nhn c tn hiu cnh bo, h thng
an ninh ti trung tm s lp tc kch hot t ng quay s in thoi n cc lc lng
tip ng c ci t sn nh: Cnh st 113, Cnh st phng chy cha chy (trong
trng hp chy n)T cc tn hiu bo ng, trung tm gim st an ninh s bit
c:

SINH VIN V NH CHUNG

LP: T & THCN K49

CHNG I: TNG QUAN V CC H THNG GIM ST


Thng tin ni bo v: Tn khch hng, a ch, s in thoi lin h
Khu vc pht ra tn hiu bo ng: Hin th chnh xc khu vc pht ra tn hiu
bo ng trn bn trc tuyn google map.
Loi tn hiu bo ng: Bo ng t nhp, bo chy, h tr khn cp
S ng i: Bn ng i t trung tm gim st an ninh ti khu vc cn
bo v.
1.1.2. ng dng ca h thng gim st an ninh t xa.
Dch v gim st an ninh t xa tr thnh mt lc lng tr gip c lc trong
vic x l cc s c ti tr s cc doanh nghip, nh my, cao c, chung c cao cp,
kho tng, cc khu bit th, nh ring
H tr thng bo thng tin cho khch hng v ti sn, tnh trng an ton ca cc
khu vc gim st. Cnh bo kp thi khi c s c xy ra.
1.2. H thng gim st giao thng.

Hnh 2: M hnh h thng gim st giao thng.

1.2.1. Gii thiu v h thng gim st giao thng.


H thng gim st giao thng c cng ty c phn cng ngh Futech nghin
cu v pht trin nhm h tr cc n v chc nng kim sot v pht hin nhng
phng tin vi phm giao thng. H thng cn a ra nhng bng chng vi phm xc
thc nht khi ngi vi phm giao thng i sai ln ng, chy qu tc cho php,
vt n , xe bin s en
SINH VIN V NH CHUNG

LP: T & THCN K49

CHNG I: TNG QUAN V CC H THNG GIM ST


1.2.2. Mc ch ca h thng.
Gim st cc phng tin giao thng lin tc 24/24.
Pht hin cc phng tin vi phm giao thng: i sai ln ng, vt qu tc
cho php, vt n
T ng chp nh v nhn dng bin s xe vi phm.
Gim st cc phng tin tham gia giao thng ti trung tm iu hnh.
Bo co thng k tnh hnh vi phm giao thng theo thi gian thc.
Tit kim nhn lc cho vic gim st v x l cc trng hp vi phm giao
thng.
1.2.3. Nguyn l ca h thng.

Hnh 3: S nguyn l h thng gim st giao thng.

1.2.4. c im ca h thng.
Ton b thng tin v xe vi phm c t ng in ra ngay ti thi im xe vi
phm hoc xut ra di nhiu dng bo co khc nhau.
M hnh qun l Client Sever gip cho vic qun l nhiu im vi phm giao
thng c d dng v tp trung ti trung tm iu hnh giao thng.
H thng c th kt ni vi c s d liu ng k xe trn ton quc. V vy c
th bit c y thng tin v xe v ch xe.

SINH VIN V NH CHUNG

LP: T & THCN K49

CHNG I: TNG QUAN V CC H THNG GIM ST


1.2.5. Giao din phn mm.

Hnh 4: Hnh nh xe vi phm vt qu tc cho php.

Hnh 5: Hnh nh xe vi phm vt n .

1.3.

Gii thiu v ti.

1.3.1. Tnh cp thit ca ti.


Ngy nay nhu cu i li ca con ngi ngy cng tng cao ko theo s gia tng
nhanh chng ca cc phng tin c nhn, c bit l xe my. y cng l i tng
d b mt cp do ch bo v cha cao. T thc t ny sinh ra nhu cu qun l,
gim st cho loi phng tin ny. Do em a tng thit k thit b c th theo
di v chng trm cho phng tin c nhn ny.
SINH VIN V NH CHUNG

LP: T & THCN K49

CHNG I: TNG QUAN V CC H THNG GIM ST


1.3.2. Mc ch v phng php nghin cu.
1.3.2.1. Mc ch.
Thit k 1 thit b c kh nng thu thp v tr ca phng tin, gi d liu v
cho ch phng tin thng qua mng GSM.
Thit b nhn d liu c th l in thoi ca ch phng tin hoc 1 module
sim c kt ni vi my tnh hin th d liu (v tr, tc phng tin, cc thng tin v
xe) ln bn s.
iu khin tt m thit b thng qua tin nhn.
1.3.2.2. Phng php nghin cu.
S dng sim908 kt hp chip x l nhn d liu GPS t v tinh gi v, ng
gi d liu v gi ln mng GSM.
Trng hp xe b nh cp phi cp nhp thng xuyn v tr ca xe v lu li
v tr cui cng k t khi d liu GPS b mt.
Khi xe b tai nn cn gi ngay v tr tai nn cho s ca trung tm cu h.
Xy dng bn s trn C# hin th trc quan v tr ca xe => m bo d
dng cho vic qun l v tm kim trong trng hp xe b mt.
1.3.3. Gii hn ti.
Vic truyn d liu thc hin qua tin nhn mng in thoi nn gy tn km.
tng pht trin : Truyn d liu qua mng internet s tit kim hn rt nhiu.
chnh xc ca thit b ph thuc nhiu vo phn cng sim ca nh sn xut
v thi tit hot ng. ti mi ch tm dng ch x l d liu th sim chuyn v,
cha thc hin thut ton chng nhiu cho h thng.

SINH VIN V NH CHUNG

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR

CHNG II
GII THIU MODULE SIM900, SIM908
TP LNH AT COMMAND, CM BIN CHUYN NG
2.1. Tng quan v h thng thng tin di ng GSM.
2.1.1. Gii thiu v cng ngh GSM.
H thng thng tin di ng ton cu (Global System for Mobile
Communications vit tt: GSM) l mt cng ngh dng cho mng thng tin di ng.
Dch v GSM c s dng bi hn 2 t ngi trn 212 quc gia v vng lnh th.
Cc mng thng tin di ng GSM cho php c th roaming vi nhau do nhng my
in thoi di ng GSM ca cc mng GSM khc nhau vn c th s dng c nhiu
ni trn th gii.
GSM l chun ph bin nht cho in thoi di ng trn th gii. Kh nng ph
sng rng khp ni ca chun GSM lm cho n tr nn ph bin trn th gii, cho
php ngi s dng c th s dng in thoi di ng ca h nhiu vng trn th
gii. GSM khc vi cc chun tin thn ca n v c tn hiu v tc , cht lng
cuc gi. N c xem nh l mt h thng in thoi di ng th h th hai (Second
Generation 2G). GSM l mt chun m, hin ti n c pht trin bi 3rd
Generation Partnership Project (3GPP). ng v pha quan im khch hng, li th
chnh ca GSM l cht lng cuc gi tt hn, gi thnh thp v dch v tin nhn.
Thun li i vi nh iu hnh mng l kh nng trin khai thit b t nhiu ngi
cung ng. GSM cho php nh iu hnh mng c th sn sng dch v khp ni, v
th ngi s dng c th s dng in thoi ca h khp ni trn th gii.
2.1.2. c im cng ngh GSM.
GSM l mng in thoi di ng thit k gm nhiu t bo (cell) do cc my
in thoi di ng kt ni vi mng bng cch tm kim cc cell gn n nht.
Cc mng di ng GSM hot ng trn 4 bng tn. Hu ht th hot ng
bng tn 900 MHz v 1800 MHz.
S dng cng ngh phn chia theo thi gian TDM (time division multiplexing),
cho php truyn 8 knh thoi ton tc hay 16 knh thoi bn tc trn 1 knh v tuyn.
Tc truyn d liu cho c 8 knh l 270.833 kbit/s v chu k ca mt khung
l 4.615 m.
2.1.3. Cu trc mng GSM.

SINH VIN V NH CHUNG

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR

Hnh 6: Cu trc mng GSM.

Mt mng GSM cung cp y cc dch v cho khch hng s kh phc


tp. Cu trc mng GSM chia theo phn h bao gm cc thnh phn nh sau:
Phn h chuyn mch NSS: Network switching SubSystem.
Phn h v tuyn RSS = BSS + MS: Radio SubSystem.
Phn h vn hnh v bo dng OMS: Operation and Maintenance SubSystem.
BSS: Base Station Subsystem = TRAU + BSC + BTS.
TRAU: B chuyn i m v phi hp tc .
BSC: B iu khin trm gc.
BTS: Trm thu pht gc.

Chc nng ca BSS: iu khin mt s trm BTS x l cc bn tin bo hiu:


Khi to kt ni iu khin chuyn giao: Intra & Inter BTS HO Kt ni n cc
MSC, BTS, OMC.

Chc nng ca BTS: Thu pht v tuyn nh x knh logic vo knh vt l


M ha v gii m Mt m/ gii mt m iu ch/ gii iu ch.

Mng v h thng chuyn mch Network and Switching Subsystem.

Phn mng GPRS (GPRS care network): L mt phn lp thm cung cp


dch v truy nhp internet.

SINH VIN V NH CHUNG

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR

Mt s phn khc phc v vic cung cp cc dch v cho mng GSM nh gi,
nhn tin SMS
2.2. Tng quan v h thng nh v ton cu GPS.
2.2.1. Gii thiu v h thng nh v ton cu GPS.
H thng nh v ton cu (Global Positioning System - GPS) l h thng xc
nh v tr da trn v tr ca cc v tinh nhn to, do B quc phng Hoa K thit k,
xy dng, vn hnh v qun l. Trong cng mt thi im, ta ca mt im trn
mt t s c xc nh nu xc nh c khong cch t im n t nht ba v
tinh.
Tuy c qun l bi B quc phng Hoa K, chnh ph Hoa K vn cho php
mi ngi trn th gii s dng mt s chc nng ca GPS min ph, bt k quc gia
no.
2.2.2. S hot ng ca h thng nh v GPS.
Cc v tinh GPS bay vng quanh Tri t hai ln trong mt ngy theo mt qu
o rt chnh xc v pht tn hiu c thng tin xung Tri t. Cc my thu GPS nhn
thng tin ny v bng php tnh lng gic s tnh c chnh xc v tr ca ngi
dng. V bn cht my thu GPS so snh thi gian tn hiu c pht i t v tinh vi
thi gian nhn c chng. Sai lch v thi gian cho bit my thu GPS cch v tinh
bao xa. Ri vi nhiu qung cch o c ti nhiu v tinh my thu c th tnh c
v tr ca ngi dng v hin th ln bn in t ca my.
My thu phi nhn c tn hiu ca t nht ba v tinh tnh ra v tr hai chiu
(kinh v v ) v theo di c chuyn ng. Khi nhn c tn hiu ca t nht
4 v tinh th my thu c th tnh c v tr ba chiu (kinh , v v cao). Mt
khi v tr ngi dng tnh c th my thu GPS c th tnh cc thng tin khc, nh
tc , hng chuyn ng, bm st di chuyn, khong hnh trnh, khong cch ti
im n, thi gian mt tri mc, ln v nhiu th khc na.
2.2.3. chnh xc ca h thng nh v GPS.
Cc my thu GPS ngy nay cc k chnh xc, nh vo thit k nhiu knh hot
ng song song ca chng. Cc my thu 12 knh song song (ca Garmin) nhanh chng
kha vo cc qu v tinh khi mi bt ln v chng duy tr kt ni bn vng, thm ch
trong tn l rm rp hoc thnh ph vi cc to nh cao tng. Trng thi ca kh quyn
v cc ngun gy http://vi.wikipedia.org/wiki/Sai_s%E1%BB%91 sai s khc c th

SINH VIN V NH CHUNG

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


nh hng ti chnh xc ca my thu GPS. Cc my thu GPS c chnh xc trung
bnh trong vng 15 mt.
Cc my thu mi hn vi kh nng WAAS (Wide Area Augmentation System) c
th tng chnh xc trung bnh ti di 3 mt. Khng cn thm thit b hay mt ph
c c li im ca WAAS. Ngi dng cng c th c chnh xc tt hn vi
GPS vi sai (Differential GPS - DGPS) sa li cc tn hiu GPS c chnh xc
trong khong 3 n 5 mt. Cc phng v b bin M vn hnh dch v sa li ny. H
thng bao gm mt mng cc i thu tn hiu GPS v pht tn hiu sa li bng cc
my pht hiu. thu c tn hiu sa li, ngi dng phi c my thu tn hiu vi
sai bao gm c n-ten dng vi my thu GPS ca h.
2.2.4. Cc thnh phn ca h thng nh v GPS.
GPS hin ti gm 3 phn chnh: phn khng gian, kim sot v s dng. Khng
qun Hoa K pht trin, bo tr v vn hnh cc phn khng gian v kim sot. Cc v
tinh GPS truyn tn hiu t khng gian, v cc my thu GPS s dng cc tn hiu ny
tnh ton v tr trong khng gian 3 chiu (kinh , v v cao) v thi gian hin
ti.
2.2.4.1. Phn khng gian.
Phn khng gian gm 24 v tinh (21 v tinh hot ng v 3 v tinh d phng)
nm trn cc qu o xoay quanh tri t. Chng cch mt t 20.200 km, bn knh
qu o 26.600 km. Chng chuyn ng n nh v quay hai vng qu o trong
khong thi gian gn 24 gi vi vn tc 7 nghn dm mt gi. Cc v tinh trn qu o
c b tr sao cho cc my thu GPS trn mt t c th nhn thy ti thiu 4 v tinh
vo bt k thi im no.
Cc v tinh c cung cp bng nng lng mt tri. Chng c cc ngun pin d
phng duy tr hot ng khi chy khut vo vng khng c nh sng mt tri. Cc
tn la nh gn mi qu v tinh gi chng bay ng qu o nh.
2.2.4.2. Phn kim sot.
Mc ch trong phn ny l kim sot v tinh i ng hng theo qu o v
thng tin thi gian chnh xc. C 5 trm kim sot t ri rc trn tri t. Bn trm
kim sot hot ng mt cch t ng, v mt trm kim sot l trung tm. Bn trm
ny nhn tn hiu lin tc t nhng v tinh v gi cc thng tin ny n trm kim sot
trung tm. Ti trm kim sot trung tm, n s sa li d liu cho ng v kt hp vi
hai an-ten khc gi li thng tin cho cc v tinh. Ngoi ra, cn mt trm kim sot
trung tm d phng v su trm quan st chuyn bit.
SINH VIN V NH CHUNG

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


Trm trung tm cng c th truy cp t cc ng-ten mt t ca U.S. Air Force
Satellite Control Network (AFSCN) v cc trm quan st NGA (National GeospatialIntelligence Agency). Cc ng bay ca v tinh c ghi nhn bi cc trm quan st
chuyn dng ca Khng qun Hoa K t Hawaii, Kwajalein, o Ascension, Diego
Garcia, Colorado Spring, Colorado v Cape Canveral, cng vi cc trm quan st
NGA c vn hnh Anh, Argentina, Ecuador, Bahrain, c v Washington DC.
Thng tin ng bay ca v tinh i c gi n Air Force Space Command's MCS
Schriever Air Force Base 25 km ng ng nam ca Colorado Springs, do 2nd Space
Operations Squadron (2 SOPS) ca U.S. Air Force vn hnh. Sau 2 SOPS lin lc
thng xuyn vi mi v tinh GPS thng qua vic cp nht nh v s dng cc ngten mt t chuyn dng hoc dng chung (AFSCN)(cc ng-ten GPS mt t chuyn
dng c t Kwajalein, o Ascension, Diego Garcia, v Cape Canaveral). Cc
thng tin cp nht ny ng b ha vi cc ng h nguyn t t trn v tinh trong
vng mt vi phn t giy cho mi v tinh, v hiu chnh lich thin vn ca m hnh
qu o bn trong mi v tinh. Vic cp nht c to ra b b lc Kalman s dng
cc tn hiu/thng tin t cc trm quan st trn mt t, thng tin thi tit khng gian,
v cc d liu khc.
2.2.4.3. Phn s dng.
Phn s dng l thit b nhn tn hiu v tinh GPS v ngi s dng dng cc thit
b ny ny bit v tr cng nh tc ca mnh.
2.3. Gii thiu phn cng SIM900.
2.3.1. c im ca Sim900.
c thit k cho th trng ton cu, SIM900 l mt c cu GSM/GPRS bng
tn, lm vic trn di tn s GSM 850MHz, EGSM 900MHz, DCS 1800MHz v PCS
1900MHz. Cc c tnh SIM900 nh GPRS nhiu khe thao tc: lp 10/ lp 8 v h tr
cc s m ha GPRS CS-1, CS-2, CS-3 v CS-4.
Vi mt hnh dng nh 24mm x 24mm x 3mm, SIM900 c th gp hu ht cc
nhu cu khng gian trong cc ng dng ca bn, nh l M2M, in thoi thng minh,
PDA v cc thit b in thoi khc. Giao tip vt l ti ng dng in thoi thng qua
mt lp SMT 68 chn, n cung cp tt c cc giao tip phn cng gia module vi
mch ca ngi thit k.
Bn phm v giao din hin th SPI s lm cho ngi s dng linh hot hn trong
pht trin ng dng ngi dng.

SINH VIN V NH CHUNG

10

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


Cng ni tip v cng Debug c th gip bn d dng pht trin cc ng dng ca
mnh.
Mt knh audio cho mt u vo microphone v 1 u ra loa.
SIM900 c thit k vi k thut tit kim nng lng cho s tiu th dng nh
1.5mA trong ch SLEEP.
SIM900 c tch hp giao thc TCP/IP; cc lnh AT m rng cho TCP/IP c
pht trin cho khch hng s dng giao thc TCP/IP mt cch d dng, n thc s
hu ch cho cc ng dng truyn d liu.

Hnh7: S chc nng SIM900

2.3.2. Mch phn cng sim900.


2.3.2.1. Khi ngun.
Ngun nui SIM900 l mt ngun mt chiu VBAT = 3.4V 4.5V. Trong mt s
trng hp, tn hiu gn sng trong mt vic truyn d liu t ngt c th l nguyn
nhn dn n st p khi dng tiu th tng ti dng nh c trng 2A. Do ngun
cp cho sim phi cn cung cp mt dng in ln ti 2A.

Hnh 8: Mch tham chiu cho ngun cp SIM900

SINH VIN V NH CHUNG

11

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


2.3.2.2. Tt\bt SIM900 s dng chn PWKEY.
Chng ta c th tt\bt SIM900 bng vic iu khin chn PWKEY xung mc
in p thp vi mt in tr dng gii hn (khuyn ngh 1K) ni tip cho mt thi
gian tr ngn v nh ra sau . Chn ny c ni ln VDD_EXT trong module.

Hnh 9: Mch ng dng tt/bt SIM900.

2.3.2.3. Ngun nui RTC.


Ngun cp cho chc nng RTC (Real Time Clock) ca module c th c cung
cp bi mt t in bn ngoi hoc mt pin thng qua chn VRTC.

Hnh 10: Ngun cp RTC t pin sc li.

Hnh 11: Ngun cp RTC t pin khng sc li.

Hnh 12: Ngun cp RTC t t in.

SINH VIN V NH CHUNG

12

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


2.3.2.4. Giao tip ni tip.
SIM900 cung cp hai cng ni tip khng ng b khng cn bng. Mt cng ni
tip v mt cng debug. Module GSM c thit k nh mt DCE (Data
Commuication Equipment), i theo kt ni truyn thng DCE-DTE (Data Terminal
Equipment). Module v client (DTE) c kt ni thng qua tn hiu tip theo. Di tn
t ng cho php t 1200bps n 115200bps.
TXD : Gi d liu ti ng tn hiu RXD ca DTE.
RXD : Nhn d liu t ng tn hiu TXD ca DTE.
DBG_TXD : Gi d liu ti ng tn hiu RXD ca DTE.
DBG_RXD : Nhn d liu t ng tn hiu TXD ca DTE.

Hnh 13 : Kt ni ca giao tip ni tip.

Hnh 14 : Mch chuyn mc RS232

SINH VIN V NH CHUNG

13

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


2.3.2.5. Giao tip m thanh.
Module cung cp mt knh u vo tng t AIN, c th s dng cho chc nng
micr. Micr in dung c khuyn co khi giao tip s dng micr. Cc u ra kt
ni vi thit b nhn. u ra thit b nhn ch c th iu khin trc tip qua in tr
32.

Hnh 15: Cu hnh giao tip Speaker

Hnh 16 : Cu hnh giao tip tai nghe

2.3.2.6. Giao tip Sim card.


STT chn
Chc nng
30
Ngun cp cho simcard, in p c th l 3V10%
hoc 1.8V10%. Dng in khong 10mA.
SIM_DATA
31
D liu vo ra simcard.
SIM_CLK
32
Tn hiu ng h simcard.
SIM_RST
33
Tn hiu reset simcard.
SIM_PRESENCE
34
Tn hiu pht hin sim trong simcard.
Tn chn
SIM_VDD

Bng1: nh ngha cc chn giao tip sim.

SINH VIN V NH CHUNG

14

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR

Hnh 17: Mch tham chiu cho simcard 8 chn.

Hnh 18: Mch tham chiu cho simcard 6 chn

2.3.2.7. S ch th trng thi mng.


Chn NETLIGHT c th c s dng iu khin n led ch th trng thi
mng. Trng thi ca chn c lit k trong bng di.
Trng thi
Off
64ms On/ 800ms Off
64ms On/ 3000ms Off
64ms On/ 300ms Off

Chc nng sim900


SIM900 khng lm vic.
SIM900 khng tm thy mng.
SIM900 tm thy mng.
Giao tip GPRS.

Bng 2: Bng trng thi lm vic ca NETLIGHT.

SINH VIN V NH CHUNG

15

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR

Hnh 19: Mch tham chiu ca NETLIGHT.

2.4. Gii thiu phn cng Sim908.


2.4.1. c im ca Sim908.
c thit k cho th trng ton cu, Sim908 c tch hp mt c cu
GSM/GPRS hiu sut cao v mt c cu GPS. C cu GSM/GPRS l mt m un
GSM/GPRS bng tn, n hot ng tn s GSM 850MHz, EGSM 900MHz, DCS
1800MHz v PCS 1900MHz. Cc c tnh SIM908 nh GPRS nhiu khe thao tc: lp
10/ lp 8 v h tr cc s m ha GPRS CS-1, CS-2, CS-3 v CS-4.
Vi mt hnh dng nh 30mmx30mmx3.2mm, Sim908 c th gp hu ht cc
nhu cu khng gian trong ng dng ngi dng nh l M2M, in thoi thng minh,
PDA, thit b theo di v cc thit b di ng khc.
Sim908 c 80 chn dn, n cung cp tt c cc giao tip phn cng gia module
v mch ca ngi dng.

Cng ni tip v cng debug gip ngi dng d dng pht trin cc ng dng.

Cc ng vo ra dnh cho chc nng nghe,gi v x l m thanh.

H tr lp trnh cho mc ch chung u vo, u ra.

Bn phm v giao din hin th SPI s mang li cho ngi dng s linh hot
pht trin cc ng dng.
SINH VIN V NH CHUNG

16

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR

Chn RF v giao din kt ni.

SIM908 c thit k vi k thut tit kim nng lng cho s tiu th dng
nh 1mA trong ch SLEEP.

SIM908 c tch hp giao thc TCP/IP; cc lnh AT m rng cho TCP/IP


c pht trin cho khch hng s dng giao thc TCP/IP mt cch d dng, n
thc s hu ch cho cc ng dng truyn d liu.
2.4.2. ng dng Sim908.
Sim908 l module sim tch hp GPRS v GPS do SimCom pht trin s gip cho
ngi s dng tit kim c rt nhiu thi gian v chi ph cho vic s dng. N c
kh nng theo di lin tc bt k v tr no vi tn hiu cc k n nh v chnh xc.
Vi nhng li th ni tri nh vy, Sim908 c ng dng trong hu ht cc thit b
gim st hnh trnh v nh v v tinh.

Hnh 20: M hnh gim st hot ng phng tin giao thng.

2.5.

Cm bin chuyn ng PIR.

2.5.1. Nguyn l pht hin.


PIR l ch vit tt ca Passive InfraRed sensor (PIR sensor), tc l b cm bin
th ng dng ngun kch thch l tia hng ngoi. Tia hng ngoi (IR) chnh l cc tia
nhit pht ra t cc vt th nng. Trong cc c th sng, trong chng ta lun c thn
nhit (thng thng l 37 C), v t c th chng ta s lun pht ra cc tia nhit,
hay cn gi l cc tia hng ngoi, ngi ta s dng mt t bo in chuyn i tia
nhit ra dng tn hiu in v nh m c th lm ra cm bin pht hin cc vt th
SINH VIN V NH CHUNG

17

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


nng ang chuyn ng. Cm bin ny gi l th ng v n khng dng ngun nhit
t pht (lm ngun tch cc, hay ch ng) m ch ph thuc vo cc ngun thn
nhit, l thn nhit ca cc thc th khc, nh con ngi con vt...
2.5.2. Cu trc cm bin PIR.

Hnh 21: Cu trc cm bin chuyn ng PIR.

Trn y l u d PIR, loi bn trong gn 2 cm bin tia nhit, n c 3 chn


ra, mt chn ni masse, mt chn ni vi ngun in p DC, mc p lm vic c th t
3 n 15V. Gc d ln. tng nhy cho u d, dng knh Fresnel, n c thit
k cho loi u c 2 cm bin, gc d ln, c tc dng ngn tia t ngoi.

2.5.3. Nguyn l lm vic ca loi u d PIR.


Cc ngun nhit (vi ngi v con vt l ngun thn nhit) u pht ra tia hng
ngoi, qua knh Fresnel, qua kch lc ly tia hng ngoi, n c cho tiu t trn 2
cm bin hng ngoi gn trong u d, v to ra in p c khuch i vi transistor
FET. Khi c mt vt nng i ngang qua, t 2 cm bin ny s cho xut hin 2 tn hiu
v tn hiu ny s c khuch i c bin cao v a vo mch so p tc
ng vo mt thit b iu khin hay bo ng.

SINH VIN V NH CHUNG

18

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR

Hnh 22: Nguyn l pht hin chuyn ng ngang ca cc ngun thn nhit.

Hnh v cho thy 2 vng cm ng nhy cm tng ng vi 2 cm bin trong


u d. Khi c mt con vt i ngang, t thn con vt s lun pht ra tia nhit, n c
tiu t mnh vi knh Fresnel v ri tiu t trn bia l cm bin hng ngoi, vy khi
con vt i ngang, u ra ca u d chng ta s thy xut hin mt tn hiu, tn hiu
ny s c cho vo mch x l to tc dng iu khin hay bo ng.

Hnh 23: Mch ng dng cm bin PIR ng ngt thit b qua rle.

Phn tch s mch in:


S cho thy, b u do PIR c 3 chn, chn 3 cho ni masse, chn 1 ni vo
ng ngun v chn 2 cho xut ra tn hiu, nguyn do phi phn cc cho u PIR l
v bn trong n c dng transistor FET. R2 (100K) l in tr ly tn hiu. Tn hiu
ny cho qua 2 tng khuch i vi IC1A v IC2B. y, ngi ta dng mch hi tip
nghch vi R4 (1M), R3 (10K) v t C2 (10uF) nh li cho tng khuch i ny
(do 1M/10K = 100, nn li tng ny ly khong 100), t C3 (0.1uF) c tc dng p
SINH VIN V NH CHUNG

19

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


dy tn hp li, ch cho lm vic vng tn thp b vng tn cao (v tc nhn nhit c
qun tnh ln, thng thay i rt chm), tn hiu ly ra trn chn 1 cho qua in tr
gim bin R5 (10K) v t lin lc C4 (10uF) vo tng khuch i sau trn chn s 6.
Mch dng in tr R6 (1M), diode D1, D2 v in tr R7 (1M) to thnh cu
chia p, n ly p phn cc cho chn 5 ca tng khuch i v to in p mu (Vref)
cp cho chn 9 (ng vo o) v chn 12 (ng vo khng o) ca 2 tng so p IC1C
v IC1D. in tr R8 (1M) v t C5 (0.1uF) to tc dng hi tip nghch, n nh cho
tng khuch i IC1B. Tn hiu cm bin sau khi c khuch i cho ra trn chn 7,
ri cng lc a vo 2 tng so p trn chn 10 v chn 13. y l 2 tng so p c chu
trnh hi sai, dng to ra xung kch thch c dc tt, kch vo tng a hi n n
trong ic CD4538, diode D3 v diode D4 c cng dng cch ly trnh nh hng qua li
ca 2 ng ra trn chn 8 v chn 14.
CD 4538 l ic logic c 2 tng n n, n nh thi gian qu (thi gian tr)
theo thi hng ca in tr R10 (1M) v t C6 (1uF) trn chn s 2. Xung lm chuyn
trng thi a vo trn chn 4, khi chuyn mch mc p cao cho xut hin trn chn s
6, n s kch dn transistor Q1, v Q1 cp dng cho relay ng cc tip im l
kim. Do dng mch n n, nh thi theo thi hng ca R10 v t C6, nn ch sau
mt thi gian qui nh, mch s t tr li trng thi n nh ban u, Q1 s tt v relay
s b ct dng v nh tip im l kim ra. Mch c th lm vic vi mc ngun nui t
5 n 12V (Ch mc ngun nui chn loi relay cho thch hp).
Tm li, khi c ngi i ngang qua b u d, ngun thn nhit ca ngi hay
con vt s tc kch vo u d PIR, th relay s c cp dng ng cc tip im
l kim, Bn c th dng cc tip im ny m n, v sau mt lc mch n n tr
v trng thi vn c v n s t tt.
2.6. Kho st tp lnh AT Command.
Cc lnh AT l cc hng dn c s dng iu khin mt modem. AT l
mt cch vit gn ca ch Attention. Mi dng lnh ca n bt u vi AT hay at.
l l do ti sao cc lnh modem c gi l cc lnh AT. Nhiu lnh ca n c
s dng iu khin cc modem quay s s dng dy mi (wired dial-up modems),
chng hn nh ATD (Dial), ATA (Answer), ATH (Hool control) v ATO (return to
online data state), cng c h tr bi cc modem GSM/GPRS v cc in thoi di
ng.
Bn cnh b lnh AT thng dng ny, cc modem GSM/GPRS v cc in thoi
di ng cn c h tr bi mt b lnh AT c bit i vi cng ngh GSM. N bao
gm cc lnh lin quan ti SMS nh AT+ CMGS (gi tin nhn SMS), AT+CMSS
SINH VIN V NH CHUNG

20

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


(gi tin nhn SMS t mt vng l tr), AT+CMGL (chui lit k cc tin nhn SMS)
v AT+CMGR (c tin nhn SMS).
Ngoi ra, cc modem GSM cn h tr mt b lnh AT m rng. Nhng lnh AT
m rng ny c nh ngha trong cc chun ca GSM. Vi cc lnh AT m rng
ny, bn c th lm mt s th nh sau:

c, vit, xa tin nhn.

Gi tin nhn SMS.

Kim tra chiu di tn hiu.

Kim tra trng thi sc pin v mc sc ca pin.

c, vit v tm kim v cc mc danh b.

2.6.1. Cc lnh kim tra ban u.


Ly thng tin c bn v in thoi di ng hay modem GSM/GPRS. V d nh
tn ca nh sn xut (AT+CGMI), s model (AT+CGMM), s IMEI (International
Mobile Equipment Identity) (AT+CGSN) v phin bn phn mm (AT+CGMR).
Ly cc thng tin c bn v nhng ngi k tn di y. Th d, MSISDN
(AT+CNUM) v s IMS (International Mobile Subscriber Identity) (AT+CIMI).
Ly thng tin trng thi hin ti ca in thoi di ng hay modem GSM/GPRS.
V d nh trng thi hot ng ca in thoi (AT+CPAS), trng thi ng k mng
mobile (AT+CREG), chiu di sng radio (AT+CSQ), mc sc pin v trng thi sc
pin (AT+CBC).
c (AT+CPBR), vit (AT+CPBW) hay tm kim (AT+CPBF) c mc v danh
b in thoi (phonebook).
Thc thi cc nhim v lin quan ti an ton, chng hn nh m hay ng cc
kha chc nng (AT+CLCK), kim tra xem mt chc nng c kha hay cha v
thay i password (AT+CPWD).
iu khin hot ng ca cc m kt qu/cc thng bo li ca cc lnh AT. V
d, bn c th iu khin cho php hay khng cho php kch hot hin th thng bo
li (AT+CMEE) v cc thng bo li nn c hin th theo dng s hay theo dng
dng ch (AT+CMEE=1 hay AT+CMEE=2).

SINH VIN V NH CHUNG

21

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


Thit lp thay thay i cu hnh ca in thoi di dng hay modem GSM/GPRS.
V d, thay i mng GSM (AT+COPS), loi dch v ca b truyn tin (AT+CBST),
cc thng s protocol lin kt vi radio (AT+CRLP), a ch trung tm SMS
(AT+CSCA) v khu vc lu tr cc tin nhn SMS (AT+CPMS).
Lu v phc hi cc cu hnh ca in thoi di ng hay modem GSM/GPRS.
V du, lu (AT+COPS) v phc hi (AT+CRES) cc thit lp lin quan ti tin nhn
SMS chng hn nh a ch trung tm tin nhn SMS.
2.6.2. Cc lnh khi to.
Lnh AT<cr>
Nu lnh thc hin c th tr v: OK
Bt u thc hin cc lnh tip theo.
Nu lnh khng thc hin c th tr v dng:
+CMS ERROR <err>
Lnh AT+CMGF=[<mode>] <cr>
Nu lnh thc hin c th tr v: OK
<mode> : 0 dng d liu PDU.
1 dng d liu kiu text.
Nu lnh khng thc hin c th tr v dng:
+CMS ERROR <err>
Lnh AT&W[<n>]
Nu lnh thc hin c th tr v: OK
Lu cu hnh cho GSM Module Sim.
Nu lnh khng thc hin c th tr v dng:
+CMS ERROR <err>
Lnh ATE[<value>]
Nu lnh thc hin c th tr v: OK

SINH VIN V NH CHUNG

22

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


<value> 0 Tt ch Echo
1 Bt ch Echo
Nu lnh khng thc hin c th tr v dng:
+CMS ERROR <err>
2.6.3. Cc lnh x l cuc gi.
Lnh thc hin cuc gi:
ATD<cr>
V d: mun quay s ti s in thoi 01266609025 th ta g lnh
ATD01266609025;<cr>
<cr>: Enter
Lnh nhc my:
ATA<cr>
V d: khi c s in thoai no gi n s in thoi c gn trn modem Sim, ta
mun nhc my kt ni th g lnh:
ATA <cr>
Lnh b cuc gi:
ATH<cr>
V d: khi c s in thoai no gi n s in thoi c gn trn modem Sim, ta
khng mun nhc my m t chi cuc gi th g lnh:
ATH<cr>
2.6.4. Cc lnh v SMS.
Lnh xa tin nhn:
AT+CMGD
V d: mun xa mt tin nhn no c lu trn sim th ta thc hin lnh sau:
AT+CMGD=<index> <cr>

SINH VIN V NH CHUNG

23

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


<index>: v tr ngn nh lu tin nhn
Nu lnh thc hin c th tr v: OK
Nu lnh khng thc hin c th tr v dng:
+CMS ERROR <err>
Lnh c tin nhn:
AT+CMGR=<index>[,mode] <cr>
<index> : s nguyn, l v tr ngn nh cha tin nhn cn c.
<mode> : 0 dng d liu PDU.
1 dng d liu kiu text.
Nu nh lnh c thc hin th kiu d liu tr v di dng text ( mode=1):

T SMS-DELIVER:

+CMGR:<stat>,<oa>,[<alpha>],<scts>[,<tooa>,<fo>,<pid>,<dcs>,<sca>,<tosca>,<
length>]<CR><LF><data>

T SMS-SUBMIT:

+CMGR:<stat>,<da>,[<alpha>][,<toda>,<fo>,<pid>,<dcs>,[<vp>],<sca>,<tosca>,<
length>]<CR><LF><data>

T SMS STATUS - REPORTS:

+CMGR: <stat>,<fo>,<mr>,[<ra>],[<tora>],<scts>,<dt>,<st>

T SMS-COMMANDS:

+CMGR:<stat>,<fo>,<ct>[,<pid>,[<mn>],[<da>],[<toda>],<length><CR><LF><c
data>]

T CBM storage

+CMGR:<stat>,<sn>,<mid>,<dcs>,<page>,<pages><CR><LF><data>
Nu d liu tr v dng PDU(mode=0):
+CMGR: <stat>,[<alpha>],<length><CR><LF><pdu>

SINH VIN V NH CHUNG

24

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


Nu lnh b li th tr v di dng: +CMS ERROR: <err>

Lnh gi tin nhn SMS:


AT+CMGS

Nu gi tin nhn di dng text:


(+CMGF=1):

+CMGS=<da>[<toda>]<CR> text is entered <ctrl-Z/ESC>

Nu gi tin nhn dng PDU:


(+CMGF=0):

+CMGS=<length><CR> PDU is given <ctrl-Z/ESC>


Lnh c thc hin thnh cng thi d liu tr v:
Dng text : +CMGS: <mr>
OK
Dng PDU : +CMGS: <mr>
OK
Lnh b li : +CMS ERROR: <err>
Lnh vit tin nhn ri lu vo ngn nh:
AT+CMGW
Nu vit tin nhn di dng text :
AT+CMGW=[<oa/da>[,<tooa/toda>[,<stat>]]]<CR>text is entered <ctrl-Z/ESC>
<ESC>
Lnh c thc hin ng th d liu tr v dng:
+CMGW: <index>
OK
Lnh sai th d liu tr v dng: +CMS ERROR: <err>
SINH VIN V NH CHUNG

25

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


Nu vit tin nhn di dng PDU:
AT+CMGW=<length>[,<stat>]<CR> PDU is given <ctrl-Z/ESC>
Lnh gi tin nhn t mt ngn nh no :
AT+CMSS=<index>[,<da>[,<toda>]]
Nu lnh c thc hin thnh cng d liu tr v dng:
Dng text:

+CMGS: <mr> [,<scts>]

OK
Dng PDU: +CMGS: <mr> [,<ackpdu>]
OK
Nu lnh b li th d liu tr v dng: +CMS ERROR: <err>
Lnh bo hiu tin nhn mi:
AT+CNMI=[<mode>[,<mt>[,<bm> [,<ds>[,<bfr>]]]]]
Nu lnh c thc hin ng th d liu tr v dng: OK
Nu lnh b li th d liu tr v dng: +CMS ERROR: <err>
2.6.5. Cc lnh v GPRS.
Bng 3: Chc nng cc lnh GPRS.

STT

Chc nng lnh

Lnh

AT+CGATT

Lnh gn hay tch thit b khi GPRS.

AT+CGDCONT

nh ngha dng PDP.

AT+CGQMIN

Cht lng dich v mc thp nht.

AT+CGQREQ

Cht lng dich v.

AT+CGDATA

Trng thi d liu vo.

AT+CGREG

Tnh trng ng k ca mng.

AT+CGCOUNT

m gi d liu vo.

SINH VIN V NH CHUNG

26

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


2.6.6. Cc lnh v GPS.
SimCom cung cp cho chng ta h thng cc lnh AT Command iu khin
GPS, nhim v quan trng l kh nng p dng lnh vo tng ng dng m chng ta
mong mun.
2.6.6.1. Tng quan cc tp lnh c bn.
STT

Lnh

Chc nng

AT+CGPSPWR

iu khin ngun GPS.

AT+CGPSRST

t li ch GPS(Hot/Warm/Cold).

AT+CGPSINF

Nhn thng tin v tr GPS hin ti.

AT+CGPSOUT

iu khin d liu ra NMEA GPS.

AT+CGPSSTATUS

Tnh trng GPS.

AT+CGPSIPR

Set tc baudrate.

Bng 4: Chc nng cc lnh GPS.

2.6.6.2. Cu hnh cc tp lnh v GPS.


Lnh AT+CGPSPWR:
a. Lnh test:
AT+CGPSPWR=?
Phn hi: +CGPSPWR:(danh sch cc mode)
b. Lnh c:
AT+CGPSPWR?
Phn hi: +CGPSPWR:[mode]
c. Lnh ghi:
AT+CGPSPWR=[mode]
[mode]:0 tt ngun GPS.
[mode]:1 bt ngun GPS.
SINH VIN V NH CHUNG

27

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


Lnh AT+CGPSRST:
a. Lnh test:
AT+CGPSRST=?
Phn hi: +CGPSRST:(danh sch cc mode)
b. Lnh c:
AT+CGPSRST?
Phn hi: +CGPSRST:[mode]
c. Lnh ghi:
AT+CGPSRST=[mode]
[mode]: 0 reset GPS in COLD start mode
[mode]: 1 mode t ng
Lnh AT+CGPSINF:
a. Lnh test:
AT+CGPSINF=?
Phn hi: +CGPSINF:(0,2,4,8,16,32,64,128)
b. Lnh c:
AT+CGPSINF=[mode]
Nu [mode] l 0 th:
Phn hi:
[mode],[kinh ],[v ],[ cao],[UTC time],[TTFF],[num],[tc ],[course]
Nu [mode] l:
21: cc thng s xem ph lc A.1 $GPGGA [1]
22: cc thng s xem ph lc A.2 GPGLL [1]
23: cc thng s xem ph lc A.3 GPGSA [1]
24: cc thng s xem ph lc A.4 GPGSV [1][2]
SINH VIN V NH CHUNG

28

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


25: cc thng s xem ph lc A.5 GPRMC [1]
26: cc thng s xem ph lc A.6 GPVTG [1]
27: cc thng s xem ph lc A.7 GPZDA [1]
[1] khng bao gm cc thng s:massage ID, checksum, [CR][LF]
[2] bao gm cc thng s:
Satellites in view
Satellites ID
elevation
azimuth
SNR(C/N0)
Lnh AT+CGPSOUT:
a. Lnh test:
AT+CGPSOUT=?
Phn hi: +CGPSOUT:(0-255)
b. Lnh c:
AT+CGPSOUT?
Phn hi: +CGPSOUT:[mode]
c. Lnh ghi:
AT+CGPSOUT=[mode]
Nu mode l 0 th: gim thiu thng tin ng ra GPS NMEA t Debug UART
Nu: Bit1=1, cho php d liu ng ra NMEA $GPGGA, xem ph lc A.1
Bit2=1, cho php d liu ng ra NMEA $GPGLL, xem ph lc A.2
Bit3=1, cho php d liu ng ra NMEA $GPGSA, xem ph lc A.3
Bit4=1, cho php d liu ng ra NMEA $GPGSV, xem ph lc A.4
SINH VIN V NH CHUNG

29

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


Bit5=1, cho php d liu ng ra NMEA $GPRMC, xem ph lc A.5
Bit6=1, cho php d liu ng ra NMEA $GPVTG, xem ph lc A.6
Bit7=1, cho php d liu ng ra NMEA $GPZDA, xem ph lc A.7
Lnh AT+CGPSSTATUS:
a. Lnh test
AT+CGPSSTATUS=?
Phn hi: +CGPSSTATUS:(danh sch cc mode )
b. Lnh c:
AT+CGPSSTATUS?
Phn hi: +AT+CGPSSTATUS: [mode]
Cc thng s GPS MODE RESET
[mode] l mt gi tr chui:
Location UnKnown: GPS khng chy
Location Not Fix: GPS chy v khng c nh
Location 2D Fix: trng thi GPS c nh 2D
Location 3D Fix: trng thi GPS c nh 3D
Lnh AT+CGPSIPR:
a. Lnh test:
AT+CGPSIPR=?
Phn hi: +CGPSIPR:(danh sch mode)
b. Lnh c:
AT+CGPSIPR:[mode]
c. Lnh ghi:
AT+CGPSIPR=[mode]
[mode]: 4800,9600,19200,38400,57600,115200,230400,460800
SINH VIN V NH CHUNG

30

LP: T & THCN K49

CHNG II: MODULE SIM900- SIM908 TP LNH AT CM BIN PIR


2.6.7. Cc lnh khc.
Lnh a model v ch ngh:
AT+CFUN
V d mun tt ht chc nng lin quan n truyn nhn sng RF v cc chc nng
lin quan n sim th g lnh:
AT+CFUN=0 <cr>
OK
Lnh chuyn t ch ngh sang ch hot ng bnh thng:
AT+CFUN
V d sim ang ch ngh ta mun chuyn sang ch hot ng bnh thng th
g lnh:
AT+CFUN=1 <cr>
OK
Lnh reset mode:
ATZ <cr>
OK
Lnh tt ch echo:
ATE0 <cr>

SINH VIN V NH CHUNG

31

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307

CHNG III
KHO ST VI IU KHIN ATMEGA128 DS1307
Tng quan vi iu khin AVR.
Vi iu khin AVR do hng Atmel ( Hoa K ) sn xut c gii thiu ln u
nm 1996. AVR c rt nhiu dng khc nhau bao gm dng Tiny AVR ( nh AT tiny
13, AT tiny 22) c kch thc b nh nh, t b phn ngoi vi, ri n dng AVR
(chng hn AT90S8535, AT90S8515,) c kch thc b nh vo loi trung bnh v
mnh hn l dng Mega (nh ATmega32, ATmega128,) vi b nh c kch thc
vi Kbyte n vi trm Kb cng vi cc b ngoi vi a dng c tch hp trn chip,
cng c dng tch hp c b LCD trn chip (dng LCD AVR ). Tc ca dng Mega
cng cao hn so vi cc dng khc. S khc nhau c bn gia cc dng chnh l cu
trc ngoi vi, cn nhn th vn nh nhau. t bit, nm 2008, Atmel li tip tc cho ra
i dng AVR mi l XmegaAVR, vi nhng tnh nng mnh m cha tng c cc
dng AVR trc . C th ni XmegaAVR l dng MCU 8 bit mnh m nht hin
nay.
3.1.1. c tnh vi iu khin ATmeg128.
ROM : 128 Kbytes
SRAM: 4Kbytes
EEPROM : 4Kbytes
64 thanh ghi I/O
160 thanh ghi vo ra m rng
32 thanh ghi a mc ch.
2 b nh thi 8 bit (0,2).
2 b nh thi 16 bit (1,3).
B nh thi watchdog
B dao ng ni RC tn s 1 MHz, 2 MHz, 4 MHz, 8 MHz
ADC 8 knh vi phn gii 10 bit ( dng Xmega ln ti 12 bit )
2 knh PWM 8 bit
6 knh PWM c th lp trnh thay i phn gii t 2 ti 16 bit
B so snh tng t c th la chn ng vo
Hai khi USART lp trnh c
Khi truyn nhn ni tip SPI
Khi giao tip ni tip 2 dy TWI
H tr boot loader
6 ch tit kim nng lng
La chn tn s hot ng bng phn mm
ng gi 64 chn kiu TQFP.
Tn s ti a 16MHz
in th : 4.5v - 5.5v
3.1.

SINH VIN V NH CHUNG

32

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


3.1.1. Cu trc vi iu khin AVR.

Hnh 24: Cu trc vi iu khin AVR.

3.1.2. Cu trc b nh v cng vo - ra.


3.1.2.1. Cu trc b nh.
B nh vi iu khin AVR c cu trc Harvard l cu trc c ng Bus ring
cho b nh chng trnh v b nh d liu. B nh AVR c chia lm 2 phn chnh:
B nh chng trnh ( program memory ) v b nh d liu ( Data memory ).
B nh chng trnh : B nh chng trnh ca AVR l b nh Flash c dung
lng 128 K bytes. B nh chng trnh c rng bus l 16 bit. Nhng a ch u
tin ca b nh chng trnh c dng cho bng vc t ngt ( xem chi tit v bng
vc t ngt chng 4 ). Cn l vi iu khin ATmega128 b nh chng
trnh cn c th c chia lm 2 phn : phn boot loader (Boot loader program
section) v phn ng dng ( Application program section ).
Phn boot loader cha chng trnh boot loader. Chng trnh Boot loader l
mt phn mm nh np trong vi iu khin v c chy lc khi ng. Phn mm
ny c th ti vo trong vi iu khin chng trnh ca ngi s dng v sau thc
thi chng trnh ny. Mi khi reset vi iu khin CPU s nhy ti thc thi chng
trnh boot loader trc, chng trnh boot loader s d xem c chng trnh no cn
np vo vi iu khin hay khng, nu c chng trnh cn np, boot loader s np
chng trnh vo vng nh ng dng (Application program section ), ri thc thi
SINH VIN V NH CHUNG

33

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


chng trnh ny. Ngc li, boot loader s chuyn ti chng trnh ng dng c sn
trong vng nh ng dng thc thi chng trnh ny.
Phn ng dng (Application program section ) l vng nh cha chng trnh
ng dng ca ngi dng. Kch thc ca phn boot loader v phn ng dng c th
ty chn. Hnh 2.1 th hin cu trc b nh chng trnh c s dng v khng s dng
boot loader, khi s dng phn boot loader ta thy 4 word u tin thay v ch th cho
CPU chuyn ti chng trnh ng dng ca ngi dng (l chng trnh c nhn start)
th ch th CPU nhy ti phn chng trnh boot loader thc hin trc, ri mi
quay tr li thc hin chng trnh ng dng.

Hnh 25: B nh chng trnh c v khng s dng Bootloader.

B Nh D Liu : B nh d liu ca AVR chia lm 2 phn chnh l b nh


SRAM v b nh EEPROM. Tuy cng l b nh d liu nhng hai b nh ny li tch
bit nhau v c nh a ch ring. B nh SRAM c dng lng 4 K bytes, B nh
SRAM c hai ch hot ng l ch thng thng v ch tng thch vi

SINH VIN V NH CHUNG

34

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


ATmega103, mun thit lp b nh SRAM hot ng theo ch no ta s dng bit
cu ch M103C ( M103C fuse bit (9) ).
B nh SRAM ch bnh thng : ch bnh thng b nh SRAM
c chia thnh 5 phn: Phn u l 32 thanh ghi chc nng chung (General Purpose
Register ) R0 n R31 c a ch t $0000 ti $001F. Phn th 2 l khng gian nh
vo ra vi 64 thanh ghi vo ra ( I/O Register ) c a ch t $0020 ti $005F. Phn th
3 dng cho vng nh dnh cho cc thanh ghi vo ra m rng (Extended I/O Registers)
c a ch t $0060 ti $00FF. Phn th 4 l vng SRAM ni vi 4096 byte c a ch
t $0100 ti $10FF. Phn th 5 l vng nh SRAM ngoi ( External SRAM ) bt u
t a ch $1100, vng SRAM m rng ny c th m rng ln n 64 K byte. Khi ni
b nh SRAM c dung lng 4 K byte l ni ti phn th 4 ( SRAM ni ). Nu tnh c
cc thanh ghi th b nh SRAM trong ch bnh thng s l 4.25 K byte = 4352
byte.
B nh SRAM ch tng thch ATmega103 : ch ny b nh SRAM c
bn cng ging ch bnh thng, ngoi tr phn th 3 l vng nh dnh cho cc
thanh ghi vo ra m rng khng tn ti, ngoi ra kch thc ca phn SRAM ni (
internal SRAM ) ch c 4000 byte so vi 4096 byte ch bnh thng. Hnh 2.2 th
hin s b nh d liu c hai ch : Bnh thng v tng thch ATmega103.
T hnh 2.2 ta thy nu cu hnh b nh SRAM hot ng ch tng thch
ATmega103 th ta s b mt i 160 thanh ghi vo ra m rng ( extended I/O Register ),
l nhng thanh ghi ng vai tr quan trng trong cc ch hot ng ca vi iu
khin.

Hnh 26: Cu trc b nh d liu.

3.1.2.2. Cng vo/ra Atmega128.


Cng vo ra l mt trong s cc phng tin vi iu khin giao tip vi cc
thit b ngoi vi. ATmega128 c 7 cng ( Port ) vo/ra 8 bit l : PortA, PortB, PortC,
SINH VIN V NH CHUNG

35

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


PortD, PortE, PortF, PortG, tng ng vi 56 ng vo ra. Cc cng vo ra ca AVR
l cng vo ra hai chiu c th nh hng, tc c th chn hng ca cng l hng
vo (input ) hay hng ra (output ). Tt cc cc cng vo ra ca AVR iu c tnh
nng c Chnh sa Ghi ( Read Modify write ) khi s dng chng nh l cc
cng vo ra s thng thng. iu ny c ngha l khi ta thay i hng ca mt chn
no th n khng lm nh hng ti hng ca cc chn khc. Tt c cc chn ca
cc cng ( port ) u c in tr ko ln ( pull-up ) ring, ta c th cho php hay khng
cho php in tr ko ln ny hot ng.
in tr ko ln l mt in tr c dng khi thit k cc mch in t logic.
N c mt u c ni vi ngun in p dng (thng l Vcc hoc Vdd) v u
cn li c ni vi tn hiu li vo/ra ca mt mch logic chc nng. in tr ko
ln c th c lp t ti cc li vo ca cc khi mch logic thit lp mc logic
li vo ca khi mch khi khng c thit b ngoi ni vi li vo. in tr ko ln
cng c th c lp t ti cc giao din gia hai khi mch logic khng cng loi
logic, c bit l khi hai khi mch ny c cp ngun khc nhau. Ngoi ra, in tr
ko ln cn c lp t ti li ra ca khi mch khi li ra khng th ni ngun to
dng, v d cc linh kin logic TTL c cc gp h. i vi h logic lng cc vi
ngun nui 5 Vdc th gi tr ca in tr ko ln thng nm trong khong 1000 n
5000 Ohm, ty theo yu cu cp dng trn ton gii hot ng ca mch. Vi lgc
CMOS v logic MOS chng ta c th s dng cc in tr c gi tr ln hn nhiu,
thng t vi ngn n mt triu Ohm do dng r r cn thit li vo l rt nh.
Trong vic thit k cc vi mch ng dng, nu mt IC c ng ra loi cc thu h
giao tip vi nhiu IC khc th gi tr ca in tr ko ln s tng i nh (khong
vi trm Ohm). Bi v lc ny h s fanout ln dn n dng ng ra ca IC phi ln
cung cp cho cc ng vo ca cc IC khc, nu khng vi mch s hot ng chp
chn hoc c th khng hot ng.
3.1.2.3. Cch thc hot ng.
Khi kho st cc cng nh l cc cng vo ra s thng thng th tnh cht ca
cc cng ( PortA, PortB,PortG ) l tng t nhau, nn ta ch cn kho st mt cng
no trong s 7 cng ca vi iu khin l . Mi mt cng vo ra ca vi iu khin
c lin kt vi 3 thanh ghi : PORTx, DDRx, PINx. ( y x l thay th cho A,
B,G ). Ba thanh ghi ny s c phi hp vi nhau iu khin hot ng ca
cng, chng hn thit lp cng thnh li vo c s dng in tr pull-up...
Thanh ghi DDRx.
y l thanh ghi 8 bit ( c th c ghi ) c chc nng iu khin hng ca cng
(l li ra hay li vo ). Khi mt bit ca thanh ghi ny c set ln 1 th chn tng ng
SINH VIN V NH CHUNG

36

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


vi n c cu hnh thnh ng ra. Ngc li, nu bit ca thanh ghi DDRx l 0 th
chn tng ng vi n c thit lp thnh ng vo. Ly v d: Khi ta set tt c 8 bit
ca thanh ghi DDRA u l 1, th 8 chn tng ng ca portA l PA1, PA2, PA7
(tng ng vi cc chn s 50, 49, 44 ca vi iu khin) c thit lp thnh ng ra.
Thanh ghi PORTx.
PORTx l thanh ghi 8 bit c th c ghi. y l thanh ghi d liu ca PORTx,
Nu thanh ghi DDRx thit lp cng l li ra, khi gi tr ca thanh ghi PORTx cng
l gi tr ca cc chn tng ng ca PORTx, ni cch khc, khi ta ghi mt gi tr
logic ln 1 bit ca thanh ghi ny th chn tng ng vi bit cng c cng mc
logic. Khi thanh ghi DDRx thit lp cng thnh li vo th thanh ghi PORTx ng vai
tr nh mt thanh ghi iu khin cng. C th , nu mt bit ca thanh ghi ny c
ghi thnh 1 th in tr treo ( pull-up resistor ) chn tng ng vi n s c kch
hot, ngc li nu bit c ghi thnh 0 th in tr treo chn tng ng s khng
c kch hot, cng trng thi cao tr ( Hi-Z ).

Thanh ghi PINx.


PINx khng phi l mt thanh ghi thc s, y l a ch trong b nh I/O kt
ni trc tip ti cc chn ca cng. Khi ta c PORTx tc ta c d liu c cht
trong PORTx, cn khi c PINx th gi tr logic hin thi chn ca cng tng ng
c c. V th i vi thanh ghi PINx ta ch c th c m khng th ghi. Bng 25
th hin cc cc thit lp cch hot c th c ca cng.

Bng cu hnh cho cc chn cng:


DDRxn PORTxn

PDU

I/O

Pull-up

Ch thch

Ng vo

Khng

Cao tr.

Ng vo

Nh 1 ngun dng.

SINH VIN V NH CHUNG

37

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


0

Ng vo

Khng

Cao tr.

Ng ra

Khng

Ng ra thp.

Ng ra

Ng ra cao.

Bng 5: Cu hnh cho cc chn cng.

3.1.3. B nh thi Atmega128.


ATmega128 c 4 b nh thi: b nh thi 1 v 3 l b nh thi 16 bit, b nh
thi 0 v 2 l b nh thi 8 bit.

Hnh 27: S khi b nh thi 16 bit.

3.1.3.1. Cc nh ngha.
Cc nh ngha sau s c s dng cho b nh thi 1 v 3 :
o BOTTOM : B m t ti gi tr BOTTOM khi n c gi tr 0000h
o MAX

: B m t ti gi tr MAX khi n bng FFFFh

SINH VIN V NH CHUNG

38

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


o TOP
: B m t gi tr TOP khi n bng vi gi tr cao nht trong chui
m, gi tr cao nht trong chui m khng nht thit l FFFFh m c th l bt k gi
tr no c quy nh trong thanh ghi OCRnX (X=A,B,C) hay ICRn, ty theo ch
thc thi.
3.1.3.2.

Cc thanh ghi b nh thi 1.

Thanh ghi TCCR1A (Timer/Counter1 Control Register)

Bit 7:6 COMnA1:0: Compare Output Mode for Channel A


Bit 5:4 COMnB1:0: Compare Output Mode for Channel B
Bit 3:2 COMnC1:0: Compare Output Mode for Channel C
Bit 1:0 WGMn1:0: Waveform Generation Mode
Bit 7:2 COMnX1:0 (X=A, B, C): Compare Output Mode for Channel X : iu
khin cch hot ng ca ng ra so snh (compare output) ca ln lt cc chn
OCnA, OCnB v OCnC. Nu mt hay c hai bit COMnA1:0 c set ln 1 th ng ra
OCnA s u tin hn chc nng port I/O thng thng m n kt ni ti . Nu mt
hay c hai bit COMnB1:0 c set ln 1 th ng ra OCnB s u tin hn chc nng
port I/O thng thng m n kt ni ti . Nu mt hay c hai bit COMnC1:0 c set
ln 1 th ng ra OCnC s u tin hn chc nng port I/O thng thng m n kt ni
ti, iu ny c ngha l mi mt chn ca vi iu khin c th thc hin nhiu chc
nng khc nhau, bnh thng cc chn OCnA, OCnB, OCnC hot ng nh cc chn
vo ra thng thng, nhng khi b nh thi ang hot ng cc ch c s
dng ti chc nng so snh khp (compare match) nh cc ch CTC, PWM,ca
b nh thi th hnh vi ca chn ng ra OCnA, OCnB, OCnC s do b nh thi iu
khin. Tuy nhin ch l bit ca thanh ghi DDR tng ng vi cc chn OCnA,
OCnB, OCnC phi c set cho php ng ra. Khi OCnA, OCnB, OCnC c kt
ni ti chn th tc dng ca cc bit COMnX1:0 cn ph thuc vo la chn ca cc
bit WGM3:0, ngha l khi ta set mt hay c hai Bit COMn1:0 ln 1 th chc nng ng
ra so snh c u tin, tuy nhin cch hot ng ng ra OCnX nh th no th cn
ph thuc vo vic la chn ca cc bit WGMn3:0, c th hin trong cc bng di
(Bng 4, 5, 6). Trong cc ch PWM, khi gi tr cc thanh ghi dng so snh
(OCRnX, ICRn) c gi tr bng vi TOP, th s kin so snh khp (compare match) b
b qua. Tuy vy cc chn OCnX vn b set hay xa (ty vo cc bit COMnX 1:0)
BOTTOM.
SINH VIN V NH CHUNG

39

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307

Bng 6: u ra so snh ch non-PWM.

Bng 7: u ra so snh ch fast-PWM.

Bng 8: u ra so snh ch PWM hiu chnh pha v tn s.

Bit 1:0 WGMn1:0: Waveform Generation Mode : Kt hp vi cc bit


WGMn3:2 tm trong thanh ghi TCCRnB , nhng bit ny cho php ta la chn ch
SINH VIN V NH CHUNG

40

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


thc thi ca b nh thi, nh c th iu khin vic m tun t ca b m. Gi
tr b m ln nht l TOP v dng sng to ra chn OCnX (X=A, B, C; n=1, 3)
c s dng cho nhiu mc ch khc nhau (bng 61). Cc ch thc thi c h
tr bi khi Timer/counter l : Normal mode ( counter ), Clear Timer on Compare
match (CTC) mode , PWM mode. l vi b nh thi 1 th c 4 bit WGM l:
WGM13, WGM12,WGM11 v WGM10.

Bng 9: La chn cc ch thc thi ca b nh thi 1.

Thanh ghi TCCR1B.

Bit 7 ICNCn: Input Capture Noise Canceler


Bit 6 ICESn: Input Capture Edge Select
Bit 5 Reserved Bit
Bit 4:3 WGMn3:2: Waveform Generation Mode
Bit 2:0 CSn2:0: Clock Select

SINH VIN V NH CHUNG

41

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


Bit 7 ICNCn: Input Capture Noise Canceler (vit tt: ICNC): Vic set bit ny
ti 1 s kch hot chc nng chng nhiu ca b chng nhiu li vo ( ICNC ). Khi
chc nng ICNC c kch hot th ng vo t chn ICPn s c lc. Chc nng lc
i hi 4 mu c gi tr bng nhau lin tip chn ICPn cho s thay i ng ra ca n
( xem chi tit v khi Input Capture ).
Bit 6 ICESn: Input Capture Edge Select: Bit ny la chn cnh chn Input
Capture Pin (ICPn) dng bt s kin trigger ( Trigger event (10)). Khi bit ICESn
c thit lp thnh 0 th mt cnh dng xung ( falling (3)) c dng nh mt
trigger. Ngc li, khi bit ny c set thnh 1 th mt cnh m ln (rising (4) ) c
dng nh mt trigger. Khi xy ra s kin Input capture (2) (theo thit lp ca bit
ICESn l 1 hay 0) th gi tr ca b m c ghi vo thanh ghi Input Capture
Register ICRn (n=1, 3), v khi c ICFn (Input Capture Flag) c set. iu ny s
to ra mt ngt Input capture nu ngt ny c cho php. Khi thanh ghi ICRn c
s dng nh mt gi tr TOP th chn ICPn khng c kt ni v v th chc nng
Input capture khng c cho php.
Bit 5 : D tr.
Bit 4:3-WGMn3:2: Waveform Generation Mode: Xem phn thanh ghi
TCCR1A.
Bit 2:0 CSn2:0: Clock Select : Dng la chn tc xung clock (bng 8).
cm b nh thi hot ng ta ch cn cho {CSn2, CSn1, CSn0} = {0, 0, 0}.

Bng 10: La chn tc xung clock.

SINH VIN V NH CHUNG

42

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


Thanh ghi TCCR1C.

Bit 7 FOCnA: Force Output Compare for Channel A.


Bit 6 FOCnB: Force Output Compare for Channel B.
Bit 5 FOCnC: Force Output Compare for Channel C.
Bit 4:0 Reserved Bits.
Cc bit FOCnA/FOCnB/FOCnC ch hot ng khi cc bit WGMn3:0 ch nh
ch Non-PWM. Khi cc bit FOCnA/FFOCnB/FOCnC c set thnh 1 th ngay lp
tc mt s kin So snh khp cng ch (Forced Compare Match (1)) xy ra trong
b to sng. Ng ra OCnA/OCnB/OCnC c thay i theo thit lp ca cc bit
COMnX 1:0 (n=1, 3; X=A, B, C), ngha l bnh thng s kin so snh khp ch
xy ra khi khi gi tr b nh thi (thanh ghi TCNTn (n=1, 3) ) bng vi gi tr thanh
ghi OCRnX( n=1,3; X=A,B,C), nhng khi cc bit FOCnX( n=1, 3; X=A, B, C) c
set thnh 1 th s kin so snh khp s xy ra mc d gi tr ca b nh thi khng
bng vi gi tr ca thanh ghi OCRnX( n=1,3; X=A,B,C). Ch l cc bit
FOCnA/FOCnB/FOCnC cng hot ng nh l nhng que d (strobe), v th n l gi
tr hin thi ca cc bit COMnX1:0 xc nh tc ng ca so snh cng ch (forced
compare). Cc que d FOCnA/FOCnB/FOCnC khng to ra bt k ngt no v cng
khng xa b nh thi trong ch CTC s dng thanh ghi OCRnA nh l gi tr
TOP. Cc bit FOCnA/FOCnB/FOCnC ch c th ghi, khi c cc bit ny ta lun nhn
c gi tr 0. Bit 4:0 d tr, phi ghi thnh 0 khi ghi vo thanh ghi TCCRnC.
3.1.4. Cu trc ngt ca ATmega128.
3.1.4.1. Khi nim v ngt.
Ngt l mt s kin bn trong hay bn ngoi lm ngt b vi iu khin bo
cho n bit rng thit b cn dch v ca n. Mt b vi iu khin c th phc v mt
vi thit b, c hai cch thc hin iu ny l s dng cc ngt (interrupt) v
thm d (polling). Trong phng php s dng cc ngt th mi khi c mt thit b bt
k cn n dch v ca n th n bo cho b vi iu khin bng cch gi mt tn hiu
ngt. Khi nhn c tn hiu ngt th b vi iu khin ngt tt c nhng g n ang
thc hin chuyn sang phc v thit b. Chng trnh i cng vi ngt c gi l
SINH VIN V NH CHUNG

43

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


trnh dch v ngt ISR (Interrupt Service Routine) hay cn gi l trnh qun l ngt
(Interrupt handler). Cn trong phng php thm d th b vi iu khin hin th lin
tc tnh trng ca mt thit b cho v iu kin tho mn th n phc v thit b. Sau
n chuyn sang hin th tnh trng ca thit b k tip cho n khi tt c u c
phc v. Mc d phng php thm d c th th hin th tnh trng ca mt vi thit
b v phc v mi thit b khi cc iu kin nht nh c tho mn nhng n khng
tn dng ht cng dng ca b vi iu khin. im mnh ca phng php ngt l b
vi iu khin c th phc v c rt nhiu thit b (tt nhin l khng ti cng mt
thi im). Mi thit b c th nhn c s ch ca b vi iu khin da trn mc
u tin c gn cho n. i vi phng php thm d th khng th gn mc u tin
cho cc thit b v n kim tra tt c mi thit b theo kiu quay vng . Quan trng hn
l trong phng php ngt th b vi iu khin cng cn c th che hoc lm l mt
yu cu dch v ca thit b. iu ny li mt ln na khng th thc hin c trong
phng php thm d. L do quan trng nht m phng php ngt c u chung
nht l v phng php thm d lm hao ph thi gian ca b vi iu khin bng cch
hi d tng thit b k c khi chng khng cn n dch v.
3.1.4.2. Trnh phc v ngt v bng vector ngt.
i vi mi ngt th phi c mt trnh phc v ngt ISR (Interrupt Service
Routine) hay trnh qun l ngt (Interrupt handler). Khi mt ngt c gi th b vi
iu khin phc v ngt. Khi mt ngt c gi th b vi iu khin chy trnh phc
v ngt. i vi mi ngt th c mt v tr c nh trong b nh gi a ch ISR ca
n. Nhm cc v tr nh c dnh ring gi cc a ch ca cc ISR c gi l
bng vector ngt.
Khi kch hot mt ngt b vi iu khin i qua cc bc sau:
Vi iu khin kt thc lnh ang thc hin v lu a ch ca lnh k tip (PC)
vo ngn xp.
N nhy n mt v tr c nh trong b nh c gi l bng vc t ngt ni lu
gi a ch ca mt trnh phc v ngt.
B vi iu khin nhn a ch ISR t bng vc t ngt v nhy ti . N bt u
thc hin trnh phc v ngt cho n lnh cui cng ca ISR l RETI (tr v t ngt).
Khi thc hin lnh RETI b vi iu khin quay tr v ni n b ngt. Trc
ht n nhn a ch ca b m chng trnh PC t ngn xp bng cch ko hai byte
trn nh ca ngn xp vo PC. Sau bt u thc hin cc lnh t a ch .

SINH VIN V NH CHUNG

44

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


Bng vector ngt:

Bng 11: Bng vector ngt ca ATmega128.


SINH VIN V NH CHUNG

45

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


3.1.4.3. Th t u tin ngt.
Khng nh vi iu khin h 8051, th t u tin ca cc ngt c th thay
i c ( bng cch lp trnh ). Vi vi iu khin AVR th t u tin cc ngt l
khng th thay i v theo qui tc: Mt vec t ngt c a ch thp hn trong b nh
chng trnh c mc u tin cao hn . Chn hn ngt ngoi 0 ( INT0 ) c mc
u tin cao hn ngt ngoi 1 ( INT1 ). cho php mt ngt ngi dng cn cho php
ngt ton cc ( set bit I trong thanh ghi SREG ) v cc bit iu khin ngt tng ng.
Khi mt ngt xy ra v ang c phc v th bit I trong thanh ghi SREG b xa, nh
th khi c mt ngt khc xy ra n s khng c phc v, do cho php cc ngt
trong khi mt ISR ( interrupt service routine ) khc ang thc thi, th trong chng
trnh ISR phi c lnh SEI set li bit I trong SREG.
3.1.4.4. Ngt lng nhau.
Khi AVR ang thc hin mt trnh phc v ngt thuc mt ngt no th li c mt
ngt khc c kch hot. Trong nhng trng hp nh vy th mt ngt c mc u
tin cao hn c th ngt mt ngt c mc u tin thp hn. Lc ny ISR ca ngt c
mc u tin cao hn s c thc thi. Khi thc hin xong ISR ca ngt c mc u tin
cao hn th n mi quay li phc v tip ISR ca ngt c mc u tin thp hn trc
khi tr v chng trnh chnh. y gi l ngt trong ngt ( hnh 24 ).

Hnh 28: Cc ngt lng nhau.

3.1.4.5. Ngt ngoi Atmega128.


ATmega128 c 8 ngt ngoi t INT0 n INT7 ( y cha k ti ngt reset ).
Tm ngt ny tng ng vi 8 chn ca MCU l INT0 ,INT1, , INT7. l ngay
c khi cc chn INT0, INT1, , INT7 ca MCU c cu hnh nh l chn li ra, th
cc ngt ngoi vn c tc dng nu c cho php. Cc ngt ngoi c th bt mu theo
SINH VIN V NH CHUNG

46

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


kiu cnh ln ( Rising ), cnh xung ( Falling ) hay mc thp ( Low level ). iu ny
c qui nh trong hai thanh ghi EICRA v EICRB.

Bng 12: iu khin kiu bt mu ngt.

3.1.5. B truyn nhn d liu ni tip USART.


USART (Universal Synchronous and Asynchronous serial Receiver and
Transmitter): B Truyn Nhn Ni Tip ng B V Bt ng B Ph Dng, y l
khi chc nng dng cho vic truyn thng gia vi iu khin vi cc thit b khc.
Trong vn truyn d liu s, c th phn chia cch thc (method) truyn d liu ra
hai ch (mode) c bn l : Ch truyn nhn ng b (Synchronous) v Ch
truyn nhn bt ng b (Asynchronous). Ngoi ra, nu gc phn cng th c th
phn chia theo cch khc l: Truyn nhn d liu theo kiu Ni tip (serial) v
Song song (paralell).
Truyn ng B: l kiu truyn d liu trong b truyn (Transmitter) v b
nhn (Receiver) s dng chung mt xung ng h (clock). Do , hot ng truyn v
nhn d liu din ra ng thi. Xung clock ng vai tr l tn hiu ng b cho h
thng (gm khi truyn v khi nhn). u im ca kiu truyn ng b l tc
nhanh, thch hp khi truyn d liu khi (block).
Truyn Bt ng B: L kiu truyn d liu trong mi b truyn
(Transmitter) v b nhn (Receiver) c b to xung clock ring, tc xung clock
hai khi ny c th khc nhau, nhng thng khng qu 10%. Do khng dng chung
xung clock, nn ng b qu trnh truyn v nhn d liu, ngi ta phi truyn cc
bit ng b (Start, Stop,) i km vi cc bit d liu. Cc b truyn v b nhn s
da vo cc bit ng b ny quyt nh khi no th s thc hin hay kt thc qu
trnh truyn hoc nhn d liu. Do , h thng truyn khng b cn c gi l h
thng truyn t ng b. T hai kiu truyn d liu c bn trn, ngi ta a ra
nhiu giao thc (Protocol) truyn khc nhau nh: SPI (ng b), USRT (ng b),
UART (bt ng b),Tuy vy, cng c giao thc truyn m khng th xp c vo
kiu no: ng b hay bt ng b, chng hn kiu truyn I2C (Trong AVR gi l
SINH VIN V NH CHUNG

47

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


TWI), tuy vy mt cch hi gng p th c th thy giao thc truyn I2C gn vi
kiu ng b hn v cc thit b giao tip vi nhau theo chun I2C iu dng chung
mt xung clock.
3.1.5.1. B truyn nhn ni tip ATmega128.
ATmega128 c hai b USART l USART0 v USART1. Hai b USART ny l
c lp nhau, iu ny c ngha l hai khi USART0 v USART1 c th hot ng
cng mt lc. S khi n gin ca khi USART th hin trong hnh 25.

Hnh 29: S khi b USART.

S khi ca b USART phn chia thnh ba phn r rng: Khi to xung clock
(Clock Generator), Khi truyn (Transmitter) v Khi nhn (Receiver). Cn cc thanh
ghi iu khin USART c dng chung.
Khi to xung clock logic bao gm logic ng b cho u vo ng h bn
ngoi s dng bi hot ng ca mt slave ng b v b pht tc truyn. Chn
XCK (Transfer Clock) ch c s dng trong trong ch truyn ng b.
Khi truyn bao gm mt b m ghi, mt thanh ghi dch ni tip, b pht kim
tra li chn l v iu khin logic x l cc nh dng khung ni tip khc nhau. B
m ghi cho php truyn d liu lin tc m khng cn bt k khong thi gian tr
gia cc khung.
Khi nhn l phn phc tp nht ca module USART bi ng h ca n v
cc thit b phc hi d liu. Cc thit b phc hi d liu c s dng nhn cc
SINH VIN V NH CHUNG

48

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


d liu khng ng b. Noi vic thm vo cc thit b phc hi d liu, b nhn cn
bao gm mt kim tra chn l, iu khin logic, mt thanh ghi dch, v mt b m
nhn hai mc (UDR). B nhn h tr cc nh dng khung truyn ging nhau nh b
truyn, v c th pht hin khung truyn b li, trn d liu v cc li chn l.

Hnh 30: S khi v cc mc logic trong khi pht xung ng h.

M t cc tn hiu:
Txclk: ng h truyn (Tn hiu ni).
Rxclk: ng h nhn (Tn hiu ni).
Xcki: u vo t chn XCK (Tn hiu ni). S dng cho slave ng b hot
ng.
Xcko: ng h u ra t chn XCK (Tn hiu ni). S dng cho master ng
b hot ng.
Fosc: Tn s chn XTAL (ng h h thng).

Bng 13: Phng trnh tnh ton ci t thanh ghi tc truyn.

SINH VIN V NH CHUNG

49

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


3.2.

Chip thi gian thc DS1307.

3.2.1. Cc tnh nng.


Thi gian thc m gi, pht, giy, ngy, thng, nm.

56 bytes non-volatile RAM cho vic lu tr d liu, 2 dy giao din ni tip.

C th lp trnh tn hiu u ra.


Tiu th t hn 500nA trong ch pin d phng vi vic chy b dao ng.
Ty chn phm vi di nhit t -40C ti 85C.
C sn trong 8 chn DIP hoc SOIC.

3.2.2. M t.
DS1307 l chip ng h thi gian thc, khi nim thi gian thc y dng vi
ngha l thi gian tuyt i m con ngi ang s dng, tnh bng giy, pht,
gi.DS1307 gm 7 thanh ghi 8 bits cha thi gian l: giy, pht, gi, th (trong
tun), ngy, thng, nm. Ngoi ra, DS1307 cn c mt thanh ghi iu khin ng ra ph
v 56 thanh ghi trng c th dng nh RAM. DS1307 c c v ghi thng qua giao
din ni tip I2C nn cu to bn ngoi rt n gin. DS1307 xut hin 2 gi SOIC
v DIP c 8 chn nh hnh di:

Hnh 31: Cu to bn ngoi DS1307.

M t cc chn chip DS1307.

SINH VIN V NH CHUNG

50

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


X1, X2 : L 2 ng kt ni vi thch anh 32.768 KHz lm ngun to dao ng
cho chip.
VBAT: Cc dng ca mt ngun pin 3V nui Chip.
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 nu Vcc khng c cp ngun nhng VBAT c cp th DS11307 vn 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, nn b trng chn ny khi ni mch.
SCL v SDA l 2 ng giao xung nhp v d liu ca giao din I2C.
S khi trong hnh di y m t nhng thnh phn chnh ca DS1307:

SINH VIN V NH CHUNG

51

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307

Hnh 32: S khi DS1307.

Ta c th kt ni DS1307 bng mt mch in nh sau:

Hnh 33: Mch kt ni DS1307.

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
cn hiu r 2 vn c bn l cu trc cc thanh ghi v cch truy xut cc thanh
ghi ny thng qua giao din I2C.
B nh DS1307 c tt c 64 thanh ghi 8 bits uc nh a ch t 0x00 ti
0x3F. Tuy nhin, thc cht ch c 8 thanh ghi u l c chc nng ng h, cn li
56 thanh ghi b trng c th c dng cha bin tm nh RAM nu mun. 7 thanh
ghi u tin cha thng tin v thi gian ca ng h gm:
Giy (Secons), pht (Minutes), 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 DS1307. Vic c gi tr t 7 thanh ghi l c thi gian
thc m chip to ra. Thanh ghi th 8 (Control) l thanh ghi iu khin xung ng ra
SQW/OUT(chn 6). T chc b nh ca DS1307 c trnh by nh hnh di:

SINH VIN V NH CHUNG

52

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307

Hnh 34: Cu trc b nh DS1307.

V 7 thanh ghi u tin l quan trng nht trong hot ng ca DS1307 nn


chng ta s kho st cc thanh ghi ny mt cch chi tit. T chc theo tng bit ca cc
thanh ghi nh sau:

Hnh 35: Cu trc thanh ghi DS1307.

Thanh ghi giy (SECONDS): Thanh ghi ny l thanh ghi u tin trong b
nh ca DS1307, a ch ca n l 0x00. 4 bits thp ca thanh ghi ny cha m BCD 4
bits ca ch s hng n v ca gi tr giy. Do gi tr cao nht ca ch s chng chc
l 5 nn ch cn 3 bit(SECONDS64) l c th m ha c(5=101). Bit cao nht,
bit 7 trong thanh ghi ny l 1bit iu khin c tn CH (Clock halt-treo ng h), nu
bit ny c set bng 1 th 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 0x01, cha gi tr pht ca ng
h.Tng t nh thanh ghi SECONDS, ch c 7 bit ca thanh ghi ny c dng lu
m BCD ca pht, bit 7 lun =0.
SINH VIN V NH CHUNG

53

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307


Thanh ghi gi (HOURS): C th ni y l thanh ghi phc tp nht trong
DS1307.Thanh ghi ny c a ch 0x02. Bn bit thp ca thanh ghi ny dng cho ch
s hng n v ca gi. Do DS1307 h tr 2 loi mode hin th gi l 12h v 24h, bit
th 6 trong thanh ghi gi dng xc lp mode gi. Nu bit 6=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 gi ln nht trong mode ny l 2 (bng 10 h nh phn). Nu bit 6=1, th mode
12h c la chn, trong trng hp ny ch c bit 4 dng m ha ch s hng
chc ca gi tr gi, bit 5 dng ch bui trong ngy AM hoc PM. Bit 5=0 l AM v
ngc li. Bit 7 lun =0.
Thanh ghi th (DAY): Nm a ch 0x03. 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 c
ngha.
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(t 0 n 99). Ch ,
DS1307 ch dng cho 100 nm, nn gi tr nm ch c 2 ch s cn phn u ca nm
do ngi dng t thm vo(20xx).

Ngoi ra, 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 ca thanh
ghi ny l a ch ca thanh ghi trong b nh m ngi dng mun truy cp. Gi tr
ca thanh ghi a ch c set trong lnh Write.

V DS1307 l mt Slave I2C nn ch c 2 mode hot ng giao tip vi chip


ny. 2 mode ca DS1307 bao gm Data Write v Data Read. Mode Data Write c
dng khi xc lp gi tr ban u cho cc thanh ghi thi gian hoc dng canh chnh
thi gian. Trong ch ny, AVR l mt Master truyn d liu n DS1307 (Slave
nhn d liu). Mode Data Read c s dng khi c thi gian t ng h DS1307
vo AVR hin th hoc so snh. Trong ch ny, AVR l 1 Master nhn d liu
v DS1307 l Slave truyn d liu.

SINH VIN V NH CHUNG

54

LP: T & THCN K49

CHNG III: KHO ST VI IU KHIN ATMEGA128 DS1307

Hnh sau y m t cu trc d liu trong ch Data Write:

Ch Data Read c m t nh sau:

SINH VIN V NH CHUNG

55

LP: T & THCN K49

CHNG IV: THIT K V THI CNG

CHNG IV
THIT K V THI CNG
4.1. Phng n thit k.
H thng c thit k gm 2 khi chnh:
Khi x l ti trung tm: S dng module GSM Sim900 kt ni vi my tnh
nhn d liu v hin th trn phn mm qun l, truyn d liu iu khin.
Khi x l gn trn phng tin: S dng module Sim908 kt hp vi vi iu
khin xc nh v tr, vn tc v trng thi ca phng tin, gi d liu v mch
trung tm, ng thi nhn v x l d liu trung tm gi n.
Thit k giao din qun l trn phn mm C#.
4.2.

S khi tng qut ca ton h thng v chc nng tng khi.

4.2.1. S tng qut h thng.

Hnh 36: S khi tng qut h thng qun l gim st phng tin c nhn.

SINH VIN V NH CHUNG

55

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


4.2.2. Chc nng tng khi.
Khi giao tip SMS
C chc nng gi nhn tin nhn SMS cho vic iu khin cc thit b. Khi ny
cn hai thit b truyn nhn thng tin :
Thit b truyn nhn d liu GSM Sim900: dnh cho mch x l trung tm.
c kt ni vi my tnh qua cng truyn thng RS232. Nhn d liu t mch gn
trn thit b, hin th thng tin trn giao din qun l, ng thi iu khin mch gn
trn thit b.
Thit b truyn nhn d liu GPS/GSM Sim908: Gn trn mch lp trn phng
tin. Lin tc cp nht d liu GPS v kim tra thng tin iu khin thc hin cc
chc nng quy nh. Gi d liu v mch trung tm khi pht hin yu cu hoc c s
c xy ra.
Khi x l phn mm v giao tip.
C chc nng c tin nhn SMS t module GSM. Khi ny c nhim v trung
tm v quan trng trong vic x l v iu khin phn mm. Vic x l s do phn
mm c to ra bi phn mm Visual Basic.Net ca Microsoft . Phn mm ny c
nhim v giao tip vi module GSM v giao tip vi Khi x l phn cng thng qua
cng RS232.
Khi model nhn c tin nhn iu khin th phn mm s c tin nhn thng
qua vic gi tp lnh AT c tin nhn. Sau s x l lnh iu khin v truyn d
liu bt tt thit b thng qua vic truyn tin nhn SMS. Trong phn mm giao tip cn
t thuc tnh cho vic chn cng kt ni vi Modem Sim900 v RS232 . Phn mm
cn cho php hin th v tr thit b trn bn , tc ca thit b. Khi ngi dng
mun gim st thit b thng qua in thoi th phn mm cng cho php gi tin nhn
SMS n ngi dng thng bo cc trng thi thit b. Nu xy ra s c th phn
mm s t ng gi tin nhn n cc c quan c thm quyn.
Khi x l phn cng.
Khi x l phn cng l khi trung tm trong vic x l v iu khin phn
cng. Khi ch do mt vi iu khin ATmega128 m nhn v c nhim v gi nhn
d liu vi khi x l phn mm mt cch lin tc. Khi Khi x l phn mm gi
thng tin iu khin th Khi x l phn cng s c lp trnh thc thi thng qua
iu khin cc chn cng. Vi iu khin c nhim v c thng tin t cm bin, giao
tip vi khi LCD- Keypad v Khi cng sut.
SINH VIN V NH CHUNG

56

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


Khi cm bin.
S dng cm bin chuyn ng PIR pht hin s dch chuyn ca thit b. L
tn hiu khi x l pht tn hiu cnh bo ti trung tm.
Khi giao tip LCD v bn phm.
Khi ny bao gm mt LCD 16x2 v mt bn phm ma trn c nhim v hin th
d liu v ci t thng tin thi gian.
Khi cng sut.
Khi ny bao gm Moc3020 v Triac BT136 c nhim v giao tip gia mc
logic TTL vi in p xoay chiu 220VAC thng qua vic ng ngt cc role. Khi
cng sut vi dng thp, p ng ti tiu th cng sut thp v dn dng nh bng
n, cc thit b bo hiu.
4.3. Thit k phn cng.
4.3.1. S nguyn l.
S nguyn l khi ngun h thng.

Hnh 37: S nguyn l khi ngun h thng.

SINH VIN V NH CHUNG

57

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


S nguyn l mch x l trung tm:

Hnh 38: S nguyn l mch x l trung tm.

S nguyn l mch gn trn thit b.


Mch sim 908.

Hnh 39: S nguyn l mch Sim908.

SINH VIN V NH CHUNG

58

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


Mch iu khin chnh.

Hnh 40: S nguyn l mch iu khin chnh.

4.3.2. Thit k mch in.


Mch x l trung tm

Hnh 41: Mch x l trung tm.

SINH VIN V NH CHUNG

59

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


Mch Sim908.

Hnh 42: Mch Sim908.

Mch iu khin chnh.

Hnh 43: Mch iu khin chnh.

SINH VIN V NH CHUNG

60

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


4.4. Thit k chng trnh phn mm.
4.4.1. Lu thut ton chng trnh.
Lu thut ton chng trnh chnh.
BEGIN

Khi to d liu
truyn USART

Kim tra cng


COM GPS?

Kim tra cng


COM GSM?

Bo nhn chui d liu

Bo c tin nhn mi

Nhn chui d liu

Nhn tin nhn

X l chui d liu, tch


ly thng tin

X l tin nhn v iu
khin thit b

Hnh 44: Lu thut ton chng trnh chnh.

SINH VIN V NH CHUNG

61

LP: T & THCN K49

CHNG IV: THIT K V THI CNG

Lu thut ton x l tin nhn.


X l tin nhn

Ly ni dung tin nhn


v s in thoi gi

Kim tra
mt khu?

Bo sai mt khu

Kim tra c
php?

Bo sai c php

iu khin thit b

Kt thc
Hnh 45: Lu thut ton x l tin nhn.

x l tin nhn, trc tin vi iu khin thc hin tch ly phn ni dung ca
tin nhn v s in thoi gi tin nhn ti. Sau vi iu khin s tin hnh phn
tch ni dung lnh iu khin. Trc tin l kim tra mt khu, nu mt khu sai vi
iu khin s thc hin gi tin nhn bo sai mt khu cho thu bao gi tin nhn iu
khin. Nu mt khu l ng, s kim tra tip c php iu khin, nu c php ng, vi
iu khin s tin hnh iu khin thit b, nu khng s thc hin gi tin nhn bo
sai c php cho thu bao gi tin nhn iu khin.

SINH VIN V NH CHUNG

62

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


Lu chng trnh nhn d liu.
NHN
D LIU

Thit lp b nh
lu d liu

C d liu
n?

Nhn k t

S
K t kt
thc?

Lu k t
Tng a ch lu

Kt thc
Hnh 46: Lu thut ton nhn d liu.

Lu chng trnh truyn d liu.


Gi d liu

ang bn
gi?
S

S
Gi k t, Tng ln
k t tip theo.

K t cui?

Kt thc

Hnh 47: Lu thut ton gi d liu.

SINH VIN V NH CHUNG

63

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


Lu chng trnh ly d liu.
Ly d liu

K t u s
in thoi?

K t u
d liu?

Ly a ch u d liu

Ly k t u s in thoi.
Tng ln n k t tip
theo

Tng bin m k t tip theo

a ch cui
d liu?

Kt thc
Hnh 48: Lu chng trnh ly d liu.

Khi nhn c ni dung tin nhn t trung tm. x l iu khin, vi iu khin


phi xc nh cc vng d liu c ch cho mc ch iu khin, gm vng d liu cha
s in thoi gi tin nhn iu khin v vng nh cha ni dung iu khin.
Chng trnh con ly a ch lm nhim v ly cc a ch bt u ca vng nh lu
s in thoi, a ch bt u v kt thc ca vng nh lu ni dung iu khin.
Lu chng trnh iu khin thit b.
M thit b
M chn iu khin thit b

Thit b m?

Kt thc
Hnh 49: Lu chng trnh con iu khin m thit b.

SINH VIN V NH CHUNG

64

LP: T & THCN K49

CHNG IV: THIT K V THI CNG

Tt thit b

Tt chn iu khin thit b


S

Thit b tt?

Kt thc

Hnh 50: Lu chng trnh con iu khin tt thit b.

Lu chng trnh gi tin nhn.


GI
TIN NHN

Gi chui: AT+CMGS=
Gi S in thoi

Ch module sim
gi k t >

1
Gi ni dung

Gi k t Ctrl+Z

Kt thc
Hnh 51: Lu thut ton gi tin nhn.

SINH VIN V NH CHUNG

65

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


4.5. Phn mm giao din qun l.
4.5.1. Gii thiu phn mm Visual Studio ca Microsoft.
Microsoft Visual Studio : l mi trng pht trin tch hp chnh (Integrated
Development Environment (IDE)) c pht trin t Microsoft. y l mt loi phn
mm my tnh c cng dng gip cc lp trnh vin trong vic pht trin phn
mm.
Cc mi trng pht trin hp nht thng bao gm:
Mt trnh son tho m (source code editor) : dng vit m.
Trnh bin dch (Compiler) v/ hoc trnh thng dch (Interpreter).
Cng c xy dng t ng: khi s dng s bin dch(hoc thng dch) m ngun,
thc hin lin kt (linking) , v c th chy chng trnh mt cch t ng.

Trnh g li (debugger) : h tr d tm li.

Ngoi ra, cn c th bao gm h thng qun l phin bn v cc cng c nhm


n gin ha cng vic xy dng giao din ngi dng ha (GUI).
Nhiu mi trng pht trin hp nht hin i cn tch hp trnh duyt lp (class
browser), trnh qun l i tng (object inspector), lc phn cp lp (class
hierarchy diagram), s dng trong vic pht trin phn mm theo hng i
tng.
Nh vy, Microsoft Visual Studio c dng pht trin Console (thit b u
cui- bn giao tip ngi my) v GUI (giao din ngi dng ha) cng vi cc
trnh ng dng Window Forms, cc website, cng nh ng dng, dch v web (web
applications and web services) . Chng c pht trin da trn m ngn ng gc
(native code) cng nh m c qun l (managed code) cho cc nn tng c h tr
Microsoft Windows, Windows Mobile, .Net Framework, .Net Compact Framework v
Microsoft Silverlight. Visual Studio h tr rt nhiu ngn ng lp trnh, c th k tn
nh sau: C/C++ ( Visual C++), VB.NET (Visual Basic .NET), v C# (Visual C#)
cng nh h tr cc ngn ng khc nh F#, Python, v Ruby; ngoi ra cn h tr c
XML/XSLT, HTML/XHTML, JavaScript v CSS
4.5.2.Xy dng giao din truyn thng ni tip RS232 dng Visual Studio.
Vn truyn thng giao tip gia vi iu khin vi my tnh khng cn xa l
trong cc ng dng qun l v gim st c thc hin bi vi iu khin. N gip
cho ngi qun l d dng bit c nhng thng tin cn thit ch thng qua mn
SINH VIN V NH CHUNG

66

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


hnh my tnh. Ngoi mt s chng trnh c tch hp sn trn windows nh
hyperterminal, Terminal th ngi dng cn c th t to cho mnh mt giao din
truyn thng ni tip giao tip vi vi iu khin thng qua chng trnh Microsoft
Visual Studio. Cc bc xy dng chng trnh nh sau:

To 1 project mi. Vo File/New/Project.

Hnh 52: To project mi trn C#.

Chn Windows Forms Application v t tn cho project ti khung Name. Sau


mt mn hnh giao din lp trnh Form xut hin.

SINH VIN V NH CHUNG

67

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


Hnh 53: La chn khun mu cho project.

Tip n xy dng giao din nh hnh di:

Hnh 54: Thit k giao din.

Cu trc form bao gm: Cc button (Kt Ni, Ngt, Xa, Thot), cc TextBox, v
cc ComboBox (COM, BaudRate, Data Bits, Parity, Stop Bit).
Toolbox cha cc Controls. y s
dng Common Controls.
Cc Button to nt nhn.

To danh sch cha cc Texbox.

Textbox cha cc vn bn, dng


hin th d liu truyn v nhn.

SINH VIN V NH CHUNG

68

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


t tn cho cc thuc tnh.
Thay i cc thuc tnh ca i
tng.
Tn hin th ca i tng.

t tn cho i tng (thun tin cho


vic lp trnh).

Phn tip theo l vit code cho project.

Sau khi hon thnh vic thit k giao din v t tn cho cc thuc tnh, ta tin
hnh vit code cho project. vo mi trng vit code kch phi vo giao din, chn
ViewCode.
Ngoi cc th vin sn c, ta thm cc th vin sau thc hin cc chc nng
SerialPost.
Using System.IO.
Using System.IO.Ports.
Using System.xml.
Trong namespace ta vit on code sau:
SINH VIN V NH CHUNG

69

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


namespace Truyen_Thong_RS232
{
public partial class Form1 : Form
{
SerialPort P = new SerialPort(); // Khai bo 1 Object SerialPort mi.
string InputData = String.Empty; // Khai bo string buff dng cho hin th d liu
sau ny.
delegate void SetTextCallback(string text); // Khai bo delegate SetTextCallBack
vi tham s string.
public Form1()
{
InitializeComponent();
// Ci t cc thng s cho COM
// Mng string port cha tt c cc cng COM ang c trn my tnh.
string[] ports = SerialPort.GetPortNames();
// Thm ton b cc COM tm c vo combobox cbCom.
cbCom.Items.AddRange(ports); // S dng AddRange thay v dng foreach.
P.ReadTimeout = 1000;
// Khai bo hm delegate bng phng thc DataReceived ca Object SerialPort;
// Ci ny khi c s kin nhn d liu s nhy n phng thc DataReceive
P.DataReceived += new SerialDataReceivedEventHandler(DataReceive);
// Ci t cho BaudRate
string[] BaudRate = { "1200", "2400", "4800", "9600", "19200", "38400",
"57600", "115200" };
cbRate.Items.AddRange(BaudRate);
// Ci t cho DataBits
SINH VIN V NH CHUNG

70

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


string[] Databits = { "6", "7", "8" };
cbBits.Items.AddRange(Databits);
//Ci t cho Parity
string[] Parity = { "None", "Odd", "Even" };
cbParity.Items.AddRange(Parity);
//Ci t cho Stop bit
string[] stopbit = { "1", "1.5", "2" };
cbBit.Items.AddRange(stopbit);
}
private void btSend_Click(object sender, EventArgs e)
{
if (P.IsOpen)
{
if (txtSend.Text == "") MessageBox.Show("Cha c d liu!","Thng Bo");
else P.Write(txtSend.Text);
}
else MessageBox.Show("COM cha m.", "Thng bo",MessageBoxButtons.OK,
MessageBoxIcon.Error);
txtSend.Clear();
}

Cng vic khi to hon tt, cn by gi l cng vic quan trng nht.
chnh l xy dng cc hm, th tc cho vic nhn v truyn d liu qua COM.
// Hm ny c s kin nhn d liu gi n. Mc ch hin th.
private void DataReceive(object obj, SerialDataReceivedEventArgs e)
{
InputData = P.ReadExisting();
SINH VIN V NH CHUNG

71

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


if (InputData != String.Empty)
{
SetText(InputData); // Chnh v vy phi s dng y quyn ti y. Gi
delegate khai bo trc .
}
}
private void SetText(string text)
{
if (this.txtkq.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText); // khi to 1 delegate
mi gi n SetText.
this.Invoke(d, new object[] { text });
}
else this.txtkq.Text += text;
}

n y l phn nhn xong, tip n phn gi d liu i, nhp p vo


button SEND ly s kin ri vit hm theo bn di:
// n hm gi data xung COM
private void btSend_Click(object sender, EventArgs e)
{
if (P.IsOpen)
{
if (txtSend.Text == "") MessageBox.Show("Cha c d liu!","Thng Bo");
else P.Write(txtSend.Text);
}
SINH VIN V NH CHUNG

72

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


else MessageBox.Show("COM cha m.", "Thng bo", MessageBoxButtons.OK,
MessageBoxIcon.Error);
txtSend.Clear();
}
4.5.3. Thit k giao din qun l.
Giao din qun l c xy dng nhm mc ch nhn d liu t mch trung tm
thng qua cng truyn thng RS232 v hin th d liu bao gm v tr, tc , thi
gian trn mn hnh qun l. Thng qua ng dng MapControl ta c th ly d liu bn
t Google v hin th chnh xc v tr ca phng tin nh ta c gi v t
mch gn trn thit b. vic ny s gip qun l, gim st phng tin mt cch trc
quan nht.

Hnh 55: Giao din qun l, gim st.

Do d liu v tinh gi v l d liu th, cha th s dng ngay do ta cn tch


d liu bao gm kinh , v v vn tc x l. on code sau s thc hin vic
tch chui d liu v thc hin x l d liu:
+CMT:"+841226387399","","13/03/19,17:17:14+28"17"17"14"21.03333"105.464"30
string[] mang = inputdata.Split(new char[] {'"'}); // Dng hm Split ct
chui, gn d liu vo mng.
lat = double.Parse(mang[3]);
SINH VIN V NH CHUNG

73

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


txtlong.Text = lat.ToString();
lon = double.Parse(mang[4]);
vantoc = double.Parse(mang[5]);
lat = lat / 100;
string[] lat1 = lat.ToString().Split('.');
double dlat1 = lat - Convert.ToDouble(lat1[0]);
dlat1 = dlat1 * 100;
double lat2 = (dlat1 * 10000000) / 60;
string[] lat3 = lat2.ToString().Split('.');
double dlat2 = Convert.ToDouble(lat3[0]);
dlat2 = dlat2 / 10000000;
vido = Convert.ToDouble(lat1[0]) + dlat2;
lon = lon / 100;
string[] lon1 = lon.ToString().Split('.');
double dlon1 = lon - Convert.ToDouble(lon1[0]);
dlon1 = dlon1 * 100;
double lon2 = (dlon1 * 10000000) / 60;
string[] lon3 = lon2.ToString().Split('.');
double dlon2 = Convert.ToDouble(lon3[0]);
dlon2 = dlon2 / 10000000;
kinhdo = Convert.ToDouble(lon1[0]) + dlon2;
double vantoc1 = vantoc * 1.852; // i vn tc t Knot sang Km/h.
Chui d liu c module gn trn thit b gi v mi khi xy ra s c xe b
nh cp hoc khi ngi dng cn bit thng tin v v tr ca thit b thng qua iu
khin tin nhn. Sau khi thng tin c gi v, trn mn hnh qun l s a ra mt
form bo hiu.
SINH VIN V NH CHUNG

74

LP: T & THCN K49

CHNG IV: THIT K V THI CNG


Trn form ny ngi qun l s bit c ta cng nh thi gian xy ra s c.
Khi mun bit v tr phng tin trn bn ch cn nhn vo button Local Vehide.
Khi v tr phng tin s c hin th mt cch trc quan nht, gip cho vic theo
di phng tin mt cch d dng.

Hnh 56: Form bo nhn d liu.

SINH VIN V NH CHUNG

75

LP: T & THCN K49

CHNG V: TNG KT

CHNG V
KT QU THC NGHIM V HNG PHT TRIN
5.1. Kt qu thc hin n.
Qua thi gian nghin cu, thi cng n c bn c hon thnh. Bng s
n lc c gng ca bn thn v s phn chia thi gian thc hin hp l, bn cnh
cn l s hng dn nhit tnh, tn tm ca thy Trn Mnh Cng, n ny c
hon thnh ng thi gian nh nh v t c yu cu t ra theo yu cu l
thit k v thi cng h thng qun l, gim st phng tin c nhn. Trong qu trnh
thc hin ti, em thu c nhng kt qu nht nh nh sau:
Mch in vi cc module nh trn mch c thit k, thi cng hon chnh v
c th nghim nhiu ln v hot ng n nh trong thc t.
Xy dng c m hnh ng dng iu khin cc thit b v h thng bo
ng.
n trnh by kh y v chc nng, cu trc ca tng khi module nh
trn board mch in tch hp. Nh vy, gip ngi c c th nm bt, hiu c
chc nng ca tng module mt cch d dng. Bn cnh , ni dung ca n c
trnh by kh chi tit r rng bng cch s dng nhng t ng thng dng, cc hnh
nh i km gip ngi c d hiu v c th thc hin mt cch tng t, t hiu qu
trong mt thi gian ngn.
H thng qun l gim st phng tin c nhn c thc hin nh trong n
l mt h thng vi cc chc nng t c nh sau:
H thng c th iu khin tt/ bt ngun cp cho phng tin t xa thng qua
tin nhn SMS:
+ Sau khi gi tin nhn th vi ni dng tin nhn , b x l s thc thi qu
trnh x l, sau l iu khin thit b mt cch t ng.
+ H thng c kh nng c bo v tt, ngha l ngi s dng phi bit lnh
iu khin v mt khu (password) th mi ng nhp c vo h thng i vi s
dng tin nhn SMS.
H thng c chc nng cnh bo s c :
+ Mt cm bin chuyn ng c thit k, lp trnh kt hp vi trung tm x
l c kh nng gi thng tin d liu ti trung tm x l. Ngoi ra cn c kh nng gi

SINH VIN V NH CHUNG

76

LP: T & THCN K49

CHNG V: TNG KT
tin nhn ngc li cho ngi iu khin v t ng thi hnh cc thao tc c lp trnh
sn khi c bo ng xy ra.
thc hin c cc chc nng nu trn, em tm hiu, nghin cu cc vn
c lin quan ti ti nh : h vi iu khin AVR, Module Sim908, Module
Sim900, cc ngn ng lp trnh tng ng nh Codevision, b lnh AT Command
dnh cho Module, v cc vn khc lin quan ti ti.
Sau thi gian thc hin, n c hon thnh ng thi gian cho php v
trnh by kh y cc mng kin thc lin quan, cc vn lin quan ti n.
Song do kin thc bn thn cn hn ch v thi gian thc hin ngn nn n khng
trnh khi nhng thiu st, em rt mong nhn c nhng kin ng gp ca thy c
v cc bn em hon thin hn na n ca mnh.
5.2.

Hnh nh mch thc nghim.

Ton b h thng c xy dng nhm p ng vic trao i d liu gia thit b


gn trn phng tin v thit b t ti trung tm. ng truyn d liu c s dng
c th l ng truyn mng GPRS hoc ng truyn mng in thoi GSM. Do
cn hai thit b c th p ng c vn ny. Ti khu trung tm s gn module
Sim900 v trn phng tin gn module Sim908 (do nhu cu cn ly thm d liu v
tinh).

Hnh 57: Mch x l trung tm Sim900

SINH VIN V NH CHUNG

77

LP: T & THCN K49

CHNG V: TNG KT

Hnh 58: Module Sim908

Hnh 59: Mch x l gn trn phng tin.

SINH VIN V NH CHUNG

78

LP: T & THCN K49

CHNG V: TNG KT
Sau khi kt ni cc khi v cp ngun cho thit b. D liu kinh v v ca
phng tin c thu t v tinh qua vi x l hin th trn mn hnh LCD. Mt role
c thit k ng ngt ngun cp cho phng tin mi khi xy ra s c.

Hnh 60: Mch x l gn trn phng tin.

Khi pht hin c s c mt cp (pht hin thng qua cm bin chuyn ng),
khi vi x l s t ng gi v trung tm ta cng nh thi gian phng tin xy ra
s c. Ti trung tm trn mn hnh qun l s a ra cnh bo v v tr ca xe.

Hnh 61: Form cnh bo c d liu gi v trung tm.

SINH VIN V NH CHUNG

79

LP: T & THCN K49

CHNG V: TNG KT
Khi nhn thy c cnh bo, ngi qun l mun bit chnh xc v tr v cc thng
tin lin quan n phng tin cn nhn vo button Local Vehide. Khi v tr cng
nh cc thng tin v phng tin c hin th trc quan trn mn hnh, t ngi
qun l s a ra c cc gii php khc phc ti u nht.

Hnh 62: V tr v cc thng tin v thit b trn mn hnh qun l.

Ngoi ra, ngi ch phng tin cng c th iu khin c vic tt bt ngun


ca phng tin hoc yu cu v thng tin phng tin thng qua cu trc tin nhn
c nh trc. Sau khi nhn c tin nhn, vi iu khin s xc nh ni dung ca
tin nhn v x l theo yu cu. T ngi s dng c th m bo an ton cho
phng tin bt c ni u.

Hnh 63: Module gi v tr phng tin ti in thoi ch phng tin


SINH VIN V NH CHUNG

80

LP: T & THCN K49

CHNG V: TNG KT
5.1. Hng pht trin ca ti.
Do thi gian thc hin ti c hn v lng kin thc bn thn cn hn ch
nn n mi ch p ng c mt phn nh ca mt h thng hon chnh. V vy,
n ny thm phong ph hn, mang nhiu tnh thc t hn na, c kh nng ng
dng cao hn th cn a thm vo nhng yu cu nh sau:
Ngoi vic iu khin gim st bng tin nhn SMS, ta cng c th iu khin
Camera chp hnh ri sau gi tin nhn a phng tin n in thoi.
S dng thm nhiu loi cm bin khc, chng hn nh cm bin nhit
o nhit lm vic ca phng tin, cm bin xng o mc tiu hao nhin liu, h
thng bo chy t ng ng dng vo n.
M rng iu khin c nhiu hn na cc thit b gn trn phng tin.
Phn cng cho mi module cn c tch ri nhm d dng cho vic chnh
sa, thay i.
Hy vng vi nhng hng pht trin nu trn cng vi nhng kin ng gp
ca cc thy c v cc bn, nhng ngi i sau s pht trin hn na ti ny, khc
phc nhng hn ch, tn ti ca ti, lm cho ti tr nn phong ph hn, mang
tnh ng dng cao hn vo trong thc t cuc sng, phc v cho nhng li ch ca con
ngi trong tng lai.

SINH VIN V NH CHUNG

81

LP: T & THCN K49

CHNG V: TNG KT

TI LIU THAM KHO


1. Tp lnh AT dnh cho Module Sim ca hng SimCom.
2. Datasheet Sim900, Sim908 hng SimCom.
3. Pawan Kumar - Accident Alert Vehicle Tracking System.
4. L Trung Thng - Ti liu vi iu khin ATmega128.
5. Phm Vn Nam, Nguyn Trng Kin n tt nghip iu khin thit b
trong nh t xa bng tin nhn SMS.
6. Mt s trang web:

www.codeproject.com
www.alldatasheet.com
www.dientuvietnam.com
www.picvietnam.com
www.congdongcviet.com

SINH VIN V NH CHUNG

82

LP: T & THCN K49