Pic 18f Usb


Closed Thread
Results 1 to 40 of 135

Thread: Pic 18f Usb

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Steve,

    You're dealing with bytes. PORTA.1 = USBBuffer[1] isn't the way to go since you can't place a byte value on a single port "bit".

    Just place the byte you need on the whole port. You can send a single byte value from the PC app, and simply place it on the port.

    I.E. PORTA = USBBuffer[1] should work fine - assuming that's the byte element you want.

    Sending data from the PIC to PC is equally simple. Here's one example. Note this isn't everything. Just the sections I added to the framework file created with EasyHID.
    Code:
    DEFINE OSC 48
    DEFINE LOADER_USED 1
    DEFINE	ADC_BITS 10     ' Set number of bits in result
    DEFINE	ADC_CLOCK 3     ' Set clock source /32
    DEFINE	ADC_SAMPLEUS 50 ' Set sampling time in uS
    DEFINE RESET_ORG 800h ' <-- I used the Microchip USB loader
    
    BufferSize      con 8
    DataBuffer      Var Byte(BufferSize) ' data buffer
    DataBufferCount Var Byte             ' buffer size
    Quanta  con 1251
    TRISD = 0
    PORTD = 0
    
    ' Variables
    X       VAR byte
    Adval   VAR WORD
       
        TRISA.0 = 1        ' RA0 input
        ADCON1 = %00001110 ' A/D channel 0
        ADCON2 = %10000011 ' Right justify for 10-bit
        USBInit
    
    Main:
        GOSUB DoUSBIn
        PORTD = DataBuffer[7]
        
        ADCIN 0,adval		     ' Read A/D channel 0 into ADval variable
        ADval = ADval */ Quanta  ' Quanta result
        
        ' Load data buffer
        DataBuffer(0) = Adval DIG 3
        DataBuffer(1) = "."
        DataBuffer(2) = Adval DIG 2
        DataBuffer(3) = Adval DIG 1
        DataBuffer(4) = Adval DIG 0
        GOSUB DoUSBOut
        FOR X = 0 to 99 ' Short delay between updates
         PAUSEUS 1000
         USBSERVICE     ' Maintain HID connection during delay period
        NEXT X
        GOTO Main
    On the PC side, modify the USB framework file created with EasyHID something like this;
    Dim MyText As String ' create your string storage space

    In the OnRead sub-routine;
    MyText = (CStr(BufferIn(1)) + Chr$(BufferIn(2)) + CStr(BufferIn(3)) + CStr(BufferIn(4)) + CStr(BufferIn(5)) + "V")
    Label1.Caption = MyText ' Place string text into label displaying A/D value

    Judy,

    Reading three pulse widths or A/D values doesn't really have anything to do with USB. The USB PIC is just another PIC with the addition of the USB module. Using the onboard A/D is pretty much the same as any other PIC.

    All you need here is the datasheet, and a little time to experiment getting your pulse measurement & A/D routines working. Sending this all to the PC via the USB module is no more than stuffing your values into the outgoing buffer like in the previous example.

    Just be sure to keep the USB connection alive by avoiding the use of blocking functions, long delays, etc,,. It's really not that hard, but you will need to experiment a tad.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  2. #2
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    Bruce,

    Would you mind posting all of your ADC code, i am getting errors. I've had a look, but can't figure it out.

    The code posted above seems to be missing the DoUSBIn and DoUSBOut subroutines.

    I also get an error on the DataBuffer(1) = "."

    I want to use it as a 5v voltage monitor passing the results to Vb.

    Many thanks,

    Steve

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Hi Steve,

    Sure. Here's the complete file. Just be sure you've used EasyHID to generate the required USB support files.
    Code:
    DEFINE LOADER_USED 1
    DEFINE OSC 48
    DEFINE ADC_BITS 10     ' Set number of bits in result
    DEFINE ADC_CLOCK 3     ' Set clock source /32
    DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
    DEFINE RESET_ORG 800h    ' For Microchip USB loader
    
    BufferSize      con 8
    DataBuffer      Var Byte(BufferSize) ' data buffer
    DataBufferCount Var Byte             ' buffer size
    Quanta  con 1251
    TRISD = 0
    PORTD = 0
    
    ' Variables
    X       VAR byte
    Adval   VAR WORD
       
        TRISA.0 = 1        ' RA0 input
        ADCON1 = %00001110 ' A/D channel 0
        ADCON2 = %10000011 ' Right justify for 10-bit
        USBInit
    
    Main:
        GOSUB DoUSBIn
        PORTD = DataBuffer[7]
        
        ADCIN 0,Adval		 ' Read A/D channel 0 into ADval variable
        ADval = ADval */ Quanta  ' Quanta result
        
        ' Load data buffer
        DataBuffer(0) = Adval dig 3
        DataBuffer(1) = "."
        DataBuffer(2) = Adval DIG 2
        DataBuffer(3) = Adval DIG 1
        DataBuffer(4) = Adval DIG 0
        GOSUB DoUSBOut
        FOR X = 0 to 99 ' Short delay between updates
         PAUSEUS 1000
         USBSERVICE     ' Maintain HID connection during delay period
        NEXT X
        GOTO Main
    
       ' USB in...
    DoUSBIn:
        DataBufferCount = BufferSize
        USBService
        USBIn 1, DataBuffer, DataBufferCount, DoUSBIn
        RETURN
       
       ' USB out...
    DoUSBOut:
        DataBufferCount = BufferSize
        USBService
        USBOut 1, DataBuffer, DataBufferCount, DoUSBOut
        RETURN
        
        END
    Here's the PIC & VB source for a USB A/D datalogger http://www.rentron.com/mcstudio/USB_AD.zip
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    Great, thanks Bruce. Unfortuanatly i am still getting a big 0 in VB. I'm sure its only something small i am doing wrong (hopefully) !!

    Here is the code, can you spot anything obvious that i have done wrong?

    Its a PIC18F2455 where i am putting 5v onto RA0.

    Code:
    DEFINE LOADER_USED 1
    DEFINE OSC 48
    DEFINE ADC_BITS 10     ' Set number of bits in result
    DEFINE ADC_CLOCK 3     ' Set clock source /32
    DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
    'DEFINE RESET_ORG 800h    ' For Microchip USB loader
    
    BufferSize      con 8
    DataBuffer      Var Byte(BufferSize) ' data buffer
    DataBufferCount Var Byte             ' buffer size
    Quanta  con 1251
    
    
    ' Variables
    X       VAR byte
    Adval   VAR WORD
       
        TRISA.0 = 1        ' RA0 input
        ADCON1 = %00001110 ' A/D channel 0
        ADCON2 = %10000011 ' Right justify for 10-bit
        USBInit
    
    Main:
        GOSUB DoUSBIn
        PORTB = DataBuffer[7]
        
        ADCIN 0,Adval		 ' Read A/D channel 0 into ADval variable
        ADval = ADval */ Quanta  ' Quanta result
        
        ' Load data buffer
        DataBuffer(0) = Adval dig 3
        DataBuffer(1) = "."
        DataBuffer(2) = Adval DIG 2
        DataBuffer(3) = Adval DIG 1
        DataBuffer(4) = Adval DIG 0
        GOSUB DoUSBOut
     
        FOR X = 0 to 99 ' Short delay between updates
           PAUSEUS 1000
           USBSERVICE     ' Maintain HID connection during delay period
        NEXT X
    
     GOTO Main
    
    ' ************************************************************
    ' * receive data from the USB bus                            *
    ' ************************************************************
    DoUSBIn:
        DataBufferCount = BufferSize
        USBService
        USBIn 1, DataBuffer, DataBufferCount, DoUSBIn
        RETURN
       
    ' ************************************************************
    ' * wait for USB interface to attach                         *
    ' ************************************************************
    DoUSBOut:
        DataBufferCount = BufferSize
        USBService
        USBOut 1, DataBuffer, DataBufferCount, DoUSBOut
        RETURN
    END
    In VB, i have placed a label on the form and this is in the read event section

    Code:
    '*****************************************************************
    ' 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
          
        MyText = (CStr(BufferIn(1)) + Chr$(BufferIn(2)) + CStr(BufferIn(3)) + CStr(BufferIn(4)) + CStr(BufferIn(5)) + "V")
        Label1.Caption = MyText ' Place string text into label displaying A/D value
    
          
          ' ** 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

    Many thanks again.

    Steve

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Private Const VendorID = 6017
    Private Const ProductID = 2000

    Check to make sure the vendor & product ID numbers in your VB source match the vendor/product ID #'s in the PIC framework files generated by EasyHID. If these don't match, the VB app will never respond.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    Although i have changed the ProductID from 2000 to 2005, the device is recognised.

    The VB program displays a text box which says USB Device Connected or indeed Disconnected, depending on its status.

    So, i think this bit is OK.

    Steve

  7. #7
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    What exactly is happening? Are you getting nothing on the label caption? Have you verified your hardware with something simple like reading the A/D input, and displaying the result on an LCD, serial terminal, etc?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  8. #8
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    Basically i'm putting 5v on the PORTA.0 pin and just gettin 'label1' in the label of VB.

    Should VB always be updating and looking at the USBinput or should there be a READ button thats calls the procedure?

    Also on the VB side it has:

    Code:
    MyText = (CStr(BufferIn(1)) + Chr$(BufferIn(2)) + CStr(BufferIn(3)) + CStr(BufferIn(4)) + CStr(BufferIn(5)) + "V")
    Most of it contains CStr but the bit thats contains Chr$, i presume thats for the "." as sent from the PIC.

    I've tried playing around with it, but still no joy. If you have it, is it worth sending me the vb source file you have just for this bit?

    Are you on messenger, i am, i thought it might be easier to converse?

    Thanks again,

    Steve

Similar Threads

  1. USB CDC Communications for Dummies!
    By Squibcakes in forum USB
    Replies: 104
    Last Post: - 15th January 2014, 13:43
  2. Reading a slave USB with a pic
    By pcaccia in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 25th October 2008, 12:00
  3. Replies: 15
    Last Post: - 30th October 2007, 19:25
  4. USB communication with pic
    By Shamir in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 14th August 2006, 23:43
  5. USB PIC without USB Connection
    By Tissy in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 26th December 2005, 17:39

Members who have read this thread : 1

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