MEL PICBASIC Forum - USB CDC Communications for Dummies!


  • USB CDC Communications for Dummies!

    Want to communicate with your PIC via USB?
    Want to install a virtual Comm port on your PC?
    Want to send serial commands to your PIC?

    Its REALLY easy! Here's how.

    Get yourself a PIC18F2455/2550/4455 chips that has USB support. And connect the Usb to to your PIC as shown below.

    Note if using the USB bus power to power your pic remember that the max current your pic circuit can draw is 100ma! Anymore and you may blow your usb port! If in doubt stick in a fuse, or use external power.

    For this demo though you can safely use the Bus Power. Mecanique has a good circuit diagram you can follow here. http://www.mecanique.co.uk/products/...atic-large.gif

    Pin Name Cable color Description
    1 VCC Red +5 VDC (Vdd)
    2 D- White Data - (RC4)
    3 D+ Green Data + (RC5)
    4 GND Black Ground (Vss)

    Once your hardware is setup follow on....

    STEP ONE.

    In your PBP folder, copy the entire contents of the USB18 folder into a new folder where you store your pic programs;

    eg... copy c:\pbp\usb18\*.* c:\cdcdemo

    STEP TWO.

    Open the USB descriptors file (called USBDESC.asm)
    There are three lines of code in there, change the ; so that CDCDESC.ASM is used.

    Code:
    ;    include "MOUSDESC.ASM"        ; USB descriptors for mouse demo
    ;    include "JADESC.ASM"        ; USB descriptors for Jan Axelson's demo
        include "CDCDESC.ASM"        ; USB descriptors for CDC demo
    STEP THREE.

    Copy this code and save it into the c:\cdcdemo folder and then compile using pbp.

    Code:
    DEFINE    OSC 48
    
    Buffer    VAR BYTE[16]
    Cnt       VAR BYTE
    B0        VAR BYTE
    B1        VAR BYTE
    
    ADCON1 = 15               ' Set all I/Os to Digital      
    CMCON = 7                 ' Disable Comparators
    Cnt = 16
    
    USBInit                   ' Initialize USART
    
    for b0 = 0 to 15
        lookup b0,["USB CONNECTED!",10,13],B1 
        BUFFER(B0) = B1
        NEXT B0
    
    ' Main Program Loop
    Loop:
        USBService        ' Must service USB regularly
        USBOut 3, Buffer, Cnt, loop
        goto loop
    end
    When you compile the above program, PBP fuses the USB18 files in youe cdcdemo folder to generate the neccessary .HEX file. Viola!!
    run your progam.

    Windows will detect the PIC as a USB device and install the Microchip CDC driver.

    STEP FOUR.

    Open up Hyperteminal and select the virtual comport that was just installed. you should see USB CONNECTED! repeated.

    Congratulations, you can send data from the PIC to the PC via a quasi Serial connection.

    Ok How about sending and receiving data?

    Program this code below into your pic:

    Code:
    buffer    Var    Byte[16]
    cnt    Var    Byte
    LED    Var    PORTB.0
    
    Define  OSC     48
    
        USBInit
        Low LED        ' LED off
    
    ' Wait for USB input
    idleloop:
        USBService    ' Must service USB regularly
        cnt = 1    ' Specify input buffer size
        USBIn 3, buffer, cnt, idleloop
    
    ' Message received
        Toggle LED
    
    outloop:
        USBService    ' Must service USB regularly
        USBOut 3, buffer, cnt, outloop
    
        Goto idleloop    ' Wait for next buffer
    Again open Hyperterminal and the PIC will echo back the characters that you type (one byte at a time).

    Try sending a large text file. Does it keep up?

    Wow your becoming an expert now aren't you!

    Stay Tuned....
    Squib
    This article was originally published in forum thread: USB CDC Communications for Dummies! started by Squibcakes View original post