Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18F4550 and USB - help needed

Status
Not open for further replies.

jumper2high

Full Member level 3
Joined
May 24, 2009
Messages
178
Helped
20
Reputation
40
Reaction score
10
Trophy points
1,298
Location
Serbia
Activity points
2,579
pic18f tutorial

Hello Folks,
I finally laid my hands on a couple of 18Fs that I wanted to try out with USB. I've done all the typical stuff, got the datasheet, went through the basics, still getting used to the slightly different memory organization (compared to 16Fs), and more freedom :).

Anyway, I've searched all around for a USB tutorial specific to that series of MCUs, and I haven't found almost anything. I already went through the "USB" chapter of the datasheet, and I understand that part, but it really doesn't say much on how to actually "communicate". For example, when working with PWM, you set all the registers the way you want them, and turn on the module by setting a bit - and it does it's thing the way you set it. This doesn't seem to be the case with USB, it seems - because you set some things, you enable the module...and what then? The Host and the device are supposed to have a little chat about who is who and what is what, and then what happens? How do I make the MCU actually send something out?

P.S. I'm quite good with C (done many programs for Windows and Linux using C) but I've never really liked C for MCUs, I don't know why. Instead, I've either done it in ASM or Basic (had a neat little simulator specifically designed for these PICs and it had a nifty little Basic compiler that had several important things built in as procedures/macros). So, when I go through the example C code from Microchip website, I really don't get anything...mainly because I'd have to look at 16 different sources and headers just to understand what is being done to "initialize" the USB module.


Help is greatly appreciated :)




EDIT: I've changed the topic title to avoid confusion.
 

pic usb tutorial

I can hardly imagine, that you'll learn USB basics from a PIC datasheet or Microsoft driver literature. There are good tutorials and books, e.g. Jan Axelsons USB complete, see **broken link removed** for this and other references. Also the official USB 2.0 standard from usb.org is basically a better literature.

For a more practical start, most PIC toolchains, e.g. Microchip, CCS come with ready to use USB example applications. You don't need to start your application from the scratch, although this can be instructive as well.

Personally, I don't see a particular eternal struggle in the USB field. You should know, what you want to do, check if it's basically feasible (you can't e.g. interface an USB memory stick with 18F4550) and start writing your application. You shouldn't let you deter from edaboard posts, that report failure of their USB applications. There are also many examples, that even failed to blink a LED.
 

pic18f4550 tutorial

Thanks for the reply. Maybe the topic title wasn't quite appropriate (I'm not a native English speaker), but I really searched for a while now (three weeks while waiting for the MCUs to arrive) and I wasn't able to find a single tutorial for PIC18F-s regarding USB.

I got a "USB Complete" book by Jan Axelson, and I went through the first four chapters. It did give me some of the information I was looking for, but I'm still stuck with the actual firmware bit. Since I'm "learning" this for a reason (freetime project in mind) I will have to start writing my own firmware sooner or later, so I guess it's better to start from scratch right away, rather than using something pre-made.

As I said earlier, I do have a basic understanding how USB works, but I don't know how to do that with the PIC. For example, I don't know how would I make the MCU respond to the Host's interrogation, or how to send the desired data to the host.
 

short explanation on what a usb is all about

I just can say, that the standard USB example projects (e.g. CDC or HID) supplied with Microchip or CCS tools are basically working.

To design it on your own, you have to understand of course the different layers of USB communication. It's not bad to start at the physical layer respectively bit level, continue with request and response packets, in and out transactions, enumeration, data transfer and device class specific stuff.
 
pic18f2550 usb tutorial

I'll take a closer look at those when I have a chance.

I think the biggest issue I'm facing right now is actually caused by the "example code" versatility. Most of them are written so that they can be burned on different devices, with different circuits, or with different conditions - that makes them large, and for me at least, harder to understand.

That's why I was looking for something that was specific to the PIC18F series, or even better for the PIC18F4550 itself.


For the sake of discussion, let's say the device gets properly recognized and identified, and I want to send a few bytes from the device to the PC. Also, assuming the PC has the correct drivers and software to catch those bytes, could someone explain the process (in short, without spending too much time on details) of how to prepare the data (where to put them in the memory) and making the device actually transmit them outwards?
 

codes for usb enumeration of pic18f4550

Sorry for double posting, but I got a few things to add:

I've spent all day yesterday and most of the day today (almost 7PM at the moment) trying to make anything work with USB. I've started with the stuff that I got from Microchip. After a lot of time I spent actually setting up all the paths, and finally managing to compile the HID - Mouse Demo, I whacked it onto the PIC, and nothing happened. The PIC is connected in a circuit near-identical to the one found on the Microchip Development board schematic. I then tried the Bootloader that I found in the same package. Nothing happened there either.

I then tried examples from the CCS library (Drivers) but I wasn't able to get anything working since it's all stuffed in one folder with no explanation on what is what.

Afterwards, I tried a few examples that came with MikroC, with similar results - but this time, Windows actually acknowledged a device being connected, but failed to recognize it. Using the Microchip's HID USB drivers (as suggested by the readme) didn't help either, because the device was just dubbed "Unknown Device".

I then found, by accident, a British (UK) website that had a USB Demo written for PIC18F4550 (the MCU I'm using). I downloaded it, I connected up the circuit exactly as described on the article, compiled the source using MPLAB, but the configuration bits weren't "set" according to the programming software (WinPIC) so I set them manually in there. Once I programmed the device and plugged it in, nothing happened once again.

After the previous failed attempt, now slightly losing hope, I tried the Bootloader from the same british website. I downloaded the pre-compiled HEX file, and followed the instructions step by step, connecting up the circuit as described (the guide actually said to connect it up the same way it was in the previous demo (previous paragraph), and then said "Reset the device while holding SW2"...the only problem is that the circuit that was given had no switches in it, at all.

(I apologize for the really long post)

After several other failed attempts with various sources (I won't waste your time describing them all to you) I figured out some other "basics" that seem to be used for most of them, so at least SOME good came out of this, but I still haven't managed to get anything working.


So, a revised question: Does anyone know a USB/PIC18F4550 based tutorial/demo that actually works out of the box, or contains instructions on how to make it work properly? really tried hard to find one, but only ended up with a slightly elevated blood pressure. :|
 

usb pic18f tutorial

For the requested short explanation:

USB communication is basically organized on in endpoints which are independent channels. They are defined in the device descriptor data structure, that is read during device enumeration by the host, in other words, when you plug in the device.

Sending to the host involves writing the data to a buffer and tell the USB controller, that they are ready for transmission.

Regarding CCS examples, I just can say that ex_usb_serial work for me on a 18F4550, and also for other CCS users, as far as I'm aware of.
 
use usb communication with pic18f4550

FvM, once again, thank you very much for the answer!


FvM said:
...tell the USB controller, that they are ready for transmission...

Yes, but how? :) That's what I'm asking. How do you tell the Controller they are ready?
 

libusb pic18f

hi,jumper2high
do you familiar with USB Protocol ?
if not,learn it ,or you will not get 18f4550 work...

maybe this file will be useful,build it with MPLAB C30
 

pic18f cdc usb application note

Keijan,
I have read a few books about the USB protocol, so I think I have sufficient base-knowledge to get the examples to work.

Also, I'm using PIC18F devices.
 

pic18f usb tutorial

jumper2high said:
So, a revised question: Does anyone know a USB/PIC18F4550 based tutorial/demo that actually works out of the box, or contains instructions on how to make it work properly? really tried hard to find one, but only ended up with a slightly elevated blood pressure. :|

Hi, your posts are indeed too long!!! I didn't read everything, that way I'm asking you this: 1. do you got a custum board with the PIC18F4550?
2. if you got a Microchip board (PICDEM FS....) then you can use the demos
installed with the framework 2.4 for example. They are all working.
3. if you got a custom board, is it enumerating? if it's enumerating, which
demo did you used (HID, CDC...)?
4.if you use the Generic HID demo,then you can add your code in
PROCESSIO() and tell the microcontroller do everything you want.
Ok, I stop here, if I don't want my post to be as long as yours. :D:D

Best regards
Kabanga
 

program to configure pic18f4550 as usb device

Haha, thanks for the info. I managed to get it working, I was using a custom "test circuit" and I neglected to add a capacitor on Vusb. Once I did that, a few examples worked for me, including the Microchip's spinning mouse.


Now, since that is working quite nicely (I know enough data to customize the code for what I need), all I need to figure out is how to write the PC application to handle the communication between the PIC and the data source (through IPC).

I tried using libusb-win32, and tried the example code that came with it, but I didn't get far. It seems the supplied functions don't return anything (should return a list of devices on a buss but they just return NULL). Any hints?

I'm trying to avoid working with Visual C++ and Microsoft DDK/WDK if possible.
 

pic18f cdc

I'd be quite happy to hear what you have to say :)
 

pic18f4550 usb example

you can use microchip USB frame work from microchip,they are ready made USB firmwares for many PIC families and a firmware for PC18F4550 is available:
**broken link removed**

start with CDC firmware, it will be easy in interfacing from the PC side.

good luck :)
 

Hey all..
I want to control a dc motor using PIC18F4550 and PC.
There should be a usb connection between PC and controller PIC18F4550.
I want to make a GUI using visual c++ to control the motor speed and direction.
I dont know anything about usb communication.
I know only the assembly programming for PIC18 family.
I can work easily with ADC, CCP/ECCP, PWM, sensor interfacing,motor interfacing and stuff like that.
But I dont have any idea how to interface PIC18F4550 with PC using usb communication.
Is there any tutorial or guide regarding this...or if any one can help me regarding this please reply...\][/youtube][/GVideo][/quote][/code]
 

Hi, am using 18f4550 and trying to establish USB communication by using CDC code provided by microchip. My circuit is correct my 2 LEDs are blinking, but once USB connected to system it is showing as unrecognised device.Kindly provide me the solution.
 

-Make sure the D+ and D- are not switched with each other.
-Connect the 470 nf capacitor on pin 18 (Vusb).
-Don't forget the pull up resistor on pin 1 (MCLR pin).
-make sure the crystal you are using is 20 MHz or you already modified the code to the crystal you are using.

These are the most hardware problems you may face with PIC18F4550 for USB applications.

Also make sure to use decoupling capacitor 100 uf between power supply terminal and the ground.
Faced this problem few minutes agon :)
 

go to www.lvr.com
look for a HID device example there.
He has posted there Visual C# code and PIC18F4550 code.
I have used his example for a product. Unless you have to handle huge payload of data, HID is ok.
Regards

added later
My views (personal)
Almost all USB examples based on PIC18, I found are based on Microchips original USB stack - Which is really a mess! They have packed it with defines, and all sorts of typedefs. Out of two reasons. One to accommodate all PIC family of devices with a single stack. The other reason is they are developing the firmware using the perspective of a windows programmer. Any way all those are more than sufficient to drive a conventional MCU programmer crazy. Many of the functionalities can be avoided to make a simple device. But that is quiet understandable because as this is a vendor provided code, all features to be present.

What I did:-
Read 'USB complete' from lvr.com (To be frank , I am a fan of him!)
Locate an example- I choose the above mentioned one which is a adaptation of Microchip stack , Microchip USB download package also has some examples but the windows code is in Visual C++;

Get a full print out of the entire code both firmware and windows application seperately. That will be reasonably sized books because of the code layout. But it is worth (must) having a hard copy to refer back and forth.
Trap yourself in a closed room allotting at least 4 days(!) (For me).Trace the code sequence, how the functions are called, when etc. The main problem is because of the timing constraints imposed by USB protocol , the code is implemented as a state machine which branches into different routes on different calls. And many functions are scattered in different files. Really you will fed up!. Dont give up.Write the sequences down and Analise again. slowly things will get clearer.
If Microchip made things simpler to a level that your mother can do USB in PIC easily, then you are out of job today :D (Courtesy VTC tutorial on Photoshop)

After you got hold of the application track find insertion points where you can insert your tasks.

Another problem is USB stack is capable of supporting different types of transfers,- control, feature, interrupt etc. Another reason to confuse us. May be simple applications can manage with only one type after enumeration is over.

Unless huge amount of data to be transfered or any other commercial issues which prevents the use of a Device class for which windows has a default driver, better to select a HID device. Just plug - no driver nothing. (May be other classes also suitable ,but I did it as HID - so as usual it is the best!!!)

Thanks Hope this helps.
PS. The most important step in the above described method which worked for me is the print out. It is that much important. :D
Opinions are Opinions only , but still if you find any of my observations are wrong -please respond - so that I can update it. There is no use in keeping a wrong opinion.

Regards
 

Frankly speaking, don't agree with "Picstudent", actually the Microchip USB stack is very nice and so far organized, you can't blame them cause they don't write codes like IBM or Google guys :)

the CDC firmware will set your first step inside the USB world without much difficulties or spending 4 days in your room (which I did one day), in the mean while you won't face problems when interfacing from the PC side, you will treat your microcontroller as a normal "COM" port, which is easy to communicate with using any programming language you know.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top