Basic USB - Using Microchip Stack and C#.Net - Hardware PDF Print E-mail
User Rating: / 98
PoorBest 
Written by Mat   
Sunday, 26 February 2006
Article Index
Hardware
Hardware
PIC Software
Windows Drivers
Application Code

Application Code 

The application code can be found below or in the Complete Code at the top of the article.

USB Demo PC Code USB Demo PC Code (52.44 KB)

The application was developed using Visual Studio 2003 C#.Net, however I believe it should also compile correctly under C# Express 2005 , let me know if you have any success with this. In order to run the code (or compile it for that matter) you will also require the .NET Framework to be installed. The code should be pretty self explanitary however I will go over the general concepts.

application.gif
Example Application

 


The idea behind the program is to create a simple way of controlling two LED's attached to the PIC, and to create a data channel, which is tested by sending 2 bytes to the pic which it sums and returns to the application.

The application consists of three main parts, the mpusbapi.dll, usb_interface class and the main form. The DLL (located in the debug directory) acts an interface between the application and windows driver, therefore do not modify this, and just ensure that it is located in the directory of the program. The form is used to interface to the user and trigger the usb_interface to send commands to the PIC when required.

The main work of the program is located in the usb_interface class, this constructs the required packet to send to the PIC and deals with the response if necessary. The class is setup to only function with a single PIC based usb device connected, this is to reduce the complexity as most systems will not require multiple devices (I will possibly do an advanced article on this later).

You probably won't want to change any of the private functions as these are the fundemental interface to the DLL, and should only be changed if you really know what your doing! As for the public functions these are fair game, as can be modified to provide any functionality required. 

Everytime a new command is added to the pic, a coresponding function should be added to the usb_interface class, if you base the new commands on the UpdateLED and Add functions then these shouldn't be far wrong. The key points to remember are that the RecvLength should match the packet length sent by the PIC otherwise any response sent, will be disregarded and also to ensure that the second field in SendReceivePacket represents the length of the send buffer to transmit.

The packets should be constructed in send_buf first, following the same format as before, and then the SendReceievePacket function called, as in the examples. This will return any required data to receive_buf and can be used as necessary in your application.

<command><length - if required><data><data><data>.... 

(If the application returns an incorrect value for the Add function, this is most likely due to the total lack of overflow code present in the pic, this is to maintain simplicity!)

Conclusion 

So there you have it, you should now be a PIC usb expert! You should all being well be able to compile the code, and start adding your own functions to the PIC, if you have any question or comments about the article please leave them below or in the forums. Hope thats been helpful!

Mat Clayton

Discuss this article on the forums. (117 posts)



Last Updated ( Thursday, 06 April 2006 )
 
< Prev