Hello, i have a problem with usb. Here is the code:
PBP code:
DEFINE LOADER_USED 1 ' Using Mecanique 18f4550 loader
DEFINE OSC 20
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source /32
Define ADC_SAMPLEUS 50 ' Set sampling time in uS
BufferSize con 8
DataBuffer Var Byte(BufferSize) ' data buffer
DataBufferCount Var Byte ' buffer size
Quanta con 1251
' Variables
X VAR byte
Adval VAR WORD
TRISA.0 = 1 ' RA0 input
ADCON1 = %00001110 ' A/D channel 0
ADCON2 = %10000011 ' Left justify for 10-bit
USBInit
Main:
ADCIN 0,adval ' Read A/D channel 0 into ADval variable
ADval = ADval */ Quanta ' Quanta result
' Load data buffer
DataBuffer(0) = Adval dig 3 'Y.LowByte
DataBuffer(1) = "."
dATAbUFFER(2) = Adval DIG 2
DataBuffer(3) = Adval DIG 1
DataBuffer(4) = aDVAL DIG 0
gosub DoUSBOut
for X = 0 to 99 ' Short delay between updates
pausEUS 1000
USBSERVICE ' Maintain HID connection during delay period
NEXT X
goto Main
' USB in...
DoUSBIn:
DataBufferCount = BufferSize
USBService
USBIn 1, DataBuffer, DataBufferCount, DoUSBIn
return
' USB out...
DoUSBOut:
DataBufferCount = BufferSize
USBService
USBOut 1, DataBuffer, DataBufferCount, DoUSBOut
return
end
-----------------------------------------------------------
VB code:
' vendor and product IDs
Private Const VendorID = 6017
Private Const ProductID = 2000
' read and write buffers
Private Const BufferInSize = 8
Private Const BufferOutSize = 8
Dim BufferIn(0 To BufferInSize)
Dim BufferOut(0 To BufferOutSize)
Dim voltaje As String
' ************************************************** **************
' when the form loads, connect to the HID controller - pass
' the form window handle so that you can receive notification
' events...
'************************************************* ****************
Private Sub Form_Load()
' do not remove!
ConnectToHID (Me.hwnd)
End Sub
'************************************************* ****************
' disconnect from the HID controller...
'************************************************* ****************
Private Sub Form_Unload(Cancel As Integer)
DisconnectFromHID
End Sub
'************************************************* ****************
' a HID device has been plugged in...
'************************************************* ****************
Public Sub OnPlugged(ByVal pHandle As Long)
If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
' ** YOUR CODE HERE **
estado.Caption = "Plugged in"
End If
End Sub
'************************************************* ****************
' a HID device has been unplugged...
'************************************************* ****************
Public Sub OnUnplugged(ByVal pHandle As Long)
If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
' ** YOUR CODE HERE **
estado.Caption = "Unplugged"
Label1.Caption = "Nothing"
End If
End Sub
'************************************************* ****************
' controller changed notification - called
' after ALL HID devices are plugged or unplugged
'************************************************* ****************
Public Sub OnChanged()
Dim DeviceHandle As Long
' 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...
DeviceHandle = hidGetHandle(VendorID, ProductID)
hidSetReadNotify DeviceHandle, True
End Sub
'************************************************* ****************
' on read event...
'************************************************* ****************
Public Sub OnRead(ByVal pHandle As Long)
' read the data (don't forget, pass the whole array)...
If hidRead(pHandle, BufferIn(0)) Then
' ** YOUR CODE HERE **
' first byte is the report ID, e.g. BufferIn(0)
' the other bytes are the data from the microcontroller...
voltaje = (CStr(BufferIn(1)) + "." + CStr(BufferIn(3)) + CStr(BufferIn(4)) + "V")
Label1.Caption = voltaje
End If
End Sub
'************************************************* ****************
' this is how you write some data...
'************************************************* ****************
Public Sub WriteSomeData()
BufferOut(0) = 0 ' first by is always the report ID
BufferOut(1) = 10 ' first data item, etc etc
' write the data (don't forget, pass the whole array)...
'hidWriteEx VendorID, ProductID, BufferOut(0)
End Sub
------------------------------------------------------------------
It doesnt work in visual basic, i only receive ".V" in the label, but i tested with the demo datalogger and it works. I dont know whats happened. Help me please
Thanks in advance
Bookmarks