Lab X USB


Closed Thread
Results 1 to 7 of 7

Thread: Lab X USB

  1. #1
    Join Date
    Sep 2009
    Location
    South Wales (UK)
    Posts
    62

    Default Lab X USB

    Posts moved from here:
    http://www.picbasic.co.uk/forum/showthread.php?t=4529
    to make a new thread for Jim.


    Hi,

    I'm trying to get LABXUSB code to work which is a basic USB transmit/receive demo of USB. I get the same list of errors as above.

    I've copied the contents of PBP\USB18 into the folder containing the LABXUSB.BAS file. I then open the LABXUSB.BAS file in microcode studio plus, and push F9 to compile it, and still I get the above error list...

    Am I missing something very obvious (usual!)? Is there a tick box somewhere?

    Best regards, and a belated happy new year to all!

    Cheers

    Jimbo

    The LABXUSB.BAS is listed below:

    Code:
       INCLUDE "LABXUSB.INC"
       
       ' NOTE: USBBuffer(0) "aliased here as CMDByte" holds BufferOut(1) output
       ' from EasyHID.
       
       ' For inbound data packets, the value in CMDByte indicates what data the
       ' host application has requested. For outbound data packets "stuff the PIC
       ' is sending back", the value indicates to the host what data the PIC is
       ' returning.
       
    ' ************************************************************
    ' * main program loop - remember, you must keep the USB      *
    ' * connection alive with a call to USBService every couple  *
    ' * of milliseconds or so...                                 *
    ' ************************************************************
    Main:
       ' These are enabled only for routines in USBDriverService
       ' to cause a response when interrupt flag bits are tested
       @ BCF PIE2, USBIE  ; Disable USB interrupts
       @ BSF UIE, ACTVIE  ; Enable USB Bus Activity Detect Interrupt
       @ BSF UIE, IDLEIE  ; Enable USB Idle Detect Interrupt
       usbinit            ' initialise USB...
       
    ProgramStart: 
    '   GOSUB USB_Interrupts
       gosub DoUSBIn
       ' Find out which command we've received from the USB host
       ' and process the request.
       IF CMDByte = "R" THEN RelayControl
       IF CMDByte = "Z" THEN ShowStatus
      
       goto ProgramStart  
    ' ************************************************************
    ' * Control & data gathering routines                        *
    ' ************************************************************
       
    ShowStatus:
       ADCIN 0, Temp    ' Read A/D channel 0 into Temp variable
       
       ' Load USB buffer with command & data
       CMDByte = "A"    ' USBBuffer(0) = "A"
       USBBuffer(1) = Temp.HighByte
       USBBuffer(2) = Temp.LowByte
       GOSUB DoUSBOut   ' Send data to USB host        
            
    GetTemp:  ' Read temperature from Dallas 1-wire DS18B20 temp sensor
        OWOut DQ, 1, [$CC, $44]       ' Skip ROM search & do temp conversion
        
    Wait_Up:
        OWIn DQ, 4, [Busy]            ' Read busy-bit
        if Busy = 0 Then Wait_Up      ' Still busy..?, Wait_Up..!
        OWOut DQ, 1, [$CC, $BE]       ' Skip ROM search & read scratchpad memory
        OWIn DQ, 2, [Temp.Lowbyte, Temp.Highbyte]' Read two bytes / end comms
        
        ' Load USB buffer with command & data
        CMDByte = "T"  ' USBBuffer(0) = "T"
        USBBuffer(1) = Temp.HighByte
        USBBuffer(2) = Temp.LowByte
        
        ' Turn ON relay if Temperature is > 25 deg C
        ' Just an example. Comment this out if not used.
        IF (Temp >> 4) > 25 THEN
           HIGH LED
        ELSE
           LOW LED
        ENDIF
        
        GOSUB DoUSBOut   ' Send data to USB host
        
        CMDByte = "L"    ' USBBuffer(0) = "L"
        IF LED = 1 THEN  ' Read output pin
           USBBuffer(1) = "O"
           USBBuffer(2) = "N"
           USBBuffer(3) = " "
        ENDIF
        
        IF LED = 0 THEN  ' Read output pin
           USBBuffer(1) = "O"
           USBBuffer(2) = "F"
           USBBuffer(3) = "F"
        ENDIF
        GOSUB DoUSBOut  
        GOTO ProgramStart
        
    RelayControl:
        CMDByte = "L"    ' USBBuffer(0) = "L"
        IF USBBuffer(7) = "1" THEN
           HIGH LED      ' LED or relay ON
           ' Load ON status into buffer
           ' This informs the USB host as to relay status
           USBBuffer(1) = "O"
           USBBuffer(2) = "N"
           USBBuffer(3) = " "
        ENDIF
        
        IF USBBuffer(7) = "0" THEN
           LOW LED       ' LED or relay OFF
           ' Load OFF status into buffer
           ' This informs the USB host as to relay status
           USBBuffer(1) = "O"
           USBBuffer(2) = "F"
           USBBuffer(3) = "F"
        ENDIF
        GOSUB DoUSBOut   ' Send data to USB host
        GOTO ProgramStart
            
        END
    Last edited by Demon; - 23rd January 2012 at 01:56. Reason: New thread

  2. #2
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588

    Default RE: Lab X USB

    I used Darrel's DT_HID260 routine for an 18F4550 and it worked great on my Lab X1:
    http://www.picbasic.co.uk/forum/show...0434#post80434

    Code:
    '***************************************************************************
    '*  Name    : USBLabX1_RH.pbp                                              *
    '*  Author  : Steve Monfette/Darrel Taylor/Demon                    *
    '*  Date    : Jan 11 2012                                                  *
    '*  Version : 3.0                                                          *
    '*  Notes   : This is a re-creation of mister-e's USBdemo for DT_HID       *
    '*          : Meant to work with mister-e's GUI                            *
    '*          : Modified for Lab X1                                          *
    '*  Hardware : PIC 18F4550, 20mhz crystal                                  *
    '*           : Lab X1 Experimental Board, 9.5V wall adapter                *
    '*           : MeLabs U2 Programmer v4.32                                  *
    '*  Software : PIC Basic Pro v2.60C                                        *
    '*           : MicroCode Studio Plus v2.2.1.1                              *
    '*           : MPASM WIN Assembler v4.02 (mplab tools/MPASM Suite)         *
    '*  PIC mods : plastic USB connector hot-glued onto top of PIC             *
    '*           : D- wired to C4 (pin 23) directly to top of pins             *
    '*           : D+ wired to C5 (pin 24) directly to top of pins             *
    '*           : two 0.1uF ceramic caps in parallel across VUSB and VSS      *
    '***************************************************************************
    '--- if you use these, you must comment the ones in the .inc file ---
    @   __CONFIG    _CONFIG1L, _PLLDIV_5_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_OFF_2H
    @   __CONFIG    _CONFIG3H, _CCP2MX_OFF_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_ON_3H
    @   __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L
    
    DEFINE OSC 48
    CLEAR
    
    DEFINE  LCD_DREG      PORTD         ' Set LCD data port
    DEFINE  LCD_DBIT      4             ' Set starting data bit
    DEFINE  LCD_RSREG     PORTE         ' Set LCD register select port
    DEFINE  LCD_RSBIT     0             ' Set LCD register select bit
    DEFINE  LCD_EREG      PORTE         ' Set LCD enable port
    DEFINE  LCD_EBIT      1             ' Set LCD enable bit
    DEFINE  LCD_BITS      4             ' Set LCD bus size
    DEFINE  LCD_LINES     2             ' Set number of lines on LCD
    DEFINE  LCD_COMMANDUS 2000          ' Set command delay time in microseconds
    DEFINE  LCD_DATAUS    50            ' Set data delay time in microseconds
    
    ;--- Setup Interrupts ------------------------------------------------------
    INCLUDE "DT_INTS-18.bas"        ; Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas"     ; PBP Re-entry for external interrupt
    
    ASM
    INT_LIST  macro    ; IntSource,     Label,   Type, ResetFlag?
            INT_Handler   USB_Handler
            INT_Handler   INT_INT,   _ToggleLED1, PBP,    yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    endasm
    
    @   INT_ENABLE   INT_INT        ; enable external (INT) interrupts
    
    ;--- Setup USB -------------------------------------------------------------
    INCLUDE "DT_HID260.pbp"
    
    DEFINE USB_VENDORID    6017
    DEFINE USB_PRODUCTID   2000
    DEFINE USB_VERSION     1
    DEFINE USB_VENDORNAME  "Mr E/DT/RH"
    DEFINE USB_PRODUCTNAME "USBLabX1"
    DEFINE USB_SERIAL      "001"
    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  PORTD,0 ; LED indicates if USB is connected
    DEFINE USB_TXLED       PORTD,1 ;  "      "     data being sent to PC
    DEFINE USB_RXLED       PORTD,2 ;  "      "     data being received from PC
    
    ;--- Setup ADC -------------------------------------------------------------
    DEFINE ADC_BITS 8  ; Number of bits in ADCIN result
    
        TRISA = 000011               ' Set port A pins to 6 output, 2 input
        TRISB = 001111               ' Set port B pins to 4 output, 4 input
                                        ' B0 = External Interrupt
        TRISC = 000000               ' Set all port C pins to output
        TRISD = 000000               ' Set all port D pins to output
        TRISE = 000000               ' Set all port E pins to output
    
    ;--- Variables -------------------------------------------------------------
    Value0      VAR  WORD : Value0 = 0
    Value1      VAR  WORD : Value1 = 0
    DUTY1       VAR  WORD
    DUTY2       VAR  WORD
    LED1        VAR PORTD.3
    WS_CHAR     VAR BYTE
    WS_BYTE     VAR BYTE
    WS_LOOPW    VAR WORD
    ;--- Initialize ------------------------------------------------------------
        ADCON2.7 = 0       ; left justify    (Change this if ADFM in diff register)
        ADCON1 = 001101     ; AN0 & AN1 analog
        INTCON2 = 000000    ; Pull-up resistors, falling edge
    
        PORTB = 000000      ; Set output on keypad to prevent floating pins
    
        LCDOUT  $FE,1                   ' Initiate first command to LCD
        PORTD = 0                       ' Clear LEDs from LCD operations
    
        FOR WS_LOOPW = 0 to 1000        ' Wait for LCD to initalize
            PAUSE 1
        NEXT WS_LOOPW
        LCDOUT  $FE,1,"LabX1 18F4550 USB"
        PORTD = 0                       ' Clear LEDs from LCD operations
    
        FOR WS_LOOPW = 0 to 1000        ' Wait for USB to initalize
            PAUSE 1
        NEXT WS_LOOPW
        ARRAYWRITE USBTXBuffer, ["Lab X1  "]
        IF Plugged THEN GOSUB DoUSBOut     ; only send when USB is connected
        
    ;--- The Main Loop ---------------------------------------------------------
    Main:
        FOR WS_LOOPW = 0 to 1000           ; Check for incomming USB data while waiting
    @       ON_USBRX_GOSUB  _HandleRX
            PAUSE 1
        NEXT WS_LOOPW
        
        ADCIN 0, Value0            ; Send A/D about once/sec
        ADCIN 1, Value1
        ARRAYWRITE USBTXBuffer, [Value0, Value1, PORTD.3, 0, 0, 0, 0, 0]
        IF Plugged THEN GOSUB DoUSBOut     ; only send when USB is connected
    
        GOTO Main
    end
    
    ;---- Receive incoming data PORTB LEDS and CCP PWM dutycycle ---------------
    HandleRX:
        ARRAYREAD  USBRXBuffer,[WS_BYTE, DUTY1.LowByte, DUTY1.HighByte, _
                                        DUTY2.LowByte, DUTY2.HighByte]
        LCDOUT  $FE,$C0,BIN WS_BYTE
        PORTD = 0                       ' Clear LEDs from LCD operations
    return
    
    '---[INT - interrupt handler]---------------------------------------------------
    ToggleLED1:
        TOGGLE LED1
        ARRAYWRITE USBTXBuffer, ["Button  "]
        IF Plugged THEN GOSUB DoUSBOut     ; only send when USB is connected
        PORTB = 0               ' Set output on keypad to prevent floating pins
    @ INT_RETURN
    The Lab X1 has no USB connector so I fudged one onto the PIC with hotglue. Duct tape was too big...

    Robert
    Last edited by Demon; - 4th October 2016 at 17:24. Reason: rename title

  3. #3
    Join Date
    Sep 2009
    Location
    South Wales (UK)
    Posts
    62

    Default RE: Lab X USB

    Hi Robert,

    Nice pic! lol.

    Many thanks for this. I now have some hope of getting started on USB! Will take a look at this in the next day or so and fingers crossed get something going.

    I'm not actually using the lab x1, just a simple circuit I made myself with a USB connector on the required pins and an LED or two on port b. Once I get a communication set up between it and the PC, I'll make it grow.

    Cheers

    Jimbo
    Last edited by ScaleRobotics; - 23rd January 2012 at 03:25. Reason: rename title

  4. #4
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588

    Default RE: Lab X USB

    My daughter left her camera lying around so I filled it up with a bunch of goofy pics. That one was the most presentable.

    You don't have to be using the Lab X1 for my example, just remove the stuff you don't need like analog conversion, text message, etc.

    It uses Steve's Visual Basic program on the PC, so you don't have to worry about that side yet. You can get into that later, at least this stuff works as is. I have yet to modify Steve's VB in my testing, so make sure to keep the SEND and RECEIVE fields as they are. That way you don't have to worry about modifying buffer lengths, recompiling VB program and such.

    But if you use a 20 MHz crystal, 18F4550 and proper pins, you should be up and running quickly. If not, we're here.

    Make sure to have the Includes in the same folder as your PBP program, as well as the 5 USB files Steve posted in that thread (start from Darrel's post and work your way back).

    Robert


    EDIT: One last thing, make sure to use MPASM.
    Last edited by ScaleRobotics; - 23rd January 2012 at 03:26. Reason: rename title to original post title to reduce confusioni

  5. #5
    Join Date
    Sep 2009
    Location
    South Wales (UK)
    Posts
    62

    Default Re: Lab X USB

    Hello again,

    Well, some progress. I know my circuit is fine as I managed to upload a hex file that echoes via a HID terminal anything I send to the circuit. That's half the battle I guess. Will get back to your stuff and simplify it as you said and get it up and running hopefully this week. Still over the moon that the circuit works! This USB stuff is hopefully going to open up a whole new world of faster data transfer from sensors for me (serial is too slow).

    Very thankful for all these forums... hate stumbling around in the dark!

    Best regards

    Jimbo

    P.S. Just noticed you're in Montreal! Visited a friend there a few years back. Somewhere around Lincoln/St Marc off Sherbrooke. Was a lovely place that (Montreal). Loved the 'bonjour/hi' that preceded transaction in a shop
    Last edited by jimbostlawrence; - 24th January 2012 at 00:57.

  6. #6
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588

    Default Re: Lab X USB

    Here, this is even more simplified. Have momentary-ON button on Port B.0 pulled high, 4 LEDs with resistors to VSS on Port D.0, 1, 2 and 3.

    On PIC side:
    - button on Port B.0 sends "Button" text.

    On PC side using Mr E's executable as-is:
    - Port B buttons toggles LED on Port D.3.

    Code:
    '***************************************************************************
    '*  Name    : USBLabX1_RH.pbp                                              *
    '*  Author  : Steve Monfette/Darrel Taylor/Demon                    *
    '*  Date    : Jan 11 2012                                                  *
    '*  Version : 3.0                                                          *
    '*  Notes   : This is a re-creation of mister-e's USBdemo for DT_HID       *
    '*          : Meant to work with mister-e's GUI                            *
    '*          : Modified for Lab X1                                          *
    '*  Hardware : PIC 18F4550, 20mhz crystal                                  *
    '*           : Lab X1 Experimental Board, 9.5V wall adapter                *
    '*           : MeLabs U2 Programmer v4.32                                  *
    '*  Software : PIC Basic Pro v2.60C                                        *
    '*           : MicroCode Studio Plus v2.2.1.1                              *
    '*           : MPASM WIN Assembler v4.02 (mplab tools/MPASM Suite)         *
    '*  PIC mods : plastic USB connector hot-glued onto top of PIC             *
    '*           : D- wired to C4 (pin 23) directly to top of pins             *
    '*           : D+ wired to C5 (pin 24) directly to top of pins             *
    '*           : two 0.1uF ceramic caps in parallel across VUSB and VSS      *
    '***************************************************************************
    '--- if you use these, you must comment the ones in the .inc file ---
    @   __CONFIG    _CONFIG1L, _PLLDIV_5_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_OFF_2H
    @   __CONFIG    _CONFIG3H, _CCP2MX_OFF_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_ON_3H
    @   __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L
    
    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  "Mr E/DT/RH"
    DEFINE USB_PRODUCTNAME "USBLabX1"
    DEFINE USB_SERIAL      "001"
    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  PORTD,0 ; LED indicates if USB is connected
    DEFINE USB_TXLED       PORTD,1 ;  "      "     data being sent to PC
    DEFINE USB_RXLED       PORTD,2 ;  "      "     data being received from PC
    
    ;--- Setup Port directions -------------------------------------------------
    
        TRISA = %00000000               ' Set port A pins to output
        TRISB = %00000001               ' Set port B pins to 7 output, 1 input
        TRISC = %00000000               ' Set all port C pins to output
        TRISD = %00000000               ' Set all port D pins to output
        TRISE = %00000000               ' Set all port E pins to output
    
    ;--- Variables -------------------------------------------------------------
    DummyByte   VAR BYTE                ' Was used for analog logic by original demo
    WS_BYTE     VAR BYTE
    WS_LOOP     VAR WORD
    
    ;--- Initialize ------------------------------------------------------------
        ADCON1 = %00001111      ; Digital
        INTCON2 = %00000000     ; I need this for keypad on Lab X1, pull-up resistors
    
        PORTB = %00000000       ; I need this for keypad on Lab X1
                                ; Port B.0 is set high from output of Port B.4
    
        PORTD.3 = 0                ' Clear LED
        
    ;--- The Main Loop ---------------------------------------------------------
    Main:
        FOR WS_LOOP = 0 to 250          ' Very basic debounce for keypress
    @       ON_USBRX_GOSUB  _HandleRX
            PAUSE 1
        NEXT WS_LOOP
    
        IF PortB.0 = 0 then
            ARRAYWRITE USBTXBuffer, ["Button  "]
            IF Plugged THEN GOSUB DoUSBOut  ; only send when USB is connected
        ENDIF
    
        GOTO Main
    end
    
    ;---- Receive incoming data PORTB LEDS and CCP PWM dutycycle ---------------
    HandleRX:
        ARRAYREAD  USBRXBuffer,[WS_BYTE, DummyByte, DummyByte, DummyByte, DummyByte]
        TOGGLE PORTD.3                          ' Turn LED ON/OFF
    return
    Robert
    Last edited by Demon; - 4th October 2016 at 17:24.

  7. #7
    Join Date
    Sep 2009
    Location
    South Wales (UK)
    Posts
    62

    Default Re: Lab X USB

    Many thanks for these examples; some good line examples on receiving an array of variables. Just what I need.

    I think I'm going to get a book on USB so I get more of a background...

    Best regards

    Jimbo

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