Hi every body
i m trying to blink some leds with pic 18f2550 using usb connection to my labtop
i have some problems
1- when i open vb2008 interface, it doesnt identify that usb is plugged untill i touch Vusb with my hand. i aready put 200nf capacitor to ground on Vusb. i is very strange.
2- when i send data to pic, i doesnt blink the leds. i revised vb code many times. and i revised my controller code also. i just cant find what is wrong.
plz help me. it driven me crazy. espicially when it needs my to provide a path to ground with my body on Vusb!!!
pic code
Code:
DEFINE OSC 48
USBBufferSizeMax con 8 ' maximum buffer size
USBBufferSizeTX con 8 ' input
USBBufferSizeRX con 8 ' output
' the USB buffer...
USBBuffer Var Byte[USBBufferSizeMax]
USBBufferCount Var Byte
' ************************************************************
' * main program loop - remember, you must keep the USB *
' * connection alive with a call to USBService every couple *
' * of milliseconds or so... *
' ************************************************************
usbinit ' initialise USB...
ProgramStart:
USBBUFFER[0] = 0
USBBUFFER[1] = "P"
USBBUFFER[2] = "="
USBBUFFER[3] = 15
USBBUFFER[4] = "T"
gosub DoUSBOut
gosub DoUSBIn
portb = usbbuffer[3]
goto ProgramStart
' ************************************************************
' * receive data from the USB bus *
' ************************************************************
DoUSBIn:
USBBufferCount = USBBufferSizeRX ' RX buffer size
USBService ' keep connection alive
USBIn 1, USBBuffer, USBBufferCount, DoUSBIn ' read data, if available
return
' ************************************************************
' * wait for USB interface to attach *
' ************************************************************
DoUSBOut:
USBBufferCount = USBBufferSizeTX ' TX buffer size
USBService ' keep connection alive
USBOut 1, USBBuffer, USBBufferCount, DoUSBOut ' if bus available, transmit data
return
vb code
Code:
Public Class frmUSB
' vendor and product IDs
Private Const VendorID As Short = 4660 'Replace with your device's
Private Const ProductID As Short = 1 'product and vendor IDs
' read and write buffers
Private Const BufferInSize As Short = 8 'Size of the data buffer coming IN to the PC
Private Const BufferOutSize As Short = 8 'Size of the data buffer going OUT from the PC
Dim BufferIn(BufferInSize) As Byte 'Received data will be stored here - the first byte in the array is unused
Dim BufferOut(BufferOutSize) As Byte 'Transmitted data is stored here - the first item in the array must be 0
' ****************************************************************
' when the form loads, connect to the HID controller - pass
' the form window handle so that you can receive notification
' events...
'*****************************************************************
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' do not remove!
ConnectToHID(Me)
Label2.Text = "Connected to HID"
End Sub
'*****************************************************************
' disconnect from the HID controller...
'*****************************************************************
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
DisconnectFromHID()
End Sub
'*****************************************************************
' a HID device has been plugged in...
'*****************************************************************
Public Sub OnPlugged(ByVal pHandle As Integer)
If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
' ** YOUR CODE HERE **
Label2.Text = " USB plugged"
End If
End Sub
'*****************************************************************
' a HID device has been unplugged...
'*****************************************************************
Public Sub OnUnplugged(ByVal pHandle As Integer)
If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
hidSetReadNotify(hidGetHandle(VendorID, ProductID), False)
' ** YOUR CODE HERE **
Label2.Text = "USB unplugged"
End If
End Sub
'*****************************************************************
' controller changed notification - called
' after ALL HID devices are plugged or unplugged
'*****************************************************************
Public Sub OnChanged()
' get the handle of the device we are interested in, then set
' its read notify flag to true - this ensures you get a read
' notification message when there is some data to read...
Dim pHandle As Integer
pHandle = hidGetHandle(VendorID, ProductID)
hidSetReadNotify(hidGetHandle(VendorID, ProductID), True)
End Sub
'*****************************************************************
' on read event...
'*****************************************************************
Public Sub OnRead(ByVal pHandle As Integer)
' read the data (don't forget, pass the whole array)...
If hidRead(pHandle, BufferIn(0)) Then
' ** YOUR CODE HERE **
TextBox2.Text = Str$(BufferIn(3))
Label2.Text = "Data recieved"
' first byte is the report ID, e.g. BufferIn(0)
' the other bytes are the data from the microcontroller...
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BufferOut(0) = 0 'first byte is always the report ID
BufferOut(1) = Asc("P") 'first data item
BufferOut(2) = Asc("=") 'second data item
BufferOut(3) = Val(TextBox1.Text) 'third data item to be send ober usb
BufferOut(4) = Asc("T") 'fourth data item
'write the data, dont forget to send the whole array
hidWriteEx(VendorID, ProductID, BufferOut(0))
hidWriteEx(VendorID, ProductID, BufferOut(1))
Label2.Text = "Data sent"
End Sub
End Class
Bookmarks