You are on page 1of 19

,. I. i. .. i. .i ..

:o

1

HNG DAN GIAO TIEP USB-HID VI
PIC18F4550
Cho cc bn, hm ny Ch Nht ngy 25/09/2011, li l 1 ngy bun chn ko
c vic g lm c, ngi nhm nghi chn tr c v bn vi iu thuc cuc
i sng nht l nhng lc nh th ny Ngoi tr v thuc ra th cng tranh
th lt Keyboard tng cc bn 1 TUT v giao tip USB HID Class ca Microchip
PIC18F4550.
- Nh chng ta bit, con PIC18F4550 ny tng i thng dng, cc
Member trn cc 4rum nh : www.dientuvietnan.net , www.picvietnam.com
www.bkit4u.com ... thng dng. Cc ti, n c lin quan n USB th
PIC18F4550 s c chn. PIC18F4550 h tr USB2.0 Full Speed
480Mbps
- Ni v USB th chng ta thng quan tm n 2 lp thng dng, l :
USB HID v lp USB Mass Storage. V y cng thng dng 2 kiu
truyn l Bulk vi USB Mass Storage v Interrupts vi lp HID. Th
cng ni lun l chi vi Bulk th mi c th t c Full Speed , cn HID
th chng ta thng ch l 64KB/s hoc 512KB/s. So vi rs232 th Max
rs232 == 128000bps ~ 16KB/s), nhng y n tc ny ko phi thit
b no cng h tr, PIC th chc chn l ko n n tc ny ri.
- Tuy tc HID khng cao nhng n c ng dng cho cc thit n gin
nh Keyboard, Mouse, cc thit b ko yu cu v Driver
- HID truyn c c 2 chiu t Device ln Host v ngc li. Vic trao i
d liu gia Host vi Device c thc hin qua giao thc usb, qun l bng
cc endpoint(im u cui) v pipe (ng truyn).
- n y c bn l chng c th sn sng vo lm 1 thit USB lp HID
vi chc nng Plug and Play (PnP) hay ni cch khc l cm l chy, ko cn
Driver.



,. I. i. .. i. .i .. :o

2

1. Gii thiu v phn mm Complier ca PIC ca MikroC
Chng ta s phn mm MikroC Pro For PIC v5.01 bn mi nht ca nm
2011c bn vi gi l 249 USD, tnh theo t gi ti thi im ny
(25/09/2011) th chng ta cn : 249 x 20.834 = 5187.666 ~ tnh khong 5
c 2 cho n d.


- Thi th sinh vin ngho, ko c tin ch cn cch l ln thng trang ch
load phin bn mi v v search google C.x.x.k Registered n.

2. Code vi MikroC
- To 1 Project mi.
File / New / New Project
,. I. i. .. i. .i .. :o

3


V c Next m thi, ci ny th d qu ri, ko phi ni nhiu.

,. I. i. .. i. .i .. :o

4



,. I. i. .. i. .i .. :o

5



,. I. i. .. i. .i .. :o

6

- S hin 1 ca s Edit Project, chng ta cn phi chn cc thng s y.









,. I. i. .. i. .i .. :o

7


- y c 2 cch chn tn s hot ng cho CPU, hnh v chng ta nhn
thy c 2 Option l u vo xung nhp cho CPU. Vi Option 1 l : HSPLL
v Option 2 l: XT, HS.
,. I. i. .. i. .i .. :o

8

- cho n gin th chng ta s s dng HSPLL, tc l Option 1. Thch anh
tht l 20Mhz, qua b chia PLLDIV c 4Mhz v a qua b 96Mhz
PLL, u ra nhn c tn s l 96Mhz, ci ny s c chia 2 (nh hnh
v) 96 / 2 = 48 Mhz, vy l USB hot ng vi xung nhp l 48Mhz.
- Khi khai bo s dng HSPLL th XT,HS b v hiu, chnh v th chng ta s
ly xung nhp t b HSPLL cp cho CPU, nh hnh trn xung nhp s
c a qua b chia CPUDIV nh nht l 2, tc l 96/2 = 48Mhz. iu
ny c ngh l CPU s hot ng vi xung nhp cao nht 48Mhz, mc d u
vo thch anh ch c 20Mhz. V lc ny iu quan trng cn ch l chng
ta phi sa li l gi tr thch anh t cho Project l 20Mhz thnh 48Mhz
mc d thch anh tht bn ngoi l 20Mhz. (y chnh l im cn lu .)

Ngoi ra chng ta cn phi Enable cho pin 18 ly in p 3.3V ti chn ny.

,. I. i. .. i. .i .. :o

9

- n y coi nh Project c bn l chun ri, by gi l vit code
- Cc bn c th Copy & Paste m ny Demo mch ca mnh nh chnh
thch anh cho ph hp.

Chn ci ny
,. I. i. .. i. .i .. :o

10


- Coi nh xong phn code, by gi l n phn Description cho thit b.
/*
Coder : HIENCLUBVN
Lab : Hc Vin Hi Qun
Description : USB HID PIC18F4450
Complier : MikroC Pro For PIC 2011
Date : 25 / 09 / 2011
------------------
Vd : Xtal = 20Mhz
Chn HSPLL (PLL DIV 5)
Xung nhp ca USB l 48Mhz
Chn CPUDIV 2 , xung nhp ca CPU l 48Mhz
*/
unsigned char readbuff[64] absolute 0x500; // Buffers should be in USB RAM, please
consult datasheet
unsigned char writebuff[64] absolute 0x540;

char cnt;
char kk;

void interrupt(){
USB_Interrupt_Proc(); // Hm phc v ngt
}

void main(void)
{
ADCON1 |= 0x0F; // Set li cc PIN Analog thnh Digital
CMCON |= 7; // Disable chuc nang Comparators
HID_Enable(&readbuff,&writebuff); // Cho php kt ni lp HID
while(1){
while(!HID_Read()); // Tr v s byte c nhn t HOST, False nu ko nhn
c.
// Ch y, cho n khi c data nhn th thc hin cng vic ghi data nhn vo
writebuff v truyn li.
for(cnt=0;cnt<64;cnt++)
writebuff[cnt]=readbuff[cnt];
// C th thm CODE x l ti y.
while(!HID_Write(&writebuff,64)); // Tr v s byte c truyn i (64) nu
thnh cng, False nu tht bi.
}
}

,. I. i. .. i. .i .. :o

11


Chn Tool s / HID Terminal
,. I. i. .. i. .i .. :o

12


- Lu n li th mc Project to, v add n vo Project ca mnh.
Ly File ti y
,. I. i. .. i. .i .. :o

13


- By gi coi nh mi vic xong, nhn Ctrl + F9 Build

- OK, chng trnh ca chng ta Finished Successfully, ko c li.
- Tin hnh np File HEX trong Project bng Pickit2 l OK ri.

- V Schematic c th ghp ni nh 2 hnh bn di. (Nn thm cc t
104 lc ngun gia 2 Vcc v GND)
USBdsc.c c Add vo
Click Add
Chn Manager Project
,. I. i. .. i. .i .. :o

14



C th dng 2 t
104 ni song song
nh hnh 1 hoc t
470nF nh hnh 2
,. I. i. .. i. .i .. :o

15


- Sau khi np Firmware , th cc bn c th thy thit b ca mnh ri. Lu
mnh si lp HID nn cm pht l PC load c thit b ngay.

,. I. i. .. i. .i .. :o

16


- Ok, n y l ngon ri. By gi chng ta bt HID Temirnal ca MikroC
ln Debug chng trnh.
,. I. i. .. i. .i .. :o

17


- Chng ta thy USB HID Library (tn to ra trc bng Description),
nhn info xem thng tin v thit b.
,. I. i. .. i. .i .. :o

18


- Gi s nh chng ta gi 1 mng cc gi tr kiu byte t s 1 9, cn cc
byte cn li t 9 63 s bng 0, nhn Send gi. Lc ny di PIC nhn
c data v gi bt tr li (v trong firmware chng ta code nh th m),
ngay di ca s chng ta thy d liu c tr v (PC nhn v hin
th ln TextBox ).
n y coi nh mi vic xong, by gi ch l vn vit giao tip na
m thi. cho n gin ti khuyn cc bn nn code bng cc hm dll
share trn internet, c th l ca Microchip, www.lvr.com , www.florian-
leitner.de , www.waitingforfriday.com/
- Theo li kin c nhn ca ti cc bn nn u t nghin cu code ca
www.lvr.com hoc l www.waitingforfriday.com/
,. I. i. .. i. .i .. :o

19


3. Pht trin ng dng vi HID USB Library
- Tm thi n y , mt ri. Hm sau vit tip.