USB communication with pic


Results 1 to 6 of 6

Threaded View

  1. #3
    Join Date
    May 2006
    Posts
    14


    Did you find this post helpful? Yes | No

    Default the code...

    Hi,

    all the project files are attached,

    This is the pic code:

    define OSC 20 ' Osc is 20MHz
    DEFINE LOADER_USED 1 ' Bootlader if used
    DEFINE RESET_ORG 800h ' For Microchip USB Bootloader
    DEFINE INTERRUPT_ORG 808h ' For Microchip USB Bootloader

    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...
    usbbuffer[0] = $00
    usbbuffer[1] = $01
    usbbuffer[2] = $0f
    usbbuffer[3] = $ff
    ProgramStart:
    gosub DoUSBIn
    toggle portc.0
    if usbbuffer[2] = 0 then
    toggle portc.1
    endif

    gosub DoUSBOut
    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


    The portc.1 is changed no matter what I send ==> the buffer = 0, but why?

    *********************************************

    The Visual basic 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)



    Private Sub find_Click()
    hidConnect (Me.hwnd)
    DeviceHandle = hidGetHandle(VendorID, ProductID)
    did = hidGetVendorID(DeviceHandle)
    pid = hidGetProductID(DeviceHandle)
    InLng = hidGetInputReportLength(DeviceHandle)
    OutLng = hidGetOutputReportLength(DeviceHandle)

    hidSetReadNotify DeviceHandle, True
    If DeviceHandle = 0 Then
    Text1.Text = "Not FOUND"

    Else
    Text1.Text = "FOUND" & vbCrLf 'display data of the pic
    Text1.Text = Text1.Text & did & vbCrLf
    Text1.Text = Text1.Text & pid & vbCrLf
    Text1.Text = Text1.Text & InLng & vbCrLf
    Text1.Text = Text1.Text & OutLng & vbCrLf
    End If


    End Sub

    ' ************************************************** **************
    ' 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)
    ' DeviceHandle = hidGetHandle(VendorID, ProductID)

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

    ' ** 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 trans_Click()
    'send data
    DeviceHandle = hidGetHandle(VendorID, ProductID)
    If Option1 = 0 Then
    BufferOut(0) = 0 ' first by is always the report ID
    BufferOut(1) = &HFF
    BufferOut(2) = &HFF
    BufferOut(3) = &HFF
    BufferOut(4) = &HFF
    BufferOut(5) = &HFF
    BufferOut(6) = &HFF
    BufferOut(7) = &HFF
    BufferOut(8) = &HFF
    Else
    BufferOut(0) = 0 ' first by is always the report ID
    BufferOut(1) = &H0
    BufferOut(2) = &H0
    BufferOut(3) = &H0
    BufferOut(4) = &H0
    BufferOut(5) = &H0
    BufferOut(6) = &H0
    BufferOut(7) = &H0
    BufferOut(8) = &H0

    End If
    ' write the data (don't forget, pass the whole array)...
    sendsecc = hidWrite(DeviceHandle, BufferOut(0))
    'sendsecc = hidWriteEx(VendorID, ProductID, BufferOut(0))
    If sendsecc = 0 Then
    Text2.Text = "no send" & vbCrLf
    Else
    Text2.Text = "send" & vbCrLf
    Text2.Text = Text2.Text & BufferOut(0) & vbCrLf
    Text2.Text = Text2.Text & BufferOut(1) & vbCrLf
    Text2.Text = Text2.Text & BufferOut(2) & vbCrLf
    Text2.Text = Text2.Text & BufferOut(3) & vbCrLf
    Text2.Text = Text2.Text & BufferOut(4) & vbCrLf
    Text2.Text = Text2.Text & BufferOut(5) & vbCrLf
    Text2.Text = Text2.Text & BufferOut(6) & vbCrLf
    Text2.Text = Text2.Text & BufferOut(7) & vbCrLf
    Text2.Text = Text2.Text & BufferOut(8) & vbCrLf
    End If
    readseccs = 0
    ' read the data (don't forget, pass the whole array)...
    readseccs = hidReadEx(VendorID, ProductID, BufferIn(0))
    If readseccs = 0 Then
    Text3.Text = "no read"
    Else
    Text3.Text = "read" & vbCrLf
    Text3.Text = Text3.Text & "data" & vbCrLf
    Text3.Text = Text3.Text & BufferIn(0) & vbCrLf
    Text3.Text = Text3.Text & BufferIn(1) & vbCrLf
    Text3.Text = Text3.Text & BufferIn(2) & vbCrLf
    Text3.Text = Text3.Text & BufferIn(3) & vbCrLf
    Text3.Text = Text3.Text & BufferIn(4) & vbCrLf
    Text3.Text = Text3.Text & BufferIn(5) & vbCrLf
    Text3.Text = Text3.Text & BufferIn(6) & vbCrLf
    Text3.Text = Text3.Text & BufferIn(7) & vbCrLf
    Text3.Text = Text3.Text & BufferIn(8) & vbCrLf

    End If


    End Sub
    *****************************************

    On the picture you can see the display, left to right:

    1. the display as the program start.
    2. the display after pressing "find f" - we see the P.id and Vid value and the size of the reports
    3. What we send and what we get=0 after pressing "trans"

    Thanks
    Attached Images Attached Images  
    Attached Files Attached Files

Similar Threads

  1. USB CDC Communications for Dummies!
    By Squibcakes in forum USB
    Replies: 104
    Last Post: - 15th January 2014, 14:43
  2. Reading a slave USB with a pic
    By pcaccia in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 25th October 2008, 13:00
  3. Replies: 15
    Last Post: - 30th October 2007, 20:25
  4. USB PIC without USB Connection
    By Tissy in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 26th December 2005, 18:39
  5. Pic to Pic communication?
    By Rob Martin in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 11th April 2004, 20:41

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts