PIC USB to VB.NET


Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 40 of 54
  1. #1
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default PIC USB to VB.NET

    Hi everyone;
    Some time ago, i search this forum for a program in VB.net to comunicate with the USB program that mister_E and Darrel posted in this Thread ( http://www.picbasic.co.uk/forum/show...0434#post80434 ), however i could not found any source code in VB.Net, only in VB6 made by mister_e, so i decided to do it myself.

    I used the PBP code from Darrel Tayler, and converted the mister_e VB6 program to VB.Net.

    Please note that all the credits should go to Darrel Tayler and mister_e, i just convert the program to VB.Net, nothing more!

    Here is the PIC16F4550 code;
    Code:
    '***************************************************************************
    '*  Name    : USB.pbp                                                      *
    '*  Author  : Darrel Taylor, changed by Gadelhas                           *
    '*  Date    : 23-01-2011                                                   *
    '*  Version : 1.0                                                          *
    '*  Notes   : This is a re-creation of mister-e's USBdemo for DT_HID       *
    '*          : Meant to work with mister-e's GUI                            *
    '***************************************************************************
    ;--- if you un-comment these, you must comment the ones in the .inc file ---
    ASM  ; 18F2550/4550, 8mhz crystal
       __CONFIG    _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
       __CONFIG    _CONFIG1H, _FOSC_HSPLL_HS_1H
       __CONFIG    _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _VREGEN_ON_2L
       __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
       __CONFIG    _CONFIG3H, _PBADEN_OFF_3H
       __CONFIG    _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
    ENDASM
    DEFINE OSC 48
    CLEAR
    ;--- Setup Interrupts ------------------------------------------------------
    INCLUDE "DT_INTS-18.bas"     ; Base Interrupt System
    ASM
    INT_LIST  macro    ; IntSource,          Label,  Type, ResetFlag?
            INT_Handler   USB_Handler
        endm
        INT_CREATE               ; Creates the interrupt processor
    endasm
    ;--- Setup USB -------------------------------------------------------------
    INCLUDE "DT_HID260.pbp"
    DEFINE USB_VENDORID    6017
    DEFINE USB_PRODUCTID   2000
    DEFINE USB_VERSION     1
    DEFINE USB_VENDORNAME  "Hugo Oliveira"
    DEFINE USB_PRODUCTNAME "Demo USB"
    DEFINE USB_SERIAL      "001Hugo"
    DEFINE USB_INSIZE      8    ;  IN report is PIC to PC (8,16,32,64)
    DEFINE USB_OUTSIZE     8    ; OUT report is PC to PIC
    DEFINE USB_POLLIN      10   ; Polling times in mS, MIN=1 MAX=10
    DEFINE USB_POLLOUT     10
    ; --- Each USB status LED is optional, comment them if not used ------------
    ; --- They can be assigned to any pin, and no further action is required --- 
    DEFINE USB_LEDPOLARITY 1       ; LED ON State [0 or 1]  (default = 1)
    ;DEFINE USB_PLUGGEDLED  PORTB,7 ; LED indicates if USB is connected
    ;DEFINE USB_TXLED       PORTB,6 ;  "      "     data being sent to PC
    ;DEFINE USB_RXLED       PORTB,5 ;  "      "     data being received from PC
     
    ;--- Variables -------------------------------------------------------------
    Value0      VAR  WORD
    Value1      VAR  WORD
    X           VAR  WORD
    DUTY1       VAR  WORD
    DUTY2       VAR  WORD
    Old_PORTA   VAR  BYTE
    New_PORTA   VAR  BYTE
    ;--- Setup ADC -------------------------------------------------------------
    DEFINE ADC_BITS 8  ; Number of bits in ADCIN result
    ;--- Initialize ------------------------------------------------------------
    CCPR1L = 0
    CCPR2L = 0
    CCP1CON =   %00001100       ' CCP1, PWM mode
    CCP2CON =   %00001100       ' CCP2, PWM mode
    PR2     =   249             ' 0-1000 duty range
    T2CON   =   %00000101       ' TMR2 on, prescaler 1:4
    TRISB = 0
    OUTPUT PORTC.1
    OUTPUT PORTC.2
     
    ADCON2.7 = 0       ; left justify    (Change this if ADFM in diff register)
    ADCON1 = %1101     ; AN0/AN1 Analog
    ;--- The Main Loop ---------------------------------------------------------
    Main:
        FOR X = 0 to 1000           ; Check for incomming USB data while waiting
    @       ON_USBRX_GOSUB  _HandleRX
            PAUSE 1
     
            New_PORTA = PORTA                ; Check PORTA pins
            IF New_PORTA != Old_PORTA THEN
                Old_PORTA = New_PORTA
                ARRAYWRITE USBTXBuffer, ["HugoDem1"]
                GOSUB WaitToSend
                ARRAYWRITE USBTXBuffer, [Value0, Value1, PORTA.2, _
                                         PORTA.3, PORTA.4, PORTA.5,0,0]
                GOSUB WaitToSend
            ENDIF
        NEXT X
     
        ADCIN 0, Value0            ; Send A/D about once/sec
        ADCIN 1, Value1
        ARRAYWRITE USBTXBuffer, [Value0, Value1, PORTA.2, _
                                 PORTA.3, PORTA.4, PORTA.5,0,0]
        GOSUB SendIfReady
    GOTO Main
    ;--- Send Data if Plugged and ready, otherwise discard ---------------------
    SendIfReady:
        IF Plugged AND TX_READY THEN DoUSBOut
    RETURN
    ;--- Wait till Ready to send of until unplugged ----------------------------
    WaitToSend:
        WHILE Plugged and !TX_READY : WEND
        IF TX_READY THEN GOSUB DoUSBOut
    RETURN
    ;---- Receive incoming data PORTB LEDS and CCP PWM dutycycle ---------------
    HandleRX:
        ARRAYREAD  USBRXBuffer,[PORTB, DUTY1.LowByte, DUTY1.HighByte, _
                                        DUTY2.LowByte, DUTY2.HighByte]
        CCP1CON.5=DUTY1.1       ' load CCP1 duty value            
        CCP1CON.4=DUTY1.0       '      with Duty1 
        CCPR1L=DUTY1>>2         '
     
        CCP2CON.5=DUTY2.1       ' load CCP2 duty value            
        CCP2CON.4=DUTY2.0       '      with Duty2
        CCPR2L=DUTY2>>2         '
    return
    Attached is the code for PIC18F4550
    I the next post is the VB.net Program
    Hope it can help someone!
    Attached Files Attached Files
    Thanks and Regards;
    Gadelhas

  2. #2
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default VB.NET Program

    Here is the VB Program;

    The image of the program;

    Name:  VB.JPG
Views: 18922
Size:  39.9 KB
    And here is the code;

    Code:
    PublicClass Form1
    ' vendor and product IDs
    PrivateConst VendorID AsShort = 6017 'Replace with your device's
    PrivateConst ProductID AsShort = 2000 'product and vendor IDs
     
    ' read and write buffers
    PrivateConst BufferInSize AsShort = 8 'Size of the data buffer coming IN to the PC
    PrivateConst BufferOutSize AsShort = 8 'Size of the data buffer going OUT from the PC
    Dim BufferIn(BufferInSize) AsByte'Received data will be stored here - the first byte in the array is unused
    Dim BufferOut(BufferOutSize) AsByte'Transmitted data is stored here - the first item in the array must be 0
    Dim PORTB(8) AsInteger
    ' ****************************************************************
    ' when the form loads, connect to the HID controller - pass
    ' the form window handle so that you can receive notification
    ' events...
    '*****************************************************************
    PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
    ' do not remove!
    ConnectToHID(Me)
    Pictureoff.Visible = True
    Pictureon.Visible = False
    TrackBar1.Enabled = False
    TrackBar2.Enabled = False
    Label8.Text = "AN0"
    Label8.Text = "AN1"
    Label10.Text = "CCP1"
    Label11.Text = "CCP2"
    PictureBox1.Visible = False
    PictureBox3.Visible = False
    PictureBox5.Visible = False
    PictureBox7.Visible = False
    CheckBox1.Enabled = False
    CheckBox2.Enabled = False
    CheckBox3.Enabled = False
    CheckBox4.Enabled = False
    CheckBox5.Enabled = False
    CheckBox6.Enabled = False
    CheckBox7.Enabled = False
    CheckBox8.Enabled = False
    StatusLB2.Text = "Dispositivo USB Desconectado"
    EndSub
    '*****************************************************************
    ' disconnect from the HID controller...
    '*****************************************************************
    PrivateSub Form1_FormClosed(ByVal sender AsObject, ByVal e As System.Windows.Forms.FormClosedEventArgs) HandlesMe.FormClosed
    Splash.Close()
    DisconnectFromHID()
    EndSub
    '*****************************************************************
    ' a HID device has been plugged in...
    '*****************************************************************
    PublicSub OnPlugged(ByVal pHandle AsInteger)
    Dim VendorName AsString = "00000000000000000000"
    Dim serial AsString = "00000000000000000000"
    If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
    ' ** YOUR CODE HERE **)
    hidGetVendorName(pHandle, VendorName, 20)
    hidGetSerialNumber(pHandle, serial, 20)
    Label1.Text = "Vendor Name: " & VendorName
    Label2.Text = "Product Name: " & ProductName
    Label3.Text = "Vendor ID: " & VendorID
    Label4.Text = "Product ID: " & ProductID
    Label5.Text = "Serial: " & serial
    Pictureoff.Visible = False
    Pictureon.Visible = True
    TrackBar1.Enabled = True
    TrackBar2.Enabled = True
    Label8.Text = Val(BufferIn(1))
    Label8.Text = Val(BufferIn(2))
    Label10.Text = TrackBar1.Value
    Label11.Text = TrackBar2.Value
    CheckBox1.Enabled = True
    CheckBox2.Enabled = True
    CheckBox3.Enabled = True
    CheckBox4.Enabled = True
    CheckBox5.Enabled = True
    CheckBox6.Enabled = True
    CheckBox7.Enabled = True
    CheckBox8.Enabled = True
    StatusLB2.Text = "Dispositivo USB Conectado"
    EndIf
    EndSub
    '*****************************************************************
    ' a HID device has been unplugged...
    '*****************************************************************
    PublicSub OnUnplugged(ByVal pHandle AsInteger)
    If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
    hidSetReadNotify(hidGetHandle(VendorID, ProductID), False)
    ' ** YOUR CODE HERE **
    Pictureoff.Visible = True
    Pictureon.Visible = False
    TrackBar1.Enabled = False
    TrackBar2.Enabled = False
    Label8.Text = "AN0"
    Label8.Text = "AN1"
    Label10.Text = "CCP1"
    Label11.Text = "CCP2"
    PictureBox1.Visible = False
    PictureBox3.Visible = False
    PictureBox5.Visible = False
    PictureBox7.Visible = False
    CheckBox1.Enabled = False
    CheckBox2.Enabled = False
    CheckBox3.Enabled = False
    CheckBox4.Enabled = False
    CheckBox5.Enabled = False
    CheckBox6.Enabled = False
    CheckBox7.Enabled = False
    CheckBox8.Enabled = False
    StatusLB2.Text = "Dispositivo USB Desconectado"
    EndIf
    EndSub
    '*****************************************************************
    ' controller changed notification - called
    ' after ALL HID devices are plugged or unplugged
    '*****************************************************************
    PublicSub 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 AsInteger
    pHandle = hidGetHandle(VendorID, ProductID)
    hidSetReadNotify(hidGetHandle(VendorID, ProductID), True)
    EndSub
    '*****************************************************************
    ' on read event...
    '*****************************************************************
    PublicSub OnRead(ByVal pHandle AsInteger)
    ' read the data (don't forget, pass the whole array)...
    Dim index AsByte
    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...
    If BufferIn(7) = 0 And BufferIn(8) = 0 Then
    '
    ' incomming ADC reading and PORTA status
    ' --------------------------------------
    ProgressBar1.Value = BufferIn(1) 'Display ADC results to
    ProgressBar2.Value = BufferIn(2) 'the progress bars
    '
    Label8.Text = Val(BufferIn(1)) 'Display Progress bars
    Label9.Text = Val(BufferIn(2)) 'Values
    '
    PictureBox1.Visible = BufferIn(3) 'Display PORTA
    PictureBox3.Visible = BufferIn(4) 'status to
    PictureBox5.Visible = BufferIn(5) 'the according
    PictureBox7.Visible = BufferIn(6) 'LEDs
    Else
    '
    ' Incomming data is a text string
    ' -------------------------------
    For index = 1 To BufferInSize ' pass the whole array but skip (0)
    If BufferIn(index) <> 0 Then' valid data?
    txtreception.AppendText(Chr(BufferIn(index)))
    EndIf
    Next
    txtreception.Text += vbNewLine
    txtreception.Select(txtreception.Text.Length - 1, 0)
    txtreception.ScrollToCaret()
    EndIf
     
    EndIf
    EndSub
    PublicSub WriteSomeData()
    ' Use to send data to the USB bus.
    ' data must be store in BufferOut array
    '
    ' Actual structure:
    ' -----------------
    ' BufferOut(0) = Report id => always 0
    ' BufferOut(1) = PORTB ' first data item, etc etc
    ' BufferOut(2) = HPWM slider1 MSB
    ' BufferOut(3) = HPWM slider1 LSB
    ' BufferOut(4) = HPWM slider2 MSB
    ' BufferOut(5) = HPWM slider2 LSB
    ' BufferOut(6) = n/a = 0
    ' BufferOut(7) = n/a = 0
    ' BufferOut(8) = n/a = 0
    BufferOut(0) = 0 ' first by is always the report ID
    BufferOut(6) = 0
    BufferOut(7) = 0
    BufferOut(8) = 0
    hidWriteEx(VendorID, ProductID, BufferOut(0)) ' send it
    EndSub
    
    PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    txtreception.Clear()
    EndSub
    PrivateSub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
    BufferOut(2) = (TrackBar1.Value) And 255 ' keep only 8 LSB
    BufferOut(3) = Int(TrackBar1.Value / 256) ' keep only 8 MSB
    Label10.Text = TrackBar1.Value ' display it
    WriteSomeData() ' send it
    EndSub
    PrivateSub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar2.Scroll
    BufferOut(4) = (TrackBar2.Value) And 255 ' keep only 8 LSB
    BufferOut(5) = Int(TrackBar2.Value / 256) ' keep only 8 MSB
    Label11.Text = TrackBar2.Value ' display it
    WriteSomeData() ' send it
    EndSub
    PublicSub ActualizaPORTAB()
    Dim i AsInteger
    BufferOut(1) = 0
    If CheckBox1.Checked = TrueThen
    PORTB(0) = 1
    Else
    PORTB(0) = 0
    EndIf
    If CheckBox2.Checked = TrueThen
    PORTB(1) = 1
    Else
    PORTB(1) = 0
    EndIf
    If CheckBox3.Checked = TrueThen
    PORTB(2) = 1
    Else
    PORTB(2) = 0
    EndIf
    If CheckBox4.Checked = TrueThen
    PORTB(3) = 1
    Else
    PORTB(3) = 0
    EndIf
    If CheckBox5.Checked = TrueThen
    PORTB(4) = 1
    Else
    PORTB(4) = 0
    EndIf
    If CheckBox6.Checked = TrueThen
    PORTB(5) = 1
    Else
    PORTB(5) = 0
    EndIf
    If CheckBox7.Checked = TrueThen
    PORTB(6) = 1
    Else
    PORTB(6) = 0
    EndIf
    If CheckBox8.Checked = TrueThen
    PORTB(7) = 1
    Else
    PORTB(7) = 0
    EndIf
    For i = 0 To 7
    BufferOut(1) = BufferOut(1) + (PORTB(i) * (2 ^ i))
    Next
    WriteSomeData()
    EndSub
    PrivateSub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox7.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub CheckBox8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox8.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    StatusLB3.Text = DateAndTime.Now.ToString
    EndSub
    EndClass
    
    Attached is the hole Project in VB.Net 2008
    Attached Files Attached Files
    Thanks and Regards;
    Gadelhas

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default Re: PIC USB to VB.NET

    Nice work, will be useful for many, that's for sure!

    I've already posted a snip for vb.net here awhile back. Amr Nekhitdid a VB.NET template based on my work
    http://helmpcb.com/software/usb-hid-...ual-basic-2005
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Re: PIC USB to VB.NET

    Quote Originally Posted by mister_e View Post
    Nice work, will be useful for many, that's for sure!

    I've already posted a snip for vb.net here awhile back. Amr Nekhitdid a VB.NET template based on my work
    http://helmpcb.com/software/usb-hid-...ual-basic-2005
    Thank you mister_e

    I see that post, when i was searching, however since it was made in VB2005, i decided to this this one in VB2008, not to much but some things change.
    Thanks and Regards;
    Gadelhas

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default Re: PIC USB to VB.NET

    Time goes by way too fast
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Re: PIC USB to VB.NET

    Quote Originally Posted by mister_e View Post
    Time goes by way too fast
    You are right... Now i have to do it in VB2010.... or perhaps in one future new version, like VB2012........ hummm i don't think so!!!
    Thanks and Regards;
    Gadelhas

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default Re: PIC USB to VB.NET

    People need to do something by themself sometimes huh?
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869

    Default Re: PIC USB to VB.NET

    Thank you both. I guess you are off the hook now steve . I will have to digest this (mostly the vb side) but I do THANK YOU both.!!!
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  9. #9
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default Re: PIC USB to VB.NET

    In the beginning earth was empty

    Then someone says

    Name:  nike-just-do-it.jpg
Views: 16800
Size:  7.1 KB

    May the force be with you

    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  10. #10
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869

    Default Re: PIC USB to VB.NET

    So does HID fall in the catagory of "pure USB"?
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  11. #11
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default Re: PIC USB to VB.NET

    HID is a generic USB class, does not require specific driver, could be considered as pure USB... at least the most simple way to develop painless USB stuff.

    I still dream Melabs would consider porting Microchip USB framework so PBP user would access other USB Class such as Audio, MIDI and such. We already have mouse & Keyboard (joystick maybe?!?)... why they've stopped there?
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  12. #12
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default Re: PIC USB to VB.NET

    Who stopped where?
    The Microchip USB "framework" is already in PBP.

    You can make your own descriptor files and code for all kinds of stuff.
    The mouse, HID and CDC examples are there to show you how it's done.

    Would you expect DMX, MODBUS or NMEA 2000 to be included just because there's an HSEROUT command?
    DT

  13. #13
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default Re: PIC USB to VB.NET

    I know that Darrel, I can even make my own compiler if I would like to. That's not the whole point. Seems pretty obvious you don't have a clue of what's all in the Microchip Framework then.

    LCDOUT & GLCD a load ask because they can't, It's somehow easy when you take time to eat/digest the datasheet...

    But still there's a MAJOR difference between simple serial/parrallel stuff on various baudrate VS USB Class & Descriptors... kinda black magic for A LOAD of people.

    Arduino, Atmel have other class already made in their free compiler, I would expect Melabs be open minded to do it for their paid compiler.

    It's a simple suggestion, for the real few time I ask something, don't be so defensive buddy... I love you
    Last edited by mister_e; - 1st June 2011 at 01:13.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  14. #14
    Join Date
    Dec 2005
    Posts
    1,073

    Default Re: PIC USB to VB.NET

    Quote Originally Posted by mister_e View Post
    Arduino, Atmel have other class already made in their free compiler, I would expect Melabs be open minded to do it for their paid compiler.
    Well, life's too short (especially, in my case) to argue with either you or Darrell who both seem to know this subject far better than I do but, I was reading the installation instructions for the Arduino and learned that neither Linux nor OSX need any drivers to connect with its USB link (Windows only needs an .inf file). It shows up in Windows as a COM port so I assume it appears as a serial port in the others as well. However, the Arduino is using a rather powerful Atmel ATMega8U2 32-pin MCU just for USB->serial so it's understandable (at least to a dim-witted geezer like me) that Microchip might not be able to accomplish quite the same thing with a single MCU.
    Last edited by dhouston; - 1st June 2011 at 02:05.

  15. #15
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default Re: PIC USB to VB.NET

    FALSE, you can create any USB device with ANY usb capable of MCU. You just need to use Generic classes, and this is just all about the USB descriptor.

    Should you need higher speed & specific feature, you develop your own USB driver... and for that, you need to do it for ANY OS platform...
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  16. #16
    Join Date
    Dec 2005
    Posts
    1,073

    Default Re: PIC USB to VB.NET

    I wasn't really talking about the OS end but about the embedded end where Arduino dedicates an MCU to USB->serial while Microchip does not..

  17. #17
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default Re: PIC USB to VB.NET

    Well mister-e,

    Looking at the Arduino language reference ...
    http://arduino.cc/en/Reference/HomeP...rence.Extended
    I don't even see any native USB commands.

    I don't look for Arduino stuff, so maybe you have a better link to native commands.
    I have seen a USB library for it. But much like you or I would write routines for PBP, it was user created.

    And the only MIDI usage I can find, is thru an FTDI chip ...
    A far cry from a compiler command.

    As for the Microchip USB "Framework" ...
    http://www.microchip.com/stellent/id...cName=en537044

    I am quite familiar with it, and I'm sure you know that it only has a MIDI Demo application.
    You've already emailed me regarding that program and indicated it wasn't very good and you wanted melabs to make a better one.

    MIDI is just that, an application.
    It's not a compiler command.

    No matter what melabs could generate for a MIDI application, somebody would want more.
    It's a loosing proposition that is better left to the programmers writing the application.

    With that said...
    It's well within the capabilities of PicBasic Pro to write a MIDI application.
    It's just up to you to do it.
    DT

  18. #18
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default Re: PIC USB to VB.NET

    I don't talk only about MIDI, oh well forget that then, I guess my suggestion are more to give Melabs idea more than to help myself, bah forget it... Since that e-mail, I've fixed their mistakes, rolled my own and everything's fine... no worries.

    NEXT!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  19. #19
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default Re: PIC USB to VB.NET

    Quote Originally Posted by mister_e View Post
    ... Since that e-mail, I've fixed their mistakes, rolled my own and everything's fine... no worries.

    NEXT!
    Great!

    Wanna share?
    Someone I know was looking for a free MIDI program.
    DT

  20. #20
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default Re: PIC USB to VB.NET

    MPLAB is free, C18 has free version, Microchip framework is free and there's a MIDI-DEMO in... or he just need to roll his own based on MIDI Demo like I did, after all
    It's well within the capabilities of PicBasic Pro to write a MIDI application.
    It's just up to you to do it.


    ... or be patient 'till the public release of Or-BIT IDE, but from what I know, it's ain't going to be free and still not 100% of all freebies inside.
    Last edited by mister_e; - 1st June 2011 at 03:58.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  21. #21
    Join Date
    Mar 2011
    Posts
    4

    Wink Re: PIC USB to VB.NET

    Hi,
    I would like to put this firmware in my pic18f4550, but it doesn't compile. If you have some hex file for the same code pls post it. I want to give it a run on my pic.

    And for another matter at all, I want to use the previous pic with a wireless router (D-Link DI-524UP) and I want to know if there is any change necessary. Also, I think that the USB Print Server will complicate my life a bit. If anyone tried this before and has any experience with it, please share. The entire project is about a small boat controlled from laptop via the wireless router, and includes two DC motors (2 PWM signals) and one stepper. Any opinion will do. Tx a lot.


  22. #22
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Re: PIC USB to VB.NET

    Quote Originally Posted by mariacho View Post
    Hi,
    I would like to put this firmware in my pic18f4550, but it doesn't compile. If you have some hex file for the same code pls post it. I want to give it a run on my pic.

    And for another matter at all, I want to use the previous pic with a wireless router (D-Link DI-524UP) and I want to know if there is any change necessary. Also, I think that the USB Print Server will complicate my life a bit. If anyone tried this before and has any experience with it, please share. The entire project is about a small boat controlled from laptop via the wireless router, and includes two DC motors (2 PWM signals) and one stepper. Any opinion will do. Tx a lot.

    Crystal Ball, please tell me why the code doesn't compile with this user?
    Thanks and Regards;
    Gadelhas

  23. #23
    Join Date
    Mar 2011
    Posts
    4

    Default Re: PIC USB to VB.NET

    i got:
    error[180] usb18mem.asm116: RES directive cannot reserve odd number of bytes in PIC18 absolute mode
    warning [230] usb18mem.asm116: no memory has been reserved by this instruction

  24. #24
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Re: PIC USB to VB.NET

    Quote Originally Posted by mariacho View Post
    i got:
    error[180] usb18mem.asm116: RES directive cannot reserve odd number of bytes in PIC18 absolute mode
    warning [230] usb18mem.asm116: no memory has been reserved by this instruction
    Post your code.

    What is your PBP version? Also list the files that you have in your project directory.

    I works fine here without any error and warning.
    Thanks and Regards;
    Gadelhas

  25. #25
    Join Date
    Mar 2011
    Posts
    4

    Post Re: PIC USB to VB.NET

    PBP 2.60, micro code studio plus 3.0.0.5
    folder content: hd_hid260.pbp, PiCUSB.pbp and all files from c:PBP/usb18

    -- code removed --

    thank you for your help
    Last edited by Darrel Taylor; - 15th July 2011 at 03:25.

  26. #26
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Re: PIC USB to VB.NET

    The code that you posted is not the same as i post in the first post! The code that you post is the code to be included in the code that i post.
    Please copy past the code form the first post and try it!
    Thanks and Regards;
    Gadelhas

  27. #27
    Join Date
    Mar 2011
    Posts
    4

    Default Re: PIC USB to VB.NET

    i do copy-paste the first post code and includein the same folder the files from firmware.zip and all the files from usb18 folder
    compile and result: c:\pic\new\dt_hid260.pbp error line 25> bad BANK number

    sorry i`m not so good at this (trying to learn)

  28. #28
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Re: PIC USB to VB.NET

    And wich code are you compiling? In MicroCode Studio you do not compile the DT_HID260.pbp, you need to compile, the one with the code from the first post. Also, are you compiling the code for the correct Chip 18F4550? Is it possible for you to put a screenshot of the Microcode Studio after you compile with the erros?
    Thanks and Regards;
    Gadelhas

  29. #29
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305

    Default Re: PIC USB to VB.NET

    Hi,

    I used the code in this thread on the VB side, and used parts of and incorporated it into my own code on the PIC side. I can communicate and send data perfectly to VB but I cannot get data out of VB into the PIC.

    I need to change a slider and have that value (0-255) sent to the PIC so it can save it to EEPROM and later use it. My code on the PIC side works perfectly if I increase or decrease that value with push buttons but I cannot get the byte into the PIC.

    Lets say I want to send that value out to the PIC and store it in byte IOUT. What is the program structure in VB?

    I am using VB 2010, pbp 2.60 and a 18F2550.

    What info do I need to post next to make this easier to figure out?

  30. #30
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Re: PIC USB to VB.NET

    Quote Originally Posted by jmgelba View Post
    Hi,

    I used the code in this thread on the VB side, and used parts of and incorporated it into my own code on the PIC side. I can communicate and send data perfectly to VB but I cannot get data out of VB into the PIC.

    I need to change a slider and have that value (0-255) sent to the PIC so it can save it to EEPROM and later use it. My code on the PIC side works perfectly if I increase or decrease that value with push buttons but I cannot get the byte into the PIC.

    Lets say I want to send that value out to the PIC and store it in byte IOUT. What is the program structure in VB?

    I am using VB 2010, pbp 2.60 and a 18F2550.

    What info do I need to post next to make this easier to figure out?

    Hi;

    have you tried with the code for the PIC post here. Since the code published here works well, i think there is something wrong with your code, so you should try with the pic code of this thread, or you should post your code to see what is wrong.
    Thanks and Regards;
    Gadelhas

  31. #31
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305

    Default Re: PIC USB to VB.NET

    Hmm. Ok, seem to have some data coming out but its when I press buttons and not when I move the slider bar Its all wrong lol!
    I'll post code later.

  32. #32
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305

    Default Re: PIC USB to VB.NET

    Ok so I stripped out some of the stuff I dont need and with the addition of a few things this is the VB side:

    Code:
    Public Class Form1
    
        ' vendor and product IDs
        Private Const VendorID As Short = 6017   'Replace with your device's
        Private Const ProductID As Short = 2000  'product and vendor IDs
    
    
        ' read and write buffers
        Private Const BufferInSize As Short = 16  'Size of the data buffer coming IN to the PC
        Private Const BufferOutSize As Short = 16 'Size of the data buffer going OUT from the PC
        Dim BufferIn(BufferInSize) As Byte       'Received data will be stored here - the first byte in the array is unused
        Dim BufferOut(BufferOutSize) As Byte     'Transmitted data is stored here - the first item in the array must be 0
    
        Dim PORTC(8) As Integer
    
        Dim IOUT1 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 Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' do not remove!
            ConnectToHID(Me)
            Pictureoff.Visible = True
            Pictureon.Visible = False
            Label8.Text = "PCB Temp COUNT"
            Label8.Text = "AN1"
            CheckBox1.Enabled = False
            CheckBox2.Enabled = False
            StatusLB2.Text = "USB DEVICE DISCONNECTED"
        End Sub
    
        '*****************************************************************
        ' disconnect from the HID controller...
        '*****************************************************************
        Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            Splash.Close()
            DisconnectFromHID()
        End Sub
    
        '*****************************************************************
        ' a HID device has been plugged in...
        '*****************************************************************
        Public Sub OnPlugged(ByVal pHandle As Integer)
            Dim VendorName As String = "00000000000000000000"
            Dim serial As String = "00000000000000000000"
    
            If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
                ' ** YOUR CODE HERE **)
                hidGetVendorName(pHandle, VendorName, 20)
                hidGetSerialNumber(pHandle, serial, 20)
    
                Label1.Text = "Vendor Name: " & VendorName
                Label2.Text = "Product Name: " & ProductName
                Label3.Text = "Vendor ID: " & VendorID
                Label4.Text = "Product ID: " & ProductID
                Label5.Text = "Serial: " & serial
                Pictureoff.Visible = False
                Pictureon.Visible = True
                Label8.Text = Val(BufferIn(1))
                Label8.Text = Val(BufferIn(2))
                CheckBox1.Enabled = True
                CheckBox2.Enabled = True
                StatusLB2.Text = "USB DEVICE CONNECTED"
            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)
                ' ** YOUR CODE HERE **
                Pictureoff.Visible = True
                Pictureon.Visible = False
                Label8.Text = "DEG C"
                Label8.Text = "AN1"
                CheckBox1.Enabled = False
                CheckBox2.Enabled = False
                CheckBox4.Enabled = False
                StatusLB2.Text = "USB DEVICE DISCONNECTED"
            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)...
            Dim index As Byte
    
            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...
    
                If BufferIn(7) = 0 And BufferIn(8) = 0 Then
                    '
                    '   incomming ADC reading and PORTA status
                    '   --------------------------------------
                    ProgressBar1.Value = BufferIn(1) 'Display ADC results to
                    ProgressBar2.Value = BufferIn(2) 'the progress bars
                    '
                    ProgressBar3.Value = BufferIn(1) 'Display ADC results to
                    ProgressBar4.Value = BufferIn(3) 'the progress bars
    
                    Label8.Text = Val(BufferIn(1)) 'Display  Progress bars
                    Label9.Text = Val(BufferIn(2))  'Values
                    Label18.Text = Val(BufferIn(3))  'Value
                    '
                    Else
                    '
                    '   Incomming data is a text string
                    '   -------------------------------
                    For index = 1 To BufferInSize   ' pass the whole array but skip (0)
                        If BufferIn(index) <> 0 Then ' valid data?
                            txtreception.AppendText(Chr(BufferIn(index)))
                        End If
                    Next
                    txtreception.Text += vbNewLine
                    txtreception.Select(txtreception.Text.Length - 1, 0)
                    txtreception.ScrollToCaret()
                End If
    
    
            End If
        End Sub
        Public Sub WriteSomeData()
            '   Use to send data to the USB bus.
            '   data must be store in BufferOut array
            '
            '   Actual structure:
            '   -----------------
            '       BufferOut(0) = Report id => always 0
            '       BufferOut(1) = PORTB  ' first data item, etc etc
            '       BufferOut(2) = HPWM slider1 MSB
            '       BufferOut(3) = IOUT
            '       BufferOut(4) = HPWM slider2 MSB
            '       BufferOut(5) = HPWM slider2 LSB
            '       BufferOut(6) = n/a = 0
            '       BufferOut(7) = n/a = 0
            '       BufferOut(8) = n/a = 0
            BufferOut(0) = 0   ' first by is always the report ID
            BufferOut(6) = 0
            BufferOut(7) = 0
            BufferOut(8) = 0
    
            hidWriteEx(VendorID, ProductID, BufferOut(0)) ' send it
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            txtreception.Clear()
        End Sub
    
    
        Public Sub ActualizaPORTC()
            Dim i As Integer
    
            BufferOut(1) = 0
    
            If CheckBox1.Checked = True Then
                PORTC(6) = 1
            Else
                PORTC(6) = 0
            End If
    
            If CheckBox2.Checked = True Then
                PORTC(1) = 1
            Else
                PORTC(1) = 0
            End If
    
                    For i = 0 To 1
                BufferOut(1) = BufferOut(1) + (PORTC(i) * (2 ^ i))
            Next
    
            WriteSomeData()
        End Sub
    
        Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
            ActualizaPORTAB()
        End Sub
    
        Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
            ActualizaPORTAB()
        End Sub
    
        'Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '   ActualizaPORTAB()
        'End Sub
    
        'Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '   ActualizaPORTAB()
        'End Sub
    
        'Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '   ActualizaPORTAB()
        'End Sub
    
        'Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '   ActualizaPORTAB()
        'End Sub
    
        'Private Sub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '   ActualizaPORTAB()
        'End Sub
    
        'Private Sub CheckBox8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '   ActualizaPORTAB()
        'End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            StatusLB3.Text = DateAndTime.Now.ToString
        End Sub
    
        Private Sub StatusLB1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatusLB1.Click
    
        End Sub
    
        Private Sub ProgressBar1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgressBar1.Click
    
        End Sub
        Private Sub GroupBox3_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox3.Enter
    
        End Sub
    
        Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click
    
        End Sub
    
        Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter
    
        End Sub
    
        Private Sub Label10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label10.Click
    
        End Sub
    
        Private Sub Label12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label12.Click
    
        End Sub
    
    End Class
    And this is the PIC side:

    Code:
    CLEAR
    
    include "modedefs.bas"
    
    Define OSC 48
    DEFINE LCD_DREG	PORTB       ' Set LCD Data port
    DEFINE LCD_DBIT	4           ' Set starting Data bit (0 or 4) if 4-bit bus
    DEFINE LCD_RSREG PORTB   ' Set LCD Register Select port
    DEFINE LCD_RSBIT 3     	' Set LCD Register Select bit
    DEFINE LCD_EREG	PORTB       ' Set LCD Enable port
    DEFINE LCD_EBIT	2           ' Set LCD Enable bit
    DEFINE LCD_BITS	4           ' Set LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES 2       ' Set number of lines on LCD
    DEFINE LCD_COMMANDUS 2000' Set command delay time in us
    DEFINE LCD_DATAUS 50      ' Set data delay time in us
    DEFINE ADC_BITS 10
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50
    
    ;--- Setup Interrupts ------------------------------------------------------
    INCLUDE "DT_INTS-18.bas"     ; Base Interrupt System
    
    ASM
    INT_LIST  macro    ; IntSource,          Label,  Type, ResetFlag?
            INT_Handler   USB_Handler
        endm
        INT_CREATE               ; Creates the interrupt processor
    endasm
    
    ;--- Setup USB -------------------------------------------------------------
    INCLUDE "DT_HID260.pbp"
    
    DEFINE USB_VENDORID    6017
    DEFINE USB_PRODUCTID   2000
    DEFINE USB_VERSION     1
    DEFINE USB_VENDORNAME  "Regal Electronics"
    DEFINE USB_PRODUCTNAME "Light Lynk - USB"
    DEFINE USB_SERIAL      "0001"
    DEFINE USB_INSIZE      16    ;  IN report is PIC to PC (8,16,32,64)
    DEFINE USB_OUTSIZE     16    ; OUT report is PC to PIC
    DEFINE USB_POLLIN      10   ; Polling times in mS, MIN=1 MAX=10
    DEFINE USB_POLLOUT     10
    
    ; --- Each USB status LED is optional, comment them if not used ------------
    ; --- They can be assigned to any pin, and no further action is required --- 
    DEFINE USB_LEDPOLARITY 1       ; LED ON State [0 or 1]  (default = 1)
    ;DEFINE USB_PLUGGEDLED  PORTB,7 ; LED indicates if USB is connected
    'DEFINE USB_TXLED       PORTC,6 ;  "      "     data being sent to PC
    DEFINE USB_RXLED       PORTB,6 ;  "      "     data being received from PC
    
    ;--- Variables -------------------------------------------------------------
    Old_PORTC   VAR  BYTE
    New_PORTC   VAR  BYTE
    
    
    ADCON1 = %00001011
    ADCON2 = %10000111
    
    TRISA = %00001111
    TRISB = %00000000
    TRISC = %00000111
    CMCON = 7
    PORTA.5 = 0
    
    LEDTEMP var word
    PCBTEMP var word
    LEDVOLT var word
    LEDCOUNT var word
    CURRENT var word
    LEDCURRENT var word
    LCUR0 var word
    LCUR1 var word
    
    ADJUST var word
    
    tb0 VAR WORD
    tb1 VAR WORD
    tb2 VAR WORD
    
    LED0 var word
    LED1 var word
    LED2 var word
    
    LEDV0 var word
    LEDV1 var word
    
    IOUT VAR byte
    IOUT1 VAR BYTE
    IOUT1 = 5
    
    JMG VAR WORD
    JMG1 VAR WORD
    JMG2 VAR WORD
    JMG3 VAR WORD
    JMG4 VAR WORD
    POT0 var word
    SCK     var     PORTB.0     ' Alias PORTB.0 to SCK (serial data clock)
    SDO     var     PORTC.7     ' Alias PORTC.7 to SDO (serial data out)
    CS      var     PORTB.1     ' Alias PORTB.1 to C_EN (chip enable)
    
    HIGH    CS                 'Chip enable high to start
    high    sck                'Mode 1,1 SCK idles high
    
    pot0.Highbyte = %00000000    'POT0 address in hex - easier for me to see which pot selected
    pot0.lowbyte = 0             'POT0 value in DEC - initialize pot - can be any value
    
    READ 10, pot0.lowbyte
    
    low     CS                          ' Make chip active
            pause   1                           ' Delay after making chip active per data sheet - only need 60nS
            shiftout    sdo,sck,5,[pot0\16]     ' Mode 1,1 in MCP4261 DS / Mode 5 PBP - clock idles high, MSB shift out first
            high    CS                          ' MCP4261 DS recommends toggling /CS between commands
            pause   1                           ' Delay keeps from banging CS pin
    
    
    ADJUST = 0
    PORTC.6 = 0
    
    Pause 1500
    LCDOUT $FE,$C0, " YJ5 LED DRIVER "
    pause 2000
    
    ;--- The Main Loop ---------------------------------------------------------
    Main:
    ARRAYWRITE USBTXBuffer, ["Random text here"]
    gosub WaitToSend
        FOR X = 0 to 1000           ; Check for incomming USB data while waiting
    @       ON_USBRX_GOSUB  _HandleRX
            PAUSE 1
        
            New_PORTC = PORTC                ; Check PORTC pins
            IF New_PORTC != Old_PORTC THEN
                Old_PORTC = New_PORTC
                ARRAYWRITE USBTXBuffer, ["Text Here!"]
                GOSUB WaitToSend
                ARRAYWRITE USBTXBuffer, [PCBTEMP, LEDTEMP, IOUT1, _
                                         PORTC.6, PORTA.4, PORTA.5,0,0]
                GOSUB WaitToSend
            ENDIF
        NEXT X
        
        'ADCIN 3, PCBTEMP            ; Send A/D about once/sec
        'ADCIN 2, LEDTEMP
        ARRAYWRITE USBTXBuffer, [tb0, LEDTEMP, IOUT1, _
                                 PORTC.6, PORTA.4, PORTA.5,0,0]
        GOSUB SendIfReady
        
    If PORTE.3 = 1 THEN MENU
    if PORTC.0 = 1 then SELEC
    if PORTC.1 = 1 then UP
    if PORTC.2 = 1 then DOWN
    
    ADCIN 3,PCBTEMP
    tb0 = PCBTEMP * 33 /92 
    tb1 = tb0*721                                     
    tb2 = Div32 255
    tb2 = tb2 - 300
    tb0 = tb2/10   
    tb1 = tb2//10
    
    ADCIN 2,LEDTEMP
    LED0 = LEDTEMP * 33 /92 
    LED1 = LED0*721                                     
    LED2 = Div32 255
    LED2 = LED2 - 264
    LED0 = LED2/10   
    LED1 = LED2//10
     
    PAUSE 200
     
    LCDOUT $FE,1,"PCB ",DEC2 tb0,".",DEC1 tb1,"C ",DEC4 IOUT1 
    LCDOUT $FE,$C0,"LED ",DEC2 LED0,".",DEC1 LED1,"C ", DEC4 LEDTEMP
    
    ADCIN 1, LEDCOUNT
    LEDVOLT = LEDCOUNT * 49 /100
    LEDV0 = LEDVOLT/10
    LEDV1 = LEDVOLT//100
    
    ADCIN 0, CURRENT
    LEDCURRENT = CURRENT * 50 /10
    LCUR0 = LEDCURRENT/10
    LCUR1 = LEDCURRENT//100       
    
    PAUSE 2000
    
    If PORTE.3 = 1 THEN MENU
    if PORTC.0 = 1 then SELEC
    if PORTC.1 = 1 then UP
    if PORTC.2 = 1 then DOWN
    
    LCDOUT $FE,1, "LED V ", DEC2 LEDV0, ".", DEC1 LEDV1,"V ", DEC4 LEDCOUNT
    LCDOUT $FE,$C0,"LED C ", DEC2 LCUR0, ".", DEC1 LCUR1,"A ", DEC4 CURRENT
    
    pause 2000
    
    If PORTE.3 = 1 THEN MENU
    if PORTC.0 = 1 then SELEC
    if PORTC.1 = 1 then UP
    if PORTC.2 = 1 then DOWN
     
    goto main
    
    MENU:
    'LCDOUT $FE, 1, "MENU" : LCDOUT $FE, $C0,"PRESSED"
    PORTC.6 = 1
    pause 300
    PORTC.6 = 0
    'LCDOUT $fe,1
    goto setup
    
    SELEC:
    LCDOUT $FE, 1, "SELECT" : LCDOUT $FE, $C0,"PRESSED"
    PORTC.6 = 1
    pause 300
    PORTC.6 = 0
    LCDOUT $fe,1
    goto main
    
    UP:
    LCDOUT $FE, 1, "UP" : LCDOUT $FE, $C0,"PRESSED"
    PORTC.6 = 1
    pause 300
    PORTC.6 = 0
    LCDOUT $fe,1
    goto main
    
    DOWN:
    LCDOUT $FE, 1, "DOWN" : LCDOUT $FE, $C0,"PRESSED"
    PORTC.6 = 1
    pause 300
    PORTC.6 = 0
    LCDOUT $fe,1
    goto main
    
    SETUP:
    LCDOUT $FE, 1, "SET LED CURRENT"
    LCDOUT $FE, $C0, DEC3 IOUT
    gosub HandleRX
    IF PORTC.1 = 1 THEN
    IOUT = IOUT + 1
    PAUSE 75
    ENDIF
    IF PORTC.2 = 1 THEN
    IOUT = IOUT - 1
    PAUSE 100
    ENDIF
    pot0.lowbyte = IOUT
    low     CS                          ' Make chip active
            pause   1                           ' Delay after making chip active per data sheet - only need 60nS
            shiftout    sdo,sck,5,[pot0\16]     ' Mode 1,1 in MCP4261 DS / Mode 5 PBP - clock idles high, MSB shift out first
            high    CS                          ' MCP4261 DS recommends toggling /CS between commands
            pause   1                           ' Delay keeps from banging CS pin
    
    
    WRITE 10, IOUT
    IF PORTC.0 = 1 THEN main
    PAUSE 75
    'IF PORTE.3 = 1 THEN VERSION
    GOTO SETUP
    
    VERSION:
    pause 100
    LCDOUT  $FE, 1, "FIRMWARE 1.0"
    LCDOUT  $FE, $C0, "PCBA 1.0 SN 0001"
    If PORTC.0 = 1 THEN SETUP
    Goto VERSION
        
    GOTO Main
    
    ;--- Send Data if Plugged and ready, otherwise discard ---------------------
    SendIfReady:
        IF Plugged AND TX_READY THEN DoUSBOut
    RETURN
    
    ;--- Wait till Ready to send of until unplugged ----------------------------
    WaitToSend:
        WHILE Plugged and !TX_READY : WEND
        IF TX_READY THEN GOSUB DoUSBOut
    RETURN
    
    ;---- Receive incoming data PORTB LEDS and CCP PWM dutycycle ---------------
    HandleRX:
        ARRAYREAD  USBRXBuffer,[JMG, JMG1, IOUT1, PORTC.6, JMG3, JMG4]
        IOUT1 = USBRXBuffer(3)
        
       return
    All I want to do is send some AD values and a couple of Bytes containing temperatures to the PC, and set a slider in VB and send the 0 - 255 value back to IOUT1 byte in PBP and also turn on or off portc.6 in VB.

  33. #33
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305

    Default Re: PIC USB to VB.NET

    The rx LED lights up as I click on buttons on the pc, so data must be getting to the pic, but I dont see it turning on or off the port pin as it should. So not sure what data is being sent or whats happening to it once it gets tot he PIC.

    Also, I dont see the rx LED lighting when I move the slider bar so there has to be an issue on the VB side.

    Can anyone help me out please? I'm a learn by example guy. I've figured out a few errors and got my self from some problems but I'm stuck now.

  34. #34
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305

    Default Re: PIC USB to VB.NET

    It looks like there are 8 bytes. The first byte has to be 0 as to the last 2. So theroetically i should be able to just do something like this:

    BufferOut(0) = 0
    BufferOut(1) = 100
    BufferOut(2) = 200
    BufferOut(3) = 300
    BufferOut(4) = 400
    BufferOut(5) = 500
    BufferOut(6) = 600
    BufferOut(7) = 0
    BufferOut(8) = 0

    On the PIC end I should be able to use ARRAYWRITE [byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8]

    Should this work?

  35. #35
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Re: PIC USB to VB.NET

    HI;

    To send data from PIC to PC, you must do something like this ( Allready posted in the code of the post #1 )

    Code:
    ARRAYWRITE USBTXBuffer, [Value0, Value1, PORTA.2,PORTA.3, PORTA.4, PORTA.5,0,0]
    You don't have to worry about the first byte be equal to 0, only ont the PC to PIC side.

    If you want to send from PC to PIC, you must do something like this ( Allready posted in the code of the post #1 )

    Code:
    PublicSub WriteSomeData()
    ' Use to send data to the USB bus.
    ' data must be store in BufferOut array
    '
    ' Actual structure:
    ' -----------------
    BufferOut(0) = 0    ' first by is always the report ID
    BufferOut(1) = 127 ' value from 1º slider
    BufferOut(2) = 255 ' value from 2º slider
    BufferOut(3) = 10
    BufferOut(4) = 100
    BufferOut(5) = 178
    BufferOut(6) = 0
    BufferOut(7) = 0
    BufferOut(8) = 0
    hidWriteEx(VendorID, ProductID, BufferOut(0)) ' send it
    EndSub
    
    Thanks and Regards;
    Gadelhas

  36. #36
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305

    Default Re: PIC USB to VB.NET

    Are the values in BufferOut(1) through BufferOut(5) decimal? Are they bytes or words?

    How do I receive these values and put them into VAR BYTES?

    Do I always need to send the 8 values of BufferOut or can I send say just BufferOut(3)?

    Can I send BufferOut(1) = "text here" for example?



    Thank you!!

  37. #37
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Re: PIC USB to VB.NET

    Quote Originally Posted by jmgelba View Post
    Are the values in BufferOut(1) through BufferOut(5) decimal? Are they bytes or words?

    How do I receive these values and put them into VAR BYTES?

    Do I always need to send the 8 values of BufferOut or can I send say just BufferOut(3)?

    Can I send BufferOut(1) = "text here" for example?



    Thank you!!
    Hi, you are not reading the code that i posted.
    If you read the code carfully you see that in VB the BufferOut Array is declared asbyte, so you can only send bytes.
    Code:
    Dim BufferOut(BufferOutSize) AsByte
    On the PIC side you receive the bytes on the USBRXBuffer array, and then you do what you want with those bytes.
    Code:
      ARRAYREAD  USBRXBuffer,[PORTB, DUTY1.LowByte, DUTY1.HighByte, DUTY2.LowByte, DUTY2.HighByte]
    On the VB side you receive tte bytes on BufferIn array, and then you do what you want with thise bytes.
    Code:
    Dim BufferIn(BufferInSize) AsByte
    Since the Define of the USB_INSIZE and USB_OUTSIZE are equal to 8, you can only send and receive 8 bytes. You can change this, to multiples values, like 16, 32, 64. YOu need always to send all th ebytes that are defined. You you Define 8, you must send 8 bytes, if you define 16 you must send 16 bytes. For instance, if you have only 10 bytes to send, you must define you USB_sizes equal to 16, and complete the array with 0's.

    Can I send BufferOut(1) = "text here" for example?
    You cannot do that, because it excedes the number of bytes. BufferOut(1) is one byte only. You cand o just this, BufferOut(1)="t", or
    BufferOut = "0abcdefgh".

    I think that you should read both PIC and VB code that i posted and review review some principles of the PBP manual.
    Thanks and Regards;
    Gadelhas

  38. #38
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305

    Default Re: PIC USB to VB.NET

    Ok, then I think I've found a problem. My buffer size is set to 16 in and out but I'm only rx/tx'ing 8.

    When receiving empty bytes, how do I deal with them? Say I only ever need to send 3 bytes, the left overs will always be empty. Do I just assign unused VAR Bytes to those incoming bytes?

  39. #39
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default Re: PIC USB to VB.NET

    You just need to parse/test them on both side. Treat USB like any proper serial communication and you should be fine. Your milleage may vary though.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  40. #40
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305

    Default Re: PIC USB to VB.NET

    Thanks guys. I think I've found my issues.

    One more question for now though, is the data within the byte transmitted msb first or lsb first from VB?

Members who have read this thread : 2

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