2010年5月11日 星期二

USB設備及電腦監控整合設計

USB設備上的控制軟體大多使用C語言來設計,一般可使用Keil C IDE軟體來開發,電腦端的控制軟體則可以使用C++或VB,本文說明利用VB來設計,其軟體可由http://www.lvr.com/hidpage.htm網站中取得,我們以usbhidio2(Viusal Basic)為例,至於USB設備上的控制軟體可到長高網站或冾長高科技。以下介紹幾個重要的部份:
1. 修改VID及PID,此部份可以修改VB或Keil C,只要兩邊一致即可。
例如:修改dscr.a51(粗體是修改的部份)
DeviceDscr: db deviceDscrEnd-DeviceDscr ;; Descriptor length
db DSCR_DEVICE ;; Decriptor type
db 10H, 01H ;; Specification Version (BCD)
db 00H ;; Device class
db 00H ;; Device sub-class
db 00H ;; Device sub-sub-class
db 64 ;; Maximum packet size
db 25H, 9H ;; Vendor ID ;;************************
db 99H, 12H ;; Product ID ;;************************
dw 0001H ;; Product version ID
db 1 ;; Manufacturer string index
db 2 ;; Product string index
db 0 ;; Serial number string index
db 1 ;; Numder of configurations
deviceDscrEnd:

2. 讓USB設備起動時能用燈號來顯示(修改periph.c中TD_Init())
該函式只執行一次。
void TD_Init(void) // Called once at startup
{
int i,j;
PORTACFG = 0x00;
OEA = 0xFF;

PORTBCFG = 0x00;
OEB = 0xFF;

OED = 0xFF;
P0=0;
for(i=0; i<1000; i++)
for(j=0; j<1000; j++);
P0=0xff;
for(i=0; i<1000; i++)
for(j=0; j<1000; j++);
P0=0;

IN07VAL |= bmEP2 ;
OUT07VAL |= bmEP2;

OUT07IEN |= bmEP2;

IN07IEN |= bmEP2;

//HID code end

suspCount = 1;

OEA = 0xFF;

Rwuen = TRUE; // Enable remote-wakeup
}
3. 讓USB設備上的燈號持續顯示,可以修改TD_Poll函式,宣告一個全域變數cnt並在TD_Init()上初始化。
int cnt;
void TD_Init(void) // Called once at startup
{
int i,j;
PORTACFG = 0x00;
OEA = 0xFF;

PORTBCFG = 0x00;
OEB = 0xFF;

OED = 0xFF;
P0=0;
for(i=0; i<1000; i++)
for(j=0; j<1000; j++);
P0=0xff;
for(i=0; i<1000; i++)
for(j=0; j<1000; j++);
P0=0;
cnt=0;
IN07VAL |= bmEP2 ;
OUT07VAL |= bmEP2;

OUT07IEN |= bmEP2;

IN07IEN |= bmEP2;

//HID code end

suspCount = 1;

OEA = 0xFF;

Rwuen = TRUE; // Enable remote-wakeup
}
void TD_Poll(void) // Called repeatedly while the device is idle
{
cnt++;
if(cnt>10000)
{
P0=1-P0;
cnt=0;
}
}
4. 能由電腦來控制故要修改ISR_Ep2out(),利用該函式來控制flag全域變數,對TD_Poll()函式進行控制。
int cnt;
int flag;
void TD_Init(void) // Called once at startup
{
int i,j;
PORTACFG = 0x00;
OEA = 0xFF;

PORTBCFG = 0x00;
OEB = 0xFF;

OED = 0xFF;
P0=0;
for(i=0; i<1000; i++)
for(j=0; j<1000; j++);
P0=0xff;
for(i=0; i<1000; i++)
for(j=0; j<1000; j++);
P0=0;
cnt=0;
flag=0; IN07VAL |= bmEP2 ;
OUT07VAL |= bmEP2;

OUT07IEN |= bmEP2;

IN07IEN |= bmEP2;

//HID code end

suspCount = 1;

OEA = 0xFF;

Rwuen = TRUE; // Enable remote-wakeup
}

void ISR_Ep2out(void) interrupt 0

{
int i;

if (EPIO[IN2BUF_ID].cntrl & bmEPBUSY)
{
TOGCTL = 0x08 | IN2BUF_ID;
WRITEDELAY();
if (TOGCTL & 0x80)
TOGCTL |= 0x20;
else
TOGCTL |= 0x40;
}


for (i=0; i < OUT2BC; i++)
{
flag=OUT2BUF[0];
// P0=OUT2BUF[0];
// P1=OUT2BUF[1]; //-------------------------------------------------
// P3=OUT2BUF[2];


}


OUT2BC = 0;


EZUSB_IRQ_CLEAR();
OUT07IRQ = bmEP2;
}
5. 最後修改VB控制程式,增加兩個按鈕程式
Private Sub Command1_Click()
If MyDeviceDetected = False Then
MyDeviceDetected = FindTheHid

End If

If MyDeviceDetected = True Then


OutputReportData(0) = 0
'Write a report to the device

Call WriteReport
Call WriteReport

'Read a report from the device.

Call ReadReport
End If

End Sub

Private Sub Command2_Click()
If MyDeviceDetected = False Then
MyDeviceDetected = FindTheHid

End If

If MyDeviceDetected = True Then


OutputReportData(0) = 1
'Write a report to the device

Call WriteReport
Call WriteReport


'Read a report from the device.

Call ReadReport
End If

End Sub

沒有留言:

張貼留言