doianhve
20-06-2007, 01:50 PM
Thôi đành làm cách cổ điển vậy.Nhớ tạo một Foder sau: D:/Pic_USB
File chính:Truyền một Xung Sin lên PC theo chuẩn Bulk endpoint81
----------------------tên là Main.C-------------------------------
////////////////////////////////////////////////////////////////////////////////
// Chu y: //
// *) De tien trong viec lap trinh va ve do thi tren PC chung ta can: //
// truoc khi chuan hoa mau can chon tan so cho thiet bi //
// bang cach gui len mot thong tin mac dinh Vd gui len chu "Tan So": //
// sau do la gui tiep 4 byte chon tan so cho thiet bi //
// *) Truyen bulk thi kich thuoc toi da chi 64byte(Full_speed) //
// 512(hight_Speed) nen ta chi co the truyen toi da //
// la 64byte=chi so mang //
// *) Toc do truyen toi da dat duoc la:1.216Mbytes/s
////////////////////////////////////////////////////////////////////////////////
#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CP UDIV1,VREGEN
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#DEFINE USB_HID_DEVICE FALSE
// Khai bao che do cho cac pipe line
#define USB_EP1_TX_ENABLE USB_ENABLE_BULK //Chon che do Bulk cho USB EndIn 0x81
#define USB_EP2_TX_ENABLE USB_ENABLE_BULK //Chon che do Bulk cho USB EndIn 0x82
#define USB_EP3_TX_ENABLE USB_ENABLE_BULK //Chon che do Bulk cho USB EndIn 0x83
#define USB_EP1_RX_ENABLE USB_ENABLE_BULK //Endpoint Out01
//Kich thuoc toi da cho bo dem truyen=64Bytes
#define USB_EP1_TX_SIZE 64
#define USB_EP2_TX_SIZE 64
#define USB_EP3_TX_SIZE 64
#define USB_EP1_RX_SIZE 8 //Kich thuoc nhan toi da=8Bytes
/////////////////////////////////////////////////////////////////////////////
// D+----------------D+ //
// D-----------------D- //
// 100nF //
// Vdd--------||------Vss //
/////////////////////////////////////////////////////////////////////////////
//------->>>........------->>>........Phai thay doi duong dan truoc khi Compiler......------->>>........
#include <D:\Pic_USB\pic18_usb.h>
#include <D:\Pic_USB\Bulk.h> //Khai bao cac thong tin va dinh dang cho USB
#include <D:\Pic_USB\usb.c> //Su dung cac thu tuc va ham de truyen USB
#define SINE_TABLE_SIZE 256
const char sine_table[SINE_TABLE_SIZE]={
127, 130, 133, 136, 139, 142, 145, 148, 151, 154, 157, 160, 163, 166, 169,
172, 175, 178, 181, 184, 186, 189, 192, 195, 197, 200, 202, 205, 207, 210,
212, 214, 216, 219, 221, 223, 225, 227, 229, 230, 232, 234, 236, 237, 239,
240, 241, 243, 244, 245, 246, 247, 248, 249, 250, 250, 251, 252, 252, 253,
253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 252, 252, 251, 250, 250,
249, 248, 247, 246, 245, 244, 242, 241, 240, 238, 237, 235, 234, 232, 230,
228, 226, 224, 222, 220, 218, 216, 214, 211, 209, 207, 204, 202, 199, 197,
194, 191, 189, 186, 183, 180, 178, 175, 172, 169, 166, 163, 160, 157, 154,
151, 148, 145, 142, 138, 135, 132, 129, 127, 124, 121, 118, 114, 111, 108,
105, 102, 99, 96, 93, 90, 87, 84, 81, 78, 75, 73, 70, 67, 64,
62, 59, 56, 54, 51, 49, 46, 44, 42, 39, 37, 35, 33, 31, 29,
27, 25, 23, 22, 20, 18, 17, 15, 14, 12, 11, 10, 9, 8, 7,
6, 5, 4, 3, 3, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12,
13, 14, 16, 17, 19, 21, 22, 24, 26, 28, 30, 32, 34, 36, 38,
41, 43, 45, 48, 50, 53, 55, 58, 60, 63, 66, 69, 71, 74, 77,
80, 83, 86, 89, 92, 95, 98, 101, 104, 107, 110, 113, 116, 119, 122, 125
};
void main(void) {
int8 i;
int8 out_data[1];
///----------------------tao mot bang thong bao truoc khi gui du lieu de chon tan so Hz lay mau-------------
char Str[31]; // tao mot mang String gom co
//strcpy(str,"Nguyen Duc Hung_DL1_K47_DHBKHN."); //Chuoi String co 31 ki tu
///---------------------------------------------------------------------------------------------------------
//out_data[0]=i;
usb_init(); // Day la ham xac dinh cau hinh phan cung USB cua Pic:Che do pullup trong.Phai co neu ko la ko chay
//usb_task();
//usb_puts(1,out_data,1,50); //usb_puts(endpoint,out_data,len(out_data),Timeout); ___len(out_data)=chi so mang
// Muon dung Endpoint nao thi phai khai bao trong bulk.C
//delay_ms(5000);
//usb_puts(1,Str,31,50);
while (true)
{
i=0;
while(i<=254)
{
//delay_ms(100);
out_data[0]=1;//sine_table[i];
usb_task(); //Day la ham co tac dung luon de y den USB dang su dung trong he thong
usb_puts(1,out_data,1,50); // Ham truyen gia tri thu duoc theo mot mang
// khong gioi han.Xem cu the trong usb.h.Du lieu truyen theo mang
++i;
}
i=0;
while(i<=254)
{
//delay_ms(100);
out_data[0]=2;//sine_table[i];
usb_task(); //Day la ham co tac dung luon de y den USB dang su dung trong he thong
usb_puts(2,out_data,1,50); // Ham truyen gia tri thu duoc theo mot mang
// khong gioi han.Xem cu the trong usb.h.Du lieu truyen theo mang
++i;
}
i=0;
while(i<=254)
{
//delay_ms(100);
out_data[0]=3;//sine_table[i];
usb_task(); //Day la ham co tac dung luon de y den USB dang su dung trong he thong
usb_puts(3,out_data,1,50); // Ham truyen gia tri thu duoc theo mot mang
// khong gioi han.Xem cu the trong usb.h.Du lieu truyen theo mang
++i;
}
} // sau khi qua cau lenh While() ko the truen USb dc nua ????????????????
//Delay_ms(2000);
//usb_task();
//usb_puts(1,Str,31,50);
}
doianhve
20-06-2007, 01:52 PM
File tiếp theo để định dạng cấu hình:
---------------------ten Bulk.c-------------------
#IFNDEF __USB_DESCRIPTORS__
#DEFINE __USB_DESCRIPTORS__
#include <D:\Pic_USB\usb.h>
#DEFINE USB_TOTAL_CONFIG_LEN 46 //46 config+interface+class+endpoint
//Thong tin ve cau truc cua thiet bi
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, //identifier for this configuration. (IF we had more than one 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
4, //So Endpoint su dung trong chuong trinh(khong tinh Endpint 0 la Endpoint Control)
0xFF, //class code, FF = vendor defined
0xFF, //subclass code, FF = vendor
0xFF, //protocol code, FF = vendor
0x00, //index of string descriptor for interface
//endpoint 81
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,0x00, //maximum packet size supported Max=64
0x01, //thoi gan hoi vong (polling) chi dung trong Ngat
//Endpoin 82
USB_DESC_ENDPOINT_LEN, //length of descriptor
USB_DESC_ENDPOINT_TYPE, //constant ENDPOINT (0x05)
0x82, //endpoint number and direction (0x82 = EP1 IN)
0x02, //transfer type supported (0 is control, 1 is iso, 2 is bulk, 3 is interrupt)
USB_EP2_TX_SIZE,0x00, //maximum packet size supported Max=64
0x01,
//Endpoin 83
USB_DESC_ENDPOINT_LEN, //length of descriptor
USB_DESC_ENDPOINT_TYPE, //constant ENDPOINT (0x05)
0x83, //endpoint number and direction (0x83 = EP1 IN)
0x02, //transfer type supported (0 is control, 1 is iso, 2 is bulk, 3 is interrupt)
USB_EP3_TX_SIZE,0x00, //maximum packet size supported Max=64
0x01,
//endpoint descriptor
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,0x00, //maximum packet size supported
0x01, //polling interval in ms. (for interrupt transfers ONLY)
};
#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
//////////////////////////////////////////////////////////////////
/// //
/// Dinh dang thiet bi USB //
/// De hieu ro hon phan nay xin doc USBcomplete.Pdf kem theo //
//////////////////////////////////////////////////////////////////
char const USB_DEVICE_DESC[] ={
USB_DESC_DEVICE_LEN, //the length of this report=18.Chuan cua USB
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).La endpoint Control
//-------------------------------------------------------------
//__vendor id va product id la ma san pham duoc host nhan ra:kich thuoc la 2 bytes
0x07,0x20, //vendor id la ma cong ty duoc dang ki tren: http:// usb.org__VD:0x07,0x20,=2007
0x04,0x01, //product id la ma san pham ___VD:0x04,0x01,=0104
//-------------------------------------------------------------
0x00,0x01, //device release number
0x01, //index of string description of manufacturer. therefore we point to 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
};
///////////////////////////////////////////////////////////////////////////////////////////
/// Day la dong thong bao cho Host biet hiet bi ma no tim thay.Ding dang thiet bi //
/// String 0 :Ngon ngu the dien,va chung ta phai ding nghia //
///////////////////////////////////////////////////////////////////////////////////////////
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____________Ngon ngu hien thi
4, //length of string index
USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING)
0x09,0x04, //Microsoft Defined for US-English
//string 1____________Ten thiet bi se duoc hien thi tren Host
8, //length of string index
USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING)
'D',0,
'L',0,
'1',0,
//string 2____________noi dung thong bao tren tastbar khi cam thiet bi vao PC
42, //_____Do dai cua chuoi thong bao thiet bi.Tinh ca so 0=So_ki_tu*2+2.neu thua se co loi ki tu
USB_DESC_STRING_TYPE, //________Dung de truyen hay thong bao cho CCS biet do la String
'D',0,
'o',0,
' ',0,
'A',0,
'n',0,
' ',0,
'T',0,
'o',0,
't',0,
' ',0,
'N',0,
'g',0,
'h',0,
'i',0,
'e',0,
'p',0,
' ',0,
'K',0,
'4',0,
'7',0,
};
#ENDIF
vBulletin® v3.8.11, Copyright ©2000-2025, vBulletin Solutions Inc.