USB CDC Communications for Dummies!


Closed Thread
Results 1 to 40 of 105

Hybrid View

  1. #1
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    257

    Talking USB CDC Communications for Dummies!

    Want to communicate with your PIC via USB?
    Want to install a virtual Commport 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

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

    Default

    Nice work Squib...;o}
    Regards,

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

  3. #3
    T.Jackson's Avatar
    T.Jackson Guest

    Thumbs up Nicely done.

    Nicely done. Worth a vote...

    It would be great to see a lot more practical examples posted. Even better if there's an electrical schematic included as well.

    Best Regards,
    Trent Jackson

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

    Default

    Steve

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

  5. #5
    Join Date
    Apr 2006
    Location
    New Hampshire USA
    Posts
    298

    Thumbs up Bravo

    Beautiful, step by step “how to” piece of art. Thank you Squib. Nicely done.
    -Adam-
    Ohm it's not just a good idea... it's the LAW !

  6. #6
    Join Date
    Jan 2007
    Posts
    70

    Default

    thanks squib!

    you are great.

  7. #7
    Join Date
    Nov 2008
    Posts
    12

    Default Help

    Hi Squipcakes

    I've followed your instructions for this post and built the schematics attached which are very similar to yours posted (see attached). But once compiled and loaded onto the chip, the chip's power led lights up but the diagnostic led's don't and the PC doesn't even see a device attached to the system. I've checked my connections and they all seem fine. I have also attached the files used to make the hex and the hex (file is the mycdc.hex). I'm using a PIC18f2450 (not the same as in the schematics but same pinning)

    What could be the problem??

    Thanks
    Attached Images Attached Images  
    Attached Files Attached Files

  8. #8
    Join Date
    Dec 2008
    Posts
    48

    Default Figures.... A Response Would Be Nice...

    Hi All. Thanks in advance for the help.

    I am in a similar situation as the respondent above. Have setup everything with an 18F2550 and when plugged in, device not recognized error appears in XP. Where are the CDC drivers mentioned? A lot of the posts above applaud the project, but when a real implementation question is asked, it gets quiet.

    Please, throw the dog a bone!

    Regards,
    Kirt

  9. #9
    Join Date
    Dec 2008
    Posts
    48

    Default VID and PID

    Okay, I have got my problems down to matching VID and PID numbers in the mchpcdc.inf file and the VID and PID numbers burned on the PIC (18F2550).

    Have looked in cdcdesc.asm, but don't know where to find these values.

    Any help?

  10. #10
    Join Date
    Dec 2008
    Posts
    48

    Default Hmmm... Still Does Not Recognize Hardware

    Well.... I figured out the PID and VID stuff... The values in the cdcdesc.asm file and the values in the inf file of the driver (CDC this time) you are using must match.

    After confirming this, XP still recognizes that a USB device is attached, but when I go to install the CDC driver, an XP dialog that says hardware information cannot be found appears.

    Any help?

  11. #11
    Join Date
    Aug 2005
    Posts
    57

    Default

    Its been awhile since I went through the same project, but I don't think you need to modify those files.The pic config settings needs to be correct though.I have attached a project folder with files I did once I had it working.Look through the the files for possible clues,primarily the .bas for the config settings.
    It uses a 4mhz crystal.
    Good luck
    Attached Files Attached Files

  12. #12
    Join Date
    Jan 2009
    Posts
    6

    Default

    arniepj, I cannot get your code working either.

    There are no config fuses set in the code, and I do not know what they are supposed to be for this application.

    I cannot fing any hints in your code as to how to get it to work.

    Any help is much appreciated.

  13. #13
    Join Date
    Nov 2009
    Location
    Australia
    Posts
    10

    Default USB Device Not Recognised

    G'Day,
    Used Mecanique cct(20M 18F4550) and PBP2.6 and after adding

    @ __CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
    @ __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
    and then enabling VREGEN config bit got the Windows USB Device Not recogised message.
    Are there any other config bits that have to be changed.
    Is it possible to get a hex file for this project so I can at lest check my cct.
    tks

  14. #14
    Join Date
    Nov 2009
    Location
    Australia
    Posts
    10

    Smile All Fixed

    G'day (again)
    My problem cured when I added a 10MFD cap on the two 18F4550 power pins.
    I was using the USB 5v to power the card.
    So can now start playing(at last) with USB.
    Grant

  15. #15

    Default nearly there...

    Windows sees a pic18 usb cdc device s inserted, but finds no drivers.

    windows 7 32 bit

  16. #16

    Default

    yes the above error was me stupidly not showing windows device mangler where the right .inf file was, my bad...

    But..

    I am now doing a ' USBIN endpoint, buffer, count, nodatatloop ' every second or so, problem is when I send data to it, I only get it one byte at a time, so reading 'cat' takes 3 seconds......

    count variable is always 1 if data present, so i get 1 three times then it returns to zero

    surely I should get all 3 bytes into my buffer and a value returned of 3 for count.

    I dont lose any chars from my text string in the terminal, i think it buffers and does some form of flow control, but when i copy a text file in a dos/command window, i do lose them, i get a count of one for the first 'C' but then a zero as 'at' have got lost before i got a chance to read them.

  17. #17

    Default

    Ok cant edit the above but it goes like this, if i send 'cat' with hyperterminal or putty i get 3 individual chars at the pic end so need to read 3 times, if i send it from a program written in vb6, i get a 'cat' in one loop/poll etc and a bytecount length of 3, as expected, hope no one else falls foul of this.

Similar Threads

  1. Simple USB Comms Problem
    By awmt102 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 6th January 2010, 20:17
  2. One USB keyboard to Two USB Ports
    By picnaut in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 11th June 2009, 00:04
  3. USB CDC help..
    By jchandir in forum USB
    Replies: 6
    Last Post: - 22nd November 2008, 21:23
  4. Replies: 4
    Last Post: - 5th November 2008, 16:21
  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 : 3

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