USBDemo, something to learn USB a little bit


Closed Thread
Results 1 to 40 of 279

Hybrid View

  1. #1
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    This is a update to Mister E's VB6 demo program, and firmware that uses Darrel's DT_HID260.pbp include file. It brings back some of the functions that went away with the HID update, and it adds a few more. It now transfers 10 bit A/D conversions to the PC, and it has a really low bandwidth mschart function. It also transfers the port info up to the computer in a single byte, and it's read by VB in bits. (It is hard to read bits with VB!) Ports are changed to match the Olimex PIC-USB-STK ( http://www.olimex.com/dev/pic-usb-stk.html ) . On the chart, it's a little over a second from left to right. By the way, if anyone knows how to keep the mschart from flickering as it is being redrawn, please let me know.

    Name:  scope.PNG
Views: 19700
Size:  41.6 KB


    VB6 doesn't make it very easy to be sure you have include all the necessary files, so if you have issues, let me know. The code was made for a PIC18F4550 with a 20 mhz crystal. You will also need Darrel's DT_HID260 file from here: http://www.picbasic.co.uk/forum/show...0434#post80434

    (Thanks Steve and Darrel for giving me the USB bug!)

    Code:
    '****************************************************************************
    '*  Name    : USB_Demo_4_HID.pbp                                            *
    '*  Author  : Walter Dunckel ScaleRobotics using Darrels example as template*
    '*  Notice  : Copyright (c) 2011                                            *
    '*  Date    : 1/13/2011                                                     *
    '*  Version : 1.1                                                           *
    '*  Notes   : Added Darrel's text example                                   *
    '*          :                                                               *
    '****************************************************************************
    ;--- if you un-comment these, you must comment the ones in the .inc file ---
    ASM  ; 18F2550/4550, 20mhz crystal
       __CONFIG    _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
       __CONFIG    _CONFIG1H, _FOSC_HSPLL_HS_1H
       __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
       __CONFIG    _CONFIG3H, _PBADEN_OFF_3H
       __CONFIG    _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
    ENDASM
    'add if using Microchip's HID Bootloader
    'DEFINE LOADER_USED 1
    'DEFINE RESET_ORG 1000h ' For Microchip USB Bootloader
     
    ;ASM  ; 18F13K50/14K50  Only 12mhz crystal can be used for USB
    ;    __CONFIG    _CONFIG1L, _CPUDIV_NOCLKDIV_1L & _USBDIV_OFF_1L
    ;    __CONFIG    _CONFIG1H, _FOSC_HS_1H & _PLLEN_ON_1H & _PCLKEN_ON_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
    ;    
    ;    ;added
    ;    __CONFIG    _CONFIG2H, _WDTEN_ON_2H & _WDTPS_512_2H
    ;    __CONFIG    _CONFIG3H, _MCLRE_ON_3H 
    ;    __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _BBSIZ_OFF_4L & _XINST_OFF_4L
    ;ENDASM
     
    DEFINE OSC 48
    clear
     
    PortA = %00000000
    TrisA = %00000001
    PortB = %00000000
    TrisB = %11110100        'make pushbuttons inputs for PIC-USB-STK
    'TrisB = %00010000 'for 18k50
    PortC = %00000000
    TrisC = %00000000
    TrisD = %00000000
    CMCON = 7
    'CM1CON0 = 7  'for 18f14k50
    'CM2CON0 = 7
     
            '   CCP/PWM
            '   -------
    CCP1CON =   %00001100       ' CCP1, PWM mode
    CCP2CON =   %00001100       ' CCP2, PWM mode
    PR2     =   249             ' 12khz PWM Freq
    T2CON   =   %00000101       ' TMR2 on, prescaler 1:4
    ;--- 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  "ScaleRobotics"
    DEFINE USB_PRODUCTNAME "Cheap-Scope"
    DEFINE USB_SERIAL      "001"
    DEFINE USB_INSIZE      8   ;was 32  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 LED is optional, comment them if not used ----
    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,2 ;  "      "     data being sent to PC
    'DEFINE USB_RXLED       PORTD,1 ;  "      "     data being received from PC
     
    OutCount    VAR  WORD : OutCount = 0
    Value       VAR  word   'analog result from AN0
    Value2      var  word   'analog result from AN11
    X           VAR  WORD
    counter     var  byte
    Duty1       var  word EXT ' make them available 
    Duty2       var  byte EXT
    smallvalue  var  byte
     
    ;--- Setup ADC -------------------------------------------------------------
    DEFINE ADC_BITS 10 ; Number of bits in ADCIN result
    ADCON2.7 = 1       ; right justify    (Change this if ADFM in diff register)
    'ANSEL = %00000000  'for 18f14k50
    'ANSELH = %00000100  'for 18f14k50
    ADCON0 = %00000001 ' turn on a/d   AN0
     
    ;--- The Main Loop ---------------------------------------------------------
    pause 2000                               'wait for connection
    ARRAYWRITE USBTXBuffer, ["USB Demo"]    'show text example
    GOSUB WaitToSend  
     
    Main:
        FOR X = 0 to 2           ; Check for incomming USB data while pausing
    @     ON_USBRX_GOSUB  _HandleRX
          PAUSE 1
        NEXT X
        ADCin 8, value2
    @Duty1=_USBRXBuffer+1 
    @Duty2=_USBRXBuffer+3 
        counter = counter +1 
        ADCIN 0, Value
        ARRAYWRITE USBTXBuffer,[portb,counter,value.highbyte,value.lowbyte,Value2.highbyte,Value2.lowbyte,0,0]
        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 Duty1 
        CCPR2L=DUTY2>>2 
        portd = usbrxbuffer[0]
        'IF Plugged THEN GOSUB DoUSBOut     ; only send when USB is connected
        gosub SendIfReady
    GOTO Main
     
    ;---- This just sends the received packet back to the PC -------------------
    HandleRX:
        ARRAYWRITE USBTXBuffer,[STR USBRXBuffer\USBBufferSizeRX]
    return
     
    SendIfReady:
        IF Plugged AND TX_READY THEN DoUSBOut
    RETURN
     
    WaitToSend:
        WHILE Plugged and !TX_READY : WEND
        IF TX_READY THEN GOSUB DoUSBOut
    RETURN
    Edit:
    Ok, now looking at Darrel's post 221 http://www.picbasic.co.uk/forum/show...0611#post80611 , he already made the program compatible with most of the original functions for the 18F4550. So I (also) lifted his code to transfer text to the PC. However, I only do this at power up, so I don't slow the analog transfers down.
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by ScaleRobotics; - 2nd May 2011 at 01:30. Reason: Added code in code tags
    http://www.scalerobotics.com

  2. #2
    Join Date
    Sep 2009
    Posts
    755


    Did you find this post helpful? Yes | No

    Default

    FormMain.frm is missing...

  3. #3
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by pedja089 View Post
    FormMain.frm is missing...
    Thanks pedja089, I just added it.
    http://www.scalerobotics.com

  4. #4
    Join Date
    Sep 2009
    Posts
    755


    Did you find this post helpful? Yes | No

    Default

    Excellent project!
    Not too complicated, but there are interesting examples of communication and usage of DT's include files.
    Perhaps you should add the LCD.
    Here is my example how to sendtext from winamp to lcd:
    http://www.elektronika.ba/506/lcd-winamp-plugin-on-usb/
    I hope that you will manage the translation...

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


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default

    5 years later and this thread always evolve and got some more user in the "USB freaks" loops. Glad to see that. Congrats to all!

    Now I wish I have time to revamp it and share some part of what i've done for .NET (C# & VB) ...VB6 being a dead duck for ages now... and the .dll not that suitable now (but still workable) and then on the top of it, new PBP version

    Helm PCB (Amr Bekhit) did a good work on my first .NET version though http://helmpcb.com/?p=166
    Steve

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

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


    Did you find this post helpful? Yes | No

    Default Re: USBDemo, something to learn USB a little bit

    Reading through this it looks like there was a sample of code were the 18F2550 was using an external resonator at 20mhz using the HS fuse. Looking at the datasheet, this would only enable low speed USB, right?

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


    Did you find this post helpful? Yes | No

    Default Re: USBDemo, something to learn USB a little bit

    With those chips ... you can get Full Speed with any crystal that's a multiple of 4Mhz (up to 24Mhz).
    And the processor can run at 48Mhz too.

    They have a really nice Oscillator/PLL.
    Adjusting the Configuration bits is a little tricky though.
    DT

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. How to receive stream of bytes using PIC USART
    By unifoxz in forum mel PIC BASIC Pro
    Replies: 34
    Last Post: - 20th June 2009, 10:38
  3. Replies: 9
    Last Post: - 31st July 2008, 08:56
  4. PICBasic newbie problem
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 12th February 2008, 00:55
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

Members who have read this thread : 1

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

Tags for this Thread

Posting Permissions

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