People who are working with micro controller are very familiar with computer to device communication with serial port. It is easy and simple to use. But now serial port are vanishing day by day.Most of the laptop hasn't serial port. Usb is widely used now. Usb needs only four pins. So it seems good to use but needs to know how! My goal in this post to make easier to communicate with pc to device through usb.No need to know too much about usb. We make a short path to make windows program and pic project files.Here we will use pic18f2550 and visual c++. First a outline of what we have to do
Now make the connections as I showed in the diagram. Don’t forget to add a 20MHz oscillator at osc inputs.
Parts needed(very short list)
People who have no idea about windows programming(I also have no idea) is requested to be easy. Don't be afraid.
This program is given by microchip. You can download it from the link here.
After download install it. Don't change default directory.
After completing installation go to c drive. You can see a new folder folder Microchip Solutions v2012-10-15.
Then go to C:\Microchip Solutions v2012-10-15\USB\Device - HID - Custom Demos\Simple Demo - Windows Software\Microsoft Visual C++ 2005 Express.
This is the project folder, open it. Run GenericHIDSimpleDemo.vcxproj in visual studio.
If you use vs 2010 or 2012 or 2008 no matter, it will convert automatically to your version.
Build it then you can get the app.
the app also given in the folder as GenericHIDSimpleDemo in C:\Microchip Solutions v2012-10-15\USB\Device - HID - Custom Demos.
About the app—
Mikroc has powerful built in library function which is very easy to use. We will use such a library function named usb hid library.
Actually we have to do little works as everything is given. In example folder you may get a example project HID Read Write Interrupt. Open it from example folder and now we have to modify it as our pc host app.
Actually we have to do little works as everything is given. In example folder you may get a example project HID Read Write Interrupt. Open it and now we have to modify it as our pc host app.
Look at the program. When Toggle Led(s) button is pressed it sends a data 0x80 to readbuff[0] and in the program a simple if command checks the
Data. If true then lcd shows “Usb device”. You can use a led to get the same output just place aled to port b and on it when the Toggle Led(s) button is pressed.
The other Get Pushbuttion state is not used here.
Before it another work we skipped is to do now that is usb descriptor file. In this HID Read Write Interrupt example usb descriptor file is included.
Open it and change the product id as 04d8 vendor id as 003F as shown in the figure.
If you do not change this then pc application can not connect (when you press connect buttion) the device as it will search for a device with these ids.
Now go to edit project and make it same as shown in figure.
Then build it and pick up the hex file and load it to pic18f2550.
Make all the connections as circuit diagram.
Connect the device to the computer. Open the app GenericHIDSimpleDemo.exe.
Press the connect button. If everything is ok then other two buttons will active.
Press Toggle Led(s) button. If you use led then it will on. If you use lcd as my program then lcd will show “PIC Usb device”.
CAUTION!!!
Download the zip file. Also the pc software is uploaded in DOWNLOAD section.
I also upload a very nice book about usb, usb complete 3rd edition.
Now enjoy. any problem then just put a comment. i will try to give feedback.
- Circuit diagram to make connections
- A host(pc) software
- A pic c program file which exerts hex file to load in pic microcontroller.
Now make the connections as I showed in the diagram. Don’t forget to add a 20MHz oscillator at osc inputs.
Parts needed(very short list)
- Pic 18f2550 microcontroller
- 20MHz oscillator
- Led,switch button
- Usb connector ( I made it from a broken mouse)
- Capacitor 22uf( I used without no problem as microchip recommends 270nF).
- 1k resistor(between D+ and Vcc)
People who have no idea about windows programming(I also have no idea) is requested to be easy. Don't be afraid.
This program is given by microchip. You can download it from the link here.
After download install it. Don't change default directory.
After completing installation go to c drive. You can see a new folder folder Microchip Solutions v2012-10-15.
Then go to C:\Microchip Solutions v2012-10-15\USB\Device - HID - Custom Demos\Simple Demo - Windows Software\Microsoft Visual C++ 2005 Express.
This is the project folder, open it. Run GenericHIDSimpleDemo.vcxproj in visual studio.
If you use vs 2010 or 2012 or 2008 no matter, it will convert automatically to your version.
Build it then you can get the app.
the app also given in the folder as GenericHIDSimpleDemo in C:\Microchip Solutions v2012-10-15\USB\Device - HID - Custom Demos.
About the app—
- Connect button for connecting your device as many usb device is connected such as mouse,keyboard etc. it is done by product id and vendor id. In host program it is declared product id as 04d8 vendor id as 003F. In mikroc we also will declare it as same. When device is connected then other two button will appear properly.
- Toggle Led(s) button is a command which toggles the state of leds.
- Get Pushbutton State is a command to the device which will back the state of a switch whether it is pressed or not pressed.
Mikroc has powerful built in library function which is very easy to use. We will use such a library function named usb hid library.
Actually we have to do little works as everything is given. In example folder you may get a example project HID Read Write Interrupt. Open it from example folder and now we have to modify it as our pc host app.
Actually we have to do little works as everything is given. In example folder you may get a example project HID Read Write Interrupt. Open it and now we have to modify it as our pc host app.
Look at the program. When Toggle Led(s) button is pressed it sends a data 0x80 to readbuff[0] and in the program a simple if command checks the
Data. If true then lcd shows “Usb device”. You can use a led to get the same output just place aled to port b and on it when the Toggle Led(s) button is pressed.
The other Get Pushbuttion state is not used here.
Before it another work we skipped is to do now that is usb descriptor file. In this HID Read Write Interrupt example usb descriptor file is included.
Open it and change the product id as 04d8 vendor id as 003F as shown in the figure.
If you do not change this then pc application can not connect (when you press connect buttion) the device as it will search for a device with these ids.
Now go to edit project and make it same as shown in figure.
Then build it and pick up the hex file and load it to pic18f2550.
Make all the connections as circuit diagram.
Connect the device to the computer. Open the app GenericHIDSimpleDemo.exe.
Press the connect button. If everything is ok then other two buttons will active.
Press Toggle Led(s) button. If you use led then it will on. If you use lcd as my program then lcd will show “PIC Usb device”.
CAUTION!!!
- DON'T FORGET 1K RESISTOR IN THE FIGURE
- DON'T FORGET TO CONNECT A CAPACITOR TO VUSB PIN
Download the zip file. Also the pc software is uploaded in DOWNLOAD section.
I also upload a very nice book about usb, usb complete 3rd edition.
Now enjoy. any problem then just put a comment. i will try to give feedback.
No comments:
Post a Comment