kloppertje
Diverse projecten en experimenten: www.elexperiment.nl
Dag allemaal,
Ik ben begonnen met USB (HID) communicatie tussen de computer en een PIC18F4550. Een tijd geleden heb ik USB handel in Proton Basic uitgezet d.m.v. de instructie "Onboard_usb". Nu probeer ik in Swordfish via HID wat te babbelen met de computer, maar dit wil niet lukken. Met hetzelfde programma in een 18F2550 lukt het wel gelijk.
Kan het misschien zo zijn dat er bepaalde instellingen verkeerd staan? Volgens mij worden deze "gereset" als je de PIC opnieuw programmeert. Of zouden de poorten opgeblazen zijn door verkeerd gebruik?
stokni
Clones are people two.
Wellicht niet helemaal wat je zoekt, maar wat mij enorm heeft gehoplen met het ontwikkelen van de 18f4550 en USB is de volgende site: http://www.waitingforfriday.com/index.php/Building_a_PIC18F_USB_device
Als je dit volgt, dan begrijp je de firmware van de PIC, en de interactie met de software die dit aan gaat sturen.
zeker de moeite waard om even te checken.
Edit : Tip: als je switched tussen de 18f2550 en de 18f4550 moet je de firmware in mplap even opnieuw compileren, waarbij je in mplap de het juiste type selecteert, anders krijg je geen verbinding, zo weet ik uit ervaring..
[Bericht gewijzigd door stokni op (24%)]
kloppertje
Diverse projecten en experimenten: www.elexperiment.nl
Ondertussen heb ik al een nieuwe 18F4550 erin gestoken, en dat werkt prima. Als iemand een werkend voorbeeld nodig heeft voor HID communicatie tussen de microcontroller en de pc heb ik nog wel wat. Het is dan geprogrammeerd in Visual Basic 2010 en Swordfish.
Ik heb wel belangstelling voor je voorbeeldproject. Het liefst zou ik zelf gebruik willen maken van Proton en Visual Basic 2010. Maar Swordfish kan ik me vast ook wel eigen maken.
Als ik eerst maar eens een schakelaartje in kan lezen en een uitgangetje hoog/laag maken zou ik al heel tevreden zijn.
Kun je me je projectgegevens mailen?
Op 11 augustus 2011 21:21:02 schreef kloppertje:
Ondertussen heb ik al een nieuwe 18F4550 erin gestoken, en dat werkt prima. Als iemand een werkend voorbeeld nodig heeft voor HID communicatie tussen de microcontroller en de pc heb ik nog wel wat. Het is dat geprogrammeerd in Visual Basic 2010 en Swordfish.
graag, is heel welkom.
Heb vanochtend een paar 18F4550's in mijn bestel lijstje opgenomen
kloppertje
Diverse projecten en experimenten: www.elexperiment.nl
De Swordfish code komt uit de handleiding. Je kan ook de EasyHID plugin gebruiken, dan moet je wel eea aanpassen, anders werkt het niet samen met de VB2010 code. De EadyHid plugin is ook te gebruiken met Proton.
Visual Basic 2010 kun je gratis downloaden. Zet wel even "mchid.dll" in de map Windows/System32.
Swordfish code:
// device and clock...
Device = 18F4550
Clock = 48
// 20Mhz crystal, 48Mhz internal (FS USB)
Config
PLLDIV = 5,
CPUDIV = OSC1_PLL2,
USBDIV = 2,
FOSC = HSPLL_HS,
VREGEN = ON,
PWRT = Off,
BOR = OFF,
LVP = OFF,
WDT = OFF
// import modules...
Include "usbhid.bas"
// TX report...
Structure TTXReport
Time As Word
Message As String
End Structure
Dim TXReport As TTXReport Absolute TXReportRAM
// RX report...
Structure TRXReport
LED0 As Bit
LED1 As Bit
End Structure
Dim RXReport As TRXReport Absolute RXReportRAM
// alias port pins to LEDs...
Dim
LED0 As PORTE.2,
LED1 As PORTB.1
// initialise...
TXReport.Time = 0
High(LED0)
Low(LED1)
DelayMS(200)
// connect to USB...
Repeat
Until Attached
Low(LED0)
// main program loop...
While true
// if we have data, set port values, update message
// and then reset time counter...
If DataAvailable Then
ReadReport
LED0 = RXReport.LED0
LED1 = RXReport.LED1
TXReport.Message = "PORT CHANGED"
WriteReport
TXReport.Time = 0
DelayMS(100)
// no data, set waiting message...
Else
TXReport.Message = "WAITING..."
WriteReport
Inc(TXReport.Time)
EndIf
Wend
Visual Basic 2010 code:
form1.vb:
Option Strict Off
Option Explicit On
Friend Class MainForm
Inherits System.Windows.Forms.Form
' vendor and product IDs
Private Const VendorID As Short = 6017
Private Const ProductID As Short = 2000
' read and write buffers
Private Const BufferInSize As Short = 8
Private Const BufferOutSize As Short = 8
Dim BufferIn(BufferInSize) As Byte
Dim BufferOut(BufferOutSize) As Byte
' ****************************************************************
' when the form loads, connect to the HID controller - pass
' the form window handle so that you can receive notification
' events...
'*****************************************************************
Private Sub MainForm_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
' do not remove!
ConnectToHID(Me.Handle.ToInt32)
End Sub
'*****************************************************************
' disconnect from the HID controller...
'*****************************************************************
Private Sub MainForm_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs 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 **
TextBox2.Text = "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)
TextBox2.Text = "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 **
' first byte is the report ID, e.g. BufferIn(0)
' the other bytes are the data from the microcontroller...
'TextBox2.Text = BufferIn(1)
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
BufferOut(0) = 0 ' first by is always the report ID
If CheckBox1.Checked Then
BufferOut(1) = 1 ' first data item, etc etc
Else
BufferOut(1) = 0 ' first data item, etc etc
End If
' write the data (don't forget, pass the whole array)...
hidWriteEx(VendorID, ProductID, BufferOut(0))
End Sub
End Class
Module1.vb:
Option Strict Off
Option Explicit On
Imports System
Imports System.Threading
Imports System.Runtime.InteropServices
Module HIDDLLInterface
' this is the interface to the HID controller DLL - you should not
' normally need to change anything in this file.
'
' WinProc() calls your main form 'event' procedures - these are currently
' set to..
'
' MainForm.OnPlugged(ByVal pHandle as long)
' MainForm.OnUnplugged(ByVal pHandle as long)
' MainForm.OnChanged()
' MainForm.OnRead(ByVal pHandle as long)
' HID interface API declarations...
Declare Function hidConnect Lib "mcHID.dll" Alias "Connect" (ByVal pHostWin As Integer) As Boolean
Declare Function hidDisconnect Lib "mcHID.dll" Alias "Disconnect" () As Boolean
Declare Function hidGetItem Lib "mcHID.dll" Alias "GetItem" (ByVal pIndex As Integer) As Integer
Declare Function hidGetItemCount Lib "mcHID.dll" Alias "GetItemCount" () As Integer
Declare Function hidRead Lib "mcHID.dll" Alias "Read" (ByVal pHandle As Integer, ByRef pData As Byte) As Boolean
Declare Function hidWrite Lib "mcHID.dll" Alias "Write" (ByVal pHandle As Integer, ByRef pData As Byte) As Boolean
Declare Function hidReadEx Lib "mcHID.dll" Alias "ReadEx" (ByVal pVendorID As Integer, ByVal pProductID As Integer, ByRef pData As Byte) As Boolean
Declare Function hidWriteEx Lib "mcHID.dll" Alias "WriteEx" (ByVal pVendorID As Integer, ByVal pProductID As Integer, ByRef pData As Byte) As Boolean
Declare Function hidGetHandle Lib "mcHID.dll" Alias "GetHandle" (ByVal pVendoID As Integer, ByVal pProductID As Integer) As Integer
Declare Function hidGetVendorID Lib "mcHID.dll" Alias "GetVendorID" (ByVal pHandle As Integer) As Integer
Declare Function hidGetProductID Lib "mcHID.dll" Alias "GetProductID" (ByVal pHandle As Integer) As Integer
Declare Function hidGetVersion Lib "mcHID.dll" Alias "GetVersion" (ByVal pHandle As Integer) As Integer
Declare Function hidGetVendorName Lib "mcHID.dll" Alias "GetVendorName" (ByVal pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Integer
Declare Function hidGetProductName Lib "mcHID.dll" Alias "GetProductName" (ByVal pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Integer
Declare Function hidGetSerialNumber Lib "mcHID.dll" Alias "GetSerialNumber" (ByVal pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Integer
Declare Function hidGetInputReportLength Lib "mcHID.dll" Alias "GetInputReportLength" (ByVal pHandle As Integer) As Integer
Declare Function hidGetOutputReportLength Lib "mcHID.dll" Alias "GetOutputReportLength" (ByVal pHandle As Integer) As Integer
Declare Sub hidSetReadNotify Lib "mcHID.dll" Alias "SetReadNotify" (ByVal pHandle As Integer, ByVal pValue As Boolean)
Declare Function hidIsReadNotifyEnabled Lib "mcHID.dll" Alias "IsReadNotifyEnabled" (ByVal pHandle As Integer) As Boolean
Declare Function hidIsAvailable Lib "mcHID.dll" Alias "IsAvailable" (ByVal pVendorID As Integer, ByVal pProductID As Integer) As Boolean
' windows API declarations - used to set up messaging...
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Integer, ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
Delegate Function SubClassProcDelegate(ByVal hwnd As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Declare Function DelegateSetWindowLong Lib "USER32.DLL" Alias "SetWindowLongA" _
(ByVal hwnd As Integer, ByVal attr As Integer, ByVal lval As SubClassProcDelegate) As Integer
' windows API Constants
Public Const WM_APP As Integer = 32768
Public Const GWL_WNDPROC As Short = -4
' HID message constants
Private Const WM_HID_EVENT As Decimal = WM_APP + 200
Private Const NOTIFY_PLUGGED As Short = 1
Private Const NOTIFY_UNPLUGGED As Short = 2
Private Const NOTIFY_CHANGED As Short = 3
Private Const NOTIFY_READ As Short = 4
' local variables
Private FPrevWinProc As Integer ' Handle to previous window procedure
Private FWinHandle As Integer ' Handle to message window
Private Ref_WinProc As New SubClassProcDelegate(AddressOf WinProc)
' Set up a windows hook to receive notification
' messages from the HID controller DLL - then connect
' to the controller
Public Function ConnectToHID(ByVal pHostWin As Integer) As Boolean
FWinHandle = pHostWin
pHostWin = hidConnect(FWinHandle)
FPrevWinProc = DelegateSetWindowLong(FWinHandle, GWL_WNDPROC, Ref_WinProc)
End Function
' Unhook from the HID controller and disconnect...
Public Function DisconnectFromHID() As Boolean
DisconnectFromHID = hidDisconnect
SetWindowLong(FWinHandle, GWL_WNDPROC, FPrevWinProc)
End Function
' This is the procedure that intercepts the HID controller messages...
Private Function WinProc(ByVal pHWnd As Integer, ByVal pMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
If pMsg = WM_HID_EVENT Then
Select Case wParam
' HID device has been plugged message...
Case Is = NOTIFY_PLUGGED
MainForm.OnPlugged(lParam)
' HID device has been unplugged
Case Is = NOTIFY_UNPLUGGED
MainForm.OnUnplugged(lParam)
' controller has changed...
Case Is = NOTIFY_CHANGED
MainForm.OnChanged()
' read event...
Case Is = NOTIFY_READ
MainForm.OnRead(lParam)
End Select
End If
' next...
WinProc = CallWindowProc(FPrevWinProc, pHWnd, pMsg, wParam, lParam)
End Function
End Module