Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 05, 2016, 07:35:51 19:35


Login with username, password and session length


Pages: [1]
Print
Author Topic: Proton Compiler and USB ... and pc interfacing too  (Read 3862 times)
0 Members and 1 Guest are viewing this topic.
jeanninemtv
Senior Member
****
Offline Offline

Posts: 315

Thank You
-Given: 57
-Receive: 25


« on: November 23, 2011, 05:12:52 17:12 »

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 Smiley


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 Smiley

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...

Code:
'****************************************************************
'*  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


Code:
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?





« Last Edit: November 23, 2011, 05:18:23 17:18 by jeanninemtv » Logged
jeanninemtv
Senior Member
****
Offline Offline

Posts: 315

Thank You
-Given: 57
-Receive: 25


« Reply #1 on: November 24, 2011, 10:27:31 10:27 »


btw i tested the oscillo dll from

http://www.sonsivri.to/forum/index.php?topic=12862.0

so this is the result of mixing the sample vb6 code of the oscillo with the sample vb6 code of the usb link

Code:
'oscilloscope dll connection declarations
Private Declare Function ScopeCreate Lib _
 "OscDLL_VB.dll" (ByVal Prm As Integer, ByVal P_IniName As String, ByVal P_IniSuffix As String) As Integer

Private Declare Function ScopeShow Lib _
 "OscDLL_VB.dll" (ByVal handle As Integer) As Integer
 
 Private Declare Function AtOpenLib Lib _
 "OscDLL_VB.dll" (ByVal Prm As Integer) As Integer
 
 Private Declare Function ScopeHide Lib _
 "OscDLL_VB.dll" (ByVal handle As Integer) As Integer
 
Private Declare Function ScopeDestroy Lib _
 "OscDLL_VB.dll" (ByVal handle As Integer) As Integer
 
Private Declare Function ScopeCleanBuffers Lib _
 "OscDLL_VB.dll" (ByVal handle As Integer) As Integer

Private Declare Function ShowNext Lib _
 "OscDLL_VB.dll" (ByVal handle As Integer, ByRef PArrDbl As Double) As Integer
 
Private Declare Function ExternalNext Lib _
 "OscDLL_VB.dll" (ByVal handle As Integer, ByRef PArrDbl As Double) As Integer
 
Dim ScopeStarted As Boolean
Dim scopehandle As Integer
Dim Slowly As Boolean

' 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




Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
 

Private Sub Check1_Click()
    If Check1.Value = 1 Then
        Slowly = True
    Else
        Slowly = False
    End If
End Sub

Private Sub Command1_Click()
   
    If Command1.Caption = "Load Scope" Then
   
        Command1.Caption = "Unload Scope"
        scopehandle = ScopeCreate(2, "Scope_Desk.ini", "")
        ScopeShow (scopehandle)
       
    Else
   
        Command1.Caption = "Load Scope"
        ScopeStarted = False
        Call ScopeHide(scopehandle)
        Call ScopeDestroy(scopehandle)
       
    End If

End Sub

Private Sub Command2_Click()
    If Command2.Caption = "Run" Then
    ConnectToHID (Me.hwnd)
   
        Command2.Caption = "Stop"
        Command1.Enabled = False
       
        Dim D1(2) As Double
        Dim Db As Double
        Dim i As Integer
       
        ScopeStarted = True
       
        D1(0) = 0
        D1(1) = 0
        D1(2) = 0
       
        Do
            DoEvents
           
 '           For i = 0 To 100 Step 1
           
                D1(0) = ch0 'D1(0) - 11: If D1(0) < -280 Then D1(0) = 20
                D1(1) = ch1 'D1(1) - 3.1333: If D1(1) < -280 Then D1(1) = 20
                D1(2) = 4 'D1(2) - 5.1333: If D1(2) < -280 Then D1(2) = 20
           
                Db = D1(0) + D1(1) + D1(2)
           
                Call ExternalNext(scopehandle, Db)
                Call ShowNext(scopehandle, D1(0))
           
'            Next i
           
            If Slowly Then wait_ms (1)
           
        Loop Until (Not ScopeStarted)
       
    Else
   
        Command2.Caption = "Run"
        Command1.Enabled = True
       
        ScopeStarted = False
    End If
End Sub

Private Sub Form_Load()
    Slowly = False
    AtOpenLib (1)
End Sub

Private Function wait_ms(delay As Integer)
   
    Dim StartOfWait As Long
    StartOfWait = GetTickCount

    Do
      DoEvents
    Loop Until (GetTickCount - StartOfWait) > delay

End Function
'*****************************************************************
' 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)
   
      ' ** 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
 '*************

and it's working Smiley ... but it still kinda slow ... 1 mean to catch up 100 - 200 hz signal ...  but still good for monitoring purposes Smiley
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC