PIC18F and USB - Confused

Thread Starter

deme

Joined Mar 24, 2014
11
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!
 

takao21203

Joined Apr 28, 2012
3,702
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!
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.
 

Thread Starter

deme

Joined Mar 24, 2014
11
Hard to say without knowing the 18F PIC you use.
...
The softwares are not really bloated, you can choose interrupt or polling.
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.

I used 18f14k50 and adapted it for 18f13k50. Wasnt a big issue at all.
Is this in XC8?

Thanks
 

THE_RB

Joined Feb 11, 2008
5,438
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.
...
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.
 

hexreader

Joined Apr 16, 2011
581
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.
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.
 

Thread Starter

deme

Joined Mar 24, 2014
11
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.
:) 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.
 

John P

Joined Oct 14, 2008
2,026
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).
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.
 

takao21203

Joined Apr 28, 2012
3,702
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.
 

Thread Starter

deme

Joined Mar 24, 2014
11
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.
 

ErnieM

Joined Apr 24, 2011
8,377
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:

Rich (BB code):
 *****************************************************************************/
#if defined(__18CXX)
void main(void)
#else
int main(void)
#endif
{   
    InitializeSystem();

    #if defined(USB_INTERRUPT)
        USBDeviceAttach();
    #endif

    while(1)
    {
        #if defined(USB_POLLING)
        // Check bus status and service USB interrupts.
        USBDeviceTasks(); // Interrupt or polling method.
        #endif
                      

        // Application-specific tasks.
        // Application related code may be added here, or in the ProcessIO() function.
        ProcessIO();        
    }//end while
}//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.
 

fernan82

Joined Apr 19, 2014
26
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.
 

embpic

Joined May 29, 2013
189
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.
 

Thread Starter

deme

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

Thread Starter

deme

Joined Mar 24, 2014
11
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.
 

josip

Joined Mar 6, 2014
67
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...



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.
 

THE_RB

Joined Feb 11, 2008
5,438
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.
 
Top