Hello, i would like to discuss with us about usb link with proton. As a beginner in the matter i want to share and get help from us
Posted on: November 23, 2011, 04:53:59 16:53 - Automerged
1.-Test Hardware...
I did a very simple board with a 18F4455 and two potentiometers, three buttons and three leds , nothing special... but for practical purposes i tested it on isis

2.-Code generation...
I used easyHID (as i don't know if is there another more simple tool to generate both pc and pic base code)
the pc program is to be done in a first step with vb6 and (if possible) migration to a more modern version of visual or recoding using visualC template...
'****************************************************************
'* Author : Julio Aguilar Angulo *
'* Version : 1.0 *
'****************************************************************
Device 18F4455
Xtal 48
USB_Descriptor = "DESC.inc"
All_Digital= true
' Hardware Settings:
' LCD settings: PORTD
Declare LCD_RSPin PORTD.2 ' RS pin RD0
Declare LCD_ENPin PORTD.3 ' ENA pin RD1
Declare LCD_DTPin PORTD.4 ' 4 bit bus mode
' adc settings: PORTA
Adin_Res 10 ' 10 bit resolution(0-5V = 0 - 1023)
Adin_Tad FRC ' Internal RC osc for sampling
Adin_Stime 100 ' adc recharging time of 100uS
' USB Buffer...
Symbol USBBufferSizeMax = 8
Symbol USBBufferSizeTX = 8
Symbol USBBufferSizeRX = 8
Dim USBBuffer[USBBufferSizeMax] As Byte
' some useful flags...
Dim PP0 As Byte System ' USBPOLL status return
Symbol CARRY_FLAG = STATUS.0 ' high if microcontroller does not have control over the buffer
Symbol ATTACHED_STATE = 6 ' is USB attached
'*****************************************************************
' Variable declarations:
' adc variables
Dim sample As Word ' rename entire contents of adc results register
Dim acc_sample As Word ' temporal register for calculations
Dim sample1 As Word ' rename entire contents of adc results register
Dim acc_sample1 As Word ' temporal register for calculations
' adc calculations variables
' adc test variables
Dim tvolt As Byte ' volts register
Dim tmilli As Byte ' milivolts register
Dim tvolt1 As Byte ' volts register
Dim tmilli1 As Byte ' milivolts register
'****************************************************************
' adc hardware settings
TRISA = $FF ' RA.0 - RA.1 --> ch0 ch1
TRISD = 0
ADCON1 = %10001010 ' PORTA.0 ES ANALÓGICA
ADCON2 = %10000000 ' EL RESULTADO DEL ADC JUSTIFICADO A LA DERECHA
TRISB=0
LATB=0
PORTB=0
Clear
' lcd messages layout
Print At 1,1,"Ch0: --,-- V"
Print At 2,1,"Ch1: --,-- V"
PORTD.0=1
' ************************************************************
' * main program loop - remember, you must keep the USB *
' * connection alive with a call to USBPoll, USBIn or USBOut *
' * every couple of milliseconds or so *
' ************************************************************
GoSub AttachToUSB
mainloop:
' data acquisition
sample = ADIn 0
sample1 = ADIn 1
' data conversion to 'Volts'
acc_sample = 488 * (acc_sample / 10) '
acc_sample1 = 488 * (acc_sample1 / 10) '
tvolt = acc_sample / 10000 ' VOLTS
tmilli = (acc_sample // 10000) / 100 ' MILLIVOLTS
tvolt1 = acc_sample1 / 10000 ' VOLTS
tmilli1 = (acc_sample1 // 10000) / 100 ' MILLIVOLTS
acc_sample = 0
acc_sample1 = 0
' displaying the measured data:
Print At 1,6,Dec2 tvolt,".",Dec2 tmilli
USBPoll
Print At 2,6,Dec2 tvolt1,".",Dec2 tmilli1
USBBuffer[0] = 0
USBBuffer[1] = tvolt
USBBuffer[2] = tmilli
USBBuffer[3] = tvolt1
USBBuffer[4] = tmilli1
GoSub DoUSBOut ' transmit data via the usb link
DelayMS 1
GoTo mainloop
' ************************************************************
' * receive data from the USB bus *
' ************************************************************
DoUSBIn:
USBIn 1, USBBuffer, USBBufferSizeRX, DoUSBIn
Return
' ************************************************************
' * transmit data *
' ************************************************************
DoUSBOut:
USBOut 1, USBBuffer, USBBufferSizeTX, DoUSBOut
Return
' ************************************************************
' * wait for USB interface to attach *
' ************************************************************
AttachToUSB:
Repeat
USBPoll
Until PP0 = ATTACHED_STATE
Return
Posted on: November 23, 2011, 05:01:32 17:01 - Automerged
now the vb6 code for the pc
Dim Vari As String
' 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) As Byte
Dim BufferOut(0 To BufferOutSize) As Byte
Dim tvolt As Byte
Dim tvolt1 As Byte
Dim tmilli As Byte
Dim tmilli1 As Byte
Dim ch0 As Double
Dim ch1 As Double
' ****************************************************************
' 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
Me.Caption = " connected"
' ** YOUR CODE HERE **
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
Me.Caption = "not connected"
' ** YOUR CODE HERE **
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
tvolt = BufferIn(2)
tmilli = BufferIn(3)
tvolt1 = BufferIn(4)
tmilli1 = BufferIn(5)
ch0 = tvolt + (tmilli / 100)
ch1 = tvolt1 + (tmilli1 / 100)
' Matlab.Execute ("data=[data" + Str(ch0) + "] ")
Label1.Caption = Format(ch0, "##,##0.00")
Label4.Caption = Format(ch1, "##,##0.00")
' ** YOUR CODE HERE **
' first byte is the report ID, e.g. BufferIn(0)
' the other bytes are the data from the microcontrolller...
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
'*************
Private Sub Timer1_Timer()
Matlab.Execute ("data=" + Str(ch0))
Text1.Text = Matlab.Execute("data")
Matlab.Execute ("vector= [vector data]")
Matlab.Execute ("plot(vector)")
End Sub
And now my questions ...
Posted on: November 23, 2011, 05:05:44 17:05 - Automerged
Questions:
1: If i use the usb for acquisition only ( i mean no lcd displaying , just send data directy to the pc to display it after). How fast can i do that in hid mode??? (prior to get a specification of bandwidth and know how fast would be my variable to be measured (in fact i would like to sample sounds as well as slow variables like temperature increasing of a sensor) )
2: Can i gain speed by increasing the bus size to 64 and packing inside multiple samples at time???
3: I'm thinking about using matlab or scilab for plotting the signals and the use the data analysis capabilities. i'm on the way to do that... data gathering via vb6 then plot to matlab but it takes important time. So i'm thinking about it as a data capturing mode... so i sample especifically to post analysis and in other mode to do simple data displaying ...
4: Is there any way to reuse the mcHID dll inside matlab to avoid consuming time in linking vb and matlab?