Professional Documents
Culture Documents
USB pIC Tieng Viet
USB pIC Tieng Viet
s
o 0
COFF
T (temp 25 C)(0.02 s / =
o o o
(50 C 25 C)(0.02 s / C) 1.2 s = =
Lu : di 25
o
C
COFF
T 0 =
C HOLD IC SS S
T (C )(R R R ) ln(1/ 2048) = + +
Vi cc s liu tiu biu
C
T (25pf )(1k 2k 2.5k ) ln(0.0004883) s = O+ O+ O = 5.03 s
ACQ
T 0.2 s 5 s 1.2 s 6.4 s = + + =
Chn xung nhp bin i A/D: Thi gian bin i A/D mi bit c nh ngha l .
Yu cu thi gian bin i A/D l 11* cho bin i A/D 10 bit. C 7 ty chn cho :
AD
T
T
AD
T
AD
2
OSC
T
4
OSC
T
8
OSC
T
16
OSC
T
32
OSC
T
64
OSC
T
Dao ng RC ni
bin i A/D hot ng ng, phi nh c th c nhng ln hn ti thiu.
AD
T
AD
T
Bng 2.2 theo tn s hot ng ca vi iu khin
AD
T
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
20
Thi gian bin i A/D = , trong l thi gian t x
ACQ AD AD1
T 11*T T + +
AD1
T
2.12 KHI SO SNH TNG T
Khi so snh gm 2 mch so snh c th nh cc cu hnh khc nhau. Li vo c th chn
t li vo tng t a hp vi cc chn t RA0 n RA5. Li ra digital (bnh thng hay
o) c qua thanh ghi iu khin so snh.
Bng 2.3 Li ra (digital) ca mch so snh c qua thanh ghi iu khin so snh
Li ra mch so snh 1 v 2 ln lt bit 6 v 7, li ra o mch so snh 1 v 2 ln lt
bit 4 v 5 ca thanh ghi iu khin so snh.
C 8 cu hnh ca khi so snh c nh bi thanh ghi CMCON
2.13 KHI THAM CHIU IN P SO SNH
Khi tham chiu in p so snh l mt cu chia p gm 16 in tr (trn hnh 2.11)
.Mc ch ca khi ny l to ra in p chun kh trnh cho cho cc mch so snh tng
t. nh cu hnh cho tham chiu in p so snh bng thanh ghi iu khin tham chiu
in p so snh CVRCON.
Hnh 2.11 S tham chiu in p so snh
2.14 KHI PHT HIN IN P CAO/THP
H vi iu khin PIC18F2455/2550/4455/4550 c khi pht hin in p cao/thp. y l
mch kh trnh cho php ngi s dng xc nh c. Nu in p ti im cn pht hin
cao hn hay thp hn in p t th c ngt HLVDIF c SET v nu s dng ngt
chng trnh r nhnh n a ch vector ngt v phn mm thc hin p ng ngt. Lin
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
21
quan n khi pht hin in p cao/thp l thanh ghi iu khin pht hin in p cao/thp
HLVDCON (High/Low Voltage Detect Control)
Hnh 2.12 S khi pht hin in p cao thp
2.15 NGT
PIC18F2455/2550/4455/4550 c nhiu ngun tn hiu ngt v hai mc u tin ngt.
Vector ngt u tin mc cao l 000008h v vector ngt u tin mc thp l 000018h. C
10 thanh ghi c dng iu khin ngt l:
RCON
INTCON
INTCON2
INTCON3
PIR1, PIR2
PIE1, PIE2
IPR1, IPR2
Mi ngun tn hiu ngt c 3 bit iu khin hot ng. Chc nng ca 3 bit ny l:
Bit c ch th mt s kin ngt xy ra
Bit cho php cho php ngt, chng trnh nhy n a ch vector ngt ca tn
hiu ngt khi bit c c set
Bit u tin ngt chn mc u tin ngt
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
22
Hnh 2.13 S logic cc tn hiu ngt
Mc u tin ngt c cho php bng cch SET bit IPEN (RCON<7>). Khi u tin
c cho php, c 2 bit cho php ngt ton cc. SET bit GIEH (INTCON<7>) cho php tt
c cc ngt m SET u tin cao. SET bit GIEL (INTCON<6>) cho php tt c cc ngt
m SET u tin thp. Khi c ngt, bit cho php v c cho php ngt ton cc SET
chng trnh s nhy n a ch vector ngt 000008h hoc 000018h ty thuc vo mc
u tin. Cc ngt ring r c th khng cho php thng qua cc bit cho php tng ng.
Ngt USB
Khng ging nh cc giao din ngoi vi khc, khi USB c kh nng pht ra nhiu tn hiu
ngt. Cc tn hiu ngt USB gm c vi tn hiu ngt lin lc thng thng, vi tn hiu
ngt s kin trng thi v vi tn hiu ngt s kin li. Trong khi USB c trang b mch
logic ngt ring cho khi USB ( trnh by trong phn 2.7 v s logic ngt USB hnh
2.6)
2.16 CC CHI TIT C BIT H PIC18F2455/2550/4455/4550
H vi iu khin ny c vi chi tit c bit nhm tng tin cy ti a v gim gi thnh
do cc linh kin bn ngoi c gim ti a. Cc chi tit c bit c th k ra l:
Ngun dao ng phong ph
Cc reset:
- reset ngun bt (POR)
- timer ngun bt (PWRT)
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
23
- timer khi ng dao ng (OST)
- reset Brown-out (BOR)
Qun l nng lng vi nhiu ch hot ng
Theo di an ton xung nhp
Khi ng 2 tc
Bo v m chng trnh
Lp trnh ni tip trong chip
Tp lnh phong ph
y l h vi iu khin 8 bit mnh, mm do v cht lng ca hng MICROCHIP ni
ring v ca c th gii ni chung.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
24
CHNG 3. CNG USB
3.1 CNG USB
Cng USB (Universal Serial Bus) thc cht l mt bus ni tip c pht trin bi
mt nhm gm nhiu cng ty: Compaq, DEC, IBM, Intel, Microsoft, NEC v Nortel.
tng khi xy dng cng USB l pht trin mt cng c th kt d dng n nhiu thit b
m rng khc nhau. Bus USB c pht trin qua mt s phin bn sau:
USB 1.0: c gii thiu vo nm 1996, l phin bn u tin. Tc truyn d
liu gia my tnh v thit b kt ni c th t c l 12Mbit/s.
USB 1.1: c gii thiu vo nm 1997 k tha v pht trin t USB 1.0. Bn cnh
nhng tnh nng ca USB 1.0, USB 1.1 h tr hai tc truyn l: 12Mbit/s cho
thit b (Full speed) v 1,5Mbit/s cho cc thit b c tc thp (low speed).
USB 2.0: c gii thiu vo nm 2000, vi tnh tng thch vi cc phin bn
trc v thm la chn tc cao 480Mbit/s (High-Speed). Thng 12 nm 2000
cng ty ECN a ra chun u ni kiu mi (kiu u ni B).
USB 3.0 ra i t nm 2008, tc c th gp hng chc ln tc ca phin bn
USB 2.0.
(a)
b)
Hnh 3.1 Biu tng ca bus usb (a), cp v cng kt ni (b)
Vi cc my tnh c sn xut gn y u trang b mt s cng giao tip USB cho
php d dng kt ni n cc thit b c h tr USB nh: my in, my qut hnh, camera,
cc thit b o lng,
V mt phn mm t phin bn OEM 2.1 ca Windows 95 bt u c h tr mt s
tnh nng. Trong phin bn Windows 98 mt s thit b c cung cp trnh iu khin
thit b (Device Driver) km theo Windows. Cc gi phn mn h tr cho cng USB tip
tc xy dng v tch hp trong cc phin bn ca Windows Me, Windows 2000 v
Windows XP lm cho cng USB tr ln rt thng dng.
Mt s c im ca bus USB l:
D dng s dng: khng phi quan tm nhiu n cu hnh v ci t chi tit. iu
khin lung d liu thng qua b m bng vic qun l giao thc t sn bn trong
Nhanh: giao din khng b tht c chai vi cc thit b truyn thng chm.
Tin cy: t xy ra do c c ch t ng sa cha li.
Mm do: c th kt ni nhiu loi thit b vi giao din ny.
Cung cp ngun trn bus: iu ny rt thun tin cho cc thit b xch tay vi dng
tiu th nh c th c cung cp thng qua bus.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
25
c h tr bi h thng: khi mt thit b c u ni vo, h thng t pht hin
v gi phn mm ci t cho chng. c tnh ngy gi l cm nng (Plus and Play)
l mt trong nhng c tnh m cc cng trc khng nh LPT hay RS232
khng c c.
Kt ni nhiu thit b: c th kt ni n 127 thit b c cu hnh v tc khc
nhau trn mt h thng bus USB.
Bng 3.1 So snh giao din USB vi cc giao din thng dng trn my tnh
Dng truyn Giao din
S thit
b ln
nht
Chiu di
(m)
Tc
ln nht
(bit/sec)
Thit b s dng
Ni tip khng
ng b
USB 127
5 m
1.5, 12,
480MB
Mouse, Keyboard,
Disk Drive
Ni tip khng
ng b
RS 232 2 9 30.5 20115K
Modem, Mouse, cc thit
b iu khin khc
Ni tip khng
ng b
RS 485 32256 1200 10M
H thng thu d liu v
iu khin
Ni tip hng
ngoi
IrDA 8 1.8 115M Printers, kt ni mng
Synchronous
Serial
MiCroWire 8 3 2M
Truyn thng vi vi iu
khin
Synchronous
Serial
SPI 40 3 2.1M
Truyn thng vi vi iu
khin
Synchronous
Serial
IC 64 5.5 3.4M
Truyn thng vi vi iu
khin
Serial (Fire
Wire)
IEEE 1394 15 4.5 400M Video, Mass Storage
Parallel
IEEE 488 1024 18 8M Cc thit b khoa hc
Serial Ethernet 2 480 10M1G Networked PC
Serial Current
Loop
MIDI 2 15 31.5K Music, Show Control
Parallel Parallel port 2 3 - 9 8M
Printer, Scanners, Disk
Drive
3.2 M HNH BUS USB
Cu trc c bn ca mng USB l cc tng hnh sao. Mt h thng USB bao gm:
mt hay nhiu thit b ngoi vi(Peripherals), mt hay nhiu Hub v mt ch duy nht
(thng l my tnh) gi l ch iu khin (Host Controller). Ch iu khin kt hp cht
ch vi Hub gc (Root Hub) m rng cc im ni n ch, t hub gc s kt ni n
cc thit b hay cc Hub khc. Hub l thit b c s dng m rng cc cng, v d
(hnh 3.3) mt hub gm 1 kt ni ni v my ch (upstream) v 4 cng ra k ni vi thit
b ngoi (downstream).
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
26
Hnh 3.2 M hnh kt ni bus ca USB
Hnh 3.3 S kt ni sao mt bus USB
My ch nhn bit mt thit b c u ni vo bus bng cch hi vng cc hub
lin tc. Khi mt thit b c cm vo mt cng ca Hub, Hub bo ti my ch bit c
thit b cm vo. Ch USB a ra lnh cho php v t li cng , thit b gn thm p
li v ch USB yu cu mt bn m t t thit b. T bn m t thit b ch USB bit yu
cu, trng thi, loi thit b v lp h tr nu c ca thit b. Ch USB gn cho thit b mt
a ch duy nht v gi phn mm iu khin cho thit b. i vi cc thit b c h tr
driver trong h thng cc thit b c mt s c im chung c xp vo thuc mt lp.
Khi mt thit b khai bo thuc mt lp no h thng gi phn mm iu khin v cho
php trao i d liu m khng cn np thm driver cho thit b. V d mt thit b khai
bo vi h thng thuc lp HID (Human Interface Device) khi thit b c h thng t
ng nhn v np Device Driver iu khin cho n. Bng 3.2 m t mt s lp h tr cho
cc thit b thng dng trong Windows.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
27
Hnh 3.4 Kt ni USB theo hnh sao qua cc Hub
Bng 3.2 Cc lp thit b h tr theo h iu hnh
Class (lp) Loi thit b c h tr
Audio Audio, thit b nghe nhc, h thng m thanh
Chip/smart card interface devices
(CCID)
Smart Card Devices
Common class (CCS) Mt s thit b thng dng
Communications device Modems, Telephones v cc giao din mng
HID
Cc thit b giao tip ngi my nh: chut,
bn phm,v.v.
Hub Hub USB
FrDA Thit b hng ngoi
Mass strorage a cng, CD-ROM, DVD-ROM
Monitor Mn hnh v cc thit b hin th
Physical interface devices Joysticks v cc thit b khc
POS terminals
My bn hng, my tnh tin v cc thit b
EFTPOS
Power
Cc thit b gim st v iu khin ngun, v d
nh: qun l pin.
Printer class Printers
Imaging class Scanners v Cameras.
3.3 CC KIU TRUYN USB
D liu trao i vi gia thit b USB vi my tnh s dng mt trong bn kiu
truyn l: truyn iu khin, truyn ngt, truyn khi v truyn theo kiu ng b.
1) Truyn iu khin (Control Transfers): thng s dng ci t phn cng v a
ra cc lnh cho iu khin thit b. Kiu truyn ny c lm vic mc u tin cao
vi kh nng kim sot li t ng. Mi gi tin c th truyn ln n 64byte.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
28
2) Truyn ngt (Interrupt Transfers): s dng cho cc thit b cn cung cp mt lng d
liu nh (ch truyn theo hng vo) v tun hon chng hn nh: chut, bn phm u
s dng kiu truyn ngt. Khng ging nh c ch ngt, y khng c yu cu ngt
c gi ti ch USB m my tnh s hi vng theo chu k xem c thit b no gi d
liu ti khng. Thng thit b gi ti ch my tnh 8 byte d liu trong mt ln truyn.
Cc thit b s dng kiu truyn ngt nh bn phm, chut hay cng c th dng cho
mt thit b o lng khc.
3) Truyn theo khi (Bulk Transfers): khi c lng d liu ln cn truyn v cn kim
sot li tryun, nhng li khng c yu cu thc p v thi gian truyn th d liu
thng c truyn theo kiu khi. Kiu truyn ny thng p dng cho cc thit b
nh my in, my qut.
4) Truyn ng b (Isochronous Transfers): khi lng d liu cn truyn ln vi tc
d liu c qui nh, chng hn nh dng cho card m thanh th thng p dng
truyn ng b. Theo cch truyn ny mt gi tr tc d liu xc nh c duy tr.
Vic hiu chnh li khng c thc hin v cc li l t cng khng gy nh hng
ng k n cht lng cuc truyn.
Trong lnh vc o lng v iu khin bng my tnh thng s dng kiu truyn
iu khin v c mc u tin cao v c th kim sot c li. Ngoi ra cn c th thc
hin c giao thc truyn ring trong mt s trng hp ng dng c th tip tc x l.
3.4 GIAO DIN VT L BUS USB
Bus USB c hai kiu u ni thng dng: kiu A v kiu B, thit k nh vy nhm
mc ch khng xy ra u ni nhm gia u cm vi thit b v u cm vi Hub. Khc
vi chun RS 232, vic u ni bus USB khng chia ra cc kiu u ni khc nhau,
chng hn ni thng v bt cho. Bus USB s dng mt Cable bn si dy ni vi cc
thit b, trong mt cp ng truyn hai si xon c dng lm cc d liu vi sai (D+
v D-) cn hai dy lm ngun nui 5V v ni t (GND) (hnh 3.5).
Hnh 3.5 Cable USB
Cable ni lun thc hin s lin kt 1:1, ngha l Cable ch ni vi Hub v thit b.
Kt ni qua Cable USB s dng 4 ng dn c mu khc nhau qui nh cho cc tn hiu
trn bus.
Bng 3.3 Cc dy dn trong USB
Chn Tn gi Mu dy M t
1
2
3
4
V
CC
D-
D+
GND
Trng
Xanh lc
en
+5V
CC
D liu (D-)
D liu (D+)
Ni t
Cc cm pha sau my tnh thng l kiu u ni A, qua c th ni trc tip
cc thit b USB vo my tnh PC. Cc thit b c tc thp chng hn nh chut cng c
th u thng vo cm ny bng mt phch cm cng kiu A.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
29
a)
b)
Hnh 3.6 u cm USB kiu A trn my tnh v u cm kiu B trn thit b
c)
d)
Hnh 3.7 nh s cc chn ni cm USB v cab ni kiu A v B
Ngoi cc u cm thng dng A v B bus USB cn c cc u cm kiu Mini v
Micro s dng cho cc thit b nh di ng nh: my quay phim, my chp, in thoi di
ng.
Trong cc trng hp khc thit b thng c mt cm kiu B, vic u ni my
tnh c thc hin bng mt Cable kiu A-B (hnh 3.8).
Hnh 3.8 Kt ni USB v Cable
Cc Cable dng ko di khong cch t my tnh n cc thit b thng l kiu
A-A. Ngy nay, cc Cable ni USB u c cc nh sn xut cung cp di dng hon
chnh, trn : u cm, di, cht lng bc kim chng nhiu u khng th thay i
c. V vy, tu theo mc ch s dng m la chn Cable cho ph hp.
Mt c tnh khc l cc thit b USB c th c tc truyn d liu khc nhau: tc
thp (Low Speed) v tc ton phn (Full Speed). Nn c th xy ra trng hp
cc thit b c tc cao li c kt ni vi my tnh qua mt Cable c tc truyn
thp. khc phc tnh trng ny tt c cc Cable ni u c ch to thch ng vi
tc cao. Loi Cable truyn vi tc thp c ch nh dng cho cc thit b, c th
c nh sn xut ch r khi cung cp.
Qua cm USB c th ly ra n p +5V vi dng in tiu th khong 100mA,
trong mt s trng hp c th ly ra dng ln n 500mA. Hai ng dn D+ v D- cho
php u ni vi cc chip USB chuyn dng hay cc vi iu khin c tch hp thm giao
tip USB.
Tn hiu trn hai ng D+ v D- l cc tn hiu vi sai vi mc in p bng 0/3.3V.
in p ngun nui USB c th ln ti a +5.25V v khi chu dng ti ln c th gim
xung + 4.2V.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
30
Khi ghp ni cc thit b vi bus USB thng phi phn bit r cc thit b s dng
ngun nui ring, chng hn nh my in vi cc thit b nhn in p ngun nui qua bus
(Bus Powered). Trong nhiu trng hp c hai ch ngun nui c th cng tn ti
cho php la chn theo cch thit k ca USB, dng tiu th ly t bus c t ng hn
ch. Khi dng tiu th vt qu gii hn cho php th in p cung cp t ng c ngt.
Cc thit b USB c chia ra gm 3 ch ngun:
Ch Low power: dng thit b c cung cp ti a 100mA, in p 4.4V 5.25V.
Ch High power: dng cung cp trong ch ny c th ln n 500mA, in p
4.7V 5.25V.
Ch Self power: ngun nui c cung cp t ngoi.
My tnh nhn bit c mt thit b Low Speed (1.5Mbps) hay Full Speed
(12Mbps) c cm vo thng qua in p trn trn chn D+ hay trn chn D- nhy ln
mc cao. V vy, khi thit k cc mch giao tip vi my tnh qua cng USB cn ch ni
chn D+ ln ngun nui (+5V) i vi thit b Full - Speed. Cn i vi thit b Low -
Speed cn ni chn D- ni ln ngun nui. Hnh 3.9 v hnh 3.10 m t s u ni
Cable qua cng USB cho thit b Full - Speed v Low - Speed. V pha ch USB hay hub
gm hai in tr ni dy D+ v D- xung t, nh vy bnh thng khi khng c thit b
cm vo, 2 ng ny c mc in p l 0V. Khi c mt thit b c kt ni vo cng,
mt trong hai ng dy c in p nhy ln mc cao. Bng cch ny Hub nhn bit
mt thit b mi c cm thm vo h thng bus.
Hnh 3.9 Kt ni vi thit b USB Full - Speed
Hnh 3.10 Kt ni vi cc thit b USB Low - Speed
Mi Hub v mi dy Cable u gy ra s lm tr tn hiu nhng thi gian tr khng
c vt qu gi tr cc i c qui nh. Bus USB cho php n 7 Hub u ni tip
nhau, do vy c th u ni ti a l 127 thit b vo bus USB. Trn thc t con s ny l
l thuyt v tuy c th u ni n 127 thit b nhng cng nhiu thit b u ni th tc
truyn cng chm do di thng ca ton b bus b phn chia n tng thit b u vo bus.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
31
3.5 GIAO THC TRUYN
1) Cc trng trong truyn thng USB
Th t bit c truyn qua bus USB l bit c trng s nh nht (LSB) c truyn
u tin cho n bit MSB. USB c 4 loi gi truyn trn bus l: gi nhn dng (Token), gi
d liu (Data), gi khi u khung (Start Of Frame) v gi bt tay (Handshake). Trong
mi gi li gm c nhiu trng, truyn thng USB c cc loi trng sau:
Trng ng b (SYNC): tt c cc gi u c bt u bng trng SYNC, l
chui nh phn m NRZI (Non Return to Zero Inverted) ca chui KJKJKJ. Vic
s dng SYNC lm khi u cho mt gi n ging nh bit Start trong RS 232. Tt
c cc gi u gii hn gia SOP (Start Of - Packet) v EOP (End Of Packet).
Trng nhn dng (PID): theo sau ngay trng SYNC trong mi gi ca USB,
gi c chiu di 8bit gm 4bit thp l 4bit nhn dng gi, 4bit cao l o ca 4bit
thp nhm kim sot li khi truyn. Trng PID m t loi gi s c truyn sau
n. C ch USB v cc thit b USB u nhn bit c cc m trng nhn dng
c qui nh. Nu m nhn c l khng nm trong cc m qui nh ch
USB s khng h tr. Sau y l bng danh sch m trng nhn dng ca cc loi
gi trong USB.
Bng 3.4 Cc loi gi truyn trong USB
Loi PID Tn PID PID [3:0] M t
Out 0001B Ch USB ghi ra.
Token In 1001B Ch USB c t thit b.
SOF 0101B Khi u khung v s khung.
SETUP 1101B Ch USB ci t thit b.
Data DATA0 0011B Gi d liu data0
DATA1 1011B Gi d liu data1
ACK 0010B Tr li xc nhn.
NAK 1010B Thit b ngoi khng chp nhn d liu
hay d liu truyn b hng.
Handshake STALL 1110B im cui tm ngng hay khng h tr.
Special
PRE
1100B
Ch USB a ra cho php ng ra ca
bus trao i vi cc thit b c tc thp.
Trng a ch (ADDR): l a ch ring truy cp ti thit b. Thit b c th nhn
d liu hay truyn d liu v my ch (ph thuc vo gi tr gi nhn dng PID)
thng qua a ch ca n. Trng a ch s dng 7 bit [A0 A6]. Trng a ch c
trong cc gi IN, SETUP, OUT. Mi a ch, ch nh cho mt thit b n l khi bt
u c cp ngun cc thit b c a ch mc nh l zero v ch USB nhn bng
m t ca thit b thng qua a ch mc nh ny.
Trng im cui (End Point): mt trng im cui s dng 4 bit cho php la
chn im cui trong thit b. Tr im cui zero, s im cui l ring bit cho
tng thit b chc nng. Mt im cui l a ch ca mt b m trong thit b,
trng a ch c nh ngha trong cc gi IN, SETUP v OUT. T im cui ti
ch USB thit lp mt knh trao i thng tin gi l ng ng. Tt c cc thit b
chc nng u h tr mt ng ng mc nh vi im cui l zero, ch USB s
trao i vi thit b mi gn vo thng qua ng ng ny.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
32
Trng s khung: trng s khung c di 11bit, n c gi trong gi SOF
(Start Of Frame Packets). SOF c gi bi ch USB theo chu k 1ms 0.005ms.
Trng d liu: trng d liu c di t 0 n 1023byte, cc byte c gi i
ln lt. Trong mi byte d liu bit d liu nh nht c gi trc v cui cng l
bit bit c trong s ln nht (Msb).
2) Cc loi gi trong truyn thng USB
Gi nhn dng (Token Packets): ch cho bit loi truyn ngay sau n. C 3 loi gi
nhn dng:
o IN: Bo thit b bit ch USB mun c thng tin.
o OUT: bo cho thit b rng ch USB mun gi thng tin.
o SETUP: c s dng bt u trong truyn iu khin.
Dng chung cc gi nhn dng nh hnh sau:
Hnh 3.11 Gi nhn dng
Cc gi nhn dng m USB s dng l mt trong cc gi IN, OUT, Setup. Mi gi
u c 4 trng: PID c chiu di 1 byte ch ra loi gi, 7 bit a ch (Address), 4 bit im
cui (End point) v cui cng l 5 bit kim tra li truyn (CRC).
Gi khi u khung (Start Of Frame): gi khi u khung c pht ra t ch USB
theo chu k 1.00ms 0.005ms, gi khi u khung (SOF) bao gm cc trng:
o 8bit: PID nhn dng loi gi.
o 11bit: s khung.
o 5bit: kim tra li CRC5.
Hnh 3.12 Gi SOF
Gi d liu (Data Packets): mt gi d liu bao gm mt trng PID (8bit). Trng
d liu c chiu di nm trong khong 0 1023byte v 16bit CRC. C hai loi gi
d liu c nhn bit qua trng PID l Data 0 v Data 1, hai loi gi ny c
nh ngha h tr trong truyn d liu ng b.
Hnh 3.13 Gi d liu
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
33
Gi bt tay (Handshake Packets): c 3 loi gi bt tay
ACK: tr li d liu nhn thnh cng.
NAK: bo thit b khng tr li d liu nhn hay nhn b li. Tt c cc gi bt tay
u c truyn theo kiu ngt bo tin cho ch USB bit d liu khng c gi.
STALL: ch ra rng thit b USB l khng c kh nng truyn hoc nhn d liu hay
ng ng yu cu l khng h tr.
Hnh 3.14 Gi bt tay
3.6 CC QU TRNH TRUYN USB
1) Truyn iu khin
Truyn iu khin c thc hin qua 3 giai on: Setup, Data v Status. i vi
thit b Low - Speed kch thc gi tin l 8byte, cn i vi Full - Speed kch thc gi c
th l 8, 16, 32 hay 64byte.
a) Giai on 1(Setup): gm 3 gi tin:
+ Gi nhn dng: do ch USB gi ti thit b USB. Trong gi ny ch USB gi a
ch v im cui ca thit b.
+ Gi nhn dng: c gi ngay sau khi gi Setup kt thc v trng PID ca gi
lun l kiu Data0.
+ Gi bt tay: gi cui cng do thit b USB tr li: ACK nu khng c li, NAK nu
c li xy ra.
Hnh 3.15 Cc gi tin trong giai on 1 ca truyn iu khin
b) Giai on 2 (Data): giai on ny cng cha 3 gi tin: nhn dng, d liu v bt tay
(hnh 3.16).
Gi d liu: ch ra d liu theo chiu vo (IN) hay ra (OUT).
Gi d liu: cha d liu cn truyn.
o Trng hp d liu c chiu t thit b USB vo ch USB. Nu khng c li
xy ra, d liu s c gi v ch USB. Trng hp c li xy ra ty theo li
m thit b USB gi tr li bng STALL hoc NAK.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
34
o Trong trng hp d liu c chiu t ch USB n thit b USB th gi d
liu s c gi ti thit b.
o Gi bt tay: khi nhn c d liu ch USB s tr li bng gi ACK ti thit
b. Trong trng hp ra ty theo d liu m thit b USB nhn c li hay
khng m n gi tr li ch USB bng mt trong cc gi: ACK, NACK,
STALL.
Hnh 3.16 Cc gi tin trong giai on 2 ca truyn iu khin
c) Giai on 3 (Status Stage): tr v trng thi ton b qu trnh
Chiu vo: ty theo qu trnh trao i c li xy ra hay khng m thit b USB s tr
li ti ch USB mt trong cc tn hiu: ACK, STALL hay NACK (hnh 3.17a).
Chiu ra: ch gi cc gi tr li n thit b USB.
Hnh 3.17 Cc gi trong giai on 3 ca truyn iu khin.
2) Truyn ngt
Khng ging vi phng thc trao i tin theo ngt m chng ta xt ti chng
2, tc l tn hiu yu cu trao i tin do thit b a ra v yu cu CPU phc v ngt cho
n. Truyn ngt trong USB hon ton do ch USB hi vng. Nu mt thit b USB ch
ng yu cu n phi i n khi ch USB hi ti v p ng. Qu trnh truyn ngt din
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
35
ra tng i n gin ch c 3 gi trong mi ln truyn (hnh 3.18). Truyn ngt thng s
dng trong cc thit b truyn thng c tnh di b, cc thit b nh v n gin.
Truyn ngt qui nh kch thc cc gi ty thuc vo tc thit b:
Vi thit b Low - Speed: kch thc gi ti a 8 byte.
Vi thit b Full - Speed kch thc gi ti a l 64byte.
Vi thit b High - Speed kch thc gi ti a c th ln n 1024byte.
Hnh 3.18 Cc giai on truyn ngt
a) Interrupt IN: ch USB s hi cc im cui ngt theo chu k. Tc qut hi vng c
th c t trong gi m t im cui ca mi thit b. Mi khi hi vng yu cu ch phi
gi mt IN Token. Nu IN Token b hng, thit b b qua gi ny v tip tc gim st bus
cho cc IN Token mi.
Nu c yu cu thit b gi mt gi d liu ti ch USB v ch my ch tr li. Nu
khng c ngt thit b tr li ch bng NACK hay STALL.
b) Intrrupt OUT: khi ch mun gi d liu ti thit b ngt, n pht ra mt OUT Token
v theo sau l mt gi d liu ngt (Interrupt Data). Nu OUT Token hoc gi d liu
(Data Packet) b hng, thit b USB s b qua gi ny. Nu b m im cui ngt ca
thit b trng n s tr v gi ACK cho ch USB. Trong trng hp b m im cui l
khng trng do d liu nhn trc , th thit b USB s tr v NAK. Tuy nhin nu mt
li xy ra vi mt im cui, n s tr v STALL.
3) Truyn ng b (Isochronous Transfers)
S dng khi d liu cn truyn ln v tc d liu c qui nh nh: audio,
video stream. Trong cch truyn ny mt gi tr tc d liu c duy tr v khng kim
tra li. Truyn ng b cng cho h tr trn c hai chiu: truyn ng b theo chiu vo
(Isochronous IN) v truyn ng b theo chiu ra (Isochronous OUT). Kch thc ln nht
ca gi d liu truyn trong kiu ny:
Thit b Full - Speed l 1023byte.
Thit b High - Speed l 1024byte.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
36
Hnh 3.19 Truyn ng b
4) Truyn khi (Bulk Transfers)
S dng khi c lng d liu ln cn truyn v cn kim sot li truyn, nhng li
khng c yu cu thc p v thi gian truyn th d liu thng c truyn theo khi
(Bulk transfers). Kiu truyn ny thng p dng cho cc thit b nh my in, my
qut,v.v. Truyn khi ch h tr cho cc thit b Full - Speed v High - Speed. Kch
thc gi d liu ln nht m thit b h tr:
Vi thit b Full - Speed: 8, 16, 32 hay 64byte.
Vi thit b High - Speed: 512byte.
Truyn khi cng tng i n gin, ch gm 3 gi: gi nhn dng, gi d liu v
bt tay.
Hnh 3.20 Cc gi trong truyn khi
a) Chiu vo: khi ch USB sn sng nhn d liu, n pht ra mt IN Token. Nu thit
b nhn c mt IN Token vi mt li n s b qua gi. Nu IN Token nhn c khng
c li th thit b USB tr v gi d liu cn truyn. Trng hp li xy ra thit b gi v
NACK hoc STALL.
b) Chiu ra: khi ch USB mun gi ti thit b chc nng mt gi d liu, n gi ti thit
b USB mt gi OUT Token v tip theo sau l gi d liu cn truyn. Trng hp gi
OUT Token, d liu b hng hay b m thit b ang y n s gi tr li bng gi
NACK. Trng hp khng c li xy ra, khi nhn xong d liu thit b USB tr li bng
gi ACK. Trong trng hp im cui b li n s tr li bng STALL.
3.7 IM DANH V GI PHN MM IU KHIN
1) im danh
Kh nng t ng nhn ra v ci t phn mn iu khin cho cc thit b USB l do
h thng thng xuyn kim tra thng tin thit b trn cng. Vic im danh c thc
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
37
hin c lp bi h iu hnh, khng cn mt chng trnh ngi s dng phi thc hin
cng vic ny. Khi mt thit b USB c kt ni vo mng, hub nhn ra n thng qua
mc in p trn ng d liu. V trong mi thit b USB ng d liu c ni ngun
thng qua in tr, do khi kt ni vo bus in p ng d liu trn hub c a ln
mc cao bo cho hub bit mt thit b kt ni vo cng tng ng.
Khi h iu hnh nhn bit mt thit b mi c kt ni, n yu cu thit b kt ni
cung cp thng tin v thit b. Thit b c nhim v a ra bng cung cp thng tin v mnh
di dng mt bng m t (Description). Bng m t l mt cu trc d liu c qui nh
chnh xc v ni dung, cch sp xp d liu v chiu di ca n. H iu hnh da vo
thng tin cung cp t bng m t thit b m gi cc phn mm iu khin tng ng.
Qu trnh im danh v ci t cc thit b USB din ra gm cc bc nh sau:
Hub thng bo cho my ch bit l c mt thit b mi c ni vo.
My ch hi li Hub xem thit b c ni vo cng no.
My ch by gi bit c thit b mi c ni vo cng no. N a ra mt lnh
ni sang cng v thc hin mt thao tc t ti bus.
Hub to ra mt tn hiu reset vi di 10ms v cp dng in vi cng 100mA
cho thit b. By gi thit b sn sng hot ng v tr li qua a ch mc nh 0.
Trc khi thit b USB nhn c a ch bus ring, n vn c th c trao i qua
a ch mc nh 0. My ch c 8 byte u tin ca bng tm lc thit b khng
nh cc gi d liu c th c chiu di bng bao nhiu.
My ch gn cho thit b mt a ch bus ring.
Qua a ch mi my ch c tt c cc thng tin, cu hnh c t thit b.
My ch gn cho thit b mt trong cc cu hnh c th. By gi thit b s c php
ly ra dng in tiu th nh trong bng tm lc cu hnh ca n nh. Nh
vy, tt c sn sng v c a vo s dng.
Hnh 3.21 Qu trnh nhn v ng k mt thit b USB khi cm vo
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
38
Hnh 3.22 Kt ni ch USB vi cc thit b chc nng thnh cc ng ng
Mi yu cu t my ch c b vi x l trn thit b USB nhn ra v tr li. Ti
mi im cui (Endpoint) trn thit b c b tr mt mt b nh FIFO (First In, First
Out) lm b m vo/ra cho im cui. Khi nhn c d liu, b vi x l phn tch lnh,
c d liu ci t trong ROM v ghi ln b m ra. Thng tin u tin thit b gi l bng
m t thit b (Device Descriptor) c di 18 byte. My ch c 8 byte u tin trong
bng ny xc nh lp thit b sau ton b bng m t c c. T cc thng tin c
c, trong mt vi trng hp m lp (bDeviceClass) ch cho ta thy l thit b thuc v
mt lp xc nh, qua h iu hnh c th ch nh phn mm iu khin dng cho thit
b. Nh vy, chng hn i vi mt chut USB ngi dng khng cn c thm phn mm
ring. Nh cc thng tin ny thit b c a vo lm vic v c ghi vo b qun l
thit b. Khi thit b thuc v mt lp, thng tin v thit b c th c vit chi tit trong
bDeviceClass v bDeviceProtocol.
bDescriptorType: loi bng m t (01h: m t thit b).
bcdUSB: l s BCD ch r phin bn ca USB:
Version 1.0: l 0100h.
Version 1.1: l 0110h.
Version 2.0: l 0200h.
Bng 3.5 S sp xp ni dung ca bng m t thit b
Tn trng S byte M t
bLength 1 Chiu di ca bng tm lc tnh bng byte
bDescriptorType 1 Kiu Descriptor (01h = Device Descriptor)
bcdUSB 2 Phin bn USB
bDeviceClass 1 M lp (Class)
bDeviceSubClass 1 M lp con (SubClass)
bDeviceProtocol 1 M giao thc
bMaxPacketSize 1 ln ca EPO-FIFO
idVendor 2 ID nh cung cp
idProduct 2 ID sn phm
bcdDevice 2 S hiu nh sn xut
iManufacturer 1 Ch s xu dng cho nh sn xut
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
39
iProduct 1 Ch s xu dng cho sn phm
iSerialNumber 1 Ch s xu dng cho s hiu sn phm
bNumConfigurations 1 S lng cu hnh cui cng.
bDeviceClass: cha m lp thit b, ch cho bit n thuc lp thit b no.
bDeviceSubclass: trng ny s dng nhn bit lp con trong mt lp xc nh. Nu
gi tr bDevice l 0 th gi tr ny cng l 0.
bMaxPacketSize: kch thc ln nht cho b m im cui zero (Endpoint-O-
FIFO).
idVendor v idProduct: s hiu nh sn xut v s hiu sn phm, c h thng s
dng tm Device Driver tng ng cho thit b.
bcdDevice: s dng cung cp version ca thit b.
Ba ch s chui (iManufacturer, iProduct v iSerialNumber): cung cp tn nh sn
xut, tn thit b v s serial ca sn phm.
bNumConfigurations: ch ra s cu hnh thit b ng vi tc hin ti.
Cc bng m t cu hnh c dng cu trc phn lp, cho php m t cc thng s
thit lp v cc tnh cht khc nhau. i vi tng thit b c th:
Mi thit b c mt bng tm lc thit b duy nht.
C th c nhiu cu hnh.
Trong mi cu hnh c th cho ra nhiu giao din.
Mi giao din c th c nhiu cch thit lp la chn.
Trong mt cu hnh c th tn ti nhiu giao din, cc giao din chc nng c
chia ra theo cc im cui ang c. Mi giao din c th c nhiu cch thit lp, cc cch
ny khc nhau lin quan n di thng b chim dng.
Hnh 3.23 Cu trc hnh cy ca bng m t thit b USB
Ngoi bng tm lc thit b cn c bng tm lc cu hnh, mt hay nhiu bng
tm lc giao din, cc bng tm lc im cui. H thng cng phc tp th thng s tin
phi qun l cng nhiu, chng hn thit b c bao nhiu im cui vi ln FIFO m
th no, liu thit b c h tr nhiu cu hnh, nhiu giao din v bao nhiu cch thit lp
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
40
la chn i vi mi giao din. Nh vy c th ni bng m t thit b l mt dng hnh
cy (hnh 3.23).
Bng lit k cu hnh m t s lng giao din v cu hnh hin thi cng nh bng
tm lc xu cui cng ang tn ti. Trong bmAttributes c th c ch dn liu thit b
c cung cp in p ngun t bus v liu chc nng RemoteWakeup c h tr cng
nh c nh thc t bus. Hm Maxpower ch dn nhu cu v dng in ly t bus, n
v tnh l 2mA. Cng dng in ny cng c th c cung cp t mt Hub trong ch
t cp in.
Bng 3.6 Bng m t cu hnh
Tn trng S byte M t
bLength 1 Chiu di ca Descriptor tnh theo byte
bPescriptorType 1 Kiu Descriptor (02h = Descriptor cu hnh)
wTotalLength 1 Chiu di ca Descriptor c ca cu hnh ny
bNumInterfaces 1 S lng giao din
bConfigurationValue 1 S hiu ca cu hnh ny
bConfiguration 1 Ch s xu
bmAttributes 1 Thuc tnh ca cu hnh
Maxpower 1 Dng tiu th, n v tnh x2mA
Nhiu bng tm lc ghp ni c th c qui nh ni dung bng vic t cc
thng s la chn. Thng tin quan trng nht v tng cch thit lp l s lng im cui
trong mt bng lit k Endpoint thuc v mi im Endpoint. Nu nh thit b thuc v
mt lp thit b c ch nh trong Device - Descriptor th cc thng tin tng ng c
th c cho y mt ln na.
Bng 3.7 S sp xp ni dung ca bng tm lc giao din
Tn trng S byte M t
bLength 1 Chiu di ca kiu Descriptor tnh ra byte
bPescriptorType 1 1 (04h = Interface Descriptor)
bInterfaceNumber 1 S giao din.
bAlternateSettiny 1 t thng s theo cch la chn.
bNumEndpoint 1 S lng im cui khng c EPO
bInterfaceClass 1 Giao din lp cui cng.
bInterfaceSubClass 1 Giao din lp con cui cng.
bInterfaceProtocol 1 M giao thc cui cng.
iInterface 1 Ch s xu (String-Index)
Bng 3.8 Bng tm lc im cui
Tn trng S byte M t
bLength 1 Chiu di ca bng lit k tnh ra byte.
bPescriptorType 1 1 kiu lit k (05h = lit k im cui).
bEndpointAddress 1 a ch Endpoint (ED2, OUT).
bmAttributes 1 Kiu truyn (2 = truyn khi).
bMaxPacketSize 1 Dung lng b nh FIFO.
bMaxPacketSize 1 Khong hi cui cng.
Khi h thng im danh tt c cc thng tin quan trng ca thit b u c kim
tra. Khi h iu hnh kim tra xem liu ti nguyn c yu cu nh: phn mm iu
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
41
khin, dng tiu th, di thng bus c mt. Sau khi hon thnh khu im danh, ta c
th s dng ngay cc yu cu iu khin (Control-Request) qua Endpoint 0. Cc Endpoint
v cc hot ng khc cn phi c cho php hot ng bng vic gn thng s thit lp
ln hn 0.
2) Phn mm iu khin
Chc nng ca phn mm iu khin (driver) thit b ni chung, c th m t nh
mt mt xch lin kt gia phn cng v phn mm ca mt h thng. Trong mi trng t
Window 98 khng c mt chng trnh ng dng no c th truy cp trc tip ln phn
cng. Cch tt nht l gi ra mt phn mm iu khin thit b, phn mm ny trao i
trc tip vi phn cng hoc qua lp m nm su hn trong h thng. Phn mm iu
khin c cc nh sn xut thit b cung cp km theo v c vit trong khun kh u
t cho thit b ca mt cng ty. Phn mm lm vic trong ch li vi quyn u tin cao
hn. Mt phn mm iu khin c xem xt ging nh mt thit b o, ngha l mt
chng trnh ngi dng s dng mt phn mm iu khin ging nh chnh mt thit b.
Tt c cc phn mm iu khin USB u da trn Win32-Driver-Model (WDM),
m hnh ny c s dng trong Window NT v c tng thch trong Window 98. Tt
c cc lnh gi phn mm iu khin u chy qua phm mm qun l vo/ra (I/O
Manager). Cc chng trnh ngi dng khc nhau gi gi yu cu ti phn mm qun l
vo/ra, phn mm ny sau phn phi ti phn mm iu khin c th.
Vi mc ngi dng ch cn quan tn n 5 hm truy cp trong Windows:
M mt thit b bng hm CreateFile( ).
Truy nhp ghi s dng hm WriteFile( ).
Truy nhp c s dng hm ReadFile( ).
Kt thc qu trnh truy nhp s dng CloseHadre( ).
i vi cc trng hp khng theo khun mu trn, ta c th thc hin hm truy
cp bng hm DeviceloControl( ).
Trong gii hn gio trnh ny, khng i su vo cch to ra cc Driver cho thit b
iu khin v chng kh phc tp.
3.8 MT S VI MCH S DNG TRONG KT NI USB
Trn th trng hin nay cung cp mt s IC chuyn dng s dng cho vic kt
ni USB. Bn cnh nhiu vi iu khin (Microcontroller) ca cc hng khc nhau cng
tch hp cng USB to thun li trong vic s dng. Cc IC ny u tch hp phn cng
b tr t ng pht hin cc gi d liu, yu cu t ch USB, v t ng gi cc bng m
t thit b ti ch USB.
Cc vi mch h tr kt ni USB l cc Microcontroller c h tr phn cng, phn
do. Phn mm trao i dnh cho ngi thit k to iu khin ti a cho ngi s dng.
Cc Microcontroller h tr USB vi cc tc trao i tin: 1.5Mbps (Low - Speed),
12Mbps (Full - Speed) v 480Mbps (Hight - Speed). Cc thit b truyn tin tc thp v
trung bnh nh: CY7C63001, AN21XX, CY7C64xxx, PIC18f4550, 68HC705JB2,v.v...
Cc thit b c tc cao nh: FTDI FT8U232, FTDI FT8U245, CY68013 FX2,
NET2280,v.v... Sau y l mt s vi iu khin hay dng:
1) CY7C63001
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
42
B vi x l 8bit theo khin trc Harvard.
Chy h 6MHz.
128byte RAM.
H tr USB tc 1.5Mbps.
ng gi 20 chn.
2) PIC16C745, PIC16C765 (Microchip)
H tr USB 1Mbps vi 6 im cui.
5 knh ADC 8bit,v.v
3) PIC18F4550 (Microchip)
H tr USB 12Mbps vi 16 im cui. Mi im cui 2 b m 512byte.
Chy xung nhp 48MHz.
4) NET2888 (Netchip)
H tr USB 1.0 v 1.1.
H tr 5 m cui.
Chy vi xung nhp 48MHz.
5) 68HC705JB3 (Motorola)
H tr USB tc 1.5Mbps.
H tr 2 ngt iu khin im cui.
144byte RAM.
6) PDIUSBD11 (ca Philips)
H tr USB 12Mbps.
Mt im iu khin v 6 im cui chung.
Chy vi xung nhp 12MHz.
7) CY7C68001
H tr USB tc 480Mbps v 12Mbps.
H tr 5 im cui vi b m 512byte cho truyn khi v 1024byte cho truyn
ng thi.
4Kbyte FIFO.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
43
3.9 CC MCH CHUYN I USB SANG RS232.
0
C3
0.1uF
USB Connector
2
1
3
4
-Data
+5V
+Data
GND
D1
LED
C1
4.7uF
U1A
100301/SO
5
1 4
20
15
16
8
19
24
27
28
17
2
5
7 1
8
2
1
2
6
12
13
14
22
23
3
11
2
9
10
6
RXD
TXD VCCIO
VCC
USBDP
USBDM
NC1
RESET
NC2
OSCI
OSCO
3V3OUT
A
G
N
D
G
N
D
1
G
N
D
2
G
N
D
3
T
E
S
T
CBUS4
CBUS3
CBUS2
CBUS1
CBUS0
RTS
CTS
DTR
DSR
DCD
RI
R1
33
C4
0.1uF
RXD
TXD
C2
0.1uF
Hnh 3.24 Mt mch chuyn i USB sang RS232 s dng FT232R.
Mch chuyn i t USB sang RS232 i khi cng rt cn thit khi m ta mun kt
ni n thit b s dng cng RS232, m my tnh khng c trang b cng RS232. Trn
mch hnh 3.24 m t s mch ca mt b chuyn i t USB sang tn hiu RS232 s
dng IC chuyn dng FT232R. Cng c th s dng cc vi iu khin lm nh b chuyn
i USB sang RS232, tuy nhin ta phi vit phn sn (Firmware) cho vi iu khin lm
cc cng vic ng k, nhn d liu t cng USB v chuyn d liu ra cng ni tip. Hnh
3.25 l mt mch chuyn i ghp ni vi vi iu khin PIC 16F876 s dng vi mch
PDIUSBD 11. D liu truyn gia vi iu khin vi PDIUSBD 11 l dng I2C. PDIUSBD
11 thc cht l mt vi iu khin, vic ng k c thc hin t ng vi cc thng s ci
t sn trong ROM.
Hnh 3.25 Mt mch ghp ni USB s dng PDIUSB 11
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
44
CHNG 4. THIT K V CH TO CC KHI CHC NNG
4.1. KHI CAO TH. (s nguyn l hnh 4.1)
Khi cao th c th phn thnh cc khi chc nng: dao ng (U6); to xung iu
khin cho 2 MOSFET (U5A,U4A-U4D); linh kin ngt-m l MOSFET IRF840 (Q3, Q4),
to xung cao p nh bin p ferit T1; chnh lu to in p dng (D6, D7) v cc t in
(C5, C6, C9); chnh lu to in p m (D8, D9) v cc t in (C7, C8, C10); khi phn
hi to n nh cao p (U1C, U1D, U1A, U1B, U2B v Q1, Q2); khi bo v qu cao p
(U2C, U2A, U3B, U3A); khi bo v qu dng cho Q3 v Q4 (U3D, U3A).
Khi dao ng v to xung iu khin ngt m cho MOSFET: dao ng nh U6
LM555, tn s dao ng c iu chnh nh bin tr POT5 c tn s dao ng lp
li c 32KHz, t s rng xung (duty factor) l ty . Xung dao ng t U6 LM555 a
n b chia i tn s trn U5A-CD4013B, mc ch vic chia i tn s l cho ra xung
cn xng (h s rng xung l 50%) v to xung pha ngc nhau ly ra ti Q v Q ca
U5A. Cc linh kin D6,C11, R32 v D7,C12, R33 c cng dng co hp rng xung, h
s rng xung b hn 50% c cng dng trnh hin tng 1 MOSFET cha tt hn trong
khi MOSFET kia kch dn.
Khi to xung cao p v chnh lu: Tng bin xung nh bin p, bin
xung c 1KV v khi chnh lu nhn hai c 2KVDC t s bin p phi tha mn l
1:100. Chnh lu dng nhn hai in p nh diode D6, D7 v C5, C6, san bng nhp nh
nh R30, C9. Tng t, phn chnh lu m gm cc linh kin D8, D9, C7, C8 v san bng
nhp nh nh R31, C10.
Khi phn hi to n nh cao p: B phn ly mu in p gm R8, R9 v
POT1, khuch i in p ly mu U1C, U1A, U1B v khuch i o du khi dng cao
p cc tnh m l U1D, khuch i so snh l U2B, in p chun a vo u trn ca
bin tr POT2,in p chun ny l thay i mong mun in p li ra thay i v ly
t board logic. in p li ra khuch i so snh c m nh Q1 v Q2 cp in p
nui cho bin p xung T1.
Khi bo v: Gm bo v qu cao p v bo v qu dng cho 2 MOSFET. Khi
bo v qu cao p gm: khuch i m U2C; khuch i so snh U3B v transistor Q5.
Khi in p cao p li ra cao qu mc quy nh, li ra U3B ln mc cao lm Q5 dn, ko
in th nui cho bin p T1 v khng, ng thi LED D5 sng bo c s c bo v. Bo
v qu dng cho MOSFET gm: in tr ly mu dng R21; khuch i so snh U3D. Khi
c hin tng qu dng, li ra U3D ln cao lm Q5 dn, ko in th nui cho bin p T1
v khng.
Cc c trng k thut thit k khi ngun cao p.
- in p ra t 0V n 2 kV trn 256 bc.
- Dng cao p cc i l 10mA ti 2000V, c ngha cng sut cc i ngun cao
p l 10VA.
- i cc tnh ngun cao p bng b chuyn c kh.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
45
S1
D6
4007
10K
POT5
1
3
2
R11 1K
+
-
U1A
TL084
3
2
1
4
1
1
+
-
LM339
5
4
2
3
1
2
U5A
CD4013B
5
3
1
2
14
6
4
D
CLK
Q
Q
VDD
S
R
12V
R36 4.7K
R10 10K
+
-
U1D
TL084
12
13
14
4
1
1
C11
220p
R13 20K
POLAR+
R30 39K
12V
D5
LED
+
-
U2C
LM324 10
9
8
4
1
1
C14
1nF
R14
10K
R5 10K
+
-
U2A
LM324
3
2
1
4
1
1
-12V
R32
10K
R27
4.7K
12V
U6
LM555
2
5
3
7
6
4
TR
CV
Q
DIS
THR
R
D7
4007
R26 22K
+ C4
1uF
C15
1nF
R25 330
POLAR-
+ C13
10uF
D6
1N4148
+
-
U1B
TL084
5
6
7
4
1
1
12V
D1
1N4148
D2
1N4148
R12 10K C3 1nF
-12V
R4 10K
U4B
CD4093B
5
6
4
1
4
7
OUTPUT POLAR-
U4D
CD4093B
12
13
11
1
4
7
+
- U3D
LM339
11
10
13
3
1
2
T1
1
4
5
6
8
R22 10K
U3B
R2 10K
POLAR+
R18 4.7K
Q5
C1815
3
2
1
12V
D8
4007
R31 39K
R35
10K
Q2
H1061
3
2
1
R33
10K
2K2
4.7M
+
-
U1C
TL084
10
9
8
4
1
1
RD5
R23
1K
12V
R19
1K
D8
10V
Q3
R21
1/1W
C6
1nF
R29 1K
Q4
R24
1K
C8
1nF
2K
POT3
1
3
2
R28
18K
C9
1nF
C12
220p
R9
4.7M
R4 10K
D4
10V
Q1 C1815
3
2
1
R17 1K
500K POT1
1
3
2
+
C2 10uF
-12V
POLAR-
R7
R8
R37 1K D10
1N4148
+
-
U3A
LM339
7
6
1
3
1
2
12V
R6 10K
R20
1K
U4C
CD4093B
8
9
10
1
4
7
C5
220nF
R34
330
2K
POT4
1
3
2
C10
R3 10K
+
-
U2B
LM324 5
6
7
4
1
1
U4A
CD4093B
1
2
3
1
4
7
R15 1K
R16
1K
C5
1nF
C1 100P
D3
1N4148
D7
1N4148
D9 4007
C7
1nF
10K
POT2
1
3
2
R1
10K
R4 10K
OUTPUT POLAR+
Hnh 4.1 S nguyn l khi cao p.
4.2 KHI KHUCH I PH V KHI PHT XUNG IU KHIN ADC
Khi khuch i ph gm cc b phn: b phn khuch i tuyn tnh, b phn tch
phn hot, mch phc hi ng khng (BLR: Base Line Restorer), cng phc hi ng
c bn (Base Line Restorer Gating) v b phn iu khin h s khuch i. B phn
khuch i tuyn tnh xy dng trn cc IC U1, U2, U3 v U4. U1, U2 l OP37 p ng
nhanh v tp m thp, U3, U4A l TL084 l OP-AM li vo FET nn c tp m thp v
p ng tng i nhanh. u vo ca khuch i tuyn tnh l mch b chnh pole-zero
trit bu m. B phn tch phn hot gm U4B, U4C, U4D , tc dng b phn tch
phn hot l sa dng xung thnh dng Gauss. B phn iu khin h s khuch i xy
dng trn bin tr s MCP41010 ca hng Microchip, bin tr s 8 bit nn c 256 mc
chnh h s khuch i. Mch phc hi ng khng (phc hi mc c bn) gm cc linh
kin: U5A, U5C v U5D. Nguyn tc mch phc hi ng khng l s dng vng phn
hi m thnh phn DC n nh mc mt chiu. U5C l mch tch phn ly thnh
phn mt chiu ca vng phn hi. U5A l tng khuch i m Av=1, tr khng li vo
ln, tr khng li ra b. U5D l tng khuch i o du. Cng phc hi ng c bn
gm cc linh kin: U5B, U9, U8A, cp vi sai Q1, Q2 v U7. Mch phc hi ng c bn
tc ng khi khng c xung xut hin, khi c xung xut hin cn lm mt tc ng mch
phc hi ng c bn. U9 l tng ngng pht hin c xung, U8A l mch n n
pht xung c rng bao ph xung, cp vi sai Q1-Q2 c chc nng b kha dng phn cc
cho U7 trong thi gian xut hin xung lm xa i tc ng mch phc hi ng khng
trong thi gian xung xut hin.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
46
-15V
+5V
+5V
-15V
+5V
-
+
U1
OP37
3
4
6
7
2
-
+
U2
OP37
3
4
6
7
2
-
+
U3
3
4
6
7
2
C1
1
3
2
1 3
2
Q1
3
2
1 Q2
3
2
1
SW1
1
8
3
+
- U4B
TL084
5
6
7
4
1
1
+
- U4A
TL084
3
2
1
4
1
1
+
- U4D
TL084
12
13
14
4
1
1
+
- U4C
TL084
10
9
8
4
1
1
U8A
74LS123
14
15
1
2
3
13
4
16
CEXT
REXT/CEXT
A
B
CLR
Q
Q
VCC
C2
J 1
MCP41010
CS
SCK
SI
Vss
VDD
B
W
A
+
- U5A
TL084
3
2
1
4
1
1
+
- U5D
TL084
12
13
14
4
1
1
+
- U5C
TL084
10
9
8
4
1
1
+
- U5B
TL084
5
6
7
4
1
1
+
-
U9
LM311
2
3
7
5 6
4 1
8
1
3
2
D2
1
2
D1
1
2
R27
+
- U6
TL081
3
2
6
7 1
4 5
1
2
+
-
U7
CA3080
3
2
6
7
4 5
1
2
BLR
BLR
T
o
R
B
7
T
o
R
B
5
T
o
R
B
6
INPUT
OUTPUT
R7 R8
R9
R10
R11 R12
R13
R14
C3
R15 R16
C4 R17
C5
R18
R19
R20
R21
C6
R22
C7
R23
R24 R25
C8
C9
D3 D4
R26
R28
R29
R30
P2
R31
R32
R33
R34
R35 R36
R37
C10
R38
C11
R39
R40
R41
R42
P3
R5
R6
R2 R1
P1
R3
R4
R43
Hnh 4.2 S nguyn l khi khuch i v khi pht xung iu khin ADC.
Hnh 4.3 Gin xung khi pht xung iu khin ADC
(a) Tn hiu li vo (+) U3A
(b) Tn hiu li ra gi nh ti li ra U3C
(c) Tn hiu li ra U3A
(d) Tn hiu ra ti chn 6 U4, rng 15us
(e) Tn hiu ra ti chn 10 U4 xa in tch trn t C19, rng 2 us
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
47
Cc c trng thit k khi khuch i ph:
- iu chnh h s khuch i t x20-x130 / 256 bc, tng ng 26db-
42db/256 bc.
- iu chnh h s khuch i bng phn mm trn my tnh.
4.3 KHI LOGIC
Khi logic c xy dng trn vi iu khin PIC18F4550 vi cc l do: 1. vi iu
khin ny c ADC 10 bit; 2. vi iu khin ny tch hp cng USB 2.0. Vi iu khin ny
m nhn cc chc nng:
- Bin i AD: Xung khuch i ph c gi nh a vo pin 2 (AN0) bin
i AD. Mc logic cao ti RD4 t khi pht xung iu khin ADC dng lm tn
hiu bt u bin i AD. Bin i AD xong vi iu khin pht xung ti RB4
xa in tch trn tc gi nh C19.
- iu khin h s khuch i: Vi iu khin iu khin h s khuch i ca
khuch i ph thng qua vi mch bin tr s U5 MCP41010 ti board mch
khuch i.
- iu khin in p cao p: Bin tr s U9 c iu khin bi vi iu khin,
in p ca im gia bin tr (W) c khuch i m nh U10. in p li
ra khuch i m U10 (CTL-HV) c dng lm in p chun cho khuch i
so snh ca board mch cao p.
- Truyn d liu : Truyn d liu t my tnh ti vi iu khin v ngc li.
Hng d liu t my vi tnh n vi iu khin l cc lnh iu khin: ng
m cao p; t cao p; t h s khuch i Hng d liu t vi iu khin
n my vi tnh l d liu ph, in p cao p bin i ADC hin th ln
giao din iu khin trn my vi tnh.
RD4
C25
100nF
VDD=5V
MEAS
CONN USB
1
2
3
4
1
2
3
4
RB6
RB7
+
-
U10A
LM324A
3
2
1
4
1
1
VCC=12V
X1 20MHz
RB5
VDD=5V
CTL- HV
U8 PIC18F4550
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
MCLR/Vpp/RE3
RA0/AN0
RA1/AN1
RA2/AN2/VREF-/CVREF
RA3/AN3/VREF+
RA4/TOCKI
RA5/SS
RE0/RD
RE1/WR
RE2/CS
VDD
Vss
OSC1/CLKIN
OSC2/CLKOUT
RC0/T1OSO/T1CKI
RC1/T1OSI/CCP2
RC2/CCP1
VUSB
RD0/PSP0
RD1/PSP1 RD2/PSP2
RD3/PSP3
RC4/D-/VM
RC5/D+/VP
RC6/TX/CK
RC7/RX/DT
RD4/PSP4
RD5/PSP5
RD6/PSP6
RD7/PSP7
Vss
VDD
RB0/INT
RB1
RB2
RB3
RB4
RB5
RB6
RB7
U9
+
-
U10B
LM324A
5
6
7
4
1
1
RD5
C23
20pF
RB4
U10
POT10
1
3
2
C24
100nF
-VCC=-12V
VDD=5V
C22
20pF
-VCC=-12V
AN0
R37
4.7K
MCP41010
CS
SCK
SI
Vss
VDD
B
W
A
VDD=5V
VDD=5V
VCC=12V
Hnh 4.4 S nguyn l khi logic.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
48
Cc c trng thit k khi logic:
- Cng USB thit k hot ng mode full speed.
4.4 KHI NGUN NUI IN P THP
C30
C28
C31
0VAC
D15 1N4007
D16 1N4007
OUTPUT 5V
R38
T2 1 5
6
4 8
C27
D17 1N4007
220V
R39
C26
U13
LM7912
1
2
3
VI
G
N
D
VO
AC
C32
C33
15VAC
U12
LM7805
1
2
3
VI
G
N
D
VO
OUTPUT 12V
D14 1N4007
T3
1
5
4
8
U11
LM7812
1
2
3
VI
G
N
D
VO
C29
OUTPUT -12V
15VAC
Hnh 4.5 S nguyn l khi ngun nui in p thp
Khi ngun nui in p thp c xy dng trn bin p h p T2
220VAC/15VAC, 15VAC, 40VA . Cc linh kin R38, R39, T3,C26, C27, C28 to thnh
b lc in li (line filter). Chnh lu 2 bn k in p dng: D14; D15; C29. n p
12V, 5V nh IC U11 v U12. Chnh lu 2 bn k in p m: D16; D17; C30. n p -12V
nh IC U13.
Cc c trng thit k khi ngun nui in p thp:
- Dng cc i li ra +12V, -12V, +5V l 1A
- Cng sut tiu th cc i khi ngun nui: 30VA
- in p li vo 220VAC 20%
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
49
CHNG 5. CC GII THUT PHN MM
5.1 Gii thut ng m v iu khin in p cao p, iu khin h s khuch i
khi khuch i
My vi tnh iu khin cho vi iu khin ti my phn tch bng cch gi 2 byte qua
cng USB, byte th nht tm gi l m lnh ra lnh cho vi iu khin lm cng vic g,
byte th 2 l d liu cng vic cn iu khin. Sau y l bng tng kt quy c cc byte
iu khin chuyn t my tnh n vi iu khin.
Cng vic iu khin Byte th nht (m lnh)
(dng s thp phn)
Byte th hai (d liu)
(dng s thp phn)
ng m cao p 01 0: tt cao p
1: m cao p
iu khin in p cao p 02 0 - 255
iu khin h s khuch i
khi khuch i
03 0-255
Bng 5.1 Bng tng kt quy c cc byte iu khin chuyn t my tnh n vi
iu khin.
ng m cao p: Khi vi iu khin nhn byte th nht s 01, byte th hai s 0, vi
iu khin xut ra mc cao bit RD5 lm Q5 (mch cao th) dn, ko theo Q1, Q2 ngng
dn lm mt in p nui cho bin p xung T1 (mch cao th) v tt cao p. Ngc li,
byte th hai s 1, vi iu khin xut ra mc thp bit RD5 lm Q5 ngng, ko theo Q1, Q2
dn, c in p nui cho bin p xung T1 , c cao p.
iu khin in p cao p: Khi vi iu khin nhn byte th nht l s 2, byte th
l con s 0-255, vi iu khin ly d liu byte th hai iu khin bin tr s U9-
MCP41010 bng giao thc SPI (ti mch logic). MCP41010 l bin tr s 10 . Khi d
liu byte th hai l 255, im gia W ca bin tr s i v im A v c in th bng 5V,
in th li ra ny c m bi U10-LM324 (ti mch logic) tng ng vi in th cao
p li ra cc i l 2 kV. T 0V n 2KV c chia thnh 255 bc, mi bc ng vi
8V.
KW
M ngun ng/m cao p v iu chnh cao p
Private Sub CmdHVon-off_Click() nt lnh on/off cao p
If Command2.Caption = "OFF" Then
Command2.Caption = "ON"
Command1.Enabled = True
If Not MyDeviceDetected Then
MyDeviceDetected = FindTheHid
End If
If MyDeviceDetected Then
OutputReportData(0) = 2
If HScroll1.Value <= 125 Then
OutputReportData(1) = 2 * HScroll1.Value
Else
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
50
OutputReportData(1) = 250
End If
OutputReportData(2) = 255
OutputReportData(3) = 255
OutputReportData(4) = 255
OutputReportData(5) = 255
OutputReportData(6) = 255
OutputReportData(7) = 255
Call ReadAndWriteToDevice
End If
Label3.Caption = Str(8 * HScroll1.Value) + "V"
Else
Command2.Caption = "OFF"
Command1.Enabled = False
If Not MyDeviceDetected Then
MyDeviceDetected = FindTheHid
End If
If MyDeviceDetected Then
OutputReportData(0) = 2
OutputReportData(1) = 0
OutputReportData(2) = 255
OutputReportData(3) = 255
OutputReportData(4) = 255
OutputReportData(5) = 255
OutputReportData(6) = 255
OutputReportData(7) = 255
Call ReadAndWriteToDevice
End If
Label3.Caption = "0V"
End If
End Sub
Private Sub HScrollHV_Change() thanh cun t cao p
'High Voltage
If Not MyDeviceDetected Then
MyDeviceDetected = FindTheHid
End If
If MyDeviceDetected Then
If Label10.Caption = "HV ON" Then
OutputReportData(0) = 2
If HScroll1.Value <= 125 Then
OutputReportData(1) = 2 * HScroll1.Value
Else
OutputReportData(1) = 250
End If
OutputReportData(2) = 255
OutputReportData(3) = 255
OutputReportData(4) = 255
OutputReportData(5) = 255
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
51
OutputReportData(6) = 255
OutputReportData(7) = 255
Call ReadAndWriteToDevice
Else
OutputReportData(1) = 0
OutputReportData(2) = 255
OutputReportData(3) = 255
OutputReportData(4) = 255
OutputReportData(5) = 255
OutputReportData(6) = 255
OutputReportData(7) = 255
Call ReadAndWriteToDevice
End If
End If
Label3.Caption = Str(8 * HScroll1.Value) + "V"
End Sub
iu khin h s khuch i khi khuch i: Khi vi iu khin nhn byte th
nht l s 3, byte d liu th hai vi iu khin dng iu khin bin tr s U5 (ti mch
khuch i). H s khuch i ca U2 (ti mch khuch i) tnh theo cng thc sau:
18 WB
V
17 WA
R R
A 1
R R
+
= +
+
M ngun iu khin h s khuch i khi khuch i
Private Sub HScrollAmp_Change() thanh cun t h s khuch i
'amplifier gain
If Not MyDeviceDetected Then
MyDeviceDetected = FindTheHid
End If
If MyDeviceDetected Then
OutputReportData(0) = 4
OutputReportData(1) = HScroll2.Value
OutputReportData(2) = 255
OutputReportData(3) = 255
OutputReportData(4) = 255
OutputReportData(5) = 255
OutputReportData(6) = 255
OutputReportData(7) = 255
Call ReadAndWriteToDevice
End If
Label4.Caption = Str(0.063 * HScroll2.Value + 26) + "db"
End Sub
5.2 Gii thut hin th ph: Mt im ph tng trng bi mt im (Shape1(i)), cao
l hm ca s m M(i) v gi tr thanh cun Vscroll1.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
52
Hnh 5.1 Lu thut ton o v hin th ph.
Cc on m ngun hin th ph
Private Sub Form_Load() hin th ph lc form-load
Dim i As Integer
Line4.Visible = False
For i = 0 To 1023
Shape1(i).Height = 40
Shape1(i).Width = 40
Shape1(i).Left = 400 + i * 15
Shape1(i).Top = 5800
Next
Option1.Value = True
Option4.Value = True
Command1.Caption = "START"
Command1.Enabled = False
Timer1.Enabled = False
Label10.Caption = "HV OFF"
Label10.ForeColor = &H0&
Image1.Left = 300
Image1.Top = 4750
Line2.X1 = 540
Line2.X2 = 540
Line2.Y1 = 5800
Line2.Y2 = 5200
Image2.Left = 800
Image2.Top = 4750
Line1.X1 = 1040
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
53
Line1.X2 = 1040
Line1.Y1 = 5800
Line1.Y2 = 5200
S = 0
Hr = 0
Mn = 0
End Sub
Private Sub VScroll1_Change() hin th ph lc ko thanh cun tng gim chiu cao ph
If FlagInt = True Or FlagTra = True Then
Line4.Visible = False
FlagTra = False
For i = 0 To 1023
Line3(i).Visible = False
Next
FlagInt = False
End If
For i = 0 To 1023
Shape1(i).Top = 5800 - VScroll1.Value * M(i)
Next
Image2.Top = Shape1(Vtt2).Top - 1000
Line1.Y1 = Shape1(Vtt2).Top
Line1.Y2 = Shape1(Vtt2).Top - 600
Image1.Top = Shape1(Vtt1).Top - 1000
Line2.Y1 = Shape1(Vtt1).Top
Line2.Y2 = Shape1(Vtt1).Top - 600
End Sub
Private Sub Option1_Click() ty chn full scale
'Full Scale
If FlagInt = True Or FlagTra = True Then
Line4.Visible = False
FlagTra = False
For i = 0 To 1023
Line3(i).Visible = False
Next
FlagInt = False
End If
For i = 0 To 1023
Shape1(i).Left = 400 + i * 15
Shape1(i).Visible = True
Next
End Sub
Private Sub Option2_Click() ty chn low half scale
'Low Half Scale
If FlagInt = True Or FlagTra = True Then
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
54
Line4.Visible = False
FlagTra = False
For i = 0 To 1023
Line3(i).Visible = False
Next
FlagInt = False
End If
For i = 0 To 511
Shape1(i).Left = 400 + i * 30
Shape1(i).Visible = True
Shape1(i + 512).Visible = False
Next
End Sub
Private Sub Option3_Click() ty chn high half scale
'High Half Scale
If FlagInt = True Or FlagTra = True Then
Line4.Visible = False
FlagTra = False
For i = 0 To 1023
Line3(i).Visible = False
Next
FlagInt = False
End If
For i = 512 To 1023
Shape1(i).Left = -14960 + i * 30
Shape1(i).Visible = True
Shape1(i - 512).Visible = False
Next
End Sub
Private Sub Timer1_Timer() cp nht ph
tempL = ReadBuffer(2) ' byte thap
tempH = ReadBuffer(3) ' byte cao
tempLo = tempL
tempHo = tempH
Ch = tempH * 256 + tempL
If Ch > 30 Then 'cat cac kenh thap
M(Ch) = M(Ch) + 1
Shape1(Ch).Top = 5800 - VScroll1.Value * M(Ch)
End If
End Sub
on m ngun ko con tr 1 v 2
Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As
Single)
Dim j As Integer
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
55
Dim k, c, Akev, Bkev As Double
Dim tamAkev As String
If Button = 1 Then
If FlagInt = True Or FlagTra = True Then
Line4.Visible = False
FlagTra = False
For i = 0 To 1023
Line3(i).Visible = False
Next
FlagInt = False
End If
If ((Image1.Left >= 200) And (Image1.Left <= 15600)) Then
Image1.Move Image1.Left + x - 180, 5000 'Image2.Top + Y - 100
If Option1.Value = True Then
For j = 0 To 1023
If (Abs(Image1.Left + x - Shape1(j).Left) < 40) Then
Image1.Top = Shape1(j).Top - 1000
Line2.X1 = Shape1(j).Left
Line2.X2 = Shape1(j).Left
Line2.Y1 = Shape1(j).Top
Line2.Y2 = Shape1(j).Top - 600
If Label24.Caption = "Cal" Then
Label9.Caption = Str(M(j)) + "/" + Str(Format(Kev(j), "00.000"))
Else
Label9.Caption = Str(M(j)) + "/" + Str(j)
End If
Vtt1 = j
End If
Next
End If
If Option2.Value = True Then
For j = 0 To 511
If (Abs(Image1.Left + x - Shape1(j).Left) < 40) Then
Image1.Top = Shape1(j).Top - 1000
Line2.X1 = Shape1(j).Left
Line2.X2 = Shape1(j).Left
Line2.Y1 = Shape1(j).Top
Line2.Y2 = Shape1(j).Top - 600
Label9.Caption = Str(M(j)) + "/" + Str(j)
Vtt1 = j
End If
Next
End If
If Option3.Value = True Then
For j = 512 To 1023
If (Abs(Image1.Left + x - Shape1(j).Left) < 40) Then
Image1.Top = Shape1(j).Top - 1000
Line2.X1 = Shape1(j).Left
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
56
Line2.X2 = Shape1(j).Left
Line2.Y1 = Shape1(j).Top
Line2.Y2 = Shape1(j).Top - 600
Label9.Caption = Str(M(j)) + "/" + Str(j)
Vtt1 = j
End If
Next
End If
Else
If Image1.Left < 200 Then
Image1.Left = 220
End If
If Image1.Left > 15600 Then
Image1.Left = 15500
End If
End If
Label17.Caption = ""
Label19.Caption = ""
End If
End Sub
Private Sub Image2_MouseMove(Button As Integer, Shift As Integer, x As Single, y As
Single)
Dim j As Integer
Dim k, c, Akev, Bkev As Double
Dim tamAkev As String
If Button = 1 Then
If FlagInt = True Or FlagTra = True Then
Line4.Visible = False
FlagTra = False
For i = 0 To 1023
Line3(i).Visible = False
Next
FlagInt = False
End If
If ((Image2.Left >= 200) And (Image2.Left <= 15600)) Then
Image2.Move Image2.Left + x - 180, 5000 'Image2.Top + Y - 100
If Option1.Value = True Then
For j = 0 To 1023
If (Abs(Image2.Left + x - Shape1(j).Left) < 40) Then
Image2.Top = Shape1(j).Top - 1000
Line1.X1 = Shape1(j).Left
Line1.X2 = Shape1(j).Left
Line1.Y1 = Shape1(j).Top
Line1.Y2 = Shape1(j).Top - 600
If Label24.Caption = "Cal" Then
Label8.Caption = Str(M(j)) + "/" + Str(Format(Kev(j), "00.000"))
Else
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
57
Label8.Caption = Str(M(j)) + "/" + Str(j)
End If
Vtt2 = j
End If
Next
End If
If Option2.Value = True Then
For j = 0 To 511
If (Abs(Image2.Left + x - Shape1(j).Left) < 40) Then
Image2.Top = Shape1(j).Top - 1000
Line1.X1 = Shape1(j).Left
Line1.X2 = Shape1(j).Left
Line1.Y1 = Shape1(j).Top
Line1.Y2 = Shape1(j).Top - 600
Label8.Caption = Str(M(j)) + "/" + Str(j)
Vtt2 = j
End If
Next
End If
If Option3.Value = True Then
For j = 512 To 1023
If (Abs(Image2.Left + x - Shape1(j).Left) < 40) Then
Image2.Top = Shape1(j).Top - 1000
Line1.X1 = Shape1(j).Left
Line1.X2 = Shape1(j).Left
Line1.Y1 = Shape1(j).Top
Line1.Y2 = Shape1(j).Top - 600
Label8.Caption = Str(M(j)) + "/" + Str(j)
Vtt2 = j
End If
Next
End If
Else
If Image2.Left < 200 Then
Image2.Left = 220
End If
If Image2.Left > 15600 Then
Image2.Left = 15500
End If
End If
Label17.Caption = ""
Label19.Caption = ""
End If
End Sub
5.3 Gii thut lm trn ph: Mi on ph ngn c th xem nh mt a thc ton hc.
Gi tr ca a thc v o hm ca n c th xem nh l hm ca s m trong mi knh
ca ph v cho bi cng thc sau [3]:
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
58
=-
=
m
n,m k,n,m
k m n,m
1
Y (i) C y(i k)
N
+ (5.1)
Vi l o hm bc n ca ph lm trn ti knh i
n,m
Y (i)
l s m ti knh th (i+k) + y(i k)
v l cc hng s cho trong bng (3.2)
k,n,m
C
n,m
N
m lin h vi s im lm trn m l: m= 2m+1
S im ti u dng lm trn ph thuc vo dng c th ca vng ph ang xt.
r n m
n,m
N k
0 1 2 3 4 5 6 7
k,n,m
C
2
3
0 5
7
9
11
13
15
35
21
231
429
143
1105
17
7
59
89
25
167
12
6
54
84
24
162
-3
3
39
69
21
147
-2
14
44
16
122
-21
9
9
87
-36
0
42
-11
-13
-78
k,n,m
C
2 1 5
7
9
11
13
15
10
28
60
110
182
280
0
0
0
0
0
0
1
1
1
1
1
1
2
2
2
2
2
2
3
3
3
3
3
4
4
4
4
5
5
5
6
6
7
k,n,m
C
3 1 5
7
9
11
13
15
12
252
1188
5148
24024
334152
0
0
0
0
0
0
8
58
126
296
832
7506
-1
67
193
503
1489
13843
-22
142
532
1796
17842
-86
294
1578
18334
-300
660
14150
-1133
4121
-12922
k,n,m
C
2
3
2 5
7
9
11
13
15
7
42
462
429
1001
6188
-2
-4
-20
-10
-14
-56
-1
-3
-17
-9
-13
-53
k,n,m
C
Bng 5.2 Cc h s v
n,m
N
k,n,m
C
Vi r l bc ca a thc lm khp
m=2m+1
V d:
- Lm trn ca s 5 im, bc 2: r = 2, n=0, m = 5, ta c cc h s lm trn
l: -3/35 = -0.09; 12/35 = 0.34; 17/35 = 0.48; 12/35 = 0.34; -3/35 = -0.09
- Lm trn ca s 7 im, bc 3: r = 3, n = 0, m = 7, cc h s lm trn l:
-2/21 = -0.1; 3/21 = 0.14; 6/21 = 0.29; 7/21 = 0.33; 6/21 = 0.29; 3/21 = 0.14;
-2/21 = -0.1
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
59
- Lm trn ca s 9 im, bc 3: r=3, n=0, m=9, cc h s lm trn l:
-21/231 = -0.09; 14/231 = 0.06; 39/231 = 0.17; 54/231 = 0.23; 59/231 = 0.26
; 54/231 = 0.23; 39/231 = 0.17; 14/231 = 0.06; -21/231 = -0.09
Hnh 5.2 Lu thut ton lm trn ph ca s 5 im.
M ngun lm trn ph
Private Sub CmdSpline_Click()
Dim Response As Integer
Dim st As String
Response = MsgBox("You should save spectrum before spline", vbYesNo)
If Response = 6 Then 'yes
CommonDialog1.Filter = "DataFile(*.spt)|*.spt|AllData(*.*)|*.*"
CommonDialog1.ShowSave
file1 = CommonDialog1.FileName
st = ""
Open file1 For Output As #30
Close #30
Open file1 For Append As #30
For i = 0 To 1023
st = Str(M(i)) + Chr(9)
Print #30, st
Next
Close #30
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
60
End If
FlagInt = True
If Option1(0).Value = True Then ' lam tron 3 diem
For i = 0 To 1021
SPL(i + 1) = 0.25 * M(i) + 0.5 * M(i + 1) + 0.25 * M(i + 2)
Next
SPL(0) = 0.5 * M(0) + 0.5 * M(1)
SPL(1023) = 0.5 * M(1022) + 0.5 * M(1023)
For i = 0 To 1023
M(i) = SPL(i)
Next
End If
If Option1(1).Value = True Then 'lam tron 5 diem
For i = 0 To 1019
SPL(i + 2) = -0.09 * M(i) + 0.34 * M(i + 1) + 0.48 * M(i + 2) + 0.34 * M(i + 3) - 0.09 *
M(i + 4)
Next
SPL(0) = 0.48 * M(0) + 0.68 * M(1) - 0.18 * M(2)
SPL(1) = 0.34 * M(0) + 0.48 * M(1) + 0.34 * M(2) - 0.18 * M(3)
SPL(1022) = -0.18 * M(1020) + 0.34 * M(1021) + 0.48 * M(1022) + 0.34 * M(1023)
SPL(1023) = -0.18 * M(1021) + 0.68 * M(1022) + 0.48 * M(1023)
For i = 0 To 1023
M(i) = SPL(i)
Next
End If
If Option1(2).Value = True Then 'lam tron 7 diem
For i = 0 To 1017
SPL(i + 3) = -0.1 * M(i) + 0.14 * M(i + 1) + 0.29 * M(i + 2) + 0.33 * M(i + 3) + 0.29 * M(i
+ 4) + 0.14 * M(i + 5) - 0.1 * M(i + 6)
Next
SPL(0) = 0.33 * M(0) + 0.58 * M(1) + 0.28 * M(2) - 0.2 * M(3)
SPL(1) = 0.29 * M(0) + 0.33 * M(1) + 0.29 * M(2) + 0.28 * M(3) - 0.2 * M(4)
SPL(2) = 0.14 * M(0) + 0.29 * M(1) + 0.33 * M(2) + 0.29 * M(3) + 0.14 * M(4) - 0.2 *
M(5)
SPL(1021) = -0.2 * M(1018) + 0.14 * M(1019) + 0.29 * M(1020) + 0.33 * M(1021) + 0.29
* M(1022) + 0.14 * M(1023)
SPL(1022) =- 0.2 * M(1019) + 0.28 * M(1020) + 0.29 * M(1021) + 0.33 * M(1022) + 0.29
* M(1023)
SPL(1023) = -0.2 * M(1020) + 0.28 * M(1021) + 0.58 * M(1022) + 0.33 * M(1023)
For i = 0 To 1023
M(i) = SPL(i)
Next
End If
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
61
If Option1(3).Value = True Then 'lam tron 9 diem
For i = 0 To 1015
SPL(i + 4) = -0.09 * M(i) + 0.06 * M(i + 1) + 0.17 * M(i + 2) + 0.23 * M(i + 3) + 0.26 *
M(i + 4) + 0.23 * M(i + 5) + 0.17 * M(i + 6) + 0.06 * M(i + 7) - 0.09 * M(i + 8)
Next
SPL(0) = 0.26 * M(0) + 0.46 * M(1) + 0.34 * M(2) + 0.12 * M(3) - 0.18 * M(4)
SPL(1) = 0.23 * M(0) + 0.26 * M(1) + 0.23 * M(2) + 0.34 * M(3) + 0.12 * M(4) - 0.09 *
M(5)
SPL(2) = 0.17 * M(0) + 0.23 * M(1) + 0.26 * M(2) + 0.23 * M(3) + 0.17 * M(4) + 0.12 *
M(5) - 0.18 * M(6)
SPL(3) = 0.06 * M(0) + 0.17 * M(1) + 0.23 * M(2) + 0.26 * M(3) + 0.23 * M(4) + 0.17 *
M(5) + 0.06 * M(6) - 0.18 * M(7)
SPL(1020) = -0.18 * M(1016) + 0.06 * M(1017) + 0.17 * M(1018) + 0.23 * M(1019) +
0.26 * M(1020) + 0.23 * M(1021) + 0.17 * M(1022) + 0.06 * M(1023)
SPL(1021) = -0.18 * M(1017) + 0.12 * M(1018) + 0.17 * M(1019) + 0.23 * M(1020) +
0.26 * M(1021) + 0.23 * M(1022) + 0.17 * M(1023)
SPL(1022) = -0.18 * M(1018) + 0.12 * M(1019) + 0.34 * M(1020) + 0.23 * M(1021) +
0.26 * M(1022) + 0.23 * M(1023)
SPL(1023) = -0.18 * M(1018) + 0.12 * M(1020) + 0.34 * M(1021) + 0.46 * M(1022) +
0.26 * M(1023)
For i = 0 To 1023
M(i) = SPL(i)
Next
End If
If frmMain.Option1.Value = True Then
For i = 0 To 1023
frmMain.Shape1(i).Left = 400 + i * 15
frmMain.Shape1(i).Top = 5800 - frmMain.VScroll1.Value * M(i)
frmMain.Shape1(i).Visible = True
Next
End If
If frmMain.Option2.Value = True Then
For i = 0 To 511
frmMain.Shape1(i).Left = 400 + i * 30
frmMain.Shape1(i).Top = 5800 - frmMain.VScroll1.Value * M(i)
frmMain.Shape1(i).Visible = True
frmMain.Shape1(i + 512).Visible = False
Next
End If
If frmMain.Option3.Value = True Then
For i = 512 To 1023
frmMain.Shape1(i).Left = 400 + i * 30
frmMain.Shape1(i).Top = 5800 - frmMain.VScroll1.Value * M(i)
frmMain.Shape1(i).Visible = True
frmMain.Shape1(i - 512).Visible = False
Next
End If
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
62
End Sub
5.4 Gii thut ly tch phn gia 2 v tr con tr v hin th min ly tch phn: Tch
phn gia 2 v tr con tr l tng cc s m ca cc knh gia 2 v tr con tr, gi l
INT. Gi vttt l knh ca con tr bn tri v gi vttp l knh ca con tr bn phi,
Hnh 5.3 Gii thut ly tch phn gia hai v tr con tr
M ngun ly tch phn
Private Sub mnuIntegral_Click()
Dim Integral As Long
Integral = 0
FlagInt = True
For i = 0 To 1023
Line3(i).Visible = False
Next
If Vtt1 > Vtt2 Then
For i = Vtt2 To Vtt1
Line3(i).X1 = Shape1(i).Left
Line3(i).X2 = Shape1(i).Left
Line3(i).Y1 = 5800
Line3(i).Y2 = 5800 - VScroll1.Value * M(i)
Line3(i).Visible = True
Integral = Integral + M(i)
Next
Else
For i = Vtt1 To Vtt2
Line3(i).X1 = Shape1(i).Left
Line3(i).X2 = Shape1(i).Left
Line3(i).Y1 = 5800
Line3(i).Y2 = 5800 - VScroll1.Value * M(i)
Line3(i).Visible = True
Integral = Integral + M(i)
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
63
Next
End If
Label17.Caption = Str(Integral)
End Sub
3.5 Gii thut ly din tch hnh thang: Din tch hnh thang gia 2 v tr con tr
M(vtt1) M(vtt2)
DTHT (vtt2 vtt1 1)
2
+
= - + (5.2)
Hnh 5.4 Lu thut ton tnh din tch hnh thang ti v tr 2 con tr
M ngun tnh din tch hnh thang
Private Sub mnuSurface_Click()
Dim Trape As Long
Dim Day As Integer
Day = Abs(Vtt2 - Vtt1) + 1
Trape = (M(Vtt1) + M(Vtt2)) / 2 * Day
Label19.Caption = Str(Trape)
Line4.X1 = Shape1(Vtt1).Left
Line4.X2 = Shape1(Vtt2).Left
Line4.Y1 = Shape1(Vtt1).Top
Line4.Y2 = Shape1(Vtt2).Top
Line4.Visible = True
Line3(Vtt1).X1 = Shape1(Vtt1).Left
Line3(Vtt1).X2 = Shape1(Vtt1).Left
Line3(Vtt1).Y2 = Shape1(Vtt1).Top
Line3(Vtt1).Y1 = 5800
Line3(Vtt1).Visible = True
Line3(Vtt2).X1 = Shape1(Vtt2).Left
Line3(Vtt2).X2 = Shape1(Vtt2).Left
Line3(Vtt2).Y2 = Shape1(Vtt2).Top
Line3(Vtt2).Y1 = 5800
Line3(Vtt2).Visible = True
FlagTra = True
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
64
End Sub
5.6 Gii thut chun ha nng lng cho ph
Gi x l v tr knh v l nng lng ti knh , ta c ng chun nng lng
bc n theo cng thc sau:
i
E
i
x
(5.3) E
-
-
= + + + +
n n 1
n n 1 1
a x a x ... a x a
0
1
T
Ti im quan st th nht:
1
E
-
-
= + + + +
n n 1
n 1 n 1 2 1 1 0
a x a x ... a x a
Ti im quan st th hai:
2
E
-
-
= + + + +
n n 1
n 2 n 1 2 1 2 0
a x a x ... a x a
Ti im quan st th m:
m
E
-
-
= + + + +
n n 1
n m n 1 m 1 m 0
a x a x ... a x a
ng chun nng lng c vit li di dng ma trn:
(5.4) = E X*A
X l ma trn (m,n+1), m hng, n+1 ct
A, E l ma trn ct, m ct
tm cc h s a, ta gii cc phng trnh i s (5.4)
= X*A E
h phng trnh i s l c nghim th + m n
Nhn 2 v phng trnh trn vi ( l chuyn v ca X) t bn tri, ta c:
T
X
T
X
(5.5) =
T
X *X*A X *E
Nhn 2 v ca phng trnh (5.5) vi ( inv l hm ly nghch o ma
trn), ta c:
T
inv(X *X)
=
T T
A inv(X *X)*X *E (5.6)
Ta dng phng trnh (5.6) tnh cc h s a ca hm s xp x bng a thc.
Hnh 5.5 Lu thut ton chun ha nng lng
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
65
on m ngun chun nng lng
Private Sub CmdCal_Click()
Dim l As Integer
Dim m As Integer
Dim min As Integer
Dim Bac As Integer
Dim i, j As Integer
Dim b() As Double
Dim c() As Double
Dim d() As Double
Dim e() As Double
Dim F() As Double
Dim p() As Double
Dim mat1() As Double
l = 0
m = 0
For i = 0 To 9 c cc knh
If Text1(i) <> "" Then
l = l + 1
Else
Exit For
End If
Next
For i = 0 To 9 c cc mc nng lng tng ng vi s knh
If Text2(i) <> "" Then
m = m + 1
Else
Exit For
End If
Next
If m <= l Then
min = m
Else
min = l
End If
min = min - 1
If min >= 0 Then
ReDim X(0, min) As Double
ReDim Y(min, 0) As Double
ReDim z(min, 0) As Double
ReDim e(min, 0) As Double
For i = 0 To (min)
X(0, i) = CDbl(Text1(i).Text)
Y(i, 0) = CDbl(Text2(i).Text)
Next
Bac = min
b = taomtA(X, Bac)
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
66
c = Mat.Transpose(b)
d = Mat.Multiply(c, b)
mat1 = Mat.Find_R_C(d)
e = Mat.Inv(d)
F = Mat.Multiply(e, c)
p = Mat.Multiply(F, Y)
For i = 0 To min
z(i, 0) = 0
Next
For i = 0 To min
For j = 0 To Bac
z(i, 0) = z(i, 0) + p(j, 0) * X(0, i) ^ (Bac - j)
e(i, 0) = Abs(z(i, 0) - Y(i, 0))
Next
Text4(i).Text = Format(e(i, 0), "00.00e-00")
Text3(i).Text = Format(z(i, 0), "00.0000")
Next
For j = 0 To Bac
Text5(j).Text = Format(p(Bac - j, 0), "00.0000")
Next
For i = 0 To 1023
Kev(i) = 0
Next
For i = 0 To 1023
For j = 0 To Bac
Kev(i) = Kev(i) + p(j, 0) * i ^ (Bac - j)
Next
Next
Else
End If
End Sub
Private Function taomtA(X() As Double, n As Integer) As Double()
Dim mat1() As Double
Dim hang As Double
Dim i, j As Integer
mat1 = Mat.Find_R_C(X)
hang = mat1(0, 1) - 1
ReDim A(hang, n) As Double
For i = 0 To hang
For j = 0 To n
A(i, j) = 1
Next
Next
For j = n - 1 To 0 Step -1
For i = 0 To hang
A(i, j) = A(i, j + 1) * X(0, i)
Next
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
67
Next
taomtA = A
End Function
5.7 Gii thut o thi gian cht phn trm
D
T %
Thi gian cht phn trm c nh ngha:
D
T %
=
D
D
R
T
T % *100%
T
(5.7)
Vi , N l s xung m c trong thi gian , l thi gian cht
trn mt xung. Nu ta hin th thi gian cht trong khong thi gian o =1 giy v
n v giy, th cng thc (3.7) tr thnh:
D D/P
T T * = N
R
T
D/P
T
D
T %
R
T
D/P
T
(5.8)
D D/P
T % T *N*100% =
Xc nh thi gian cht trn mt xung bng thc nghim ( )
D/P
T
Dng my pht xung cp vo li vo MCA
Hnh 5.6 a/ Biu din xung li vo MCA v thi gian cht 1 xung.
b/ Khi tn s xung li vo ln hn
c/ Khi tn s xung li vo ln, xut hin hin tng c
xung khng c x l, tc m gim mt na.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
68
Bng thc nghim, tng dn tn s xung ti mt lc no thy xut hin hin tng
tc m gim mt na th dng, nh trn hnh 3.6 c/, ta thy
D/P
1
T
F
= , F l tn s xung
li vo cc i. Kt qu thc nghim cho thy F = 50KHz, nh vy
D/P
T 20m = s
Hnh 5.7 Lu thut ton o thi gian cht phn trm
M ngun o thi gian cht phn trm
D
T %
Private Sub Timer1_Timer() '1 ms
Dim StrTdead, SDead As String
tempL = ReadBuffer(2) ' byte thap c d liu bin i AD
tempH = ReadBuffer(3) ' byte cao
CntDead = CntDead + 1 s m xung
StrTdead = Time
SDead = Mid(StrTdead, 7, 2)
If SDead <> GiDead Then
TimeDead = CntDead * 0.002 20us*100
Label27.Caption = Str(TimeDead) & " %"
GiDead = SDead
CntDead = 0
End If
End Sub
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
69
5.8 M ngun lu file, m file
Private Sub mnuSaveAS_Click()
Dim st As String
CommonDialog1.Filter = "DataFile(*.spt)|*.spt|AllData(*.*)|*.*"
CommonDialog1.ShowSave
file1 = CommonDialog1.FileName
st = ""
Open file1 For Output As #30
Close #30
Open file1 For Append As #30
For i = 0 To 1023
st = Str(M(i)) + Chr(9)
Print #30, st
Next
Close #30
End Sub
Private Sub mnuOpen_Click()
Dim NextLine As String
Dim DataRead As Integer
Dim j As Integer
With CommonDialog1
.Filter = "DataFile(*.spt)|*.spt|AllData(*.*)|*.*"
.ShowOpen
End With
If CommonDialog1.FileName <> "" Then
Open CommonDialog1.FileName For Input As #30
j = 0
NextLine = ""
Do While Not EOF(30)
Line Input #30, NextLine
M(j) = CInt(NextLine)
NextLine = ""
j = j + 1
Loop
Close #30
End If
If Option1.Value = True Then
For i = 0 To 1023
Shape1(i).Left = 400 + i * 15
Shape1(i).Top = 5800 - VScroll1.Value * M(i)
Shape1(i).Visible = True
Next
End If
If Option2.Value = True Then
For i = 0 To 511
Shape1(i).Left = 400 + i * 30
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
70
Shape1(i).Top = 5800 - VScroll1.Value * M(i)
Shape1(i).Visible = True
Shape1(i + 512).Visible = False
Next
End If
If Option3.Value = True Then
For i = 512 To 1023
Shape1(i).Left = 400 + i * 30
Shape1(i).Top = 5800 - VScroll1.Value * M(i)
Shape1(i).Visible = True
Shape1(i - 512).Visible = False
Next
End If
End Sub
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
71
CHNG 6. O CC C TRNG CA PHN CNG
6.1 PHI TUYN TCH PHN V PHI TUYN VI PHN
Dng my pht xung chun cp vo li vo, mi gi tr bin in p c o vi
thi gian 1 pht, ghi nhn s knh trn MCA, tnh dc trung bnh a, di b v rng
knh trung bnh, sau tnh s liu ct lch knh v rng knh nh bng sau y.
stt Bin
xung
vo
S
knh
lch
knh
( D)
rng
knh
stt Bin
xung
vo
S
knh
lch
knh
( D)
rng
knh
01 0.2 49 0.06 26 2.7 556 0.06 20
02 0.3 69 0.34 20 27 2.8 576 0.34 20
03 0.4 90 -0.38 21 28 2.9 597 -0.38 21
04 0.5 110 -0.10 20 29 3.0 617 -0.1 20
05 0.6 130 0.18 20 30 3.1 637 0.18 20
06 0.7 151 -0.50 21 31 3.2 658 -0.50 21
07 0.8 171 -0.26 20 32 3.3 678 -0.26 20
08 0.9 191 0.02 20 33 3.4 698 0.02 20
09 1.0 211 0.30 20 34 3.5 718 0.30 20
10 1.1 232 -0.42 21 35 3.6 739 -0.42 21
11 1.2 252 -0.14 20 36 3.7 759 -0.14 20
12 1.3 272 0.14 20 37 3.8 779 0.14 20
13 1.4 292 0.42 20 38 3.9 799 0.42 20
14 1.5 313 -0.30 21 39 4.0 820 -0.30 21
15 1.6 333 -0.02 20 40 4.1 840 -0.02 20
16 1.7 353 0.26 20 41 4.2 860 0.26 20
17 1.8 374 -0.46 21 42 4.3 881 -0.46 21
18 1.9 394 -0.18 20 43 4.4 901 -0.18 20
19 2.0 414 0.10 20 44 4.5 921 0.10 20
20 2.1 434 0.38 20 45 4.6 941 0.38 20
21 2.2 455 -0.34 21 46 4.7 962 -0.34 21
22 2.3 475 -0.06 20 47 4.8 982 -0.06 20
23 2.4 495 0.22 20 48 4.9 1002 0.22 20
24 2.5 516 -0.50 21 49 5.0 1022 0.5 20
25 2.6 536 -0.22 20
dc trung bnh a =202.8
di b = 8.5
rng knh trung bnh = 20.3
Bng 6.1 S liu xc nh phi tuyn tch phn v vi phn
phi tuyn tch phn c nh ngha l t s gia lch knh cc i ( ) v
s knh cc i nhn 100%.
D
MAX
e
D
= =
MAX
i
MAX
0.5
100% 100% 0.05%
sokenh 1024
(6.1)
Vy trn ton giai o e
i
0.05%
phi tuyn vi phn c nh ngha l t s gia lnh rng knh ln nht
v rng trung bnh nhn 100%.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
72
do rong kenh - do rong kenh tb
100%
do rong kenh tb
MAX
d
e (6.2)
e
-
= =
d
21 20.3
100% 3.4%
20.3
+
e
-
= =
d
20 21.3
100% 1.5%
20.3
-
Vy trn ton giai o e
d
3.4%
S dng my pht xung Reference Pulser CANBERRA 1407
6.2 PHN GII (FWHM: rng na chiu cao)
c o vi nh ph Cs 137 vi detector nhp nhy CANBERRA. Chun nng
lng vi nh ph Cs137 l 664 Kev v hai nh ph Co-60 l 1174.24 v 1332.50 s
dng cng c tnh rng na chiu cao, c kt qu
FWHM (6.4) 48.6Kev =
6.3 TRI NH PH
c o vi nh ph Cs 137 ti cc h s khuch i khc nhau, mi v tr ph o
lin tc vi thi gian 24h.
Gii knh tri tri nh ph %
100 -100
=
0.0
0.0%
100
533 - 534
=
0.5
0.1%
533
1000 -1001
=
0.5
0.05%
1000
Bng 6.2 S liu tri nh ph
tri nh ph b hn 0.05%
6.4 THI GIAN CHT TRN MT XUNG ( )
D/P
T
Nh trnh by phn 3.7, thi gian cht trn mt xung c xc nh bng
thc nghim bng cch xc nh tn s lm vic ti a F. F c xc nh bng thc
nghim l 50KHZ, nn
D/P
T
D/P
1 1
T 2
F 50
m = = = 0 s
6.5 TRI H S KHUCH I PH
nhit 25
o
C, t bin tn hiu li vo 0,2V, bin tn hiu ra li ra 8V: h
s khuch i 40 ln
nhit 55
o
C, bin tn hiu li ra o c l 8,03V: h s khuch i l 40,15
ln.
o o V
V
A 0.15
/30 C /30 C 0.37%/30 C
A 40
D
= =
o
o 0 V
V
A
/ C 0.01%/ C
A
D
=
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
73
Dng tn hiu sin, tn s 1KHz, cc t C1, C9 khuch i ph thay bng gi tr
0.1 , in p xoay chiu o bng Volt k s Kaise SK-6511. F m
6.6 TRI MC ZERO CA KHUCH I PH
tn s xung cc i 50Khz, rng xung 1 , tri mc zero li ra khuch
i ph l t 0mV xung -25mV khi nhit thay i t 25
s m
o
C n 55
0
C, gii mc
mt chiu li ra l . 10V
3
o 0 DC
MAX
DCMAX
V 15*10
/ C /30 C
V 10
-
D
=
0 0 DC
DCMAX
V
/ C 0.005%/ C
V
D
=
Dng oscilloscope KIKUSUI COS2040 40 MHz o mc DC.
6.7 CC C TRNG KHI CAO P
Cao p thay i t 0V n 2000V ng vi 256 mc, mi bc thay i 8V.
Kh nng cp dng cc i l 10 mA
chnh xc in p t trn ton gii in p ra 0.2%
tri in p theo nhit
0
0.005%/ C
tri in p theo thi gian 0.01%/24hr
Dng Volt k s Kaise SK-6511
6.8 CC C TRNG KHI NGUN NUI IN P THP
- Dng cc i li ra +12V, -12V, +5V l 1A
- Cng sut tiu th cc i khi ngun nui: 30VA
- in p li vo 220VAC 20%
6.9 SO SNH CC C TRNG MCA CH TO V CANBERRA series 30
S liu ca CANBERRA series 30 ly ti liu CANBERRA 81-82, xut bn
nm 1982 ca hng CANBERRA
c trng MCA ch to CANBERRA series 30 [4]
phi tuyn tch phn e
i
0.05% 0.025%
phi tuyn vi phn e
d
3.4% 1%
tri nh ph 0.1% 0.05%
tri h s khuch i ph
0
0.01%/ C
0
0.01%/ C
tri mc zero ca khuch
i ph
0.005% /
o
C
(trn ton giai o)
0.005% /
o
C
(trn ton giai o)
Bng 6.3 So snh cc c trng vi CANBERRA series 30
Ba c trng u: MCA ch to c c trng tng i tt, gn tng ng vi
CANBERRA series 30.
Hai c trng sau: MCA ch to c c trng bng vi CANBERRA series 30
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
74
CHNG 7.
CC KT QU
7.1 PHN MM V CC KT QU O
Giao din ha iu khin my phn tch a knh trn my vi tnh c thit k
vi cc tnh nng iu khin cao p; iu khin h s khuch i; t thi gian o; hin th
thi gian o; hin th ph vi cc mode hin th: Ton gii (Fullcale),na gii cao (High
Half Scale), na gii thp (low Half Scale). Hai con tr cho bit s m/ s knh
(Counts/channel) hoc s m/mc nng lng (Counts/keV). Ngoi ra cn thit k cc
cng c tin ch nh: tnh tch phn gia 2 v tr con tr; din tch hnh thang gia 2 v tr
con tr; cng c chun nng lng; cng c lm trn ph; cng c tnh rng na chiu
cao (FWHM).
7.2 CC KT QU O PH Co-60 v Cs-137
Hnh 7.1 Ph Cs-137 v Co-60 o vi thi gian 5 pht, cao p 872V, detector nhp
nhy CANBERRA hin th ch ton gii
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
75
Hnh 7.2. Ph Cs-137 o vi thi gian 5 pht, detector nhp nhy CANBERRA Bicron
model 302-2x2
Hnh 7.3 Ph Co-60 o vi thi gian 5 pht, detector nhp nhy CANBERRA Bicron
model 302-2x2
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
76
7.3 CNG C TNH PHN V TNH DIN TCH HNH THANG GIA 2
V TR CON TR
Hnh 7.4 Cng c tnh tch phn gia 2 con tr (phn t xm)
Hnh 7.5 Cng c tnh din tch hnh thang gia 2 con tr
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
77
7.4 CNG C TNH RNG NA CHIU CAO (FWHM).
Hnh 7.6 Cng c tnh rng na chiu cao (FWHM)
Hnh 7.7 Cng c lm trn ph, ph Cs-137 lm trn 1 ln ca s 7 im
7.5 CNG C CHUN HA NNG LNG
Chun ha nng lng theo a thc bc cao, bc ca a thc do phn mm t
xc nh, s bc bng s im quan st tr mt. Trn form chun nng lng c ct
nng lng xp x do a thc (ct th 3) , ct lch (ct th 4) v ct cc h s ca
a thc xp x (ct th 5). Trn form ny cn c lu v m chun nng lng.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
78
Hnh 7.8 Cng c chun ha nng lng cho ph
Hnh 7.9 Ph Co-60 v Cs-137 chun nng lng ti 3 nh ph tng
ng vi mc nng lng l 1332.50 keV, 1173.24 keV v 776 keV.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
79
7.6 TNH NNG LU PH, M PH
Hnh 7.10 Cc tnh nng lu ph v m ph lu.
Hnh 7.11 Hnh thc bn ngoi my phn tch a knh ch to
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
80
Hnh 7.12 nh bn trong my phn tch a knh ch to
KT LUN: Thit k v ch to my phn tch a knh ghp vi my tnh qua cng
USB l phng n kh thi trong iu kin k thut ca Vit nam. S dng PIC18F4550
l hp l m nhim cc cng vic: 1. Giao tip my tnh qua cng USB; 2. iu
khin hot ng cc khi chc nng; 3. Thc hin bin i AD 10 bit tng ng vi s
knh 1024. Phn mm vi cc tnh nng: 1.Hin th ph vi cc kiu: ton gii; na gii
cao; na gii thp; 2. Tnh nng ly tch phn gia 2 v tr 2 con tr; 3. Ly din tch
hnh thang gia 2 v tr 2 con tr; 4. Lm trn ph theo gii thut ly trung bnh ca s
3 im, 5 im, 7 im, 9 im; 4. Cc chc nng iu khin cao p, iu chnh h s
khuch i, tt m qu trnh o, vv. Vi cc c trng cn bn cho thy tng ng v
gn tng ng vi cc c trng my phn tch a knh Series 30 ca CANBERRA.
My c th dng cho sinh vin thc tp mn thc tp vt l ht nhn ti khoa Vt l.
ti cp b: Thit k v ch to my phn tch a knh (1024 knh) ghp vi my tnh qua cng USB
Ch nhim ti: Nguyn Vn Sn
81
TI LIU THAM KHO
[1]. Analog to digital converter 7070-CANBERRA
[2]. Analog to digital converter 8075-CANBERRA
[3]. BI QUANG KHNH, lun vn thc s vt l X L Ph Gamma Bng
Thut Ton Di Truyn, i hc Khoa hc T nhin, TP HCM 2009.
[4]. CANBERRA 81-82, 1982, CANBERRA
[5]. PHAN VN CHUN, GIO TRNH GIAO DIN MY TNH V
IU KHIN, Lt 2010.
[5]. PIC18F2455/2550/4455/4550 DATA SHEET, 2004, MICROCHIP
[6]. SPECTROSCOPE AMPLIFIER 2021-CANBERRA
[7]. Website WWW.CANBERRA.COM
[8]. Website WWW.MICROCHIP.COM
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 1
PH LC A
M NGUN PHN MM TRN MY VI TNH
M NGUN FORM MAIN
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Dim Hr, Mn, S, G, p, Gi, GiDead As Integer
Dim Ch, tempL, tempH, tempLo, tempHo, DEM, CntDead, TimeDead As Integer
Dim Sbd As Double
Dim Strtam, Strtam1, Strtam2 As String
Dim Vtt1, Vtt2 As Integer
Dim FlagInt, FlagTra, FlagCal As Boolean
Private Sub Command1_Click() NT START-STOP
If Command2.Caption = "ON" Then
If Command1.Caption = "START" Then
Command1.Caption = "STOP"
Timer1.Enabled = True
Strtam = Time
h = Mid(Strtam, 1, InStr(1, Strtam, ":") - 1)
Strtam = Mid(Strtam, InStr(1, Strtam, ":") + 1)
Mn = Mid(Strtam, 1, InStr(1, Strtam, ":") - 1)
Strtam = Mid(Strtam, InStr(1, Strtam, ":") + 1)
S = Mid(Strtam, 1, InStr(1, Strtam, " "))
Sbd = CDbl(h) * 3600 + CDbl(Mn) * 60 + CDbl(S)
Timer2.Enabled = True
G = 0
p = 0
Gi = 0
Else
Command1.Caption = "START"
Timer1.Enabled = False
Timer2.Enabled = False
Label27.Caption = ""
End If
End If
End Sub
Private Sub Command2_Click() NT ON-OFF CAO P
If Command2.Caption = "OFF" Then
Command2.Caption = "ON"
Command1.Enabled = True
If Not MyDeviceDetected Then
MyDeviceDetected = FindTheHid
End If
If MyDeviceDetected Then
OutputReportData(0) = 2
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 2
If HScroll1.Value <= 125 Then
OutputReportData(1) = 2 * HScroll1.Value
Else
OutputReportData(1) = 250
End If
OutputReportData(2) = 255
OutputReportData(3) = 255
OutputReportData(4) = 255
OutputReportData(5) = 255
OutputReportData(6) = 255
OutputReportData(7) = 255
Call ReadAndWriteToDevice
End If
Label3.Caption = Str(8 * HScroll1.Value) + "V"
Else
Command2.Caption = "OFF"
Command1.Enabled = False
If Not MyDeviceDetected Then
MyDeviceDetected = FindTheHid
End If
If MyDeviceDetected Then
OutputReportData(0) = 2
OutputReportData(1) = 0
OutputReportData(2) = 255
OutputReportData(3) = 255
OutputReportData(4) = 255
OutputReportData(5) = 255
OutputReportData(6) = 255
OutputReportData(7) = 255
Call ReadAndWriteToDevice
End If
Label3.Caption = "0V"
End If
End Sub
Private Sub Command4_Click() NT CLEAR
Dim i As Integer
Dim Response As Integer
Response = MsgBox("Are you sure to want to clear?", vbOKCancel)
If Response = 1 Then 'OK
For i = 0 To 1023
m(i) = 0
Shape1(i).Top = 5800
Next
End If
End Sub
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 3
Private Sub Command5_Click() NT EXIT
Dim F As Boolean
Dim Response As Integer
Dim st As String
Dim i As Integer
F = False
For i = 0 To 1023
If m(i) <> 0 Then
F = True
End If
Next
If F = True Then
Response = MsgBox("Do you want to save spectrum?", vbYesNo)
If Response = 6 Then 'yes
CommonDialog1.Filter = "DataFile(*.spt)|*.spt|AllData(*.*)|*.*"
CommonDialog1.ShowSave
file1 = CommonDialog1.FileName
st = ""
Open file1 For Output As #31
Close #31
Open file1 For Append As #31
For i = 0 To 1023
st = Str(m(i)) + Chr(9)
Print #31, st
Next
Close #31
End
Else
End
End If
Else
End
End If
End Sub
Private Sub Form_Load()
Dim i As Integer
Line4.Visible = False
For i = 0 To 1023
Shape1(i).Height = 40
Shape1(i).Width = 40
Shape1(i).Left = 400 + i * 15
Shape1(i).Top = 5800
Next
Option1.Value = True
Option4.Value = True
Command1.Caption = "START"
Command1.Enabled = False
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 4
Timer1.Enabled = False
Label10.Caption = "HV OFF"
Label10.ForeColor = &H0&
Image1.Left = 300
Image1.Top = 4750
Line2.X1 = 540
Line2.X2 = 540
Line2.Y1 = 5800
Line2.Y2 = 5200
Image2.Left = 800
Image2.Top = 4750
Line1.X1 = 1040
Line1.X2 = 1040
Line1.Y1 = 5800
Line1.Y2 = 5200
S = 0
Hr = 0
Mn = 0
End Sub
Private Sub HScroll1_Change() THANH KO T CAO P
If Not MyDeviceDetected Then
MyDeviceDetected = FindTheHid
End If
If MyDeviceDetected Then
If Label10.Caption = "HV ON" Then
OutputReportData(0) = 2
If HScroll1.Value <= 125 Then
OutputReportData(1) = 2 * HScroll1.Value
Else
OutputReportData(1) = 250
End If
OutputReportData(2) = 255
OutputReportData(3) = 255
OutputReportData(4) = 255
OutputReportData(5) = 255
OutputReportData(6) = 255
OutputReportData(7) = 255
Call ReadAndWriteToDevice
Else
OutputReportData(1) = 0
OutputReportData(2) = 255
OutputReportData(3) = 255
OutputReportData(4) = 255
OutputReportData(5) = 255
OutputReportData(6) = 255
OutputReportData(7) = 255
Call ReadAndWriteToDevice
End If
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 5
End If
Label3.Caption = Str(8 * HScroll1.Value) + "V"
End Sub
Private Sub HScroll2_Change() THANH KO T H S KHUCH I
If Not MyDeviceDetected Then
MyDeviceDetected = FindTheHid
End If
If MyDeviceDetected Then
OutputReportData(0) = 4
OutputReportData(1) = HScroll2.Value
OutputReportData(2) = 255
OutputReportData(3) = 255
OutputReportData(4) = 255
OutputReportData(5) = 255
OutputReportData(6) = 255
OutputReportData(7) = 255
Call ReadAndWriteToDevice
End If
Label4.Caption = Str(0.063 * HScroll2.Value + 26) + "db"
End Sub
Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As
Single) CON TR 1
Dim j As Integer
Dim k, c, Akev, Bkev As Double
Dim tamAkev As String
If Button = 1 Then
If FlagInt = True Or FlagTra = True Then
Line4.Visible = False
FlagTra = False
For i = 0 To 1023
Line3(i).Visible = False
Next
FlagInt = False
End If
If ((Image1.Left >= 200) And (Image1.Left <= 15600)) Then
Image1.Move Image1.Left + X - 180, 5000 'Image2.Top + Y - 100
If Option1.Value = True Then
For j = 0 To 1023
If (Abs(Image1.Left + X - Shape1(j).Left) < 40) Then
Image1.Top = Shape1(j).Top - 1000
Line2.X1 = Shape1(j).Left
Line2.X2 = Shape1(j).Left
Line2.Y1 = Shape1(j).Top
Line2.Y2 = Shape1(j).Top - 600
If Label24.Caption = "Cal" Then
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 6
Label9.Caption = Str(m(j)) + "/" + Str(Format(Kev(j), "00.000"))
Else
Label9.Caption = Str(m(j)) + "/" + Str(j)
End If
Vtt1 = j
End If
Next
End If
If Option2.Value = True Then
For j = 0 To 511
If (Abs(Image1.Left + X - Shape1(j).Left) < 40) Then
Image1.Top = Shape1(j).Top - 1000
Line2.X1 = Shape1(j).Left
Line2.X2 = Shape1(j).Left
Line2.Y1 = Shape1(j).Top
Line2.Y2 = Shape1(j).Top - 600
Label9.Caption = Str(m(j)) + "/" + Str(j)
Vtt1 = j
End If
Next
End If
If Option3.Value = True Then
For j = 512 To 1023
If (Abs(Image1.Left + X - Shape1(j).Left) < 40) Then
Image1.Top = Shape1(j).Top - 1000
Line2.X1 = Shape1(j).Left
Line2.X2 = Shape1(j).Left
Line2.Y1 = Shape1(j).Top
Line2.Y2 = Shape1(j).Top - 600
Label9.Caption = Str(m(j)) + "/" + Str(j)
Vtt1 = j
End If
Next
End If
Else
If Image1.Left < 200 Then
Image1.Left = 220
End If
If Image1.Left > 15600 Then
Image1.Left = 15500
End If
End If
Label17.Caption = ""
Label19.Caption = ""
End If
End Sub
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 7
Private Sub Image2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As
Single) CON TR 2
Dim j As Integer
Dim k, c, Akev, Bkev As Double
Dim tamAkev As String
If Button = 1 Then
If FlagInt = True Or FlagTra = True Then
Line4.Visible = False
FlagTra = False
For i = 0 To 1023
Line3(i).Visible = False
Next
FlagInt = False
End If
If ((Image2.Left >= 200) And (Image2.Left <= 15600)) Then
Image2.Move Image2.Left + X - 180, 5000 'Image2.Top + Y - 100
If Option1.Value = True Then
For j = 0 To 1023
If (Abs(Image2.Left + X - Shape1(j).Left) < 40) Then
Image2.Top = Shape1(j).Top - 1000
Line1.X1 = Shape1(j).Left
Line1.X2 = Shape1(j).Left
Line1.Y1 = Shape1(j).Top
Line1.Y2 = Shape1(j).Top - 600
If Label24.Caption = "Cal" Then
Label8.Caption = Str(m(j)) + "/" + Str(Format(Kev(j), "00.000"))
Else
Label8.Caption = Str(m(j)) + "/" + Str(j)
End If
Vtt2 = j
End If
Next
End If
If Option2.Value = True Then
For j = 0 To 511
If (Abs(Image2.Left + X - Shape1(j).Left) < 40) Then
Image2.Top = Shape1(j).Top - 1000
Line1.X1 = Shape1(j).Left
Line1.X2 = Shape1(j).Left
Line1.Y1 = Shape1(j).Top
Line1.Y2 = Shape1(j).Top - 600
Label8.Caption = Str(m(j)) + "/" + Str(j)
Vtt2 = j
End If
Next
End If
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 8
If Option3.Value = True Then
For j = 512 To 1023
If (Abs(Image2.Left + X - Shape1(j).Left) < 40) Then
Image2.Top = Shape1(j).Top - 1000
Line1.X1 = Shape1(j).Left
Line1.X2 = Shape1(j).Left
Line1.Y1 = Shape1(j).Top
Line1.Y2 = Shape1(j).Top - 600
Label8.Caption = Str(m(j)) + "/" + Str(j)
Vtt2 = j
End If
Next
End If
Else
If Image2.Left < 200 Then
Image2.Left = 220
End If
If Image2.Left > 15600 Then
Image2.Left = 15500
End If
End If
Label17.Caption = ""
Label19.Caption = ""
End If
End Sub
Private Sub mnuCalibate_Click() MENU CHUN NNG LNG
Dim F As New frminfo
F.Show
End Sub
Private Sub mnuExit_Click() MENU EXIT
Dim F As Boolean
Dim Response As Integer
Dim st As String
F = False
For i = 0 To 1023
If m(i) <> 0 Then
F = True
End If
Next
If F = True Then
Response = MsgBox("Do you want to save spectrum?", vbYesNo)
If Response = 6 Then 'yes
CommonDialog1.Filter = "DataFile(*.spt)|*.spt|AllData(*.*)|*.*"
CommonDialog1.ShowSave
file1 = CommonDialog1.FileName
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 9
st = ""
Open file1 For Output As #30
Close #30
Open file1 For Append As #30
For i = 0 To 1023
st = Str(m(i)) + Chr(9)
Print #30, st
Next
Close #30
End
Else
End
End If
End If
End Sub
Private Sub mnuIntegral_Click() MENU LY TCH PHN
Dim Integral As Long
Integral = 0
FlagInt = True
For i = 0 To 1023
Line3(i).Visible = False
Next
If Vtt1 > Vtt2 Then
For i = Vtt2 To Vtt1
Line3(i).X1 = Shape1(i).Left
Line3(i).X2 = Shape1(i).Left
Line3(i).Y1 = 5800
Line3(i).Y2 = 5800 - VScroll1.Value * m(i)
Line3(i).Visible = True
Integral = Integral + m(i)
Next
Else
For i = Vtt1 To Vtt2
Line3(i).X1 = Shape1(i).Left
Line3(i).X2 = Shape1(i).Left
Line3(i).Y1 = 5800
Line3(i).Y2 = 5800 - VScroll1.Value * m(i)
Line3(i).Visible = True
Integral = Integral + m(i)
Next
End If
Label17.Caption = Str(Integral)
End Sub
Private Sub mnuNew_Click() MENU NEW
For i = 0 To 1023
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 10
m(i) = 0
Next
If Option1.Value = True Then
For i = 0 To 1023
Shape1(i).Left = 400 + i * 15
Shape1(i).Visible = True
Shape1(i).Top = 5800
Next
End If
If Option2.Value = True Then
For i = 0 To 511
Shape1(i).Left = 400 + i * 30
Shape1(i).Top = 5800
Shape1(i + 512).Top = 5800
Shape1(i).Visible = True
Shape1(i + 512).Visible = False
Next
End If
If Option3.Value = True Then
For i = 512 To 1023
Shape1(i).Left = 400 + (i - 512) * 30
Shape1(i).Top = 5800
Shape1(i - 512).Top = 5800
Shape1(i).Visible = True
Shape1(i - 512).Visible = False
Next
End If
End Sub
Private Sub mnuOpen_Click() MENU OPEN
Dim NextLine As String
Dim DataRead As Integer
Dim j As Integer
With CommonDialog1
.Filter = "DataFile(*.spt)|*.spt|AllData(*.*)|*.*"
.ShowOpen
End With
If CommonDialog1.FileName <> "" Then
Open CommonDialog1.FileName For Input As #30
j = 0
NextLine = ""
Do While Not EOF(30)
Line Input #30, NextLine
m(j) = CInt(NextLine)
NextLine = ""
j = j + 1
Loop
Close #30
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 11
End If
If Option1.Value = True Then
For i = 0 To 1023
Shape1(i).Left = 400 + i * 15
Shape1(i).Top = 5800 - VScroll1.Value * m(i)
Shape1(i).Visible = True
Next
End If
If Option2.Value = True Then
For i = 0 To 511
Shape1(i).Left = 400 + i * 30
Shape1(i).Top = 5800 - VScroll1.Value * m(i)
Shape1(i).Visible = True
Shape1(i + 512).Visible = False
Next
End If
If Option3.Value = True Then
For i = 512 To 1023
Shape1(i).Left = 400 + i * 30
Shape1(i).Top = 5800 - VScroll1.Value * m(i)
Shape1(i).Visible = True
Shape1(i - 512).Visible = False
Next
End If
End Sub
Private Sub mnuSaveAS_Click() MENU SAVE AS
Dim st As String
CommonDialog1.Filter = "DataFile(*.spt)|*.spt|AllData(*.*)|*.*"
CommonDialog1.ShowSave
file1 = CommonDialog1.FileName
st = ""
Open file1 For Output As #30
Close #30
Open file1 For Append As #30
For i = 0 To 1023
st = Str(m(i)) + Chr(9)
Print #30, st
Next
Close #30
End Sub
Private Sub mnuSpline_Click() MENU LM TRN PH
Dim F As New frmSpline
F.Show
End Sub
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 12
Private Sub mnuSurface_Click() MENU LY DIN TCH HNH THANG
Dim Trape As Long
Dim Day As Integer
Day = Abs(Vtt2 - Vtt1) + 1
Trape = (m(Vtt1) + m(Vtt2)) / 2 * Day
Label19.Caption = Str(Trape)
Line4.X1 = Shape1(Vtt1).Left
Line4.X2 = Shape1(Vtt2).Left
Line4.Y1 = Shape1(Vtt1).Top
Line4.Y2 = Shape1(Vtt2).Top
Line4.Visible = True
Line3(Vtt1).X1 = Shape1(Vtt1).Left
Line3(Vtt1).X2 = Shape1(Vtt1).Left
Line3(Vtt1).Y2 = Shape1(Vtt1).Top
Line3(Vtt1).Y1 = 5800
Line3(Vtt1).Visible = True
Line3(Vtt2).X1 = Shape1(Vtt2).Left
Line3(Vtt2).X2 = Shape1(Vtt2).Left
Line3(Vtt2).Y2 = Shape1(Vtt2).Top
Line3(Vtt2).Y1 = 5800
Line3(Vtt2).Visible = True
FlagTra = True
End Sub
Private Sub Option1_Click() HIN TH TON GII
'Full Scale
If FlagInt = True Or FlagTra = True Then
Line4.Visible = False
FlagTra = False
For i = 0 To 1023
Line3(i).Visible = False
Next
FlagInt = False
End If
For i = 0 To 1023
Shape1(i).Left = 400 + i * 15
Shape1(i).Visible = True
Next
End Sub
Private Sub Option2_Click() HIN TH NA GII THP
'Low Half Scale
If FlagInt = True Or FlagTra = True Then
Line4.Visible = False
FlagTra = False
For i = 0 To 1023
Line3(i).Visible = False
Next
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 13
FlagInt = False
End If
For i = 0 To 511
Shape1(i).Left = 400 + i * 30
Shape1(i).Visible = True
Shape1(i + 512).Visible = False
Next
End Sub
Private Sub Option3_Click() HIN TH NA GII CAO
'High Half Scale
If FlagInt = True Or FlagTra = True Then
Line4.Visible = False
FlagTra = False
For i = 0 To 1023
Line3(i).Visible = False
Next
FlagInt = False
End If
For i = 512 To 1023
Shape1(i).Left = -14960 + i * 30
Shape1(i).Visible = True
Shape1(i - 512).Visible = False
Next
End Sub
Private Sub Timer1_Timer() '1 ms
Dim StrTdead, SDead As String
For i = 1 To 40
OutputReportData(0) = 3
OutputReportData(1) = 255
OutputReportData(2) = 255
OutputReportData(3) = 255
OutputReportData(4) = 255
OutputReportData(5) = 255
OutputReportData(6) = 255
OutputReportData(7) = 255
Call ReadAndWriteToDevice
tempL = ReadBuffer(2) ' byte thap
tempH = ReadBuffer(3) ' byte cao
If (tempL <> tempLo Or tempH <> tempHo) Then
tempLo = tempL
tempHo = tempH
CntDead = CntDead + 1
Ch = tempH * 256 + tempL
If Ch > 30 Then 'cat cac kenh thap
m(Ch) = m(Ch) + 1
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 14
Shape1(Ch).Top = 5800 - VScroll1.Value * m(Ch)
End If
End If
Next
StrTdead = Time
SDead = Mid(StrTdead, 7, 2)
If SDead <> GiDead Then
TimeDead = CntDead * 0.03 '* 100
Label27.Caption = Str(TimeDead) & " %"
GiDead = SDead
CntDead = 0
End If
End Sub
Private Sub Timer2_Timer()
Dim Sht As Double
Strtam1 = Strtam2
Strtam2 = Time
Strtam = Strtam2
If Strtam1 <> Strtam2 Then
h = Mid(Strtam, 1, InStr(1, Strtam, ":") - 1)
Strtam = Mid(Strtam, InStr(1, Strtam, ":") + 1)
Mn = Mid(Strtam, 1, InStr(1, Strtam, ":") - 1)
Strtam = Mid(Strtam, InStr(1, Strtam, ":") + 1)
S = Mid(Strtam, 1, InStr(1, Strtam, " "))
Sht = CDbl(h) * 3600 + CDbl(Mn) * 60 + CDbl(S)
Select Case Combo1.Text
Case "1M"
If Sbd + 60 = Sht Then
Command1.Caption = "START"
Timer1.Enabled = False
Timer2.Enabled = False
Response = MsgBox("Finish", vbDefaultButton1)
Timer3.Enabled = False
End If
Case "5M"
If Sbd + 300 = Sht Then
Command1.Caption = "START"
Timer1.Enabled = False
Timer2.Enabled = False
Response = MsgBox("Finish", vbDefaultButton1)
Timer3.Enabled = False
End If
Case "10M"
If Sbd + 600 = Sht Then
Command1.Caption = "START"
Timer1.Enabled = False
Timer2.Enabled = False
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 15
Response = MsgBox("Finish", vbDefaultButton1)
Timer3.Enabled = False
End If
Case "15M"
If Sbd + 900 = Sht Then
Command1.Caption = "START"
Timer1.Enabled = False
Timer2.Enabled = False
Response = MsgBox("Finish", vbDefaultButton1)
Timer3.Enabled = False
End If
Case "20M"
If Sbd + 1200 = Sht Then
Command1.Caption = "START"
Timer1.Enabled = False
Timer2.Enabled = False
Response = MsgBox("Finish", vbDefaultButton1)
Timer3.Enabled = False
End If
Case "25M"
If Sbd + 1500 = Sht Then
Command1.Caption = "START"
Timer1.Enabled = False
Timer2.Enabled = False
Response = MsgBox("Finish", vbDefaultButton1)
Timer3.Enabled = False
End If
Case "30M"
If Sbd + 1800 = Sht Then
Command1.Caption = "START"
Timer1.Enabled = False
Timer2.Enabled = False
Response = MsgBox("Finish", vbDefaultButton1)
Timer3.Enabled = False
End If
Case "35M"
If Sbd + 2100 = Sht Then
Command1.Caption = "START"
Timer1.Enabled = False
Timer2.Enabled = False
Response = MsgBox("Finish", vbDefaultButton1)
Timer3.Enabled = False
End If
Case "40M"
If Sbd + 2400 = Sht Then
Command1.Caption = "START"
Timer1.Enabled = False
Timer2.Enabled = False
Response = MsgBox("Finish", vbDefaultButton1)
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 16
Timer3.Enabled = False
End If
Case "45M"
If Sbd + 2700 = Sht Then
Command1.Caption = "START"
Timer1.Enabled = False
Timer2.Enabled = False
Response = MsgBox("Finish", vbDefaultButton1)
Timer3.Enabled = False
End If
Case "50M"
If Sbd + 3000 = Sht Then
Command1.Caption = "START"
Timer1.Enabled = False
Timer2.Enabled = False
Response = MsgBox("Finish", vbDefaultButton1)
Timer3.Enabled = False
End If
Case "55M"
If Sbd + 3300 = Sht Then
Command1.Caption = "START"
Timer1.Enabled = False
Timer2.Enabled = False
Response = MsgBox("Finish", vbDefaultButton1)
Timer3.Enabled = False
End If
Case "60M"
If Sbd + 3600 = Sht Then
Command1.Caption = "START"
Timer1.Enabled = False
Timer2.Enabled = False
Response = MsgBox("Finish", vbDefaultButton1)
Timer3.Enabled = False
End If
End Select
Gi = Gi + 1
If Gi = 60 Then
Gi = 0
p = p + 1
If p = 60 Then
p = 0
G = G + 1
End If
End If
If (p = 0 And G = 0) Then
Label25.Caption = Str(Gi) + "s"
Else
If (p <> 0 And G = 0) Then
Label25.Caption = Str(p) + "m" + Str(Gi) + "s"
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 17
Else
Label25.Caption = Str(G) + "h" + Str(p) + "m" + Str(Gi) + "s"
End If
End If
End If
End Sub
Private Sub timerCheckConnection_Timer() KIM TRA KT NI
If FindTheHid Then
lblConnect.Caption = "Connected"
lblConnect.ForeColor = RGB(0, 150, 0)
Else
lblConnect.Caption = "Disconnected"
lblConnect.ForeColor = RGB(150, 0, 0)
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
Dim Result As Long
If Not MyDeviceDetected Then
MyDeviceDetected = FindTheHid
End If
If MyDeviceDetected Then
OutputReportData(0) = 2
OutputReportData(1) = 0
OutputReportData(2) = 255
OutputReportData(3) = 255
OutputReportData(4) = 255
OutputReportData(5) = 255
OutputReportData(6) = 255
OutputReportData(7) = 255
Call ReadAndWriteToDevice
End If
Label3.Caption = "0V"
Result = CloseHandle(HIDHandle)
Result = CloseHandle(ReadHandle)
End Sub
Private Sub VScroll1_Change() THANH KO HIN TH
If FlagInt = True Or FlagTra = True Then
Line4.Visible = False
FlagTra = False
For i = 0 To 1023
Line3(i).Visible = False
Next
FlagInt = False
End If
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 18
For i = 0 To 1023
Shape1(i).Top = 5800 - VScroll1.Value * m(i)
Next
Image2.Top = Shape1(Vtt2).Top - 1000
Line1.Y1 = Shape1(Vtt2).Top
Line1.Y2 = Shape1(Vtt2).Top - 600
Image1.Top = Shape1(Vtt1).Top - 1000
Line2.Y1 = Shape1(Vtt1).Top
Line2.Y2 = Shape1(Vtt1).Top - 600
End Sub
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 19
M NGUN FORM LM TRN PH
Private Sub cmdExit_Click()
Hide
End Sub
Private Sub CmdSpline_Click()
Dim Response As Integer
Dim st As String
Response = MsgBox("You should save spectrum before spline", vbYesNo)
If Response = 6 Then 'yes
CommonDialog1.Filter = "DataFile(*.spt)|*.spt|AllData(*.*)|*.*"
CommonDialog1.ShowSave
file1 = CommonDialog1.FileName
st = ""
Open file1 For Output As #30
Close #30
Open file1 For Append As #30
For i = 0 To 1023
st = Str(m(i)) + Chr(9)
Print #30, st
Next
Close #30
End If
FlagInt = True
If Option1(0).Value = True Then ' lam tron 3 diem
For i = 0 To 1021
SPL(i + 1) = 0.25 * m(i) + 0.5 * m(i + 1) + 0.25 * m(i + 2)
Next
SPL(0) = 0.5 * m(0) + 0.5 * m(1)
SPL(1023) = 0.5 * m(1022) + 0.5 * m(1023)
For i = 0 To 1023
m(i) = SPL(i)
Next
End If
If Option1(1).Value = True Then 'lam tron 5 diem
For i = 0 To 1019
SPL(i + 2) = 0.1 * m(i) + 0.15 * m(i + 1) + 0.5 * m(i + 2) + 0.15 * m(i + 3) + 0.1 * m(i + 4)
Next
SPL(0) = 0.5 * m(0) + 0.3 * m(1) + 0.2 * m(2)
SPL(1) = 0.15 * m(0) + 0.5 * m(1) + 0.15 * m(2) + 0.2 * m(3)
SPL(1022) = 0.2 * m(1020) + 0.15 * m(1021) + 0.5 * m(1022) + 0.15 * m(1023)
SPL(1023) = 0.2 * m(1021) + 0.3 * m(1022) + 0.5 * m(1023)
For i = 0 To 1023
m(i) = SPL(i)
Next
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 20
End If
If Option1(2).Value = True Then 'lam tron 7 diem
For i = 0 To 1017
SPL(i + 3) = 0.05 * m(i) + 0.1 * m(i + 1) + 0.15 * m(i + 2) + 0.4 * m(i + 3) + 0.15 * m(i + 4)
+ 0.1 * m(i + 5) + 0.05 * m(i + 6)
Next
SPL(0) = 0.4 * m(0) + 0.3 * m(1) + 0.2 * m(2) + 0.1 * m(3)
SPL(1) = 0.15 * m(0) + 0.4 * m(1) + 0.15 * m(2) + 0.2 * m(3) + 0.1 * m(4)
SPL(2) = 0.1 * m(0) + 0.15 * m(1) + 0.4 * m(2) + 0.15 * m(3) + 0.1 * m(4) + 0.1 * m(5)
SPL(1021) = 0.1 * m(1018) + 0.1 * m(1019) + 0.15 * m(1020) + 0.4 * m(1021) + 0.15 *
m(1022) + 0.1 * m(1023)
SPL(1022) = 0.1 * m(1019) + 0.2 * m(1020) + 0.15 * m(1021) + 0.4 * m(1022) + 0.15 *
m(1023)
SPL(1023) = 0.1 * m(1020) + 0.2 * m(1021) + 0.3 * m(1022) + 0.4 * m(1023)
For i = 0 To 1023
m(i) = SPL(i)
Next
End If
If Option1(3).Value = True Then 'lam tron 9 diem
For i = 0 To 1015
SPL(i + 4) = 0.02 * m(i) + 0.04 * m(i + 1) + 0.09 * m(i + 2) + 0.17 * m(i + 3) + 0.36 * m(i +
4) + 0.17 * m(i + 5) + 0.09 * m(i + 6) + 0.04 * m(i + 7) + 0.02 * m(i + 8)
Next
SPL(0) = 0.36 * m(0) + 0.34 * m(1) + 0.18 * m(2) + 0.08 * m(3) + 0.04 * m(4)
SPL(1) = 0.17 * m(0) + 0.36 * m(1) + 0.17 * m(2) + 0.18 * m(3) + 0.08 * m(4) + 0.04 * m(5)
SPL(2) = 0.09 * m(0) + 0.17 * m(1) + 0.36 * m(2) + 0.17 * m(3) + 0.09 * m(4) + 0.08 * m(5)
+ 0.04 * m(6)
SPL(3) = 0.04 * m(0) + 0.09 * m(1) + 0.17 * m(2) + 0.36 * m(3) + 0.17 * m(4) + 0.09 * m(5)
+ 0.04 * m(6) + 0.04 * m(7)
SPL(1020) = 0.04 * m(1016) + 0.04 * m(1017) + 0.09 * m(1018) + 0.17 * m(1019) + 0.36 *
m(1020) + 0.17 * m(1021) + 0.09 * m(1022) + 0.04 * m(1023)
SPL(1021) = 0.04 * m(1017) + 0.08 * m(1018) + 0.09 * m(1019) + 0.17 * m(1020) + 0.36 *
m(1021) + 0.17 * m(1022) + 0.09 * m(1023)
SPL(1022) = 0.04 * m(1018) + 0.08 * m(1019) + 0.18 * m(1020) + 0.17 * m(1021) + 0.36 *
m(1022) + 0.17 * m(1023)
SPL(1023) = 0.04 * m(1018) + 0.08 * m(1020) + 0.18 * m(1021) + 0.34 * m(1022) + 0.36 *
m(1023)
For i = 0 To 1023
m(i) = SPL(i)
Next
End If
If frmMain.Option1.Value = True Then
For i = 0 To 1023
frmMain.Shape1(i).Left = 400 + i * 15
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 21
frmMain.Shape1(i).Top = 5800 - frmMain.VScroll1.Value * m(i)
frmMain.Shape1(i).Visible = True
Next
End If
If frmMain.Option2.Value = True Then
For i = 0 To 511
frmMain.Shape1(i).Left = 400 + i * 30
frmMain.Shape1(i).Top = 5800 - frmMain.VScroll1.Value * m(i)
frmMain.Shape1(i).Visible = True
frmMain.Shape1(i + 512).Visible = False
Next
End If
If frmMain.Option3.Value = True Then
For i = 512 To 1023
frmMain.Shape1(i).Left = 400 + i * 30
frmMain.Shape1(i).Top = 5800 - frmMain.VScroll1.Value * m(i)
frmMain.Shape1(i).Visible = True
frmMain.Shape1(i - 512).Visible = False
Next
End If
End Sub
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 22
M NGUN FORM CHUN NNG LNG
Option Explicit
Dim Mat As New cMathLib
Private Sub CmdClose_Click()
Hide
End Sub
Private Function taomtA(X() As Double, n As Integer) As Double()
Dim mat1() As Double
Dim hang As Double
Dim i, j As Integer
mat1 = Mat.Find_R_C(X)
hang = mat1(0, 1) - 1
ReDim A(hang, n) As Double
For i = 0 To hang
For j = 0 To n
A(i, j) = 1
Next
Next
For j = n - 1 To 0 Step -1
For i = 0 To hang
A(i, j) = A(i, j + 1) * X(0, i)
Next
Next
taomtA = A
End Function
Private Sub CmdCal_Click()
Dim l As Integer
Dim m As Integer
Dim min As Integer
Dim Bac As Integer
Dim i, j As Integer
Dim b() As Double
Dim c() As Double
Dim d() As Double
Dim e() As Double
Dim F() As Double
Dim p() As Double
Dim mat1() As Double
l = 0
m = 0
For i = 0 To 9
If Text1(i) <> "" Then
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 23
l = l + 1
Else
Exit For
End If
Next
For i = 0 To 9
If Text2(i) <> "" Then
m = m + 1
Else
Exit For
End If
Next
If m <= l Then
min = m
Else
min = l
End If
min = min - 1
If min >= 0 Then
ReDim X(0, min) As Double
ReDim Y(min, 0) As Double
ReDim z(min, 0) As Double
ReDim e(min, 0) As Double
For i = 0 To (min)
X(0, i) = CDbl(Text1(i).Text)
Y(i, 0) = CDbl(Text2(i).Text)
Next
Bac = min
b = taomtA(X, Bac)
c = Mat.Transpose(b)
d = Mat.Multiply(c, b)
mat1 = Mat.Find_R_C(d)
e = Mat.Inv(d)
F = Mat.Multiply(e, c)
p = Mat.Multiply(F, Y)
For i = 0 To min
z(i, 0) = 0
Next
For i = 0 To min
For j = 0 To Bac
z(i, 0) = z(i, 0) + p(j, 0) * X(0, i) ^ (Bac - j)
e(i, 0) = Abs(z(i, 0) - Y(i, 0))
Next
Text4(i).Text = Format(e(i, 0), "00.00e-00")
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 24
Text3(i).Text = Format(z(i, 0), "00.0000")
Next
For j = 0 To Bac
Text5(j).Text = Format(p(Bac - j, 0), "00.0000")
Next
For i = 0 To 1023
Kev(i) = 0
Next
For i = 0 To 1023
For j = 0 To Bac
Kev(i) = Kev(i) + p(j, 0) * i ^ (Bac - j)
Next
Next
Else
End If
End Sub
Private Sub Form_Load()
If frmMain.Label24.Caption = "Cal" Then
Dim NextLine As String
Dim XY(10), X(10), Y(10) As String
Dim DataRead As Integer
Dim j, Last As Integer
ClrTab
Open frmMain.Label26.Caption For Input As #31
j = 0
NextLine = ""
Do While Not EOF(31)
Line Input #31, NextLine
XY(j) = Trim(NextLine)
NextLine = ""
j = j + 1
Loop
Last = j - 1
Close #31
For j = 0 To Last
X(j) = Mid(XY(j), 1, InStr(1, XY(j), " "))
Text1(j).Text = CDbl(X(j))
Y(j) = Mid(XY(j), InStr(1, XY(j), " ") + 1, Len(XY(j)) - 1 - InStr(1, XY(j), " "))
Text2(j).Text = CDbl(Y(j))
Next
CmdCal_Click
Label15.Caption = frmMain.Label26.Caption
End If
End Sub
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 25
Private Sub mnuNew_Click()
ClrTab
Label15.Caption = ""
End Sub
Private Sub mnuOpen_Click()
Dim NextLine As String
Dim XY(10), X(10), Y(10) As String
Dim DataRead As Integer
Dim j, Last As Integer
With CalDialog
.Filter = "DataFile(*.eng)|*.eng|AllData(*.*)|*.*"
.ShowOpen
End With
If CalDialog.FileName <> "" Then
Open CalDialog.FileName For Input As #31
j = 0
NextLine = ""
Do While Not EOF(31)
Line Input #31, NextLine
XY(j) = Trim(NextLine)
'Text2(j).Text = NextLine
NextLine = ""
j = j + 1
Loop
Last = j - 1
Close #31
For j = 0 To Last
X(j) = Mid(XY(j), 1, InStr(1, XY(j), " "))
Text1(j).Text = CDbl(X(j))
Y(j) = Mid(XY(j), InStr(1, XY(j), " ") + 1, Len(XY(j)) - 1 - InStr(1, XY(j), " "))
Text2(j).Text = CDbl(Y(j))
Next
CmdCal_Click
Label15.Caption = CalDialog.FileName
frmMain.Label26.Caption = CalDialog.FileName
frmMain.Label24.Caption = "Cal"
frmMain.Label12.Caption = "Counts/Kev"
frmMain.Label13.Caption = "Counts/Kev"
End If
End Sub
Private Sub mnuSaveAS_Click()
Dim l As Integer
Dim m As Integer
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 26
Dim min As Integer
Dim st As String
Dim file1
Dim i As Integer
l = 0
m = 0
For i = 0 To 9
If Text1(i) <> "" Then
l = l + 1
Else
Exit For
End If
Next
For i = 0 To 9
If Text2(i) <> "" Then
m = m + 1
Else
Exit For
End If
Next
If m <= l Then
min = m
Else
min = l
End If
min = min - 1
ReDim X(min) As Double
ReDim Y(min) As Double
For i = 0 To (min)
X(i) = CDbl(Text1(i).Text)
Y(i) = CDbl(Text2(i).Text)
Next
CalDialog.Filter = "DataFile(*.eng)|*.eng|AllData(*.*)|*.*"
CalDialog.ShowSave
file1 = CalDialog.FileName
Open file1 For Output As #31
Close #31
Open file1 For Append As #31
For i = 0 To min
st = Str(X(i)) + " " + Str(Y(i)) + Chr(9)
Print #31, st
Next
Close #31
Label15.Caption = CalDialog.FileName
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 27
frmMain.Label26.Caption = CalDialog.FileName
frmMain.Label12.Caption = "Counts/Kev"
frmMain.Label13.Caption = "Counts/Kev"
End Sub
Private Sub ClrTab()
Dim i As Integer
For i = 0 To 9
Text1(i).Text = ""
Text2(i).Text = ""
Text3(i).Text = ""
Text4(i).Text = ""
Next
End Sub
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 28
''------------------------------------------------------------------------
''
'' M NGUN MODULE cMATHLIB TNH MA TRN
''
''------------------------------------------------------------------------
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The dimensions of the matrix are checked
' Here
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function Find_R_C(Mat() As Double) As Double()
Dim Rows As Integer, Columns As Integer
Dim i As Integer, j As Integer
Dim Result() As Double
Columns = 0
If Mat_1D(Mat, Rows) Then
ReDim Result(Rows, 1)
Result(0, 0) = Rows
Result(0, 1) = Columns + 1
For i = 1 To Rows
Result(i, 1) = Mat(i - 1)
Next i
Else
Call Mat_2D(Mat, Rows, Columns)
ReDim Result(Rows, Columns)
Result(0, 0) = Rows
Result(0, 1) = Columns
For i = 1 To Rows
For j = 1 To Columns '- 1
Result(i, j) = Mat(i - 1, j - 1)
Next j
Next i
End If
Find_R_C = Result
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Check if matrix has only one column
' shift the matrix one level and keep
' its dimensions details in Mat(0,0) and Mat(0,1)
' Mat(0,0)= no of rows
' Mat(0,1)= no of columns
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 29
Private Function Mat_1D(Mat() As Double, m As Integer) As Boolean
Dim Temp_MAT As Double
On Error GoTo Error_Handler
Temp_MAT = Mat(0, 0)
Mat_1D = False
Exit Function
Error_Handler:
Mat_1D = True
m = UBound(Mat) + 1
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Check if matrix has more than one column
' if so return the dimension as described above
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Mat_2D(Mat() As Double, m As Integer, n As Integer)
Dim Temp_MAT As Double, i As Integer
i = 0
m = UBound(Mat) + 1
On Error GoTo Error_Handler
Do Until i < -1
Temp_MAT = Mat(0, i)
i = i + 1
Loop
Error_Handler:
n = i
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function gan_m(Mat_1() As Double) As Double()
Dim mat1() As Double
Dim sol() As Double
Dim i As Integer, j As Integer
'On Error GoTo Error_Handler
mat1 = Find_R_C(Mat_1) ' // kiem tra chieu ma tran
'Mat2 = Find_R_C(Mat_2)
'If Mat1(0, 0) <> Mat2(0, 0) Or Mat1(0, 1) <> Mat2(0, 1) Then
'GoTo Error_Dimension
'End If
ReDim sol(mat1(0, 0) - 1, mat1(0, 1) - 1)
For i = 1 To mat1(0, 0)
For j = 1 To mat1(0, 1)
sol(i - 1, j - 1) = mat1(i, j)
Next j
Next i
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 30
gan_m = sol
Erase sol
'Exit Function
'Error_Dimension:
'Err.Raise "5005", , "Dimensions of the two matrices do not match !"
'Error_Handler:
'If Err.Number = 5005 Then
'Err.Raise "5005", , "Dimensions of the two matrices do not match !"
'Else
' Err.Raise "5022", , "One or both of the matrices are null, this operation cannot be done !!"
'End If
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Add two matrices, their dimensions should be compatible!
' Function returns the summation or errors due to
' dimensions incompatibility
' Example:
' Check Main Form !!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function Add(Mat_1() As Double, Mat_2() As Double) As Double()
Dim mat1() As Double, Mat2() As Double
Dim sol() As Double
Dim i As Integer, j As Integer
On Error GoTo Error_Handler
mat1 = Find_R_C(Mat_1) ' // kiem tra chieu ma tran
Mat2 = Find_R_C(Mat_2)
If mat1(0, 0) <> Mat2(0, 0) Or mat1(0, 1) <> Mat2(0, 1) Then
GoTo Error_Dimension
End If
ReDim sol(mat1(0, 0) - 1, mat1(0, 1) - 1)
For i = 1 To mat1(0, 0)
For j = 1 To mat1(0, 1)
sol(i - 1, j - 1) = mat1(i, j) + Mat2(i, j)
Next j
Next i
Add = sol
Erase sol
Exit Function
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 31
Error_Dimension:
Err.Raise "5005", , "Dimensions of the two matrices do not match !"
Error_Handler:
If Err.Number = 5005 Then
Err.Raise "5005", , "Dimensions of the two matrices do not match !"
Else
Err.Raise "5022", , "One or both of the matrices are null, this operation cannot be done !!"
End If
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Subtracts two matrices from each other, their
' dimensions should be compatible!
' Function returns the solution or errors due to
' dimensions incompatibility
' Example:
' Check Main Form !!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function Subtract(Mat_1() As Double, Mat_2() As Double) As Double()
Dim mat1() As Double, Mat2() As Double
Dim i As Integer, j As Integer, sol() As Double
On Error GoTo Error_Handler
mat1 = Find_R_C(Mat_1)
Mat2 = Find_R_C(Mat_2)
If mat1(0, 0) <> Mat2(0, 0) Or mat1(0, 1) <> Mat2(0, 1) Then
GoTo Error_Dimension
End If
ReDim sol(mat1(0, 0) - 1, mat1(0, 1) - 1)
For i = 1 To mat1(0, 0)
For j = 1 To mat1(0, 1)
sol(i - 1, j - 1) = mat1(i, j) - Mat2(i, j)
Next j
Next i
Subtract = sol
Erase sol
Exit Function
Error_Dimension:
Err.Raise "5007", , "Dimensions of the two matrices do not match !"
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 32
Error_Handler:
If Err.Number = 5007 Then
Err.Raise "5007", , "Dimensions of the two matrices do not match !"
Else
Err.Raise "5022", , "One or both of the matrices are null, this operation cannot be done !!"
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Multiply two matrices, their dimensions should be compatible!
' Function returns the solution or errors due to
' dimensions incompatibility
' Example:
' Check Main Form !!
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function Multiply(Mat_1() As Double, Mat_2() As Double) As Double()
Dim mat1() As Double, Mat2() As Double, l As Integer
Dim i As Integer, j As Integer, OptiString As String
Dim sol() As Double, MulAdd As Double
On Error GoTo Error_Handler
MulAdd = 0
mat1 = Find_R_C(Mat_1)
Mat2 = Find_R_C(Mat_2)
If mat1(0, 1) <> Mat2(0, 0) Then
GoTo Error_Dimension
End If
ReDim sol(mat1(0, 0) - 1, Mat2(0, 1) - 1)
For i = 1 To mat1(0, 0)
For j = 1 To Mat2(0, 1)
For l = 1 To mat1(0, 1)
MulAdd = MulAdd + mat1(i, l) * Mat2(l, j)
Next l
sol(i - 1, j - 1) = MulAdd
MulAdd = 0
Next j
Next i
Multiply = sol
Erase sol
Exit Function
Error_Dimension:
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 33
Err.Raise "5009", , "Dimensions of the two matrices not suitable for multiplication !"
Error_Handler:
If Err.Number = 5009 Then
Err.Raise "5009", , "Dimensions of the two matrices not suitable for multiplication !"
Else
Err.Raise "5022", , "One or both of the matrices are null, this operation cannot be done !!"
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Determinant of a matrix should be (nxn)
' Function returns the solution or errors due to
' dimensions incompatibility
' Example:
' Check Main Form !!
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function Det(Mat() As Double) As Double
Dim DArray() As Double, S As Integer
Dim k As Integer, i As Integer, j As Integer
Dim save As Double, ArrayK As Double, k1 As Integer
Dim M1 As String, mat1() As Double
On Error GoTo Error_Handler
mat1 = Find_R_C(Mat)
If mat1(0, 0) <> mat1(0, 1) Then GoTo Error_Dimension
S = mat1(0, 0)
Det = 1
DArray = mat1()
For k = 1 To S
If DArray(k, k) = 0 Then
j = k
Do While ((j < S) And (DArray(k, j) = 0))
j = j + 1
Loop
If DArray(k, j) = 0 Then
Det = 0
Exit Function
Else
For i = k To S
save = DArray(i, j)
DArray(i, j) = DArray(i, k)
DArray(i, k) = save
Next i
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 34
End If
Det = -Det
End If
ArrayK = DArray(k, k)
Det = Det * ArrayK
If k < S Then
k1 = k + 1
For i = k1 To S
For j = k1 To S
DArray(i, j) = DArray(i, j) - DArray(i, k) * (DArray(k, j) / ArrayK)
Next j
Next i
End If
Next
Exit Function
Error_Dimension:
Err.Raise "5011", , "Matrix should be a square matrix !"
Error_Handler:
If Err.Number = 5011 Then
Err.Raise "5011", , "Matrix should be a square matrix !"
Else
Err.Raise "5022", , "In order to do this operation values must be assigned to the matrix !!"
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Inverse of a matrix, should be (nxn) and det(Mat)<>0
' Function returns the solution or errors due to
' dimensions incompatibility
' Example:
' Check Main Form !!
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function Inv(Mat() As Double) As Double()
Dim sol() As Double
Dim AI() As Double, AIN As Double, AF As Double, _
mat1() As Double
Dim LL As Integer, LLM As Integer, L1 As Integer, _
L2 As Integer, LC As Integer, LCA As Integer, _
LCB As Integer, i As Integer, j As Integer
On Error GoTo Error_Handler
mat1 = Find_R_C(Mat)
If mat1(0, 0) <> mat1(0, 1) Then GoTo Error_Dimension
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 35
If Det(mat1) = 0 Then GoTo Error_Zero
ReDim sol(mat1(0, 0) - 1, mat1(0, 0) - 1)
LL = mat1(0, 0)
LLM = mat1(0, 1)
ReDim AI(LL, LL)
For L2 = 1 To LL
For L1 = 1 To LL
AI(L1, L2) = 0
Next
AI(L2, L2) = 1
Next
For LC = 1 To LL
If Abs(mat1(LC, LC)) < 0.0000000001 Then
For LCA = LC + 1 To LL
If LCA = LC Then GoTo 1090
If Abs(mat1(LC, LCA)) > 0.0000000001 Then
For LCB = 1 To LL
mat1(LCB, LC) = mat1(LCB, LC) + mat1(LCB, LCA)
AI(LCB, LC) = AI(LCB, LC) + AI(LCB, LCA)
Next
GoTo 1100
End If
1090 Next
End If
1100
AIN = 1 / mat1(LC, LC)
For LCA = 1 To LL
mat1(LCA, LC) = AIN * mat1(LCA, LC)
AI(LCA, LC) = AIN * AI(LCA, LC)
Next
For LCA = 1 To LL
If LCA = LC Then GoTo 1150
AF = mat1(LC, LCA)
For LCB = 1 To LL
mat1(LCB, LCA) = mat1(LCB, LCA) - AF * mat1(LCB, LC)
AI(LCB, LCA) = AI(LCB, LCA) - AF * AI(LCB, LC)
Next
1150 Next
Next
For i = 1 To LL
For j = 1 To LL
sol(i - 1, j - 1) = AI(i, j)
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 36
Next j
Next i
Inv = sol
Erase sol
Exit Function
Error_Zero:
Err.Raise "5012", , "Determinent equals zero, inverse can't be found !"
Error_Dimension:
Err.Raise "5014", , "Matrix should be a square matrix !"
Error_Handler:
If Err.Number = 5012 Then
Err.Raise "5012", , "Determinent equals zero, inverse can't be found !"
ElseIf Err.Number = 5014 Then
Err.Raise "5014", , "Matrix should be a square matrix !"
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Multiply two vectors, dimensions should be (3x1)
' Function returns the solution or errors due to
' dimensions incompatibility
' Example:
' Check Main Form !!
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function MultiplyVectors(Mat_1() As Double, Mat_2() As Double) As Double()
Dim mat1() As Double, Mat2() As Double
Dim i As Double, j As Double, k As Double
Dim sol(2) As Double
On Error GoTo Error_Handler
mat1 = Find_R_C(Mat_1)
Mat2 = Find_R_C(Mat_2)
If mat1(0, 0) <> 3 Or mat1(0, 1) <> 1 Then
GoTo Error_Dimension
End If
If Mat2(0, 0) <> 3 Or Mat2(0, 1) <> 1 Then
GoTo Error_Dimension
End If
i = mat1(2, 1) * Mat2(3, 1) - mat1(3, 1) * Mat2(2, 1)
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 37
j = mat1(3, 1) * Mat2(1, 1) - mat1(1, 1) * Mat2(3, 1)
k = mat1(1, 1) * Mat2(2, 1) - mat1(2, 1) * Mat2(1, 1)
sol(0) = i: sol(1) = j: sol(2) = k
MultiplyVectors = sol
Exit Function
Error_Dimension:
Err.Raise "5016", , "Dimension should be (3 x 1) for both matrices in order to do cross
multiplication !"
Error_Handler:
If Err.Number = 5016 Then
Err.Raise "5016", , "Dimension should be (3 x 1) for both matrices in order to do cross
multiplication !"
Else
Err.Raise "5022", , "One or both of the matrices are null, this operation cannot be done !!"
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Magnitude of a Vector, vector should be (3x1)
' Function returns the solution or errors due to
' dimensions incompatibility
' Example:
' Check Main Form !!
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function VectorMagnitude(Mat() As Double) As Double
Dim mat1() As Double
On Error GoTo Error_Handler
mat1 = Find_R_C(Mat)
If mat1(0, 0) <> 3 Or mat1(0, 1) <> 1 Then
GoTo Error_Dimension
End If
VectorMagnitude = Sqr(mat1(1, 1) * mat1(1, 1) + mat1(2, 1) * mat1(2, 1) + mat1(3, 1) *
mat1(3, 1))
Exit Function
Error_Dimension:
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 38
Err.Raise "5018", , "Dimension of the matrix should be (1 x 3) in order to find the vector's
norm !"
Error_Handler:
If Err.Number = 5018 Then
Err.Raise "5018", , "Dimension of the matrix should be (3 x 1) in order to find the vector's
magnitude !"
Else
Err.Raise "5022", , "In order to do this operation values must be assigned to the matrix !!"
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Transpose of a matrix
' Function returns the solution or errors
' Example:
' Check Main Form !!
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function Transpose(Mat() As Double) As Double()
Dim mat1() As Double, Tr_Mat() As Double
Dim i As Integer, j As Integer, sol() As Double
On Error GoTo Error_Handler
mat1() = Find_R_C(Mat())
ReDim Tr_Mat(0 To mat1(0, 1), 0 To mat1(0, 0))
ReDim sol(mat1(0, 1) - 1, mat1(0, 0) - 1)
Tr_Mat(0, 0) = mat1(0, 1)
Tr_Mat(0, 1) = mat1(0, 0)
For i = 1 To mat1(0, 0)
For j = 1 To mat1(0, 1)
Tr_Mat(j, i) = mat1(i, j)
Next j
Next i
For i = 1 To Tr_Mat(0, 0)
For j = 1 To Tr_Mat(0, 1)
sol(i - 1, j - 1) = Tr_Mat(i, j)
Next j
Next i
Transpose = sol
Erase sol
Exit Function
Error_Handler:
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 39
Err.Raise "5028", , "In order to do this operation values must be assigned to the matrix !!"
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Multiply a matrix or a vector with a scalar quantity
' Function returns the solution or errors
' Example:
' Check Main Form !!
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function ScalarMultiply(Value As Double, Mat() As Double) As Double()
Dim i As Integer, j As Integer
Dim mat1() As Double, sol() As Double
On Error GoTo Error_Handler
mat1 = Find_R_C(Mat)
ReDim sol(mat1(0, 0) - 1, mat1(0, 1) - 1)
For i = 1 To mat1(0, 0)
For j = 1 To mat1(0, 1)
sol(i - 1, j - 1) = mat1(i, j) * Value
Next j
Next i
ScalarMultiply = sol
Exit Function
Error_Handler:
Err.Raise "5022", , "Matrix was not assigned"
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Divide matrix elements or a vector by a scalar quantity
' Function returns the solution or errors
' Example:
' Check Main Form !!
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function ScalarDivide(Value As Double, Mat() As Double) As Double()
Dim i As Integer, j As Integer
Dim mat1() As Double, sol() As Double
On Error GoTo Error_Handler
mat1 = Find_R_C(Mat)
ReDim sol(mat1(0, 0) - 1, mat1(0, 1) - 1)
For i = 1 To mat1(0, 0)
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 40
For j = 1 To mat1(0, 1)
sol(i - 1, j - 1) = mat1(i, j) / Value
Next j
Next i
ScalarDivide = sol
Exit Function
Error_Handler:
Err.Raise "5022", , "Matrix was not assigned"
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Print a matrix to multitext text box
' Function returns the solution or errors
' Example:
' Check Main Form !!
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function PrintMat(Mat_1() As Double) As String
Dim N_Rows As Integer, N_Columns, k As Integer, _
i As Integer, j As Integer, m As Integer
Dim StrElem As String, StrLen As Long, _
Greatest() As Integer, LarString As String
Dim OptiString As String, sol As String
Dim mat1() As Double
mat1 = Find_R_C(Mat_1)
sol = ""
OptiString = ""
N_Rows = mat1(0, 0)
N_Columns = mat1(0, 1)
ReDim Greatest(N_Columns)
For i = 1 To N_Rows
For j = 1 To N_Columns
If i = 1 Then
Greatest(j) = 0
For m = 1 To N_Rows
StrElem = Format$(mat1(m, j), "0.0000")
StrLen = Len(StrElem)
If Greatest(j) < StrLen Then
Greatest(j) = StrLen
LarString = StrElem
End If
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 41
Next m
If Mid$(LarString, 1, 1) = "-" Then Greatest(j) = Greatest(j) + 1
End If
StrElem = Format$(mat1(i, j), "0.0000")
If Mid$(StrElem, 1, 1) = "-" Then
StrLen = Len(StrElem)
If Greatest(j) >= StrLen Then
For k = 1 To (Greatest(j) - StrLen)
OptiString = OptiString & " "
Next k
OptiString = OptiString & " "
End If
Else
StrLen = Len(StrElem)
If Greatest(j) > StrLen Then
For k = 1 To (Greatest(j) - StrLen)
OptiString = OptiString & " "
Next k
End If
End If
OptiString = OptiString & " " & Format$(mat1(i, j), "0.0000")
Next j
If i <> N_Rows Then
sol = sol & OptiString & vbCrLf
OptiString = ""
End If
sol = sol & OptiString
OptiString = ""
Next i
PrintMat = sol
Exit Function
End Function
Private Function Cutting(M_L As Integer)
Dim Num As Integer
Num = 0
Num = M_L \ 20
If M_L Mod 20 <> 0 Then Num = Num + 1
Cutting = Num
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Return the maximum of two numbers
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function max(ByVal X As Double, Y As Double) As Double
If X >= Y Then
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 42
max = X
ElseIf X < Y Then
max = Y
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Return the minimum of two numbers
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function min(ByVal X As Double, Y As Double) As Double
If X >= Y Then
min = Y
ElseIf X < Y Then
min = X
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''
' This routine finds the
' atan2(y,x) vlaue
'''''''''''''''''''''''''''''''''''''''''''''''
Public Function atan2(ByVal Y As Double, ByVal X As Double) As Double
Dim yy As Double, xx As Double
yy = Abs(Y)
xx = Abs(X)
If Y = 0 And X > 0 Then
atan2 = 0
ElseIf Y > 0 And X > 0 Then
atan2 = Atn(yy / xx)
ElseIf Y > 0 And X = 0 Then
atan2 = PI / 2
ElseIf Y > 0 And X < 0 Then
atan2 = PI - Atn(yy / xx)
ElseIf Y = 0 And X < 0 Then
atan2 = PI
ElseIf Y < 0 And X < 0 Then
atan2 = Atn(yy / xx) - PI
ElseIf Y < 0 And X = 0 Then
atan2 = PI / 2
ElseIf Y < 0 And X > 0 Then
atan2 = -Atn(yy / xx)
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''
' This routine returns Pi value
'''''''''''''''''''''''''''''''''''''''''''''''
Public Function PI() As Double
PI = 4 * Atn(1)
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 43
End Function
'''''''''''''''''''''''''''''''''''''''''''''''
' This routine returns the
' arc sin vlaue of an angle
'''''''''''''''''''''''''''''''''''''''''''''''
Public Function asin(ByVal X As Double) As Double
asin = Atn(X / (Sqr(Abs(1 - X * X)) + 1E-200))
End Function
'''''''''''''''''''''''''''''''''''''''''''''''
' This routine returns the
' arc cos vlaue of an angle
'''''''''''''''''''''''''''''''''''''''''''''''
Public Function acos(ByVal X As Double) As Double
acos = Atn(-X / (Sqr(1 - X * X))) + 2 * Atn(1)
End Function
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 44
M NGUN MODULE CRCMODULE KIM TRA LI TRUYN USB
Option Explicit
Function booleanAND(X As Long, Y As Long) As Long
Dim output As Long
Dim i As Integer
Dim temp, temp1 As Integer
output = 0
For i = 23 To 0 Step -1 '24 bit numbers accepted
temp = X \ (2 ^ i) 'gets the bit of text1.text
X = X - temp * (2 ^ i) 'subtracts it from the number
temp1 = Y \ (2 ^ i) 'gets the bit of text2.text
Y = Y - temp1 * (2 ^ i) 'subtracts it from the number
If temp = 1 And temp1 = 1 Then 'If both are equal to 1 then return a 1
output = output + (2 ^ i) 'This returns a decimal number
End If
Next i
booleanAND = output
End Function
Function booleanXOR(X As Long, Y As Long) As Long
Dim output As Long
Dim i As Integer
Dim temp, temp1 As Integer
output = 0
For i = 23 To 0 Step -1 '24 bit numbers accepted
temp = X \ (2 ^ i) 'gets the bit of text1.text
X = X - temp * (2 ^ i) 'subtracts it from the number
temp1 = Y \ (2 ^ i) 'gets the bit of text2.text
Y = Y - temp1 * (2 ^ i) 'subtracts it from the number
If temp = 1 Xor temp1 = 1 Then 'If one or the other but not both=1 then return a 1
output = output + (2 ^ i) 'This returns a decimal number
End If
Next i
booleanXOR = output
End Function
Function RShift(ByVal plValue As Long, piTimes As Integer) As Long
'Note a unsigned int needs to be stored
' in a long
plValue = (plValue \ (2 ^ piTimes))
RShift = plValue
End Function
Function LShift(ByVal plValue As Long, piTimes As Integer) As Long
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 45
Dim k As Integer
'Note a unsigned int needs to be stored
' in a long
plValue = plValue And CLng(RShift(CLng("&HFFFF"), piTimes))
plValue = (plValue * (2 ^ piTimes))
LShift = plValue
End Function
Public Function calc_CRC(oldCRC As Byte, newByte As Byte) As Integer
Dim shift_reg, data_bit, sr_lsb, fb_bit, j As Integer
shift_reg = oldCRC
For j = 0 To 7
data_bit = booleanAND(RShift(newByte, j), 1)
sr_lsb = booleanAND(Val(shift_reg), 1)
fb_bit = booleanAND(booleanXOR(Val(data_bit), Val(sr_lsb)), 1)
shift_reg = RShift(shift_reg, 1)
If (fb_bit = 1) Then shift_reg = booleanXOR(Val(shift_reg), 140)
Next
calc_CRC = shift_reg
End Function
M NGUN MODULE HID CA GIAO THC HID
Option Explicit
Dim bAlertable As Long
Dim Capabilities As HIDP_CAPS
Dim DataString As String
Dim DetailData As Long
Dim DetailDataBuffer() As Byte
Dim DeviceAttributes As HIDD_ATTRIBUTES
Dim DevicePathName As String
Dim DeviceInfoSet As Long
Dim ErrorString As String
Dim EventObject As Long
Public HIDHandle As Long
Dim HIDOverlapped As OVERLAPPED
Dim LastDevice As Boolean
Public MyDeviceDetected As Boolean
Dim MyDeviceInfoData As SP_DEVINFO_DATA
Dim MyDeviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA
Dim MyDeviceInterfaceData As SP_DEVICE_INTERFACE_DATA
Dim Needed As Long
Dim PreparsedData As Long
Public ReadHandle As Long
Dim Result As Long
Dim Security As SECURITY_ATTRIBUTES
Public Timeout As Boolean
'Set these to match the values in the device's firmware and INF file.
'0925h is Lakeview Research's vendor ID.
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 46
Const MyVendorID = &H461
Const MyProductID = &H20
Public ReadBuffer() As Byte
Public OutputReportData(31) As Byte
Function FindTheHid() As Boolean
'Makes a series of API calls to locate the desired HID-class device.
'Returns True if the device is detected, False if not detected.
Dim Count As Integer
Dim GUIDString As String
Dim HidGuid As GUID
Dim MemberIndex As Long
LastDevice = False
MyDeviceDetected = False
'Values for SECURITY_ATTRIBUTES structure:
Security.lpSecurityDescriptor = 0
Security.bInheritHandle = True
Security.nLength = Len(Security)
'**************************************************************************
'HidD_GetHidGuid
'Get the GUID for all system HIDs.
'Returns: the GUID in HidGuid.
'The routine doesn't return a value in Result
'but the routine is declared as a function for consistency with the other API calls.
'**************************************************************************
Result = HidD_GetHidGuid(HidGuid)
Call DisplayResultOfAPICall("GetHidGuid")
'Display the GUID.
GUIDString = _
Hex$(HidGuid.Data1) & "-" & _
Hex$(HidGuid.Data2) & "-" & _
Hex$(HidGuid.Data3) & "-"
For Count = 0 To 7
'Ensure that each of the 8 bytes in the GUID displays two characters.
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 47
If HidGuid.Data4(Count) >= &H10 Then
GUIDString = GUIDString & Hex$(HidGuid.Data4(Count)) & " "
Else
GUIDString = GUIDString & "0" & Hex$(HidGuid.Data4(Count)) & " "
End If
Next Count
'GUID for system HIDs = GUIDString
'**************************************************************************
'SetupDiGetClassDevs
'Returns: a handle to a device information set for all installed devices.
'Requires: the HidGuid returned in GetHidGuid.
'**************************************************************************
DeviceInfoSet = SetupDiGetClassDevs _
(HidGuid, _
vbNullString, _
0, _
(DIGCF_PRESENT Or DIGCF_DEVICEINTERFACE))
Call DisplayResultOfAPICall("SetupDiClassDevs")
DataString = GetDataString(DeviceInfoSet, 32)
'**************************************************************************
'SetupDiEnumDeviceInterfaces
'On return, MyDeviceInterfaceData contains the handle to a
'SP_DEVICE_INTERFACE_DATA structure for a detected device.
'Requires:
'the DeviceInfoSet returned in SetupDiGetClassDevs.
'the HidGuid returned in GetHidGuid.
'An index to specify a device.
'**************************************************************************
'Begin with 0 and increment until no more devices are detected.
MemberIndex = 0
Do
'The cbSize element of the MyDeviceInterfaceData structure must be set to
'the structure's size in bytes. The size is 28 bytes.
MyDeviceInterfaceData.cbSize = LenB(MyDeviceInterfaceData)
Result = SetupDiEnumDeviceInterfaces _
(DeviceInfoSet, _
0, _
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 48
HidGuid, _
MemberIndex, _
MyDeviceInterfaceData)
Call DisplayResultOfAPICall("SetupDiEnumDeviceInterfaces")
If Result = 0 Then LastDevice = True
'If a device exists, display the information returned.
If Result <> 0 Then
'" DeviceInfoSet for device #" & CStr(MemberIndex) & ": "
'" cbSize = " & CStr(MyDeviceInterfaceData.cbSize)
'" InterfaceClassGuid.Data1 = " &
Hex$(MyDeviceInterfaceData.InterfaceClassGuid.Data1)
'" InterfaceClassGuid.Data2 = " &
Hex$(MyDeviceInterfaceData.InterfaceClassGuid.Data2)
'" InterfaceClassGuid.Data3 = " &
Hex$(MyDeviceInterfaceData.InterfaceClassGuid.Data3)
'" Flags = " & Hex$(MyDeviceInterfaceData.Flags)
'**************************************************************************
'SetupDiGetDeviceInterfaceDetail
'Returns: an SP_DEVICE_INTERFACE_DETAIL_DATA structure
'containing information about a device.
'To retrieve the information, call this function twice.
'The first time returns the size of the structure in Needed.
'The second time returns a pointer to the data in DeviceInfoSet.
'Requires:
'A DeviceInfoSet returned by SetupDiGetClassDevs and
'an SP_DEVICE_INTERFACE_DATA structure returned by
SetupDiEnumDeviceInterfaces.
'**************************************************************************
MyDeviceInfoData.cbSize = Len(MyDeviceInfoData)
Result = SetupDiGetDeviceInterfaceDetail _
(DeviceInfoSet, _
MyDeviceInterfaceData, _
0, _
0, _
Needed, _
0)
DetailData = Needed
Call DisplayResultOfAPICall("SetupDiGetDeviceInterfaceDetail")
'(OK to say too small)
'Required buffer size for the data = Needed
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 49
'Store the structure's size.
MyDeviceInterfaceDetailData.cbSize = _
Len(MyDeviceInterfaceDetailData)
'Use a byte array to allocate memory for
'the MyDeviceInterfaceDetailData structure
ReDim DetailDataBuffer(Needed)
'Store cbSize in the first four bytes of the array.
Call RtlMoveMemory _
(DetailDataBuffer(0), _
MyDeviceInterfaceDetailData, _
4)
'Call SetupDiGetDeviceInterfaceDetail again.
'This time, pass the address of the first element of DetailDataBuffer
'and the returned required buffer size in DetailData.
Result = SetupDiGetDeviceInterfaceDetail _
(DeviceInfoSet, _
MyDeviceInterfaceData, _
VarPtr(DetailDataBuffer(0)), _
DetailData, _
Needed, _
0)
Call DisplayResultOfAPICall(" Result of second call: ")
'MyDeviceInterfaceDetailData.cbSize = CStr(MyDeviceInterfaceDetailData.cbSize)
'Convert the byte array to a string.
DevicePathName = CStr(DetailDataBuffer())
'Convert to Unicode.
DevicePathName = StrConv(DevicePathName, vbUnicode)
'Strip cbSize (4 bytes) from the beginning.
DevicePathName = Right$(DevicePathName, Len(DevicePathName) - 4)
'Device pathname = DevicePathName
'**************************************************************************
'CreateFile
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 50
'Returns: a handle that enables reading and writing to the device.
'Requires:
'The DevicePathName returned by SetupDiGetDeviceInterfaceDetail.
'**************************************************************************
HIDHandle = CreateFile _
(DevicePathName, _
GENERIC_READ Or GENERIC_WRITE, _
(FILE_SHARE_READ Or FILE_SHARE_WRITE), _
Security, _
OPEN_EXISTING, _
0&, _
0)
Call DisplayResultOfAPICall("CreateFile")
'Returned handle = HIDHandle
'Now we can find out if it's the device we're looking for.
'**************************************************************************
'HidD_GetAttributes
'Requests information from the device.
'Requires: The handle returned by CreateFile.
'Returns: an HIDD_ATTRIBUTES structure containing
'the Vendor ID, Product ID, and Product Version Number.
'Use this information to determine if the detected device
'is the one we're looking for.
'**************************************************************************
'Set the Size property to the number of bytes in the structure.
DeviceAttributes.Size = LenB(DeviceAttributes)
Result = HidD_GetAttributes _
(HIDHandle, _
DeviceAttributes)
Call DisplayResultOfAPICall("HidD_GetAttributes")
If Result <> 0 Then
'HIDD_ATTRIBUTES structure filled without error.
Else
'Error in filling HIDD_ATTRIBUTES structure.
End If
'Structure size = DeviceAttributes.Size
'Vendor ID = Hex$(DeviceAttributes.VendorID)
'Product ID = Hex$(DeviceAttributes.ProductID)
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 51
'Version Number = Hex$(DeviceAttributes.VersionNumber)
'Find out if the device matches the one we're looking for.
If (DeviceAttributes.VendorID = MyVendorID) And _
(DeviceAttributes.ProductID = MyProductID) Then
'It's the desired device.
'My device detected
MyDeviceDetected = True
Else
MyDeviceDetected = False
'If it's not the one we want, close its handle.
Result = CloseHandle _
(HIDHandle)
DisplayResultOfAPICall ("CloseHandle")
End If
End If
'Keep looking until we find the device or there are no more left to examine.
MemberIndex = MemberIndex + 1
Loop Until (LastDevice = True) Or (MyDeviceDetected = True)
'Free the memory reserved for the DeviceInfoSet returned by SetupDiGetClassDevs.
Result = SetupDiDestroyDeviceInfoList _
(DeviceInfoSet)
Call DisplayResultOfAPICall("DestroyDeviceInfoList")
If MyDeviceDetected = True Then
FindTheHid = True
'Learn the capabilities of the device
Call GetDeviceCapabilities
'Get another handle for the overlapped ReadFiles.
ReadHandle = CreateFile _
(DevicePathName, _
(GENERIC_READ Or GENERIC_WRITE), _
(FILE_SHARE_READ Or FILE_SHARE_WRITE), _
Security, _
OPEN_EXISTING, _
FILE_FLAG_OVERLAPPED, _
0)
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 52
Call DisplayResultOfAPICall("CreateFile, ReadHandle")
'Returned handle = ReadHandle
Call PrepareForOverlappedTransfer
Else
'Device not found
End If
End Function
Private Function GetDataString _
(Address As Long, _
Bytes As Long) _
As String
'Retrieves a string of length Bytes from memory, beginning at Address.
'Adapted from Dan Appleman's "Win32 API Puzzle Book"
Dim Offset As Integer
Dim Result$
Dim ThisByte As Byte
For Offset = 0 To Bytes - 1
Call RtlMoveMemory(ByVal VarPtr(ThisByte), ByVal Address + Offset, 1)
If (ThisByte And &HF0) = 0 Then
Result$ = Result$ & "0"
End If
Result$ = Result$ & Hex$(ThisByte) & " "
Next Offset
GetDataString = Result$
End Function
Private Function GetErrorString _
(ByVal LastError As Long) _
As String
'Returns the error message for the last error.
'Adapted from Dan Appleman's "Win32 API Puzzle Book"
Dim Bytes As Long
Dim ErrorString As String
ErrorString = String$(129, 0)
Bytes = FormatMessage _
(FORMAT_MESSAGE_FROM_SYSTEM, _
0&, _
LastError, _
0, _
ErrorString$, _
128, _
0)
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 53
'Subtract two characters from the message to strip the CR and LF.
If Bytes > 2 Then
GetErrorString = Left$(ErrorString, Bytes - 2)
End If
End Function
Private Sub GetDeviceCapabilities()
'**************************************************************************
'HidD_GetPreparsedData
'Returns: a pointer to a buffer containing information about the device's capabilities.
'Requires: A handle returned by CreateFile.
'There's no need to access the buffer directly,
'but HidP_GetCaps and other API functions require a pointer to the buffer.
'**************************************************************************
Dim ppData(29) As Byte
Dim ppDataString As Variant
'Preparsed Data is a pointer to a routine-allocated buffer.
Result = HidD_GetPreparsedData _
(HIDHandle, _
PreparsedData)
Call DisplayResultOfAPICall("HidD_GetPreparsedData")
'Copy the data at PreparsedData into a byte array.
Result = RtlMoveMemory _
(ppData(0), _
PreparsedData, _
30)
Call DisplayResultOfAPICall("RtlMoveMemory")
ppDataString = ppData()
'Convert the data to Unicode.
ppDataString = StrConv(ppDataString, vbUnicode)
'**************************************************************************
'HidP_GetCaps
'Find out the device's capabilities.
'For standard devices such as joysticks, you can find out the specific
'capabilities of the device.
'For a custom device, the software will probably know what the device is capable of,
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 54
'so this call only verifies the information.
'Requires: The pointer to a buffer containing the information.
'The pointer is returned by HidD_GetPreparsedData.
'Returns: a Capabilites structure containing the information.
'**************************************************************************
Result = HidP_GetCaps _
(PreparsedData, _
Capabilities)
Call DisplayResultOfAPICall("HidP_GetCaps")
'" Last error: " & ErrorString
'" Usage: " & Hex$(Capabilities.Usage)
'" Usage Page: " & Hex$(Capabilities.UsagePage)
'" Input Report Byte Length: " & Capabilities.InputReportByteLength
'" Output Report Byte Length: " & Capabilities.OutputReportByteLength
'" Feature Report Byte Length: " & Capabilities.FeatureReportByteLength
'" Number of Link Collection Nodes: " & Capabilities.NumberLinkCollectionNodes
'" Number of Input Button Caps: " & Capabilities.NumberInputButtonCaps
'" Number of Input Value Caps: " & Capabilities.NumberInputValueCaps
'" Number of Input Data Indices: " & Capabilities.NumberInputDataIndices
'" Number of Output Button Caps: " & Capabilities.NumberOutputButtonCaps
'" Number of Output Value Caps: " & Capabilities.NumberOutputValueCaps
'" Number of Output Data Indices: " & Capabilities.NumberOutputDataIndices
'" Number of Feature Button Caps: " & Capabilities.NumberFeatureButtonCaps
'" Number of Feature Value Caps: " & Capabilities.NumberFeatureValueCaps
'" Number of Feature Data Indices: " & Capabilities.NumberFeatureDataIndices
'**************************************************************************
'HidP_GetValueCaps
'Returns a buffer containing an array of HidP_ValueCaps structures.
'Each structure defines the capabilities of one value.
'This application doesn't use this data.
'**************************************************************************
'This is a guess. The byte array holds the structures.
Dim ValueCaps(1023) As Byte
Result = HidP_GetValueCaps _
(HidP_Input, _
ValueCaps(0), _
Capabilities.NumberInputValueCaps, _
PreparsedData)
Call DisplayResultOfAPICall("HidP_GetValueCaps")
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 55
'lstResults.AddItem "ValueCaps= " & GetDataString((VarPtr(ValueCaps(0))), 180)
'To use this data, copy the byte array into an array of structures.
'Free the buffer reserved by HidD_GetPreparsedData
Result = HidD_FreePreparsedData _
(PreparsedData)
Call DisplayResultOfAPICall("HidD_FreePreparsedData")
End Sub
Private Sub PrepareForOverlappedTransfer()
'**************************************************************************
'CreateEvent
'Creates an event object for the overlapped structure used with ReadFile.
'Requires a security attributes structure or null,
'Manual Reset = True (ResetEvent resets the manual reset object to nonsignaled),
'Initial state = True (signaled),
'and event object name (optional)
'Returns a handle to the event object.
'**************************************************************************
If EventObject = 0 Then
EventObject = CreateEvent _
(Security, _
True, _
True, _
"")
End If
Call DisplayResultOfAPICall("CreateEvent")
'Set the members of the overlapped structure.
HIDOverlapped.Offset = 0
HIDOverlapped.OffsetHigh = 0
HIDOverlapped.hEvent = EventObject
End Sub
Private Sub DisplayResultOfAPICall(FunctionName As String)
'Display the results of an API call.
Dim ErrorString As String
ErrorString = GetErrorString(Err.LastDllError)
'FunctionName Result = ErrorString
End Sub
Public Sub ReadAndWriteToDevice()
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 56
'Sends two bytes to the device and reads two bytes back.
Dim Count As Integer
'If the device hasn't been detected or it timed out on a previous attempt
'to access it, look for the device.
If MyDeviceDetected = False Then
MyDeviceDetected = FindTheHid
End If
If MyDeviceDetected = True Then
'Write a report to the device
Call WriteReport
'Read a report from the device.
Call ReadReport
Else
End If
End Sub
Public Sub ReadReport()
'Read data from the device.
Dim Count
Dim NumberOfBytesRead As Long
'Allocate a buffer for the report.
'Byte 0 is the report ID.
'**************************************************************************
'ReadFile
'Returns: the report in ReadBuffer.
'Requires: a device handle returned by CreateFile
'(for overlapped I/O, CreateFile must be called with FILE_FLAG_OVERLAPPED),
'the Input report length in bytes returned by HidP_GetCaps,
'and an overlapped structure whose hEvent member is set to an event object.
'**************************************************************************
Dim ByteValue As String
'The ReadBuffer array begins at 0, so subtract 1 from the number of bytes.
ReDim ReadBuffer(Capabilities.InputReportByteLength - 1)
'Do an overlapped ReadFile.
'The function returns immediately, even if the data hasn't been received yet.
Result = ReadFile _
(ReadHandle, _
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 57
ReadBuffer(0), _
CLng(Capabilities.InputReportByteLength), _
NumberOfBytesRead, _
HIDOverlapped)
Call DisplayResultOfAPICall("ReadFile")
'Waiting for ReadFile
bAlertable = True
'**************************************************************************
'WaitForSingleObject
'Used with overlapped ReadFile.
'Returns when ReadFile has received the requested amount of data or on timeout.
'Requires an event object created with CreateEvent
'and a timeout value in milliseconds.
'**************************************************************************
Result = WaitForSingleObject _
(EventObject, _
6000)
Call DisplayResultOfAPICall("WaitForSingleObject")
'Find out if ReadFile completed or timeout.
Select Case Result
Case WAIT_OBJECT_0
'ReadFile has completed
Case WAIT_TIMEOUT
'Timeout
'Cancel the operation
'*************************************************************
'CancelIo
'Cancels the ReadFile
'Requires the device handle.
'Returns non-zero on success.
'*************************************************************
Result = CancelIo _
(ReadHandle)
Call DisplayResultOfAPICall("CancelIo")
'The timeout may have been because the device was removed,
'so close any open handles and
'set MyDeviceDetected=False to cause the application to
'look for the device on the next attempt.
CloseHandle (HIDHandle)
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 58
Call DisplayResultOfAPICall("CloseHandle (HIDHandle)")
CloseHandle (ReadHandle)
Call DisplayResultOfAPICall("CloseHandle (ReadHandle)")
MyDeviceDetected = False
Case Else
'Readfile undefined error
MyDeviceDetected = False
End Select
'Report ID = ReadBuffer(0)
'Report Data = ReadBuffer(Count)
'**************************************************************************
'ResetEvent
'Sets the event object in the overlapped structure to non-signaled.
'Requires a handle to the event object.
'Returns non-zero on success.
'**************************************************************************
Call ResetEvent(EventObject)
Call DisplayResultOfAPICall("ResetEvent")
End Sub
Public Sub WriteReport()
'Send data to the device.
Dim Count As Integer
Dim NumberOfBytesWritten As Long
Dim SendBuffer() As Byte
'The SendBuffer array begins at 0, so subtract 1 from the number of bytes.
ReDim SendBuffer(Capabilities.OutputReportByteLength - 1)
'**************************************************************************
'WriteFile
'Sends a report to the device.
'Returns: success or failure.
'Requires: the handle returned by CreateFile and
'The output report byte length returned by HidP_GetCaps
'**************************************************************************
'The first byte is the Report ID
SendBuffer(0) = 0
'The next bytes are data
Ph lc A: M ngun phn mm trn my vi tnh
Ch nhim ti: Nguyn Vn Sn Ph lc A - 59
For Count = 1 To Capabilities.OutputReportByteLength - 1
SendBuffer(Count) = OutputReportData(Count - 1)
Next Count
NumberOfBytesWritten = 0
Result = WriteFile _
(HIDHandle, _
SendBuffer(0), _
CLng(Capabilities.OutputReportByteLength), _
NumberOfBytesWritten, _
0)
Call DisplayResultOfAPICall("WriteFile")
'OutputReportByteLength = Capabilities.OutputReportByteLength
'NumberOfBytesWritten = NumberOfBytesWritten
'Report ID = SendBuffer(0)
'Report Data = SendBuffer(Count)
End Sub