Nguy(cid:1225)n Ng(cid:1233)c H(cid:1133)ng L(cid:1247)p TKTL 40 HVKTQS
Ph(cid:1195)n 2: Vi(cid:1219)t Fimware cho Pic18f4550 ( ho(cid:1211)c Pic18f2550)
Trình biên d(cid:1231)ch:
1. Có th(cid:1223) vi(cid:1219)t Fimware b(cid:1205)ng t(cid:1193)t c(cid:1191) các trình biên d(cid:1231)ch thông d(cid:1257)ng, song theo ý ki(cid:1219)n (cid:70)(cid:1259)a tôi CCS là trình biên d(cid:1231)ch h(cid:1243) tr(cid:1255) vi(cid:1219)t Fimware cho chíp USB khá t(cid:1237)t. Trong ví d(cid:1257) c(cid:1259)a CCS có các ví d(cid:1257) cho c(cid:1191) HID, Costume Driver và CDC. (cid:264)(cid:1239)ng th(cid:1249)i các th(cid:1133) vi(cid:1227)n hàm cho USB (cid:255)(cid:1133)(cid:1255)c xây d(cid:1269)ng t(cid:1133)(cid:1131)ng (cid:255)(cid:1237)i thu(cid:1201)n l(cid:1255)i khi s(cid:1265) d(cid:1257)ng vì v(cid:1201)y r(cid:1193)t thu(cid:1201)n l(cid:1255)i (cid:255)(cid:1223) các b(cid:1189)n có th(cid:1223) nhanh chóng th(cid:1269)c hi(cid:1227)n USB.
Các th(cid:1261)c th(cid:1265) nghi(cid:1227)m:
2. Sau khi ph(cid:1195)n c(cid:1261)ng (cid:255)ã (cid:255)(cid:1133)(cid:1255)c (cid:255)(cid:1193)u n(cid:1237)i (cid:255)(cid:1195)y (cid:255)(cid:1259) b(cid:1189)n có th(cid:1223) n(cid:1189)p th(cid:1265) các các fimware có s(cid:1209)n t(cid:1133)(cid:1131)ng thích v(cid:1247)i ph(cid:1195)n c(cid:1261)ng và c(cid:1203)m thi(cid:1219)t b(cid:1231) vào máy tính. (cid:49)(cid:1219)u ph(cid:1195)n c(cid:1261)ng (cid:255)úng và fimware t(cid:1133)(cid:1131)ng thích máy tính s(cid:1217) thông báo “Found New Hardware” và (cid:255)òi h(cid:1235)i cài (cid:255)(cid:1211)t Driver cho thi(cid:1219)t b(cid:1231). C(cid:1195)n l(cid:1133)u ý n(cid:1219)u ph(cid:1195)n m(cid:1221)n trong chíp cài (cid:255)(cid:1211)t t(cid:1195)n s(cid:1237) th(cid:1189)ch anh không kh(cid:1247)p v(cid:1247)i th(cid:1189)ch anh ph(cid:1195)n c(cid:1261)ng s(cid:1217) gây ra l(cid:1243)i và máy tính không th(cid:1223) nh(cid:1201)n di(cid:1227)n thi(cid:1219)t b(cid:1231). N(cid:1219)u khi (cid:255)ã t(cid:1133)(cid:1131)ng thích v(cid:1221) th(cid:1189)ch anh mà máy tính v(cid:1199)n không detect thi(cid:1219)t b(cid:1231) thì có th(cid:1223) ph(cid:1195)n c(cid:1261)ng c(cid:1259)a b(cid:1189)n v(cid:1199)n còn (cid:89)(cid:1193)n (cid:255)(cid:1221) ho(cid:1211)c c(cid:1241)ng USB có v(cid:1193)n (cid:255)(cid:1221) v(cid:1221) ti(cid:1219)p xúc hãy ki(cid:1223)m tra k(cid:429). (cid:49)(cid:1219)u thi(cid:1219)t b(cid:1231) c(cid:1259)a b(cid:1189)n không b(cid:1231) l(cid:1243)i và fimware chính xác:
Khi nó (cid:255)òi Driver hãy t(cid:1189)m th(cid:1249)i b(cid:1235) qua và th(cid:1269)c hi(cid:1227)n t(cid:1189)o Driver m(cid:1247)i cho nó b(cid:1205)ng ph(cid:1195)n m(cid:1221)m WinDriver, khi t(cid:1189)o xong Driver thì thi(cid:1219)t b(cid:1231) s(cid:1217)(cid:3) (cid:255)(cid:1133)(cid:1255)c t(cid:1269)(cid:3) (cid:255)(cid:1245)ng update driver, chi ti(cid:1219)t vi(cid:1227)c t(cid:1189)o driver (cid:255)(cid:1133)(cid:1255)c h(cid:1133)(cid:1247)ng d(cid:1199)n (cid:1251) ph(cid:1195)n 3.
3. Các th(cid:1133) vi(cid:1227)n và hàm s(cid:1265) d(cid:1257)ng chính (cid:255)(cid:1223) vi(cid:1219)t USB: Có 3 file b(cid:1189)n c(cid:1195)n include vào project CCS c(cid:1259)a b(cid:1189)n là:
- #include
- #include
- #include
Hai file trên có s(cid:1209)n trong th(cid:1133) vi(cid:1227)n c(cid:1259)a CCS ch(cid:1261)a các (cid:255)(cid:1231)nh ngh(cid:429)a và các hàm ph(cid:1257)c (cid:89)(cid:1257) cho giao ti(cid:1219)p USB, file th(cid:1261) 3 là file mô t(cid:1191) thi(cid:1219)t b(cid:1231)(cid:3) (cid:255)(cid:1133)(cid:1255)c ch(cid:1229)nh s(cid:1265)a t(cid:1263) file usb_desc_scope.h (cid:70)(cid:458)ng có s(cid:1209)n trong th(cid:1133) vi(cid:1227)n c(cid:1259)a CCS (cid:255)(cid:1223) phù h(cid:1255)p v(cid:1247)i yêu c(cid:1195)u (cid:70)(cid:1259)a b(cid:1189)n. Ngoài ra còn m(cid:1245)t file n(cid:1267)a là:
- #include .(cid:255)(cid:753)(cid:869)ng d(cid:819)n/ usb_demo_bulk.h>
File này không có s(cid:1209)n trong CCS nh(cid:1133) các file trên. Nó (cid:255)(cid:1133)(cid:1255)c t(cid:1189)o ra khi b(cid:1189)n l(cid:1201)p (cid:80)(cid:1245)t Project trên CCS qua PIC Wizard, tên file do b(cid:1189)n (cid:255)(cid:1211)t.
- 1 -
Nguy(cid:1225)n Ng(cid:1233)c H(cid:1133)ng L(cid:1247)p TKTL 40 HVKTQS
4.
Các hàm (cid:255)(cid:76)(cid:1221)u khi(cid:1223)n và giao ti(cid:1219)p USB:
Khi xem các mã ngu(cid:1239)n c(cid:1259)a các file trên trong CCS, b(cid:1189)n s(cid:1217) th(cid:1193)y r(cid:1193)t nhi(cid:1221)u hàm và (cid:255)(cid:1231)nh ngh(cid:429)a khó hi(cid:1223)u. Nh(cid:1133)ng ph(cid:1195)n l(cid:1247)n b(cid:1189)n s(cid:1217) không c(cid:1195)n quan tâm t(cid:1247)i các hàm (cid:255)ó vì chúng (cid:255)(cid:1133)(cid:1255)c xây d(cid:1269)ng (cid:255)(cid:1223) trình biên d(cid:1231)ch s(cid:1265) d(cid:1257)ng. Cái chúng ta quan tâm ch(cid:1229) là (cid:87)(cid:1201)p hàm “User Functions” mà CCS (cid:255)ã xây d(cid:1269)ng s(cid:1209)n:
//// **************** USER FUNCTIONS *********************** //// //// //// //// usb_init() - Initializes the USB stack, the USB peripheral and //// //// attaches the unit to the usb bus. Enables //// //// interrupts. //// //// //// //// usb_init_cs() - A smaller usb_init(), does not attach unit //// //// to usb bus or enable interrupts. //// //// //// //// usb_put_packet() - Sends one packet to the host. //// //// If you need to send a message that spans //// //// more than one packet then see usb_puts() in //// //// usb.c //// //// //// //// usb_kbhit() - Returns true if OUT endpoint contains data from //// //// host. //// //// //// //// usb_rx_packet_size() - Returns the size of packet that was //// //// received. usb_kbhit() must return TRUE else //// //// this is not valid. Don't forget in USB there //// //// are 0 len packets! //// //// //// //// usb_get_packet() - Gets one packet that from the host. //// //// usb_kbhit() must return true before you call //// //// this routine or your data may not be valid. //// //// Once usb_kbhit() returns true you want to //// //// call this as soon as possible to get data //// //// out of the endpoint buffer so the PC can //// //// start sending more data, if needed. //// //// This only receives one packet, if you are //// //// trying to receive a multi-packet message //// //// see usb_gets() in usb.c. //// //// //// //// usb_detach() - De-attach USB from the system. //// //// //// //// usb_attach() - Attach USB to the system. //// //// //// //// usb_attached() - Returns TRUE if the device is attached to a //// //// USB cable. A macro that looks at the defined //// //// connection sense pin. //// //// //// //// usb_task() - Keeps track of connection sense, calling //// //// usb_detach() and usb_attach() when needed. //// //// //// //// For more documentation on these functions read the comments at ////
- 2 -
Nguy(cid:1225)n Ng(cid:1233)c H(cid:1133)ng L(cid:1247)p TKTL 40 HVKTQS
//// each function. //// //// //// //// The other functions defined in this file are for use by the //// //// USB code, and is not meant to be used by the user. ////
Các b(cid:1189)n có th(cid:1223) d(cid:1225) dàng tìm hi(cid:1223)u thêm cách th(cid:1261)c s(cid:1265) d(cid:1257)ng các hàm này qua các Example và các Comment c(cid:1259)a CCS. V(cid:1247)i các hàm này b(cid:1189)n (cid:255)ã có th(cid:1223)(cid:3)(cid:255)(cid:76)(cid:1221)u khi(cid:1223)n modul USB c(cid:1259)a pic18 khá linh ho(cid:1189)t và có th(cid:1223) m(cid:1251) r(cid:1245)ng chúng (cid:255)(cid:1223) phù h(cid:1255)p v(cid:1247)i (cid:80)(cid:1257)c (cid:255)ích c(cid:1259)a b(cid:1189)n.
5. (cid:55)(cid:1189)o l(cid:1189)i file mô t(cid:1191) thi(cid:1219)t b(cid:1231) usb_desc_scope1.h(cid:3)(cid:255)(cid:1133)(cid:1255)c th(cid:1269)c hi(cid:1227)n nh(cid:1133) sau:
#DEFINE USB_TOTAL_CONFIG_LEN 32 //config+interface+class+endpoint
//identifier for this configuration. (IF we had more than one
//configuration descriptor char const USB_CONFIG_DESC[] = { //config_descriptor for config index 1 USB_DESC_CONFIG_LEN, //length of descriptor size USB_DESC_CONFIG_TYPE, //constant CONFIGURATION (0x02) USB_TOTAL_CONFIG_LEN,0, //size of all data returned for this config 1, //number of interfaces this device supports 0x01, configurations) 0x00, //index of string descriptor for this configuration 0xC0, //bit 6=1 if self powered, bit 5=1 if supports remote wakeup (we don't), bits 0-4 reserved and bit7=1 0x32, //maximum bus power required (maximum milliamperes/2) (0x32 = 100mA)
//interface descriptor 0 alt 0 USB_DESC_INTERFACE_LEN, //length of descriptor USB_DESC_INTERFACE_TYPE, //constant INTERFACE (0x04) 0x00, //number defining this interface (IF we had more than one interface) 0x00, //alternate setting 2, //number of endpoints, not counting endpoint 0. 0xFF, //class code, FF = vendor defined 0xFF, //subclass code, FF = vendor 0xFF, //protocol code, FF = vendor 0x00, //index of string descriptor for interface
//endpoint descriptor USB_DESC_ENDPOINT_LEN, //length of descriptor USB_DESC_ENDPOINT_TYPE, //constant ENDPOINT (0x05) 0x81, //endpoint number and direction (0x81 = EP1 IN)
0x02, //transfer type supported (0 is control, 1 is iso, 2 is bulk, 3 is interrupt) USB_EP1_TX_SIZE & 0xFF,USB_EP1_TX_SIZE >> 8, //maximum packet size supported 0x01, //polling interval in ms. (for interrupt transfers ONLY)
//endpoint descriptor
- 3 -
Nguy(cid:1225)n Ng(cid:1233)c H(cid:1133)ng L(cid:1247)p TKTL 40 HVKTQS
USB_DESC_ENDPOINT_LEN, //length of descriptor USB_DESC_ENDPOINT_TYPE, //constant ENDPOINT (0x05) 0x01, //endpoint number and direction (0x01 = EP1 OUT) 0x02, //transfer type supported (0 is control, 1 is iso, 2 is bulk, 3 is interrupt) USB_EP1_RX_SIZE & 0xFF,USB_EP1_RX_SIZE >> 8, //maximum packet size
supported 0x01, //polling interval in ms. (for interrupt transfers ONLY)
};
//****** BEGIN CONFIG DESCRIPTOR LOOKUP TABLES ******** //since we can't make pointers to constants in certain pic16s, this is an offset table to find // a specific descriptor in the above table.
//NOTE: DO TO A LIMITATION OF THE CCS CODE, ALL HID INTERFACES MUST START AT 0 AND BE SEQUENTIAL // FOR EXAMPLE, IF YOU HAVE 2 HID INTERFACES THEY MUST BE INTERFACE 0 AND INTERFACE 1 #define USB_NUM_HID_INTERFACES 0
//the maximum number of interfaces seen on any config //for example, if config 1 has 1 interface and config 2 has 2 interfaces you must define this as 2 #define USB_MAX_NUM_INTERFACES 1
//define how many interfaces there are per config. [0] is the first config, etc. const char USB_NUM_INTERFACES[USB_NUM_CONFIGURATIONS]={1};
#if (sizeof(USB_CONFIG_DESC) != USB_TOTAL_CONFIG_LEN) #error USB_TOTAL_CONFIG_LEN not defined correctly #endif
////////////////////////////////////////////////////////////////// /// /// start device descriptors /// //////////////////////////////////////////////////////////////////
//device descriptor char const USB_DEVICE_DESC[] ={ USB_DESC_DEVICE_LEN, //the length of this report 0x01, //constant DEVICE (0x01) 0x10,0x01, //usb version in bcd 0x00, //class code (if 0, interface defines class. FF is vendor defined) 0x00, //subclass code 0x00, //protocol code USB_MAX_EP0_PACKET_LENGTH, //max packet size for endpoint 0. (SLOW SPEED SPECIFIES 8) 0xd8,0x04, //vendor id (0x04D8 is Microchip) 0x01,0x01, //product id 0x00,0x01, //device release number
- 4 -
Nguy(cid:1225)n Ng(cid:1233)c H(cid:1133)ng L(cid:1247)p TKTL 40 HVKTQS
//index of string description of manufacturer. therefore we point to
0x01, string_1 array (see below) 0x02, //index of string descriptor of the product 0x00, //index of string descriptor of serial number USB_NUM_CONFIGURATIONS //number of possible configurations };
////////////////////////////////////////////////////////////////// /// /// start string descriptors /// String 0 is a special language string, and must be defined. People in U.S.A. can leave this alone. /// /// You must define the length else get_next_string_character() will not see the string /// Current code only supports 10 strings (0 thru 9) /// //////////////////////////////////////////////////////////////////
//the offset of the starting location of each string. //offset[0] is the start of string 0, offset[1] is the start of string 1, etc. const char USB_STRING_DESC_OFFSET[]={0,4,12};
#define USB_STRING_DESC_COUNT sizeof(USB_STRING_DESC_OFFSET)
char const USB_STRING_DESC[]={ //string 0 4, //length of string index USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING) 0x09,0x04, //Microsoft Defined for US-English //string 1 8, //length of string index USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING) 'B',0, 'M',0, 'E',0, //string 2 46, //length of string index USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING) 'D',0, 'a',0, 'T',0, 'a',0, ' ',0, 'T',0, 'r',0, 'a',0, 'n',0, 's',0, 'f',0, 'e',0, 'r',0,
- 5 -
Nguy(cid:1225)n Ng(cid:1233)c H(cid:1133)ng L(cid:1247)p TKTL 40 HVKTQS
' ',0, 'B',0, 'u',0, 'l',0, 'k',0, ' ',0, 'N',0, 'N',0, 'H',0, }; #ENDIF
(cid:37)(cid:1189)n không c(cid:1195)n ph(cid:1191)i ch(cid:1229)nh s(cid:1265)a gì nhi(cid:1221)u trong file này, ch(cid:1229) c(cid:1195)n l(cid:1133)u ý (cid:255)(cid:1219)n m(cid:1245)t s(cid:1237) (cid:255)(cid:76)(cid:1223)m mà tôi (cid:255)ã (cid:255)ánh d(cid:1193)u b(cid:1205)ng màu (cid:255)(cid:1235), t(cid:1189)i (cid:255)ó (cid:255)ã có các chú thích b(cid:1205)ng ti(cid:1219)ng anh (cid:85)(cid:1193)t rõ v(cid:1221) ý ngh(cid:429)a c(cid:1259)a chúng. (cid:264)ó là s(cid:1237) thi(cid:1219)t b(cid:1231)(cid:3)(cid:255)(cid:1133)(cid:1255)c h(cid:1243) tr(cid:1255) giao ti(cid:1219)p, s(cid:1237)(cid:3)(cid:255)(cid:76)(cid:1223)m cu(cid:1237)i, vi(cid:1227)c kh(cid:1251)i t(cid:1189)o các (cid:255)(cid:1133)(cid:1249)ng (cid:1237)ng truy(cid:1221)n và nh(cid:1201)n, c(cid:1253) c(cid:1259)a gói truy(cid:1221)n và ph(cid:1133)(cid:1131)ng th(cid:1261)c truy(cid:1221)n. (cid:1250)(cid:3) (cid:255)ây tôi truy(cid:1221)n theo lo(cid:1189)i BULK. (cid:264)ó là nh(cid:1267)ng thông s(cid:1237) b(cid:1189)n c(cid:1195)n quan tâm nh(cid:1133)ng không c(cid:1195)n s(cid:1265)a. Các thông s(cid:1237) sau là vendor id & product id (cid:69)(cid:1189)n có th(cid:1223) s(cid:1265)a tùy ý mi(cid:1225)n là không trùng v(cid:1247)i thi(cid:1219)t b(cid:1231)(cid:3)(cid:255)ã có trong PC c(cid:1259)a b(cid:1189)n. Cu(cid:1237)i cùng là string index (cid:69)(cid:1189)n có s(cid:1265)a (cid:79)(cid:1189)i theo tên mà b(cid:1189)n mong mu(cid:1237)n, chú ý r(cid:1205)ng chi(cid:1221)u dài c(cid:1259)a chu(cid:1243)i ký t(cid:1269) ph(cid:1191)i phù (cid:75)(cid:1255)p v(cid:1247)i khai báo. Còn m(cid:1245)t s(cid:1237) khai báo n(cid:1267)a nh(cid:1133)ng tôi (cid:255)(cid:1223) vào trong file khác (cid:255)(cid:1223) ti(cid:1227)n vi(cid:1227)c s(cid:1265)a (cid:255)(cid:1241)i, c(cid:1257) th(cid:1223)(cid:3)(cid:255)(cid:1133)(cid:1255)c (cid:255)(cid:1223) trong file usb_demo_bulk.h 6. Qu(cid:1191)n lý file usb_demo_bulk.h:
Nh(cid:1133)(cid:3)(cid:255)ã nói (cid:1251) trên file này (cid:255)(cid:1133)(cid:1255)c t(cid:1189)o ra khi ta l(cid:1201)p Project trong CCS, bây gi(cid:1249) ta thêm vào trong (cid:255)ó m(cid:1245)t s(cid:1237) khai báo: #include <18F4550.h> #device adc=8
#FUSES NOWDT //No Watch Dog Timer #FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale #FUSES EC_IO //External clock #FUSES NOPROTECT //Code not protected from reading #FUSES BROWNOUT //Reset when brownout detected #FUSES BORV20 //Brownout reset at 2.0V #FUSES NOPUT //No Power Up Timer #FUSES NOCPD //No EE protection #FUSES STVREN //Stack full/underflow will cause reset #FUSES NODEBUG //No Debug mode for ICD #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O #FUSES NOWRT //Program memory not write protected #FUSES NOWRTD //Data EEPROM not write protected #FUSES IESO //Internal External Switch Over mode enabled #FUSES FCMEN //Fail-safe clock monitor enabled #FUSES PBADEN //PORTB pins are configured as analog input channels on RESET #FUSES NOWRTC //configuration not registers write protected #FUSES NOWRTB //Boot block not write protected #FUSES NOEBTR //Memory not protected from table reads
- 6 -
Nguy(cid:1225)n Ng(cid:1233)c H(cid:1133)ng L(cid:1247)p TKTL 40 HVKTQS
#FUSES NOEBTRB //Boot block not protected from table reads #FUSES NOCPB //No Boot Block code protection #FUSES MCLR //Master Clear pin enabled #FUSES LPT1OSC //Timer1 configured for low-power operation #FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled #FUSES PLL3 // PLL PreScaler 3 #FUSES USBDIV #FUSES VREGEN #FUSES CPUDIV1 #FUSES HSPLL
#use delay(clock=12000000) #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
USB_ENABLE_BULK //turn on EP1 for IN
USB_ENABLE_BULK //turn on EP1 for OUT
#DEFINE USB_HID_DEVICE FALSE #define USB_EP1_TX_ENABLE bulk/interrupt transfers #define USB_EP1_RX_ENABLE bulk/interrupt transfers #define USB_EP1_TX_SIZE 64 //size to allocate for the tx endpoint 1 buffer #define USB_EP1_RX_SIZE 8 //size to allocate for the rx endpoint 1 buffer
7. ch(cid:1133)(cid:1131)ng trình chính:
void setup()
{
setup_adc_ports(AN0|VSS_VDD);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_low_volt_detect(FALSE);
setup_oscillator(False);
}
Nh(cid:1267)ng (cid:255)(cid:76)(cid:1223)m chú ý tôi (cid:255)ã (cid:255)ánh d(cid:1193)u (cid:1251) trên, b(cid:1189)n c(cid:1195)n l(cid:1133)u ý r(cid:1205)ng t(cid:1195)n s(cid:1237) th(cid:1189)nh anh
ngoài s(cid:1217)(cid:3)(cid:255)(cid:1133)(cid:1255)c chia (cid:255)(cid:1223)(cid:3)(cid:255)(cid:1133)(cid:1255)c dao (cid:255)(cid:1245)ng 4M (cid:255)ây là yêu c(cid:1195)u b(cid:1203)t bu(cid:1245)c khi s(cid:1265) d(cid:1257)ng
PLL. (cid:1250)(cid:3)(cid:255)ây tôi dùng th(cid:1189)ch anh 12M nên PLL=3. M(cid:1245)t s(cid:1237) tham s(cid:1237) khác b(cid:1189)n có th(cid:1223)
xem chi ti(cid:1219)t trong datasheet.
Nh(cid:1133) v(cid:1201)y (cid:255)(cid:1219)n (cid:255)ây ta ch(cid:1229) còn m(cid:1245)t công vi(cid:1227)c n(cid:1267)a là vi(cid:1219)t hàm main.
#include "E:\MICROCONTROL\PIC\USB\usb_demo_bulk.h"
#include
void usb_debug_task(void) {
- 7 -
Nguy(cid:1225)n Ng(cid:1233)c H(cid:1133)ng L(cid:1247)p TKTL 40 HVKTQS
static int8 last_connected; static int8 last_enumerated; int8 new_connected; int8 new_enumerated;
new_connected=usb_attached(); new_enumerated=usb_enumerated();
if (new_connected && !last_connected) printf("\r\n\nUSB connected, waiting for enumaration..."); if (!new_connected && last_connected) printf("\r\n\nUSB disconnected, waiting for connection..."); if (new_enumerated && !last_enumerated) printf("\r\n\nUSB enumerated by PC/HOST"); if (!new_enumerated && last_enumerated) printf("\r\n\nUSB unenumerated by PC/HOST, waiting for enumeration...");
last_connected=new_connected; last_enumerated=new_enumerated; }
void main() { int8 out_data[2]; int8 in_data[2]; int8 send_timer=0; int8 count=0; int16 i;
setup();
// TODO: USER CODE!! printf("\r\n Transfer BULK Example"); usb_init_cs();
while (TRUE) { usb_task(); usb_debug_task(); if(usb_enumerated()) { if (!send_timer) { count++; send_timer=250; out_data[0]=count; if (usb_put_packet(1, out_data, 1, USB_DTS_TOGGLE)) printf("\r\n<-- Sending 2 bytes: 0x%X", out_data[0]); } if (usb_kbhit(1)) { usb_get_packet(1, in_data, 1);
- 8 -
Nguy(cid:1225)n Ng(cid:1233)c H(cid:1133)ng L(cid:1247)p TKTL 40 HVKTQS
printf("\r\n--> Received data: 0x%X",in_data[0]);; } send_timer--; delay_ms(1); } } (cid:38)(cid:1133) b(cid:1191)n ch(cid:1133)(cid:1131)ng trình trên gi(cid:1237)ng v(cid:1247)i ví d(cid:1257) c(cid:1259)a CCS. Trong ch(cid:1133)(cid:1131)ng trình s(cid:1265) d(cid:1257)ng hàm usb_debug_task() dùng (cid:255)(cid:1223) g(cid:1253) r(cid:1237)i b(cid:1205)ng giao ti(cid:1219)p UART, n(cid:1219)u b(cid:1189)n s(cid:1265) d(cid:1257)ng laptop không có c(cid:1241)ng COM thì có th(cid:1223) thay chúng b(cid:1205)ng vi(cid:1227)c hi(cid:1223)n th(cid:1231) ra LED. Trong ch(cid:1133)(cid:1131)ng trình chính th(cid:1269)c hi(cid:1227)n c(cid:1261) 250ms thì truy(cid:1221)n qua bus USB lên PC giá tr(cid:1231) count, giá tr(cid:1231) này sau m(cid:1243)i l(cid:1195)n truy(cid:1221)n (cid:255)(cid:1133)(cid:1255)c t(cid:259)ng lên 1, khi (cid:255)(cid:1219)n giá tr(cid:1231) 0xFF thì (cid:87)(cid:1269)(cid:3) (cid:255)(cid:1245)ng tr(cid:1191) v(cid:1221) 0. Trong ch(cid:1133)(cid:1131)ng trình còn liên t(cid:1257)c ki(cid:1223)m tra xem (cid:255)(cid:76)(cid:1223)m cu(cid:1237)i có nh(cid:1201)n (cid:255)(cid:1133)(cid:1255)c d(cid:1267) li(cid:1227)u t(cid:1263) PC không, n(cid:1219)u có thì l(cid:1193)y d(cid:1267) li(cid:1227)u trong b(cid:1245)(cid:3) (cid:255)(cid:1227)m v(cid:1221) bi(cid:1219)n in_data. C(cid:1191) truy(cid:1221)n và nh(cid:1201)n (cid:255)(cid:1221)u (cid:255)(cid:1133)(cid:1255)c ki(cid:1223)m tra b(cid:1205)ng vi(cid:1227)c hi(cid:1223)n th(cid:1231) qua UART. (cid:264)(cid:1219)n (cid:255)ây ta (cid:255)ã hoàn t(cid:1193)t vi(cid:1227)c vi(cid:1219)t fimware cho V(cid:264)K, (cid:1251)(cid:3)(cid:255)ây tôi dùng PIC18F4550 vi(cid:1227)c vi(cid:1219)t ch(cid:1133)(cid:1131)ng trình cho PIC18f2550 không có gì khác. Chúng ta b(cid:1203)t tay vào vi(cid:1227)c t(cid:1189)o driver cho thi(cid:1219)t b(cid:1231) và vi(cid:1219)t m(cid:1245)t ch(cid:1133)(cid:1131)ng trình giao di(cid:1227)n (cid:255)(cid:1131)n gi(cid:1191)n b(cid:1205)ng C#.
- 9 -