PIC18F and USB - Confused

Discussion in 'Embedded Systems and Microcontrollers' started by deme, Mar 24, 2014.

  1. deme

    Thread Starter New Member

    Mar 24, 2014
    11
    1
    Hello everyone,

    I am trying to build a minimal setup for testing out USB capabilities of PICs. I have worked with PICs before but never with hardware USB.

    Unfortunately there are few tutorials on the internet and the fact that Microchip's libraries change every few months makes some of them obsolete.

    I downloaded MAL and loaded some of the usb projects in MPLABX. First of all those projects are too bloated for me to understand. Also they are targeted towards specific demo boards and not individual microcotnrollers.

    I have been trying to assemble a minimal project based on the CDC project of the libraries, for sending "Hello World!" in the terminal but I am still having problems.

    Do you have any advice on how to build this minimal project? Which are the essential parts that have to be included in the project?

    Thank you!
     
  2. nigelwright7557

    Senior Member

    May 10, 2008
    487
    71
    Try www.waitingforfriday.com

    He has a pic18f4550 project that you just download and he provides a circuit too.
    I tried it and it just worked first time.
     
  3. deme

    Thread Starter New Member

    Mar 24, 2014
    11
    1
  4. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Hard to say without knowing the 18F PIC you use.

    Yes the software is made for demo board. If it is the same PIC, then good. you only need to adapt it- remove things you dont need and your circuit does not have. Simply comment out.

    The softwares are not really bloated, you can choose interrupt or polling.

    I used 18f14k50 and adapted it for 18f13k50. Wasnt a big issue at all.
     
  5. deme

    Thread Starter New Member

    Mar 24, 2014
    11
    1
    I have an 18LF14K50 and an 18F24J50.

    You may be right, the word "bloated" is a bit strong. I just got a bit frustrated with all the 'includes' and libraries I guess! :) . I think that many of them are just decals. It would be great if we could use USB just as easy as we use the rest of the peripherals in our code.

    Is this in XC8?

    Thanks
     
  6. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Do what the old hands do. Buy a heap of USB->TTL converter modules from ebay for under $2 each, then just connect two simple wires to the PIC; USART TX and USART RX.

    Then you can do USB comms with any PIC effortlessly (provided it has a USART).

    It also means if a static spike or something kills the USB module the rest of your project is safe, only the $2 module gets trashed.

    You can pick them up on ebay in quantities of 10 or 20 at a time, with free shipping.
     
  7. hexreader

    Active Member

    Apr 16, 2011
    250
    82
    Just to be clear - in case any newbies are reading. Three wires are needed, Tx, Rx and 0v.

    For an old hand like RB, the need for a 0v common connection is a "given" that is too obvious to mention. For a newbie, the need for a common connection is best explicitly stated.
     
    THE_RB likes this.
  8. deme

    Thread Starter New Member

    Mar 24, 2014
    11
    1
    :) I like you approach and it is a solid advice. Actually I do have lots of CP2102 and FTDIs laying around and use them a lot!

    However, it would be nice if I could build my circuits with one less IC and its components. Funny thing is that I started looking into PIC USB firmware when I realised how difficult is going to be to solder that damn QFN package on my latest dev board I am building :D.
     
  9. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    However, it's exceptional for microcontrollers to have two UARTs, so if you're setting up something which requires serial communication and also has to connect separately to the computer, you've got a problem. A single chip solution for that situation would be very nice to have, and if there's USB right there in the processor, it's very attractive to think of using it. I just wish someone would explain how to do it for any processor with the interface, and make it as easy to do as using a UART--including the computer side of the link.
     
  10. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    it is really time to make a demo circuit with the new 16f1455.

    No more USB/serial converters.

    By the way I am not sure if the program for 18f13k50 was using C18 or XC8.

    Yes all the includes are frustrating but the stuff can be left alone and it is just working.

    it can't be totally simple since enumeration is happening on the host side, and enumeration is not just built in, it must be done actively on the controller side.

    then you have different modes as well.
     
  11. deme

    Thread Starter New Member

    Mar 24, 2014
    11
    1
    For now I have to wait for the 12MHz crystals to arrive. When this happens I will work to get a minimalistic project running and will post it here.

    Another thing that I try to figure out is why there are no software implementations of USB for PICs like the V-USB for AVRs. Personally I would find it very useful.
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    There's no need to start adding chips because "USB code is hard." I understand that a plethera of files are included in the USB example programs. But that doesn't keep you from browsing them.

    Using the demo for Communications Device Class (ie, a serial emulator) "CDC - Serial Emulator - {pick a flavor of dev board here}"

    ALL C programs start from main(), so start there:

    Code ( (Unknown Language)):
    1.  *****************************************************************************/
    2. #if defined(__18CXX)
    3. void main(void)
    4. #else
    5. int main(void)
    6. #endif
    7. {  
    8.     InitializeSystem();
    9.  
    10.     #if defined(USB_INTERRUPT)
    11.         USBDeviceAttach();
    12.     #endif
    13.  
    14.     while(1)
    15.     {
    16.         #if defined(USB_POLLING)
    17.         // Check bus status and service USB interrupts.
    18.         USBDeviceTasks(); // Interrupt or polling method.
    19.         #endif
    20.                      
    21.  
    22.         // Application-specific tasks.
    23.         // Application related code may be added here, or in the ProcessIO() function.
    24.         ProcessIO();        
    25.     }//end while
    26. }//end main
    I clipped out a long comment (that you should read anyway) but all you have is at most 4 function calls. InitializeSystem() and ProcessIO() are the ones to inspect.

    ProcessIO() is a bit complex but you should be able to see where it gets info from the UART and passes it to the USB, and vice-versa.

    I've made more then 1 program starting from the example and grafting my code into ProcessIO()

    One day you may with to change the HID & VID or make it say YOUR name on install, and then you can hit the other files where that is defined.
     
    deme likes this.
  13. fernan82

    New Member

    Apr 19, 2014
    26
    9
    I've used the USB CDC with a custom board and it wasn't that bad. IIRC in the config header there was a define for CUSTOM_BOARD or something like that, uncomment that line then on the main file you need to write the initialization code for your device or just use the code for the part that's closest to yours and modified as needed. Then you can use the library functions pretty much the same as the UART ones.
     
  14. embpic

    Member

    May 29, 2013
    187
    3
    i would like to suggest one thing is that use 18f4550 pic. bcoz as you need VCOM it works without doing any change in code.
    and require C18 compiler.just compile and use it you.
     
  15. deme

    Thread Starter New Member

    Mar 24, 2014
    11
    1
    Sorry for not updating but the 12Mhz crystals haven't arrived yet (slow post from China), so I can't test my setup.
     
  16. deme

    Thread Starter New Member

    Mar 24, 2014
    11
    1
    Just a small update. The 12Mhz crystals arrived and I was able to check the circuit. I used the standard Microchip CDC example for starters. The ready MPLABX project includes configuration for the PIC18LF14K50 that I am testing. However, it seems to be for a development platform so I had to change few configuration bits to match the required clock frequency.

    The result is below!
    usb_success.png

    Now that I know the circuit works, I can start removing the unnecessary parts from the USB code.
     
    ErnieM likes this.
  17. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    Congratulations, good work!
     
  18. josip

    Member

    Mar 6, 2014
    63
    12
    BTW, TI MSP430F5xx devices have USB hardware module, and there is no extra parts for implementing USB on-board. Minus is that are not DIY friendly because of small package, but it can be done. Here is my MSP430F5510 USB DIY p2p board...

    [​IMG]

    USB is not rocket science, and I made my own USB assembler stack. There is also TI open source stack. CDC/Bulk rate is up to 1 MByte/s.

    And don't see any reason for using separate USB-UART bridge chips if they cost more than USB microcontroller itself.
     
  19. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Wow! Not only a good coder but you also seem to be a master of needlepoint and origami! :eek:
    ;)

    That's impressive. I would have just bought a $6 SMD adapter PCB.
     
  20. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    Same here, in fact I just bought two of these, but for about a third what you paid.

    We have to get you a better vendor. ;)
     
Loading...