<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://hades.mech.northwestern.edu//api.php?action=feedcontributions&amp;feedformat=atom&amp;user=DavidMeyer</id>
	<title>Mech - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://hades.mech.northwestern.edu//api.php?action=feedcontributions&amp;feedformat=atom&amp;user=DavidMeyer"/>
	<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php/Special:Contributions/DavidMeyer"/>
	<updated>2026-05-28T01:31:43Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NScopeOld&amp;diff=23223</id>
		<title>NScopeOld</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NScopeOld&amp;diff=23223"/>
		<updated>2015-04-11T21:20:20Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: moved NScopeOld to NScope over redirect&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[NScope]]&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NScope&amp;diff=23222</id>
		<title>NScope</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NScope&amp;diff=23222"/>
		<updated>2015-04-11T21:20:20Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: moved NScopeOld to NScope over redirect&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:nscope.jpg|[www.nscope.org | nscope.org]]&lt;br /&gt;
&lt;br /&gt;
MATLAB DAQ and Pong files: [[Media:nScope_MATLAB_2014.zip | nScope_MATLAB_2014.zip]]&lt;br /&gt;
&lt;br /&gt;
nScope info may be found at  [http://nscope.org nscope.org]&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NScope&amp;diff=23220</id>
		<title>NScope</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NScope&amp;diff=23220"/>
		<updated>2015-04-11T21:20:07Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: moved NScope to NScopeOld&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:nscope.jpg|[www.nscope.org | nscope.org]]&lt;br /&gt;
&lt;br /&gt;
MATLAB DAQ and Pong files: [[Media:nScope_MATLAB_2014.zip | nScope_MATLAB_2014.zip]]&lt;br /&gt;
&lt;br /&gt;
nScope info may be found at  [http://nscope.org nscope.org]&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Main_Page&amp;diff=23219</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Main_Page&amp;diff=23219"/>
		<updated>2015-04-11T21:19:08Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:mechlab.jpg|right|thumb|[http://mechatronics.mech.northwestern.edu/ Northwestern Mechatronics Design Lab]]]&lt;br /&gt;
&lt;br /&gt;
The Northwestern University mechatronics design wiki provides reference material on the theory and applications of electronics, sensors, actuators, etc., for use in mechatronics-related research and projects.  Practical applications often refer to equipment and supplies available in the [http://mechatronics.mech.northwestern.edu/ Northwestern Mechatronics Design Lab].&lt;br /&gt;
&lt;br /&gt;
The mechatronics wiki was initiated by undergraduate Ben Stephens in 2006, under the supervision of Profs. Kevin Lynch and Michael Peshkin.  Since then, many students have contributed content.&lt;br /&gt;
&lt;br /&gt;
Important:  Please be sure to read the [http://mechatronics.mech.northwestern.edu/mech-rules.pdf Rules for Using the Mechatronics Design Lab].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
(Here you can find the [[Old Index Page]] of the mechatronics wiki, as of May 2, 2009.  That page is now obsolete, and new material should be indexed on the pages below.  New pages may be indexed in multiple places.)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* [[Editing the mechatronics wiki]] (or visit [[The sandbox]] to practice your wiki skills)&lt;br /&gt;
* [[Electronics basics]]&lt;br /&gt;
* [[Sensors]]&lt;br /&gt;
* [[Actuators]]&lt;br /&gt;
* [[Communication]]&lt;br /&gt;
* Control computers and software&lt;br /&gt;
**[[Robot Club]]&lt;br /&gt;
**[[nScope]]&lt;br /&gt;
** [[Microchip PICs]]&lt;br /&gt;
** [[PC/104 stacks and Matlab xPC]]&lt;br /&gt;
** [[QNX]]&lt;br /&gt;
** [[Robot Operating System]]&lt;br /&gt;
** [[Processing]]&lt;br /&gt;
** [[Node.js]]&lt;br /&gt;
** [[Eagle]]&lt;br /&gt;
** [[KiCad]]&lt;br /&gt;
** [[C Compilers]]&lt;br /&gt;
* Courses&lt;br /&gt;
** [[DTC]]&lt;br /&gt;
** [[EDI Bootcamp]]&lt;br /&gt;
** [[ME 224 Experimental Engineering]]&lt;br /&gt;
** [[ME 333 Introduction to Mechatronics]]&lt;br /&gt;
*** [[ME 333 Readings, Videos, and Sample Code]]&lt;br /&gt;
*** [[ME 333 final projects]] (2010 and earlier)&lt;br /&gt;
** [http://www.mech.northwestern.edu/courses/descriptions/433-advanced-mechatronics.html ME 433 Advanced Mechatronics] &lt;br /&gt;
** [[Northwestern Design Competition]]&lt;br /&gt;
** [[ME 449 Robotic Manipulation]]&lt;br /&gt;
* [[Tools in the lab and shop]] (software, hardware, supplies, etc.)&lt;br /&gt;
* [[Vendors and Useful Links]]&lt;br /&gt;
* [[Projects and miscellaneous]] (projects, research, and other mechatronics-related info)&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21600</id>
		<title>USB Communication Board</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21600"/>
		<updated>2012-10-01T18:52:05Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Micro-controller Side */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; ***Under Construction*** &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
&lt;br /&gt;
The NU USB Communication Board is a universal communication board which serves as an interface between PCs and embedded systems.  Using a PIC32MX250 chip, the board translates between USB HID communication and low-level microcontroller communication protocols, including RS232, I2C, and SPI.  This board is useful in circumstances when fast, reliable communication is desired between a microcontroller and PC, but resources are not available on the microcontroller to run a full USB stack.  The schematic and images of the copper traces are shown below.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| [[image:USB_Board_Schematic.png|thumb|400px|Board Schematic|none]]&lt;br /&gt;
| [[image:USB_Board_Top.png|thumb|160px|Top Copper|none]]&lt;br /&gt;
| [[image:USB_Board_Bottom.png|thumb|160px|Bottom Copper|none]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Using the USB Communication Board =&lt;br /&gt;
&lt;br /&gt;
The USB protocol used on this board operates in the HID class.  This is beneficial because no drivers are required for any operating system, and low latency is guaranteed.  The USB in operation on this board delivers 64 byte packets every millisecond in both directions.  For this reason, all streaming data coming from the embedded side will be &amp;quot;packetized&amp;quot; by the board into 63 byte packets, using the first byte as a descriptor.&lt;br /&gt;
&lt;br /&gt;
== Micro-controller Side ==&lt;br /&gt;
&lt;br /&gt;
The board operates in one of four modes: RS232, RS232 with Hardware Flow Control, SPI, or I2C.  This mode is selected upon board power-up by the CommSelect jumpers on board.  The selection criteria are shown below&lt;br /&gt;
&lt;br /&gt;
* No Jumpers - RS232&lt;br /&gt;
* CS1 Jumped - RS232 w/ Hardware Flow Control&lt;br /&gt;
* CS2 Jumped - SPI&lt;br /&gt;
* Both Jumped - I2C&lt;br /&gt;
&lt;br /&gt;
=== RS232 ===&lt;br /&gt;
&lt;br /&gt;
In RS232 mode, the communication board will read in any characters over the RS232 U1RX line and send them in 63 byte packets to the computer via USB.  The zeroth byte of the USB 64-byte packet describes the length (N) of the data packet being sent to the computer, and has a maximum value of 63.  Bytes 1 through N are filled with the pertinent data being sent to the computer.  Bytes N+1 through 63 are meaningless.  Each one of these packets is sent out every millisecond, for a maximum throughput of 63 kB/s.&lt;br /&gt;
&lt;br /&gt;
Any packets received from the computer will be fed out the U1TX line.  Again, the zeroth byte of the packet from the computer should contain the number of relevant bytes, as to not bog down the U1TX line with meaningless data.&lt;br /&gt;
&lt;br /&gt;
=== RS232 w/ Hardware Flow Control ===&lt;br /&gt;
&lt;br /&gt;
This mode operates under the same principle, but uses the U1RTS and U1CTS lines for flow control.  This allows for higher baud-rates, and possibly higher-speed communication.&lt;br /&gt;
&lt;br /&gt;
=== SPI ===&lt;br /&gt;
&lt;br /&gt;
In development&lt;br /&gt;
&lt;br /&gt;
=== I2C ===&lt;br /&gt;
&lt;br /&gt;
In development&lt;br /&gt;
&lt;br /&gt;
== PC Side ==&lt;br /&gt;
&lt;br /&gt;
=== Processing ===&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  We have included the library in an example processing sketch for you, but if you&#039;re interested in using the Java library for something else, documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].   This is the template [[Media:USB_Processing_Template.zip|Processing sketch]] which simply listens for characters and prints them out in the text pane, and sends them back to the device.  The code directory contains the .jar file containing the Java library.  These files need to be here, but do not require editing.&lt;br /&gt;
&lt;br /&gt;
In the sketch itself, the &#039;&#039;&#039;USBListener&#039;&#039;&#039; file defines a Thread which listens to the USB port.  The function pic.read() will wait until the USB buffer is not empty, and then place the first-in data into the specified variable.  This thread is also a good place to update all state variables and send out data to the PIC using pic.write().  The &#039;&#039;&#039;HIDManager&#039;&#039;&#039; file contains the code to connect and set up the USB communication with the PIC device.  The NU USB Communication Board has product ID: 0, so the proper function call to open the pic is openPic(0);&lt;br /&gt;
&lt;br /&gt;
Here is the API [[Media:Hidapi-1.1-javadoc.zip|documentation]], which explains what each function does.&lt;br /&gt;
&lt;br /&gt;
=== Matlab ===&lt;br /&gt;
&lt;br /&gt;
In development&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
9/4/2012 - [[Media:USB_COMM.X.zip|Source Project]]&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:USB_Board_Schematic.png&amp;diff=21490</id>
		<title>File:USB Board Schematic.png</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:USB_Board_Schematic.png&amp;diff=21490"/>
		<updated>2012-09-09T16:56:23Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: uploaded a new version of &amp;amp;quot;File:USB Board Schematic.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21489</id>
		<title>USB Communication Board</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21489"/>
		<updated>2012-09-09T16:49:43Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; ***Under Construction*** &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
&lt;br /&gt;
The NU USB Communication Board is a universal communication board which serves as an interface between PCs and embedded systems.  Using a PIC32MX250 chip, the board translates between USB HID communication and low-level microcontroller communication protocols, including RS232, I2C, and SPI.  This board is useful in circumstances when fast, reliable communication is desired between a microcontroller and PC, but resources are not available on the microcontroller to run a full USB stack.  The schematic and images of the copper traces are shown below.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| [[image:USB_Board_Schematic.png|thumb|400px|Board Schematic|none]]&lt;br /&gt;
| [[image:USB_Board_Top.png|thumb|160px|Top Copper|none]]&lt;br /&gt;
| [[image:USB_Board_Bottom.png|thumb|160px|Bottom Copper|none]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Using the USB Communication Board =&lt;br /&gt;
&lt;br /&gt;
The USB protocol used on this board operates in the HID class.  This is beneficial because no drivers are required for any operating system, and low latency is guaranteed.  The USB in operation on this board delivers 64 byte packets every millisecond in both directions.  For this reason, all streaming data coming from the embedded side will be &amp;quot;packetized&amp;quot; by the board into 63 byte packets, using the first byte as a descriptor.&lt;br /&gt;
&lt;br /&gt;
== Micro-controller Side ==&lt;br /&gt;
&lt;br /&gt;
The board operates in one of four modes: RS232, RS232 with Hardware Flow Control, SPI, or I2C.  This mode is selected upon board power-up by the CommSelect jumpers on board.  The selection criteria are shown below&lt;br /&gt;
&lt;br /&gt;
* No Jumpers - RS232&lt;br /&gt;
* CS1 Jumped - RS232 w/ Hardware Flow Control&lt;br /&gt;
* CS2 Jumped - SPI&lt;br /&gt;
* Both Jumped - I2C&lt;br /&gt;
&lt;br /&gt;
=== RS232 ===&lt;br /&gt;
&lt;br /&gt;
In RS232 mode, the communication board will read in any characters over the RS232 U1RX line and send them in 63 byte packets to the computer via USB.  The zeroth byte of the USB 64-byte packet describes the length (N) of the data packet being sent to the computer, and has a maximum value of 63.  Bytes 1 through N are filled with the pertinent data being sent to the computer.  Bytes N+1 through 63 are meaningless.  Each one of these packets is sent out every millisecond, for a maximum throughput of 63 kB/s.&lt;br /&gt;
&lt;br /&gt;
Any packets received from the computer will be fed out the U1TX line.  Again, the zeroth byte of the packet from the computer should contain the number of relevant bytes, as to not bog down the U1TX line with meaningless data.&lt;br /&gt;
&lt;br /&gt;
=== RS232 w/ Hardware Flow Control ===&lt;br /&gt;
&lt;br /&gt;
This mode operates under the same principle, but uses the U1RTS and U1CTS lines for flow control.  This allows for higher baud-rates, and possibly higher-speed communication.&lt;br /&gt;
&lt;br /&gt;
=== SPI ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
=== I2C ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
== PC Side ==&lt;br /&gt;
&lt;br /&gt;
=== Processing ===&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  We have included the library in an example processing sketch for you, but if you&#039;re interested in using the Java library for something else, documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].   This is the template [[Media:USB_Processing_Template.zip|Processing sketch]] which simply listens for characters and prints them out in the text pane, and sends them back to the device.  The code directory contains the .jar file containing the Java library.  These files need to be here, but do not require editing.&lt;br /&gt;
&lt;br /&gt;
In the sketch itself, the &#039;&#039;&#039;USBListener&#039;&#039;&#039; file defines a Thread which listens to the USB port.  The function pic.read() will wait until the USB buffer is not empty, and then place the first-in data into the specified variable.  This thread is also a good place to update all state variables and send out data to the PIC using pic.write().  The &#039;&#039;&#039;HIDManager&#039;&#039;&#039; file contains the code to connect and set up the USB communication with the PIC device.  The NU USB Communication Board has product ID: 0, so the proper function call to open the pic is openPic(0);&lt;br /&gt;
&lt;br /&gt;
Here is the API [[Media:Hidapi-1.1-javadoc.zip|documentation]], which explains what each function does.&lt;br /&gt;
&lt;br /&gt;
=== Matlab ===&lt;br /&gt;
&lt;br /&gt;
In development&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
9/4/2012 - [[Media:USB_COMM.X.zip|Source Project]]&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:USB_Board_Bottom.png&amp;diff=21488</id>
		<title>File:USB Board Bottom.png</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:USB_Board_Bottom.png&amp;diff=21488"/>
		<updated>2012-09-09T16:47:09Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:USB_Board_Top.png&amp;diff=21487</id>
		<title>File:USB Board Top.png</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:USB_Board_Top.png&amp;diff=21487"/>
		<updated>2012-09-09T16:40:50Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:USB_Board_Schematic.png&amp;diff=21486</id>
		<title>File:USB Board Schematic.png</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:USB_Board_Schematic.png&amp;diff=21486"/>
		<updated>2012-09-09T16:33:15Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21485</id>
		<title>USB Communication Board</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21485"/>
		<updated>2012-09-09T16:31:24Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; ***Under Construction*** &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
&lt;br /&gt;
The NU USB Communication Board is a universal communication board which serves as an interface between PCs and embedded systems.  Using a PIC32MX250 chip, the board translates between USB HID communication and low-level microcontroller communication protocols, including RS232, I2C, and SPI.  This board is useful in circumstances when fast, reliable communication is desired between a microcontroller and PC, but resources are not available on the microcontroller to run a full USB stack.&lt;br /&gt;
&lt;br /&gt;
[[image:All-actuators-captions-small.jpg|thumb|300px|Available Actuators|right]]&lt;br /&gt;
&lt;br /&gt;
= Using the USB Communication Board =&lt;br /&gt;
&lt;br /&gt;
The USB protocol used on this board operates in the HID class.  This is beneficial because no drivers are required for any operating system, and low latency is guaranteed.  The USB in operation on this board delivers 64 byte packets every millisecond in both directions.  For this reason, all streaming data coming from the embedded side will be &amp;quot;packetized&amp;quot; by the board into 63 byte packets, using the first byte as a descriptor.&lt;br /&gt;
&lt;br /&gt;
== Micro-controller Side ==&lt;br /&gt;
&lt;br /&gt;
The board operates in one of four modes: RS232, RS232 with Hardware Flow Control, SPI, or I2C.  This mode is selected upon board power-up by the CommSelect jumpers on board.  The selection criteria are shown below&lt;br /&gt;
&lt;br /&gt;
* No Jumpers - RS232&lt;br /&gt;
* CS1 Jumped - RS232 w/ Hardware Flow Control&lt;br /&gt;
* CS2 Jumped - SPI&lt;br /&gt;
* Both Jumped - I2C&lt;br /&gt;
&lt;br /&gt;
=== RS232 ===&lt;br /&gt;
&lt;br /&gt;
In RS232 mode, the communication board will read in any characters over the RS232 U1RX line and send them in 63 byte packets to the computer via USB.  The zeroth byte of the USB 64-byte packet describes the length (N) of the data packet being sent to the computer, and has a maximum value of 63.  Bytes 1 through N are filled with the pertinent data being sent to the computer.  Bytes N+1 through 63 are meaningless.  Each one of these packets is sent out every millisecond, for a maximum throughput of 63 kB/s.&lt;br /&gt;
&lt;br /&gt;
Any packets received from the computer will be fed out the U1TX line.  Again, the zeroth byte of the packet from the computer should contain the number of relevant bytes, as to not bog down the U1TX line with meaningless data.&lt;br /&gt;
&lt;br /&gt;
=== RS232 w/ Hardware Flow Control ===&lt;br /&gt;
&lt;br /&gt;
This mode operates under the same principle, but uses the U1RTS and U1CTS lines for flow control.  This allows for higher baud-rates, and possibly higher-speed communication.&lt;br /&gt;
&lt;br /&gt;
=== SPI ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
=== I2C ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
== PC Side ==&lt;br /&gt;
&lt;br /&gt;
=== Processing ===&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  We have included the library in an example processing sketch for you, but if you&#039;re interested in using the Java library for something else, documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].   This is the template [[Media:USB_Processing_Template.zip|Processing sketch]] which simply listens for characters and prints them out in the text pane, and sends them back to the device.  The code directory contains the .jar file containing the Java library.  These files need to be here, but do not require editing.&lt;br /&gt;
&lt;br /&gt;
In the sketch itself, the &#039;&#039;&#039;USBListener&#039;&#039;&#039; file defines a Thread which listens to the USB port.  The function pic.read() will wait until the USB buffer is not empty, and then place the first-in data into the specified variable.  This thread is also a good place to update all state variables and send out data to the PIC using pic.write().  The &#039;&#039;&#039;HIDManager&#039;&#039;&#039; file contains the code to connect and set up the USB communication with the PIC device.  The NU USB Communication Board has product ID: 0, so the proper function call to open the pic is openPic(0);&lt;br /&gt;
&lt;br /&gt;
Here is the API [[Media:Hidapi-1.1-javadoc.zip|documentation]], which explains what each function does.&lt;br /&gt;
&lt;br /&gt;
=== Matlab ===&lt;br /&gt;
&lt;br /&gt;
In development&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
9/4/2012 - [[Media:USB_COMM.X.zip|Source Project]]&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21484</id>
		<title>USB Communication Board</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21484"/>
		<updated>2012-09-07T20:11:31Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Processing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; ***Under Construction*** &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
&lt;br /&gt;
The NU USB Communication Board is a universal communication board which serves as an interface between PCs and embedded systems.  Using a PIC32MX250 chip, the board translates between USB HID communication and low-level microcontroller communication protocols, including RS232, I2C, and SPI.  This board is useful in circumstances when fast, reliable communication is desired between a microcontroller and PC, but resources are not available on the microcontroller to run a full USB stack.&lt;br /&gt;
&lt;br /&gt;
= Using the USB Communication Board =&lt;br /&gt;
&lt;br /&gt;
The USB protocol used on this board operates in the HID class.  This is beneficial because no drivers are required for any operating system, and low latency is guaranteed.  The USB in operation on this board delivers 64 byte packets every millisecond in both directions.  For this reason, all streaming data coming from the embedded side will be &amp;quot;packetized&amp;quot; by the board into 63 byte packets, using the first byte as a descriptor.&lt;br /&gt;
&lt;br /&gt;
== Micro-controller Side ==&lt;br /&gt;
&lt;br /&gt;
The board operates in one of four modes: RS232, RS232 with Hardware Flow Control, SPI, or I2C.  This mode is selected upon board power-up by the CommSelect jumpers on board.  The selection criteria are shown below&lt;br /&gt;
&lt;br /&gt;
* No Jumpers - RS232&lt;br /&gt;
* CS1 Jumped - RS232 w/ Hardware Flow Control&lt;br /&gt;
* CS2 Jumped - SPI&lt;br /&gt;
* Both Jumped - I2C&lt;br /&gt;
&lt;br /&gt;
=== RS232 ===&lt;br /&gt;
&lt;br /&gt;
In RS232 mode, the communication board will read in any characters over the RS232 U1RX line and send them in 63 byte packets to the computer via USB.  The zeroth byte of the USB 64-byte packet describes the length (N) of the data packet being sent to the computer, and has a maximum value of 63.  Bytes 1 through N are filled with the pertinent data being sent to the computer.  Bytes N+1 through 63 are meaningless.  Each one of these packets is sent out every millisecond, for a maximum throughput of 63 kB/s.&lt;br /&gt;
&lt;br /&gt;
Any packets received from the computer will be fed out the U1TX line.  Again, the zeroth byte of the packet from the computer should contain the number of relevant bytes, as to not bog down the U1TX line with meaningless data.&lt;br /&gt;
&lt;br /&gt;
=== RS232 w/ Hardware Flow Control ===&lt;br /&gt;
&lt;br /&gt;
This mode operates under the same principle, but uses the U1RTS and U1CTS lines for flow control.  This allows for higher baud-rates, and possibly higher-speed communication.&lt;br /&gt;
&lt;br /&gt;
=== SPI ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
=== I2C ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
== PC Side ==&lt;br /&gt;
&lt;br /&gt;
=== Processing ===&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  We have included the library in an example processing sketch for you, but if you&#039;re interested in using the Java library for something else, documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].   This is the template [[Media:USB_Processing_Template.zip|Processing sketch]] which simply listens for characters and prints them out in the text pane, and sends them back to the device.  The code directory contains the .jar file containing the Java library.  These files need to be here, but do not require editing.&lt;br /&gt;
&lt;br /&gt;
In the sketch itself, the &#039;&#039;&#039;USBListener&#039;&#039;&#039; file defines a Thread which listens to the USB port.  The function pic.read() will wait until the USB buffer is not empty, and then place the first-in data into the specified variable.  This thread is also a good place to update all state variables and send out data to the PIC using pic.write().  The &#039;&#039;&#039;HIDManager&#039;&#039;&#039; file contains the code to connect and set up the USB communication with the PIC device.  The NU USB Communication Board has product ID: 0, so the proper function call to open the pic is openPic(0);&lt;br /&gt;
&lt;br /&gt;
Here is the API [[Media:Hidapi-1.1-javadoc.zip|documentation]], which explains what each function does.&lt;br /&gt;
&lt;br /&gt;
=== Matlab ===&lt;br /&gt;
&lt;br /&gt;
In development&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
9/4/2012 - [[Media:USB_COMM.X.zip|Source Project]]&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21483</id>
		<title>USB Communication Board</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21483"/>
		<updated>2012-09-07T20:10:41Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Processing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; ***Under Construction*** &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
&lt;br /&gt;
The NU USB Communication Board is a universal communication board which serves as an interface between PCs and embedded systems.  Using a PIC32MX250 chip, the board translates between USB HID communication and low-level microcontroller communication protocols, including RS232, I2C, and SPI.  This board is useful in circumstances when fast, reliable communication is desired between a microcontroller and PC, but resources are not available on the microcontroller to run a full USB stack.&lt;br /&gt;
&lt;br /&gt;
= Using the USB Communication Board =&lt;br /&gt;
&lt;br /&gt;
The USB protocol used on this board operates in the HID class.  This is beneficial because no drivers are required for any operating system, and low latency is guaranteed.  The USB in operation on this board delivers 64 byte packets every millisecond in both directions.  For this reason, all streaming data coming from the embedded side will be &amp;quot;packetized&amp;quot; by the board into 63 byte packets, using the first byte as a descriptor.&lt;br /&gt;
&lt;br /&gt;
== Micro-controller Side ==&lt;br /&gt;
&lt;br /&gt;
The board operates in one of four modes: RS232, RS232 with Hardware Flow Control, SPI, or I2C.  This mode is selected upon board power-up by the CommSelect jumpers on board.  The selection criteria are shown below&lt;br /&gt;
&lt;br /&gt;
* No Jumpers - RS232&lt;br /&gt;
* CS1 Jumped - RS232 w/ Hardware Flow Control&lt;br /&gt;
* CS2 Jumped - SPI&lt;br /&gt;
* Both Jumped - I2C&lt;br /&gt;
&lt;br /&gt;
=== RS232 ===&lt;br /&gt;
&lt;br /&gt;
In RS232 mode, the communication board will read in any characters over the RS232 U1RX line and send them in 63 byte packets to the computer via USB.  The zeroth byte of the USB 64-byte packet describes the length (N) of the data packet being sent to the computer, and has a maximum value of 63.  Bytes 1 through N are filled with the pertinent data being sent to the computer.  Bytes N+1 through 63 are meaningless.  Each one of these packets is sent out every millisecond, for a maximum throughput of 63 kB/s.&lt;br /&gt;
&lt;br /&gt;
Any packets received from the computer will be fed out the U1TX line.  Again, the zeroth byte of the packet from the computer should contain the number of relevant bytes, as to not bog down the U1TX line with meaningless data.&lt;br /&gt;
&lt;br /&gt;
=== RS232 w/ Hardware Flow Control ===&lt;br /&gt;
&lt;br /&gt;
This mode operates under the same principle, but uses the U1RTS and U1CTS lines for flow control.  This allows for higher baud-rates, and possibly higher-speed communication.&lt;br /&gt;
&lt;br /&gt;
=== SPI ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
=== I2C ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
== PC Side ==&lt;br /&gt;
&lt;br /&gt;
=== Processing ===&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  We have included the library in an example processing sketch for you, but if you&#039;re interested in using the Java library for something else, documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].   This is the template [[Media:USB_Processing_Template.zip|Processing sketch]] which simply listens for characters and prints them out in the text pane, and sends them back to the device.  The code directory contains the .jar file containing the Java library.  These files need to be here, but do not require editing.&lt;br /&gt;
&lt;br /&gt;
In the sketch itself, the &#039;&#039;&#039;USBListener&#039;&#039;&#039; file defines a Thread which listens to the USB port.  The function pic.read() will wait until the USB buffer is not empty, and then place the first-in data into the specified variable.  This thread is also a good place to update all state variables and send out data to the PIC using pic.write().  The &#039;&#039;&#039;HIDManager&#039;&#039;&#039; file contains the code to connect and set up the .  This class defines callback functions which are called whenever a device is added or removed.&lt;br /&gt;
&lt;br /&gt;
Here is the API [[Media:Hidapi-1.1-javadoc.zip|documentation]], which explains what each function does.&lt;br /&gt;
&lt;br /&gt;
=== Matlab ===&lt;br /&gt;
&lt;br /&gt;
In development&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
9/4/2012 - [[Media:USB_COMM.X.zip|Source Project]]&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:Hidapi-1.1-javadoc.zip&amp;diff=21482</id>
		<title>File:Hidapi-1.1-javadoc.zip</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:Hidapi-1.1-javadoc.zip&amp;diff=21482"/>
		<updated>2012-09-07T20:05:17Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: Java Documentation for the HID API library.  Version 1.1&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Java Documentation for the HID API library.  Version 1.1&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21481</id>
		<title>USB Communication Board</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21481"/>
		<updated>2012-09-07T20:03:39Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Processing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; ***Under Construction*** &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
&lt;br /&gt;
The NU USB Communication Board is a universal communication board which serves as an interface between PCs and embedded systems.  Using a PIC32MX250 chip, the board translates between USB HID communication and low-level microcontroller communication protocols, including RS232, I2C, and SPI.  This board is useful in circumstances when fast, reliable communication is desired between a microcontroller and PC, but resources are not available on the microcontroller to run a full USB stack.&lt;br /&gt;
&lt;br /&gt;
= Using the USB Communication Board =&lt;br /&gt;
&lt;br /&gt;
The USB protocol used on this board operates in the HID class.  This is beneficial because no drivers are required for any operating system, and low latency is guaranteed.  The USB in operation on this board delivers 64 byte packets every millisecond in both directions.  For this reason, all streaming data coming from the embedded side will be &amp;quot;packetized&amp;quot; by the board into 63 byte packets, using the first byte as a descriptor.&lt;br /&gt;
&lt;br /&gt;
== Micro-controller Side ==&lt;br /&gt;
&lt;br /&gt;
The board operates in one of four modes: RS232, RS232 with Hardware Flow Control, SPI, or I2C.  This mode is selected upon board power-up by the CommSelect jumpers on board.  The selection criteria are shown below&lt;br /&gt;
&lt;br /&gt;
* No Jumpers - RS232&lt;br /&gt;
* CS1 Jumped - RS232 w/ Hardware Flow Control&lt;br /&gt;
* CS2 Jumped - SPI&lt;br /&gt;
* Both Jumped - I2C&lt;br /&gt;
&lt;br /&gt;
=== RS232 ===&lt;br /&gt;
&lt;br /&gt;
In RS232 mode, the communication board will read in any characters over the RS232 U1RX line and send them in 63 byte packets to the computer via USB.  The zeroth byte of the USB 64-byte packet describes the length (N) of the data packet being sent to the computer, and has a maximum value of 63.  Bytes 1 through N are filled with the pertinent data being sent to the computer.  Bytes N+1 through 63 are meaningless.  Each one of these packets is sent out every millisecond, for a maximum throughput of 63 kB/s.&lt;br /&gt;
&lt;br /&gt;
Any packets received from the computer will be fed out the U1TX line.  Again, the zeroth byte of the packet from the computer should contain the number of relevant bytes, as to not bog down the U1TX line with meaningless data.&lt;br /&gt;
&lt;br /&gt;
=== RS232 w/ Hardware Flow Control ===&lt;br /&gt;
&lt;br /&gt;
This mode operates under the same principle, but uses the U1RTS and U1CTS lines for flow control.  This allows for higher baud-rates, and possibly higher-speed communication.&lt;br /&gt;
&lt;br /&gt;
=== SPI ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
=== I2C ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
== PC Side ==&lt;br /&gt;
&lt;br /&gt;
=== Processing ===&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  We have included the library in an example processing sketch for you, but if you&#039;re interested in using the Java library for something else, documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].   This is the template [[Media:USB_Processing_Template.zip|Processing sketch]] which simply listens for characters and prints them out in the text pane, and sends them back to the device.  The code directory contains the .jar file containing the Java library.  These files need to be here, but do not require editing.&lt;br /&gt;
&lt;br /&gt;
In the sketch itself, the &#039;&#039;&#039;USBListener&#039;&#039;&#039; file defines a Thread which listens to the USB port.  The function pic.read() will wait until the USB buffer is not empty, and then place the first-in data into the specified variable.  This thread is also a good place to update all state variables and send out data to the PIC using pic.write().  The &#039;&#039;&#039;HIDManager&#039;&#039;&#039; file contains the code to connect and set up the .  This class defines callback functions which are called whenever a device is added or removed.&lt;br /&gt;
&lt;br /&gt;
Here is the API [[Nope|documentation]], which explains what each function does.&lt;br /&gt;
&lt;br /&gt;
=== Matlab ===&lt;br /&gt;
&lt;br /&gt;
In development&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
9/4/2012 - [[Media:USB_COMM.X.zip|Source Project]]&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:USB_Processing_Template.zip&amp;diff=21480</id>
		<title>File:USB Processing Template.zip</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:USB_Processing_Template.zip&amp;diff=21480"/>
		<updated>2012-09-07T20:03:01Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: Template for developing USB communication code in Processing&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Template for developing USB communication code in Processing&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21479</id>
		<title>USB Communication Board</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21479"/>
		<updated>2012-09-07T20:01:41Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Processing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; ***Under Construction*** &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
&lt;br /&gt;
The NU USB Communication Board is a universal communication board which serves as an interface between PCs and embedded systems.  Using a PIC32MX250 chip, the board translates between USB HID communication and low-level microcontroller communication protocols, including RS232, I2C, and SPI.  This board is useful in circumstances when fast, reliable communication is desired between a microcontroller and PC, but resources are not available on the microcontroller to run a full USB stack.&lt;br /&gt;
&lt;br /&gt;
= Using the USB Communication Board =&lt;br /&gt;
&lt;br /&gt;
The USB protocol used on this board operates in the HID class.  This is beneficial because no drivers are required for any operating system, and low latency is guaranteed.  The USB in operation on this board delivers 64 byte packets every millisecond in both directions.  For this reason, all streaming data coming from the embedded side will be &amp;quot;packetized&amp;quot; by the board into 63 byte packets, using the first byte as a descriptor.&lt;br /&gt;
&lt;br /&gt;
== Micro-controller Side ==&lt;br /&gt;
&lt;br /&gt;
The board operates in one of four modes: RS232, RS232 with Hardware Flow Control, SPI, or I2C.  This mode is selected upon board power-up by the CommSelect jumpers on board.  The selection criteria are shown below&lt;br /&gt;
&lt;br /&gt;
* No Jumpers - RS232&lt;br /&gt;
* CS1 Jumped - RS232 w/ Hardware Flow Control&lt;br /&gt;
* CS2 Jumped - SPI&lt;br /&gt;
* Both Jumped - I2C&lt;br /&gt;
&lt;br /&gt;
=== RS232 ===&lt;br /&gt;
&lt;br /&gt;
In RS232 mode, the communication board will read in any characters over the RS232 U1RX line and send them in 63 byte packets to the computer via USB.  The zeroth byte of the USB 64-byte packet describes the length (N) of the data packet being sent to the computer, and has a maximum value of 63.  Bytes 1 through N are filled with the pertinent data being sent to the computer.  Bytes N+1 through 63 are meaningless.  Each one of these packets is sent out every millisecond, for a maximum throughput of 63 kB/s.&lt;br /&gt;
&lt;br /&gt;
Any packets received from the computer will be fed out the U1TX line.  Again, the zeroth byte of the packet from the computer should contain the number of relevant bytes, as to not bog down the U1TX line with meaningless data.&lt;br /&gt;
&lt;br /&gt;
=== RS232 w/ Hardware Flow Control ===&lt;br /&gt;
&lt;br /&gt;
This mode operates under the same principle, but uses the U1RTS and U1CTS lines for flow control.  This allows for higher baud-rates, and possibly higher-speed communication.&lt;br /&gt;
&lt;br /&gt;
=== SPI ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
=== I2C ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
== PC Side ==&lt;br /&gt;
&lt;br /&gt;
=== Processing ===&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  We have included the library in an example processing sketch for you, but if you&#039;re interested in using the Java library for something else, documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].   This is the template [[Nope|Processing sketch]] which simply listens for characters and prints them out in the text pane, and sends them back to the device.  The code directory contains the .jar file containing the Java library.  These files need to be here, but do not require editing.&lt;br /&gt;
&lt;br /&gt;
In the sketch itself, the &#039;&#039;&#039;USBListener&#039;&#039;&#039; file defines a Thread which listens to the USB port.  The function pic.read() will wait until the USB buffer is not empty, and then place the first-in data into the specified variable.  This thread is also a good place to update all state variables and send out data to the PIC using pic.write().  The &#039;&#039;&#039;HIDManager&#039;&#039;&#039; file contains the code to connect and set up the .  This class defines callback functions which are called whenever a device is added or removed.&lt;br /&gt;
&lt;br /&gt;
Here is the API [[Nope|documentation]], which explains what each function does.&lt;br /&gt;
&lt;br /&gt;
=== Matlab ===&lt;br /&gt;
&lt;br /&gt;
In development&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
9/4/2012 - [[Media:USB_COMM.X.zip|Source Project]]&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21478</id>
		<title>USB Communication Board</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21478"/>
		<updated>2012-09-07T19:54:32Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; ***Under Construction*** &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
&lt;br /&gt;
The NU USB Communication Board is a universal communication board which serves as an interface between PCs and embedded systems.  Using a PIC32MX250 chip, the board translates between USB HID communication and low-level microcontroller communication protocols, including RS232, I2C, and SPI.  This board is useful in circumstances when fast, reliable communication is desired between a microcontroller and PC, but resources are not available on the microcontroller to run a full USB stack.&lt;br /&gt;
&lt;br /&gt;
= Using the USB Communication Board =&lt;br /&gt;
&lt;br /&gt;
The USB protocol used on this board operates in the HID class.  This is beneficial because no drivers are required for any operating system, and low latency is guaranteed.  The USB in operation on this board delivers 64 byte packets every millisecond in both directions.  For this reason, all streaming data coming from the embedded side will be &amp;quot;packetized&amp;quot; by the board into 63 byte packets, using the first byte as a descriptor.&lt;br /&gt;
&lt;br /&gt;
== Micro-controller Side ==&lt;br /&gt;
&lt;br /&gt;
The board operates in one of four modes: RS232, RS232 with Hardware Flow Control, SPI, or I2C.  This mode is selected upon board power-up by the CommSelect jumpers on board.  The selection criteria are shown below&lt;br /&gt;
&lt;br /&gt;
* No Jumpers - RS232&lt;br /&gt;
* CS1 Jumped - RS232 w/ Hardware Flow Control&lt;br /&gt;
* CS2 Jumped - SPI&lt;br /&gt;
* Both Jumped - I2C&lt;br /&gt;
&lt;br /&gt;
=== RS232 ===&lt;br /&gt;
&lt;br /&gt;
In RS232 mode, the communication board will read in any characters over the RS232 U1RX line and send them in 63 byte packets to the computer via USB.  The zeroth byte of the USB 64-byte packet describes the length (N) of the data packet being sent to the computer, and has a maximum value of 63.  Bytes 1 through N are filled with the pertinent data being sent to the computer.  Bytes N+1 through 63 are meaningless.  Each one of these packets is sent out every millisecond, for a maximum throughput of 63 kB/s.&lt;br /&gt;
&lt;br /&gt;
Any packets received from the computer will be fed out the U1TX line.  Again, the zeroth byte of the packet from the computer should contain the number of relevant bytes, as to not bog down the U1TX line with meaningless data.&lt;br /&gt;
&lt;br /&gt;
=== RS232 w/ Hardware Flow Control ===&lt;br /&gt;
&lt;br /&gt;
This mode operates under the same principle, but uses the U1RTS and U1CTS lines for flow control.  This allows for higher baud-rates, and possibly higher-speed communication.&lt;br /&gt;
&lt;br /&gt;
=== SPI ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
=== I2C ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
== PC Side ==&lt;br /&gt;
&lt;br /&gt;
=== Processing ===&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  We have included the library in the processing sketch for you, but if you&#039;re interested in using the Java library for something else, documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].   This is the template [[Nope|Processing sketch]] which simply listens for characters and prints them out in the text pane.  Any character that is typed into the box will be sent to over USB to the connected device.  The code directory contains the .jar file containing the Java library.  These files need to be here, but do not require editing.&lt;br /&gt;
&lt;br /&gt;
In the sketch itself, the &#039;&#039;&#039;USBListener&#039;&#039;&#039; file defines a Thread which listens to the USB port.  The function pic.read() will wait until the USB buffer is not empty, and then place the first-in data into the specified variable.  This thread is also a good place to update all state variables and send out data to the PIC using pic.write().  The &#039;&#039;&#039;HIDManager&#039;&#039;&#039; file contains the code to connect and set up the .  This class defines callback functions which are called whenever a device is added or removed.&lt;br /&gt;
&lt;br /&gt;
Here is the API [[Nope|documentation]], which explains what each function does.&lt;br /&gt;
&lt;br /&gt;
=== Matlab ===&lt;br /&gt;
&lt;br /&gt;
In development&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
9/4/2012 - [[Media:USB_COMM.X.zip|Source Project]]&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21477</id>
		<title>USB Communication Board</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21477"/>
		<updated>2012-09-04T22:55:03Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Source Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; ***Under Construction*** &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The NU USB Communication Board is a universal communication board which serves as an interface between PCs and embedded systems.  Using a PIC32MX250 chip, the board translates between USB HID communication and low-level microcontroller communication protocols, including RS232, I2C, and SPI.  This board is useful in circumstances when fast, reliable communication is desired between a microcontroller and PC, but resources are not available on the microcontroller to run a full USB stack.&lt;br /&gt;
&lt;br /&gt;
== Using the USB Communication Board ==&lt;br /&gt;
&lt;br /&gt;
The board operates in one of four modes: RS232, RS232 with Hardware Flow Control, SPI, or I2C.  This mode is selected upon board power-up by the CommSelect jumpers on board.  The selection criteria are shown below&lt;br /&gt;
&lt;br /&gt;
* No Jumpers - RS232&lt;br /&gt;
* CS1 Jumped - RS232 w/ Hardware Flow Control&lt;br /&gt;
* CS2 Jumped - SPI&lt;br /&gt;
* Both Jumped - I2C&lt;br /&gt;
&lt;br /&gt;
=== RS232 ===&lt;br /&gt;
&lt;br /&gt;
In RS232 mode, the communication board will read in any characters over the RS232 U1RX line and send them in 63 byte packets to the computer via USB.  The zeroth byte of the USB 64-byte packet describes the length (N) of the data packet being sent to the computer, and has a maximum value of 63.  Bytes 1 through N are filled with the pertinent data being sent to the computer.  Bytes N+1 through 63 are meaningless.  Each one of these packets is sent out every millisecond, for a maximum throughput of 63 kB/s.&lt;br /&gt;
&lt;br /&gt;
Any packets received from the computer will be fed out the U1TX line.  Again, the zeroth byte of the packet from the computer should contain the number of relevant bytes, as to not bog down the U1TX line with meaningless data.&lt;br /&gt;
&lt;br /&gt;
=== RS232 w/ Hardware Flow Control ===&lt;br /&gt;
&lt;br /&gt;
This mode operates under the same principle, but uses the U1RTS and U1CTS lines for flow control.  This allows for higher baud-rates, and possibly higher-speed communication.&lt;br /&gt;
&lt;br /&gt;
=== SPI ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
=== I2C ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
9/4/2012 - [[Media:USB_COMM.X.zip|Source Project]]&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:USB_COMM.X.zip&amp;diff=21476</id>
		<title>File:USB COMM.X.zip</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:USB_COMM.X.zip&amp;diff=21476"/>
		<updated>2012-09-04T22:49:33Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: Source code for the USB Communication Board - 9/4/2012&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Source code for the USB Communication Board - 9/4/2012&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21475</id>
		<title>USB Communication Board</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21475"/>
		<updated>2012-09-04T22:45:25Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; ***Under Construction*** &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The NU USB Communication Board is a universal communication board which serves as an interface between PCs and embedded systems.  Using a PIC32MX250 chip, the board translates between USB HID communication and low-level microcontroller communication protocols, including RS232, I2C, and SPI.  This board is useful in circumstances when fast, reliable communication is desired between a microcontroller and PC, but resources are not available on the microcontroller to run a full USB stack.&lt;br /&gt;
&lt;br /&gt;
== Using the USB Communication Board ==&lt;br /&gt;
&lt;br /&gt;
The board operates in one of four modes: RS232, RS232 with Hardware Flow Control, SPI, or I2C.  This mode is selected upon board power-up by the CommSelect jumpers on board.  The selection criteria are shown below&lt;br /&gt;
&lt;br /&gt;
* No Jumpers - RS232&lt;br /&gt;
* CS1 Jumped - RS232 w/ Hardware Flow Control&lt;br /&gt;
* CS2 Jumped - SPI&lt;br /&gt;
* Both Jumped - I2C&lt;br /&gt;
&lt;br /&gt;
=== RS232 ===&lt;br /&gt;
&lt;br /&gt;
In RS232 mode, the communication board will read in any characters over the RS232 U1RX line and send them in 63 byte packets to the computer via USB.  The zeroth byte of the USB 64-byte packet describes the length (N) of the data packet being sent to the computer, and has a maximum value of 63.  Bytes 1 through N are filled with the pertinent data being sent to the computer.  Bytes N+1 through 63 are meaningless.  Each one of these packets is sent out every millisecond, for a maximum throughput of 63 kB/s.&lt;br /&gt;
&lt;br /&gt;
Any packets received from the computer will be fed out the U1TX line.  Again, the zeroth byte of the packet from the computer should contain the number of relevant bytes, as to not bog down the U1TX line with meaningless data.&lt;br /&gt;
&lt;br /&gt;
=== RS232 w/ Hardware Flow Control ===&lt;br /&gt;
&lt;br /&gt;
This mode operates under the same principle, but uses the U1RTS and U1CTS lines for flow control.  This allows for higher baud-rates, and possibly higher-speed communication.&lt;br /&gt;
&lt;br /&gt;
=== SPI ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
=== I2C ===&lt;br /&gt;
&lt;br /&gt;
Not yet implemented&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
9/4/2012 -&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21474</id>
		<title>USB Communication Board</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21474"/>
		<updated>2012-09-04T22:25:59Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; ***Under Construction*** &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The NU USB Communication Board is a universal communication board which serves as an interface between PCs and embedded systems.  The board translates between USB HID communication and low-level microcontroller communication protocols, including RS232, I2C, and SPI.  This board is useful in circumstances when fast, reliable communication is desired between a microcontroller and PC, but resources are not available on the microcontroller to run a full USB stack.&lt;br /&gt;
&lt;br /&gt;
== Using the USB Communication Board ==&lt;br /&gt;
&lt;br /&gt;
The board operates in one of four modes: RS232, RS232 with Hardware Flow Control, SPI, or I2C.  This mode is selected upon board power-up by the CommSelect jumpers on board.  The selection criteria are shown below&lt;br /&gt;
&lt;br /&gt;
* No Jumpers - RS232&lt;br /&gt;
* CS1 Jumped - RS232 w/ Hardware Flow Control&lt;br /&gt;
* CS2 Jumped - SPI&lt;br /&gt;
* Both Jumped - I2C&lt;br /&gt;
&lt;br /&gt;
=== RS232 ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21473</id>
		<title>USB Communication Board</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21473"/>
		<updated>2012-09-04T22:14:39Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=== ***Under Construction*** ===&lt;br /&gt;
&lt;br /&gt;
The NU USB Communication Board is a universal communication board which serves as an interface between PCs and embedded systems.&lt;br /&gt;
&lt;br /&gt;
== Using the USB Communication Board ==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21472</id>
		<title>USB Communication Board</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_Board&amp;diff=21472"/>
		<updated>2012-09-04T22:08:30Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: Created page with &amp;quot;&amp;#039;&amp;#039;&amp;#039;Under Construction&amp;#039;&amp;#039;&amp;#039;&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Under Construction&#039;&#039;&#039;&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21471</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21471"/>
		<updated>2012-09-04T21:57:48Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links&lt;br /&gt;
[http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
[http://www.usbmadesimple.co.uk/index.html USB Made Simple]&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Installation===&lt;br /&gt;
&lt;br /&gt;
====Download====&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Include Libraries and Driver in Project====&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
====Add Application-Specific USB files====&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
The file attached below is a zip containing examples of these four files.  This example provides generic USB HID operation under Microchip&#039;s VendorID (1240) and a ProductID of 0, operating in USB_INTERRUPT mode (described below).&lt;br /&gt;
&lt;br /&gt;
[[Media:PIC32_USB_Files.zip|PIC32_USB_Files]]&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
Below is some sample code in the while loop of a main function of PIC communicating via USB.  &lt;br /&gt;
 while(1){&lt;br /&gt;
    if((USBGetDeviceState() &amp;lt; CONFIGURED_STATE) ||&lt;br /&gt;
       (USBIsDeviceSuspended() == TRUE))&lt;br /&gt;
    {&lt;br /&gt;
        //Either the device is not configured or we are suspended,&lt;br /&gt;
        // so we don&#039;t want to execute any USB related application code&lt;br /&gt;
        continue;   //go back to the top of the while loop&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDRxHandleBusy(USBOutHandle))	//Check if data was received from the host.&lt;br /&gt;
    {&lt;br /&gt;
        //Do something with the data&lt;br /&gt;
        ToSendDataBuffer[0] = ReceivedDataBuffer[0];&lt;br /&gt;
        //Re-arm the OUT endpoint for the next packet&lt;br /&gt;
        USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&amp;amp;ReceivedDataBuffer,64);&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDTxHandleBusy(USBInHandle)){&lt;br /&gt;
        // Send out new data&lt;br /&gt;
        USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&amp;amp;ToSendDataBuffer[0],64);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Demo===&lt;br /&gt;
&lt;br /&gt;
This [[Media:USBExample.zip|MPLABX Project Directory]] is an example project written for the PIC32MX250F128B.&lt;br /&gt;
&lt;br /&gt;
This demo communicated with the processing demo explained below, using the 28-DIP package for the pic32 and the following pinout.&lt;br /&gt;
&lt;br /&gt;
*Pin01 - MCLR&lt;br /&gt;
*Pin03 - Configured for OC2, connected to an LED&lt;br /&gt;
*Pin04 - PGED1&lt;br /&gt;
*Pin05 - PGEC1&lt;br /&gt;
*Pin07 - Configured for AN5, reads analog voltage from potentiometer&lt;br /&gt;
*Pin08 - Vss&lt;br /&gt;
*Pin09 - External Clock, running at 20MHz&lt;br /&gt;
*Pin12 - Configured for digital in, reads switch state&lt;br /&gt;
*Pin13 - Vdd&lt;br /&gt;
*Pin15 - Vbus, connected to USB 5 Volt line&lt;br /&gt;
*Pin16 - Configured for digital out, connected to an LED&lt;br /&gt;
*Pin18 - Configured for OC3, connected to an LED&lt;br /&gt;
*Pin19 - Vss&lt;br /&gt;
*Pin20 - Vcap, 10nF capacitor to Vss&lt;br /&gt;
*Pin21 - USB, D+&lt;br /&gt;
*Pin22 - USB, D-&lt;br /&gt;
*Pin23 - Vusb, 3.3V&lt;br /&gt;
*Pin27 - AVss, 10nF capacitor to AVdd&lt;br /&gt;
*Pin28 - AVdd, 3.3V&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  Documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].  This is the [[Media:Processing_USBExample.zip|Processing sketch]] used to interface with the demo board described above.&lt;br /&gt;
&lt;br /&gt;
The code directory contains both the .jar file containing the Java library, and the .dll, .jnilib, and .so files containing the native USB libraries.  These files need to be here, but do not require editing.  &lt;br /&gt;
&lt;br /&gt;
In the sketch itself, the &#039;&#039;&#039;USBListener&#039;&#039;&#039; file defines a Thread which listens to the USB port.  The function pic.read() will wait until the USB buffer is not empty, and then place the first-in data into the specified variable.  This thread is also a good place to update all state variables and send out data to the PIC using pic.write().  The &#039;&#039;&#039;HIDManager&#039;&#039;&#039; extends the HIDManager class defined in the API.  This class defines callback functions which are called whenever a device is added or removed.&lt;br /&gt;
&lt;br /&gt;
The main file includes the typical functions, setup() and draw(), as well as an openPic() function used to search the computer for a connected PIC microcontroller and open it for communication.&lt;br /&gt;
&lt;br /&gt;
Here is the API [[Media:HIDAPI.zip|documentation]], which explains what each function does.&lt;br /&gt;
&lt;br /&gt;
==Matlab and the HID API==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21436</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21436"/>
		<updated>2012-07-09T18:17:53Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links&lt;br /&gt;
[http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
[http://www.usbmadesimple.co.uk/index.html USB Made Simple]&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Installation===&lt;br /&gt;
&lt;br /&gt;
====Download====&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Include Libraries and Driver in Project====&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
====Add Application-Specific USB files====&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
The file attached below is a zip containing examples of these four files.  This example provides generic USB HID operation under Microchip&#039;s VendorID (1240) and a ProductID of 0, operating in USB_INTERRUPT mode (described below).&lt;br /&gt;
&lt;br /&gt;
[[Media:PIC32_USB_Files.zip|PIC32_USB_Files]]&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
Below is some sample code in the while loop of a main function of PIC communicating via USB.  &lt;br /&gt;
 while(1){&lt;br /&gt;
    if((USBGetDeviceState() &amp;lt; CONFIGURED_STATE) ||&lt;br /&gt;
       (USBIsDeviceSuspended() == TRUE))&lt;br /&gt;
    {&lt;br /&gt;
        //Either the device is not configured or we are suspended,&lt;br /&gt;
        // so we don&#039;t want to execute any USB related application code&lt;br /&gt;
        continue;   //go back to the top of the while loop&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDRxHandleBusy(USBOutHandle))	//Check if data was received from the host.&lt;br /&gt;
    {&lt;br /&gt;
        //Do something with the data&lt;br /&gt;
        ToSendDataBuffer[0] = ReceivedDataBuffer[0];&lt;br /&gt;
        //Re-arm the OUT endpoint for the next packet&lt;br /&gt;
        USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&amp;amp;ReceivedDataBuffer,64);&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDTxHandleBusy(USBInHandle)){&lt;br /&gt;
        // Send out new data&lt;br /&gt;
        USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&amp;amp;ToSendDataBuffer[0],64);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Demo===&lt;br /&gt;
&lt;br /&gt;
This [[Media:USBExample.zip|MPLABX Project Directory]] is an example project written for the PIC32MX250F128B.&lt;br /&gt;
&lt;br /&gt;
This demo communicated with the processing demo explained below, using the 28-DIP package for the pic32 and the following pinout.&lt;br /&gt;
&lt;br /&gt;
*Pin01 - MCLR&lt;br /&gt;
*Pin03 - Configured for OC2, connected to an LED&lt;br /&gt;
*Pin04 - PGED1&lt;br /&gt;
*Pin05 - PGEC1&lt;br /&gt;
*Pin07 - Configured for AN5, reads analog voltage from potentiometer&lt;br /&gt;
*Pin08 - Vss&lt;br /&gt;
*Pin09 - External Clock, running at 20MHz&lt;br /&gt;
*Pin12 - Configured for digital in, reads switch state&lt;br /&gt;
*Pin13 - Vdd&lt;br /&gt;
*Pin15 - Vbus, connected to USB 5 Volt line&lt;br /&gt;
*Pin16 - Configured for digital out, connected to an LED&lt;br /&gt;
*Pin18 - Configured for OC3, connected to an LED&lt;br /&gt;
*Pin19 - Vss&lt;br /&gt;
*Pin20 - Vcap, 10nF capacitor to Vss&lt;br /&gt;
*Pin21 - USB, D+&lt;br /&gt;
*Pin22 - USB, D-&lt;br /&gt;
*Pin23 - Vusb, 3.3V&lt;br /&gt;
*Pin27 - AVss, 10nF capacitor to AVdd&lt;br /&gt;
*Pin28 - AVdd, 3.3V&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  Documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].  This is the [[Media:Processing_USBExample.zip|Processing sketch]] used to interface with the demo board described above.&lt;br /&gt;
&lt;br /&gt;
The code directory contains both the .jar file containing the Java library, and the .dll, .jnilib, and .so files containing the native USB libraries.  These files need to be here, but do not require editing.  &lt;br /&gt;
&lt;br /&gt;
In the sketch itself, the &#039;&#039;&#039;USBListener&#039;&#039;&#039; file defines a Thread which listens to the USB port.  The function pic.read() will wait until the USB buffer is not empty, and then place the first-in data into the specified variable.  This thread is also a good place to update all state variables and send out data to the PIC using pic.write().  The &#039;&#039;&#039;HIDManager&#039;&#039;&#039; extends the HIDManager class defined in the API.  This class defines callback functions which are called whenever a device is added or removed.&lt;br /&gt;
&lt;br /&gt;
The main file includes the typical functions, setup() and draw(), as well as an openPic() function used to search the computer for a connected PIC microcontroller and open it for communication.&lt;br /&gt;
&lt;br /&gt;
Here is the API [[Media:HIDAPI.zip|documentation]], which explains what each function does.&lt;br /&gt;
&lt;br /&gt;
==Matlab and the HID API==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21435</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21435"/>
		<updated>2012-07-09T18:17:06Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Processing and the HID API */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links&lt;br /&gt;
[http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
[http://www.usbmadesimple.co.uk/index.html USB Made Simple]&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Installation===&lt;br /&gt;
&lt;br /&gt;
====Download====&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Include Libraries and Driver in Project====&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
====Add Application-Specific USB files====&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
The file attached below is a zip containing examples of these four files.  This example provides generic USB HID operation under Microchip&#039;s VendorID (1240) and a ProductID of 0, operating in USB_INTERRUPT mode (described below).&lt;br /&gt;
&lt;br /&gt;
[[Media:PIC32_USB_Files.zip|PIC32_USB_Files]]&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
Below is some sample code in the while loop of a main function of PIC communicating via USB.  &lt;br /&gt;
 while(1){&lt;br /&gt;
    if((USBGetDeviceState() &amp;lt; CONFIGURED_STATE) ||&lt;br /&gt;
       (USBIsDeviceSuspended() == TRUE))&lt;br /&gt;
    {&lt;br /&gt;
        //Either the device is not configured or we are suspended,&lt;br /&gt;
        // so we don&#039;t want to execute any USB related application code&lt;br /&gt;
        continue;   //go back to the top of the while loop&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDRxHandleBusy(USBOutHandle))	//Check if data was received from the host.&lt;br /&gt;
    {&lt;br /&gt;
        //Do something with the data&lt;br /&gt;
        ToSendDataBuffer[0] = ReceivedDataBuffer[0];&lt;br /&gt;
        //Re-arm the OUT endpoint for the next packet&lt;br /&gt;
        USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&amp;amp;ReceivedDataBuffer,64);&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDTxHandleBusy(USBInHandle)){&lt;br /&gt;
        // Send out new data&lt;br /&gt;
        USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&amp;amp;ToSendDataBuffer[0],64);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Demo===&lt;br /&gt;
&lt;br /&gt;
This [[Media:USBExample.zip|MPLABX Project Directory]] is an example project written for the PIC32MX250F128B.&lt;br /&gt;
&lt;br /&gt;
This demo communicated with the processing demo explained below, using the 28-DIP package for the pic32 and the following pinout.&lt;br /&gt;
&lt;br /&gt;
*Pin01 - MCLR&lt;br /&gt;
*Pin03 - Configured for OC2, connected to an LED&lt;br /&gt;
*Pin04 - PGED1&lt;br /&gt;
*Pin05 - PGEC1&lt;br /&gt;
*Pin07 - Configured for AN5, reads analog voltage from potentiometer&lt;br /&gt;
*Pin08 - Vss&lt;br /&gt;
*Pin09 - External Clock, running at 20MHz&lt;br /&gt;
*Pin12 - Configured for digital in, reads switch state&lt;br /&gt;
*Pin13 - Vdd&lt;br /&gt;
*Pin15 - Vbus, connected to USB 5 Volt line&lt;br /&gt;
*Pin16 - Configured for digital out, connected to an LED&lt;br /&gt;
*Pin18 - Configured for OC3, connected to an LED&lt;br /&gt;
*Pin19 - Vss&lt;br /&gt;
*Pin20 - Vcap, 10nF capacitor to Vss&lt;br /&gt;
*Pin21 - USB, D+&lt;br /&gt;
*Pin22 - USB, D-&lt;br /&gt;
*Pin23 - Vusb, 3.3V&lt;br /&gt;
*Pin27 - AVss, 10nF capacitor to AVdd&lt;br /&gt;
*Pin28 - AVdd, 3.3V&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  Documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].  This is the [[Media:Processing_USBExample.zip|Processing sketch]] used to interface with the demo board described above.&lt;br /&gt;
&lt;br /&gt;
The code directory contains both the .jar file containing the Java library, and the .dll, .jnilib, and .so files containing the native USB libraries.  These files need to be here, but do not require editing.  &lt;br /&gt;
&lt;br /&gt;
In the sketch itself, the &#039;&#039;&#039;USBListener&#039;&#039;&#039; file defines a Thread which listens to the USB port.  The function pic.read() will wait until the USB buffer is not empty, and then place the first-in data into the specified variable.  This thread is also a good place to update all state variables and send out data to the PIC using pic.write().  The &#039;&#039;&#039;HIDManager&#039;&#039;&#039; extends the HIDManager class defined in the API.  This class defines callback functions which are called whenever a device is added or removed.&lt;br /&gt;
&lt;br /&gt;
The main file includes the typical functions, setup() and draw(), as well as an openPic() function used to search the computer for a connected PIC microcontroller and open it for communication.&lt;br /&gt;
&lt;br /&gt;
Here is the API [[Media:HIDAPI.zip|documentation]], which explains what each function does.&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:HIDAPI.zip&amp;diff=21434</id>
		<title>File:HIDAPI.zip</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:HIDAPI.zip&amp;diff=21434"/>
		<updated>2012-07-09T18:14:02Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: HID API Documentation&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;HID API Documentation&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21433</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21433"/>
		<updated>2012-07-09T17:27:51Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Processing and the HID API */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links&lt;br /&gt;
[http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
[http://www.usbmadesimple.co.uk/index.html USB Made Simple]&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Installation===&lt;br /&gt;
&lt;br /&gt;
====Download====&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Include Libraries and Driver in Project====&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
====Add Application-Specific USB files====&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
The file attached below is a zip containing examples of these four files.  This example provides generic USB HID operation under Microchip&#039;s VendorID (1240) and a ProductID of 0, operating in USB_INTERRUPT mode (described below).&lt;br /&gt;
&lt;br /&gt;
[[Media:PIC32_USB_Files.zip|PIC32_USB_Files]]&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
Below is some sample code in the while loop of a main function of PIC communicating via USB.  &lt;br /&gt;
 while(1){&lt;br /&gt;
    if((USBGetDeviceState() &amp;lt; CONFIGURED_STATE) ||&lt;br /&gt;
       (USBIsDeviceSuspended() == TRUE))&lt;br /&gt;
    {&lt;br /&gt;
        //Either the device is not configured or we are suspended,&lt;br /&gt;
        // so we don&#039;t want to execute any USB related application code&lt;br /&gt;
        continue;   //go back to the top of the while loop&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDRxHandleBusy(USBOutHandle))	//Check if data was received from the host.&lt;br /&gt;
    {&lt;br /&gt;
        //Do something with the data&lt;br /&gt;
        ToSendDataBuffer[0] = ReceivedDataBuffer[0];&lt;br /&gt;
        //Re-arm the OUT endpoint for the next packet&lt;br /&gt;
        USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&amp;amp;ReceivedDataBuffer,64);&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDTxHandleBusy(USBInHandle)){&lt;br /&gt;
        // Send out new data&lt;br /&gt;
        USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&amp;amp;ToSendDataBuffer[0],64);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Demo===&lt;br /&gt;
&lt;br /&gt;
This [[Media:USBExample.zip|MPLABX Project Directory]] is an example project written for the PIC32MX250F128B.&lt;br /&gt;
&lt;br /&gt;
This demo communicated with the processing demo explained below, using the 28-DIP package for the pic32 and the following pinout.&lt;br /&gt;
&lt;br /&gt;
*Pin01 - MCLR&lt;br /&gt;
*Pin03 - Configured for OC2, connected to an LED&lt;br /&gt;
*Pin04 - PGED1&lt;br /&gt;
*Pin05 - PGEC1&lt;br /&gt;
*Pin07 - Configured for AN5, reads analog voltage from potentiometer&lt;br /&gt;
*Pin08 - Vss&lt;br /&gt;
*Pin09 - External Clock, running at 20MHz&lt;br /&gt;
*Pin12 - Configured for digital in, reads switch state&lt;br /&gt;
*Pin13 - Vdd&lt;br /&gt;
*Pin15 - Vbus, connected to USB 5 Volt line&lt;br /&gt;
*Pin16 - Configured for digital out, connected to an LED&lt;br /&gt;
*Pin18 - Configured for OC3, connected to an LED&lt;br /&gt;
*Pin19 - Vss&lt;br /&gt;
*Pin20 - Vcap, 10nF capacitor to Vss&lt;br /&gt;
*Pin21 - USB, D+&lt;br /&gt;
*Pin22 - USB, D-&lt;br /&gt;
*Pin23 - Vusb, 3.3V&lt;br /&gt;
*Pin27 - AVss, 10nF capacitor to AVdd&lt;br /&gt;
*Pin28 - AVdd, 3.3V&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  Documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].  This is the [[Media:Processing_USBExample.zip|Processing sketch]] used to interface with the demo board described above.&lt;br /&gt;
&lt;br /&gt;
The code directory contains both the .jar file containing the Java library, and the .dll, .jnilib, and .so files containing the native USB libraries.  These files need to be here, but do not require editing.  &lt;br /&gt;
&lt;br /&gt;
In the sketch itself, the &#039;&#039;&#039;USBListener&#039;&#039;&#039; file defines a Thread which listens to the USB port.  The function pic.read() will wait until the USB buffer is not empty, and then place the first-in data into the specified variable.  This thread is also a good place to update all state variables and send out data to the PIC using pic.write().  The &#039;&#039;&#039;HIDManager&#039;&#039;&#039; extends the HIDManager class defined in the API.  This class defines callback functions which are called whenever a device is added or removed.&lt;br /&gt;
&lt;br /&gt;
The main file includes the typical functions, setup() and draw(), as well as an openPic() function used to search the computer for a connected PIC microcontroller and open it for communication.&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21432</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21432"/>
		<updated>2012-07-09T17:20:06Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Demo */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links&lt;br /&gt;
[http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
[http://www.usbmadesimple.co.uk/index.html USB Made Simple]&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Installation===&lt;br /&gt;
&lt;br /&gt;
====Download====&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Include Libraries and Driver in Project====&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
====Add Application-Specific USB files====&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
The file attached below is a zip containing examples of these four files.  This example provides generic USB HID operation under Microchip&#039;s VendorID (1240) and a ProductID of 0, operating in USB_INTERRUPT mode (described below).&lt;br /&gt;
&lt;br /&gt;
[[Media:PIC32_USB_Files.zip|PIC32_USB_Files]]&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
Below is some sample code in the while loop of a main function of PIC communicating via USB.  &lt;br /&gt;
 while(1){&lt;br /&gt;
    if((USBGetDeviceState() &amp;lt; CONFIGURED_STATE) ||&lt;br /&gt;
       (USBIsDeviceSuspended() == TRUE))&lt;br /&gt;
    {&lt;br /&gt;
        //Either the device is not configured or we are suspended,&lt;br /&gt;
        // so we don&#039;t want to execute any USB related application code&lt;br /&gt;
        continue;   //go back to the top of the while loop&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDRxHandleBusy(USBOutHandle))	//Check if data was received from the host.&lt;br /&gt;
    {&lt;br /&gt;
        //Do something with the data&lt;br /&gt;
        ToSendDataBuffer[0] = ReceivedDataBuffer[0];&lt;br /&gt;
        //Re-arm the OUT endpoint for the next packet&lt;br /&gt;
        USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&amp;amp;ReceivedDataBuffer,64);&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDTxHandleBusy(USBInHandle)){&lt;br /&gt;
        // Send out new data&lt;br /&gt;
        USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&amp;amp;ToSendDataBuffer[0],64);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Demo===&lt;br /&gt;
&lt;br /&gt;
This [[Media:USBExample.zip|MPLABX Project Directory]] is an example project written for the PIC32MX250F128B.&lt;br /&gt;
&lt;br /&gt;
This demo communicated with the processing demo explained below, using the 28-DIP package for the pic32 and the following pinout.&lt;br /&gt;
&lt;br /&gt;
*Pin01 - MCLR&lt;br /&gt;
*Pin03 - Configured for OC2, connected to an LED&lt;br /&gt;
*Pin04 - PGED1&lt;br /&gt;
*Pin05 - PGEC1&lt;br /&gt;
*Pin07 - Configured for AN5, reads analog voltage from potentiometer&lt;br /&gt;
*Pin08 - Vss&lt;br /&gt;
*Pin09 - External Clock, running at 20MHz&lt;br /&gt;
*Pin12 - Configured for digital in, reads switch state&lt;br /&gt;
*Pin13 - Vdd&lt;br /&gt;
*Pin15 - Vbus, connected to USB 5 Volt line&lt;br /&gt;
*Pin16 - Configured for digital out, connected to an LED&lt;br /&gt;
*Pin18 - Configured for OC3, connected to an LED&lt;br /&gt;
*Pin19 - Vss&lt;br /&gt;
*Pin20 - Vcap, 10nF capacitor to Vss&lt;br /&gt;
*Pin21 - USB, D+&lt;br /&gt;
*Pin22 - USB, D-&lt;br /&gt;
*Pin23 - Vusb, 3.3V&lt;br /&gt;
*Pin27 - AVss, 10nF capacitor to AVdd&lt;br /&gt;
*Pin28 - AVdd, 3.3V&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  Documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].  Attached is the Processing sketch used to interface with the demo board described above.&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21431</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21431"/>
		<updated>2012-07-09T17:19:01Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Add Application-Specific USB files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links&lt;br /&gt;
[http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
[http://www.usbmadesimple.co.uk/index.html USB Made Simple]&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Installation===&lt;br /&gt;
&lt;br /&gt;
====Download====&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Include Libraries and Driver in Project====&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
====Add Application-Specific USB files====&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
The file attached below is a zip containing examples of these four files.  This example provides generic USB HID operation under Microchip&#039;s VendorID (1240) and a ProductID of 0, operating in USB_INTERRUPT mode (described below).&lt;br /&gt;
&lt;br /&gt;
[[Media:PIC32_USB_Files.zip|PIC32_USB_Files]]&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
Below is some sample code in the while loop of a main function of PIC communicating via USB.  &lt;br /&gt;
 while(1){&lt;br /&gt;
    if((USBGetDeviceState() &amp;lt; CONFIGURED_STATE) ||&lt;br /&gt;
       (USBIsDeviceSuspended() == TRUE))&lt;br /&gt;
    {&lt;br /&gt;
        //Either the device is not configured or we are suspended,&lt;br /&gt;
        // so we don&#039;t want to execute any USB related application code&lt;br /&gt;
        continue;   //go back to the top of the while loop&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDRxHandleBusy(USBOutHandle))	//Check if data was received from the host.&lt;br /&gt;
    {&lt;br /&gt;
        //Do something with the data&lt;br /&gt;
        ToSendDataBuffer[0] = ReceivedDataBuffer[0];&lt;br /&gt;
        //Re-arm the OUT endpoint for the next packet&lt;br /&gt;
        USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&amp;amp;ReceivedDataBuffer,64);&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDTxHandleBusy(USBInHandle)){&lt;br /&gt;
        // Send out new data&lt;br /&gt;
        USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&amp;amp;ToSendDataBuffer[0],64);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Demo===&lt;br /&gt;
&lt;br /&gt;
Attached here is an example project for the PIC32MX250F128B.  [[Media:USBExample.zip]]&lt;br /&gt;
&lt;br /&gt;
This demo communicated with the processing demo explained below, using the 28-DIP package for the pic32 and the following pinout.&lt;br /&gt;
&lt;br /&gt;
*Pin01 - MCLR&lt;br /&gt;
*Pin03 - Configured for OC2, connected to an LED&lt;br /&gt;
*Pin04 - PGED1&lt;br /&gt;
*Pin05 - PGEC1&lt;br /&gt;
*Pin07 - Configured for AN5, reads analog voltage from potentiometer&lt;br /&gt;
*Pin08 - Vss&lt;br /&gt;
*Pin09 - External Clock, running at 20MHz&lt;br /&gt;
*Pin12 - Configured for digital in, reads switch state&lt;br /&gt;
*Pin13 - Vdd&lt;br /&gt;
*Pin15 - Vbus, connected to USB 5 Volt line&lt;br /&gt;
*Pin16 - Configured for digital out, connected to an LED&lt;br /&gt;
*Pin18 - Configured for OC3, connected to an LED&lt;br /&gt;
*Pin19 - Vss&lt;br /&gt;
*Pin20 - Vcap, 10nF capacitor to Vss&lt;br /&gt;
*Pin21 - USB, D+&lt;br /&gt;
*Pin22 - USB, D-&lt;br /&gt;
*Pin23 - Vusb, 3.3V&lt;br /&gt;
*Pin27 - AVss, 10nF capacitor to AVdd&lt;br /&gt;
*Pin28 - AVdd, 3.3V&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  Documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].  Attached is the Processing sketch used to interface with the demo board described above.&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:Processing_USBExample.zip&amp;diff=21430</id>
		<title>File:Processing USBExample.zip</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:Processing_USBExample.zip&amp;diff=21430"/>
		<updated>2012-07-09T17:16:49Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: Example Processing sketch for communicating via native USB&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Example Processing sketch for communicating via native USB&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21429</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21429"/>
		<updated>2012-07-09T17:00:18Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links&lt;br /&gt;
[http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
[http://www.usbmadesimple.co.uk/index.html USB Made Simple]&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Installation===&lt;br /&gt;
&lt;br /&gt;
====Download====&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Include Libraries and Driver in Project====&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
====Add Application-Specific USB files====&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
The file attached below is a zip containing examples of these four files.  This example provides generic USB HID operation under Microchip&#039;s VendorID (1240) and a ProductID of 0, operating in USB_INTERRUPT mode (described below).&lt;br /&gt;
&lt;br /&gt;
[[Media:PIC32_USB_Files.zip]]&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
Below is some sample code in the while loop of a main function of PIC communicating via USB.  &lt;br /&gt;
 while(1){&lt;br /&gt;
    if((USBGetDeviceState() &amp;lt; CONFIGURED_STATE) ||&lt;br /&gt;
       (USBIsDeviceSuspended() == TRUE))&lt;br /&gt;
    {&lt;br /&gt;
        //Either the device is not configured or we are suspended,&lt;br /&gt;
        // so we don&#039;t want to execute any USB related application code&lt;br /&gt;
        continue;   //go back to the top of the while loop&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDRxHandleBusy(USBOutHandle))	//Check if data was received from the host.&lt;br /&gt;
    {&lt;br /&gt;
        //Do something with the data&lt;br /&gt;
        ToSendDataBuffer[0] = ReceivedDataBuffer[0];&lt;br /&gt;
        //Re-arm the OUT endpoint for the next packet&lt;br /&gt;
        USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&amp;amp;ReceivedDataBuffer,64);&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDTxHandleBusy(USBInHandle)){&lt;br /&gt;
        // Send out new data&lt;br /&gt;
        USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&amp;amp;ToSendDataBuffer[0],64);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Demo===&lt;br /&gt;
&lt;br /&gt;
Attached here is an example project for the PIC32MX250F128B.  [[Media:USBExample.zip]]&lt;br /&gt;
&lt;br /&gt;
This demo communicated with the processing demo explained below, using the 28-DIP package for the pic32 and the following pinout.&lt;br /&gt;
&lt;br /&gt;
*Pin01 - MCLR&lt;br /&gt;
*Pin03 - Configured for OC2, connected to an LED&lt;br /&gt;
*Pin04 - PGED1&lt;br /&gt;
*Pin05 - PGEC1&lt;br /&gt;
*Pin07 - Configured for AN5, reads analog voltage from potentiometer&lt;br /&gt;
*Pin08 - Vss&lt;br /&gt;
*Pin09 - External Clock, running at 20MHz&lt;br /&gt;
*Pin12 - Configured for digital in, reads switch state&lt;br /&gt;
*Pin13 - Vdd&lt;br /&gt;
*Pin15 - Vbus, connected to USB 5 Volt line&lt;br /&gt;
*Pin16 - Configured for digital out, connected to an LED&lt;br /&gt;
*Pin18 - Configured for OC3, connected to an LED&lt;br /&gt;
*Pin19 - Vss&lt;br /&gt;
*Pin20 - Vcap, 10nF capacitor to Vss&lt;br /&gt;
*Pin21 - USB, D+&lt;br /&gt;
*Pin22 - USB, D-&lt;br /&gt;
*Pin23 - Vusb, 3.3V&lt;br /&gt;
*Pin27 - AVss, 10nF capacitor to AVdd&lt;br /&gt;
*Pin28 - AVdd, 3.3V&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;br /&gt;
&lt;br /&gt;
To send and receive USB packets in Processing on the computer, we use a Java Native Interface library which uses C libraries to communicate via USB.  Documentation and downloads for this library can be found [http://code.google.com/p/javahidapi/ here].  Attached is the Processing sketch used to interface with the demo board described above.&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21428</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21428"/>
		<updated>2012-07-09T16:41:33Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links&lt;br /&gt;
[http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
[http://www.usbmadesimple.co.uk/index.html USB Made Simple]&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Installation===&lt;br /&gt;
&lt;br /&gt;
====Download====&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Include Libraries and Driver in Project====&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
====Add Application-Specific USB files====&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
The file attached below is a zip containing examples of these four files.  This example provides generic USB HID operation under Microchip&#039;s VendorID (1240) and a ProductID of 0, operating in USB_INTERRUPT mode (described below).&lt;br /&gt;
&lt;br /&gt;
[[Media:PIC32_USB_Files.zip]]&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
Below is some sample code in the while loop of a main function of PIC communicating via USB.  &lt;br /&gt;
 while(1){&lt;br /&gt;
    if((USBGetDeviceState() &amp;lt; CONFIGURED_STATE) ||&lt;br /&gt;
       (USBIsDeviceSuspended() == TRUE))&lt;br /&gt;
    {&lt;br /&gt;
        //Either the device is not configured or we are suspended,&lt;br /&gt;
        // so we don&#039;t want to execute any USB related application code&lt;br /&gt;
        continue;   //go back to the top of the while loop&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDRxHandleBusy(USBOutHandle))	//Check if data was received from the host.&lt;br /&gt;
    {&lt;br /&gt;
        //Do something with the data&lt;br /&gt;
        ToSendDataBuffer[0] = ReceivedDataBuffer[0];&lt;br /&gt;
        //Re-arm the OUT endpoint for the next packet&lt;br /&gt;
        USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&amp;amp;ReceivedDataBuffer,64);&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDTxHandleBusy(USBInHandle)){&lt;br /&gt;
        // Send out new data&lt;br /&gt;
        USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&amp;amp;ToSendDataBuffer[0],64);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Demo===&lt;br /&gt;
&lt;br /&gt;
Attached here is an example project for the PIC32MX250F128B.  [[Media:USBExample.zip]]&lt;br /&gt;
&lt;br /&gt;
This demo communicated with the processing demo explained below, using the 28-DIP package for the pic32 and the following pinout.&lt;br /&gt;
&lt;br /&gt;
*Pin01 - MCLR&lt;br /&gt;
*Pin03 - Configured for OC2, connected to an LED&lt;br /&gt;
*Pin04 - PGED1&lt;br /&gt;
*Pin05 - PGEC1&lt;br /&gt;
*Pin07 - Configured for AN5, reads analog voltage from potentiometer&lt;br /&gt;
*Pin08 - Vss&lt;br /&gt;
*Pin09 - External Clock, running at 20MHz&lt;br /&gt;
*Pin12 - Configured for digital in, reads switch state&lt;br /&gt;
*Pin13 - Vdd&lt;br /&gt;
*Pin15 - Vbus, connected to USB 5 Volt line&lt;br /&gt;
*Pin16 - Configured for digital out, connected to an LED&lt;br /&gt;
*Pin18 - Configured for OC3, connected to an LED&lt;br /&gt;
*Pin19 - Vss&lt;br /&gt;
*Pin20 - Vcap, 10nF capacitor to Vss&lt;br /&gt;
*Pin21 - USB, D+&lt;br /&gt;
*Pin22 - USB, D-&lt;br /&gt;
*Pin23 - Vusb, 3.3V&lt;br /&gt;
*Pin27 - AVss, 10nF capacitor to AVdd&lt;br /&gt;
*Pin28 - AVdd, 3.3V&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:USBExample.zip&amp;diff=21427</id>
		<title>File:USBExample.zip</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:USBExample.zip&amp;diff=21427"/>
		<updated>2012-07-09T16:23:39Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: Example project for USB communication with PIC32MX250F128B&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Example project for USB communication with PIC32MX250F128B&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21426</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21426"/>
		<updated>2012-07-09T15:17:42Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links&lt;br /&gt;
[http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
[http://www.usbmadesimple.co.uk/index.html USB Made Simple]&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Installation===&lt;br /&gt;
&lt;br /&gt;
====Download====&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Include Libraries and Driver in Project====&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
====Add Application-Specific USB files====&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
The file attached below is a zip containing examples of these four files.  This example provides generic USB HID operation under Microchip&#039;s VendorID (1240) and a ProductID of 0, operating in USB_INTERRUPT mode (described below).&lt;br /&gt;
&lt;br /&gt;
[[Media:PIC32_USB_Files.zip]]&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
Below is some sample code in the while loop of a main function of PIC communicating via USB.  &lt;br /&gt;
 while(1){&lt;br /&gt;
    if((USBGetDeviceState() &amp;lt; CONFIGURED_STATE) ||&lt;br /&gt;
       (USBIsDeviceSuspended() == TRUE))&lt;br /&gt;
    {&lt;br /&gt;
        //Either the device is not configured or we are suspended,&lt;br /&gt;
        // so we don&#039;t want to execute any USB related application code&lt;br /&gt;
        continue;   //go back to the top of the while loop&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDRxHandleBusy(USBOutHandle))	//Check if data was received from the host.&lt;br /&gt;
    {&lt;br /&gt;
        //Do something with the data&lt;br /&gt;
        ToSendDataBuffer[0] = ReceivedDataBuffer[0];&lt;br /&gt;
        //Re-arm the OUT endpoint for the next packet&lt;br /&gt;
        USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&amp;amp;ReceivedDataBuffer,64);&lt;br /&gt;
    }&lt;br /&gt;
    if(!HIDTxHandleBusy(USBInHandle)){&lt;br /&gt;
        // Send out new data&lt;br /&gt;
        USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&amp;amp;ToSendDataBuffer[0],64);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21425</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21425"/>
		<updated>2012-07-03T23:11:25Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Add Application-Specific USB files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links&lt;br /&gt;
[http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
[http://www.usbmadesimple.co.uk/index.html USB Made Simple]&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Installation===&lt;br /&gt;
&lt;br /&gt;
====Download====&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Include Libraries and Driver in Project====&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
====Add Application-Specific USB files====&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
The file attached below is a zip containing examples of these four files.  This example provides generic USB HID operation under Microchip&#039;s VendorID (1240) and a ProductID of 0, operating in USB_INTERRUPT mode (described below).&lt;br /&gt;
&lt;br /&gt;
[[Media:PIC32_USB_Files.zip]]&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21424</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21424"/>
		<updated>2012-07-03T22:46:47Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Microchip USB Stack */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links&lt;br /&gt;
[http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
[http://www.usbmadesimple.co.uk/index.html USB Made Simple]&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Installation===&lt;br /&gt;
&lt;br /&gt;
====Download====&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Include Libraries and Driver in Project====&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
====Add Application-Specific USB files====&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
The file attached below is a zip containing examples of these four files.  This example provides generic USB HID operation under Microchip&#039;s VendorID (1240) and a ProductID of 0, operating in USB_INTERRUPT mode (described below).&lt;br /&gt;
&lt;br /&gt;
[[File:PIC32_USB_Files.zip]]&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21423</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21423"/>
		<updated>2012-07-03T22:45:36Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links&lt;br /&gt;
[http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
[http://www.usbmadesimple.co.uk/index.html USB Made Simple]&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Download===&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Include Libraries and Driver in Project===&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Add Application-Specific USB files===&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
The file attached below is a zip containing examples of these four files.  This example provides generic USB HID operation under Microchip&#039;s VendorID (1240) and a ProductID of 0, operating in USB_INTERRUPT mode (described below).&lt;br /&gt;
&lt;br /&gt;
[[File:PIC32_USB_Files.zip]]&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:PIC32_USB_Files.zip&amp;diff=21422</id>
		<title>File:PIC32 USB Files.zip</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:PIC32_USB_Files.zip&amp;diff=21422"/>
		<updated>2012-07-03T22:40:42Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: Application specific files for using the Microchip USB Stack&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Application specific files for using the Microchip USB Stack&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21421</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21421"/>
		<updated>2012-07-03T22:28:31Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* USB Hardware Notes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links&lt;br /&gt;
[http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
[http://www.usbmadesimple.co.uk/index.html USB Made Simple]&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Download===&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Include Libraries and Driver in Project===&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Add Application-Specific USB files===&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21420</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21420"/>
		<updated>2012-07-03T22:18:50Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Protocol */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links&lt;br /&gt;
[http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
[http://www.usbmadesimple.co.uk/index.html USB Made Simple]&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Download===&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Include Libraries and Driver in Project===&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Add Application-Specific USB files===&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is rather important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21419</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21419"/>
		<updated>2012-07-03T22:13:19Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* USB Stack Operation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links [http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these details are handled by the USB modules themselves, and the developer seldom needs to know these things.  However, they are useful to those who are debugging hardware issues.  More information about USB can be found at the [http://www.usbmadesimple.co.uk/index.html USB Made Simple] site.&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Download===&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Include Libraries and Driver in Project===&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Add Application-Specific USB files===&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is rather important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles USB events, host requests, and enumeration.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21418</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21418"/>
		<updated>2012-07-03T22:12:18Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links [http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these details are handled by the USB modules themselves, and the developer seldom needs to know these things.  However, they are useful to those who are debugging hardware issues.  More information about USB can be found at the [http://www.usbmadesimple.co.uk/index.html USB Made Simple] site.&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Download===&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Include Libraries and Driver in Project===&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Add Application-Specific USB files===&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
=== Microchip Stack Bug Notes ===&lt;br /&gt;
&lt;br /&gt;
For the pic32mx2xx series, the usb interrupts are not configured correctly in the Microchip stack, so the following changes need to be made.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/Include/usb_hal_pic32.h (Line 365):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This section normally sets the bits to configure IPC11, the USB interrupt location, but on the pic32mx2xx series, the USB interrupt bits are located at IPC7, as detailed in the Interrupt Controller chapter of the PIC datasheet.  We&#039;ve added a precompiler if statement to detect if a specific chip is being used.  It&#039;s simple to change this for your specific chip though.&lt;br /&gt;
&lt;br /&gt;
 #if defined(USB_INTERRUPT)&lt;br /&gt;
     // This is edited for use with the PIC32MX250F128B&lt;br /&gt;
     #ifdef __32MX250F128B__&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC7CLR=0x00FF0000;IPC7SET=0x00100000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #else&lt;br /&gt;
         #define USBEnableInterrupts() {IEC1bits.USBIE = 1; IPC11CLR=0x0000FF00;IPC11SET=0x00001000; INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();}&lt;br /&gt;
     #endif&lt;br /&gt;
 #else&lt;br /&gt;
     #define USBEnableInterrupts()&lt;br /&gt;
 #endif&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;In &amp;lt;MAL&amp;gt;/Microchip/USB/usb_device.c (Line 673)&#039;&#039;&#039;&lt;br /&gt;
The Microchip stack assumes that the USB interrupt vector is 45 but this has changed to 30 for the pic32mx2xx series.&lt;br /&gt;
&lt;br /&gt;
The line:&lt;br /&gt;
 void __attribute__((interrupt(),vector(45))) _USB1Interrupt( void )  &lt;br /&gt;
  &lt;br /&gt;
Should be: &lt;br /&gt;
 void __attribute__((interrupt(),vector(_USB_1_VECTOR))) _USB1Interrupt( void )  &lt;br /&gt;
&lt;br /&gt;
=== USB Hardware Notes ===&lt;br /&gt;
&lt;br /&gt;
It is rather important to use an external clock or oscillator in order to use the USB module.  A 48 MHz clock is required for proper operation.  The clock configuration can be seen in the data sheet, but for the pic32mx2xx series a 4 Mhz clock is required before the USB PLL.  Since I am using a 20MHz external clock, we use these lines to set DEVCFG2 to proper values.&lt;br /&gt;
 #pragma config UPLLEN   = ON        // USB PLL Enable&lt;br /&gt;
 #pragma config UPLLIDIV = DIV_5     // USB PLL Divider&lt;br /&gt;
 // 20MHz / 5 * 24 / 2 = 48MHz&lt;br /&gt;
&lt;br /&gt;
=== USB Stack Operation ===&lt;br /&gt;
&lt;br /&gt;
A function called USBDeviceTasks() defined in usb_device.c handles enumeration and detection with the PC.  This function must be called every ~1.8ms during enumeration, and at specific intervals during other operation.  This timing can be faster than the minimum with no hitch, but slower causes failure in communication.  The stack may operate in two modes, USB_POLLING, or USB_INTERRUPT as defined in usb_config.h.&lt;br /&gt;
&lt;br /&gt;
USB_POLLING requires the developer to call USBDeviceTasks() manually faster than the minimum rate, whereas USB_INTERRUPT handles all timing automatically.  In the demo, we use USB_INTERRUPT to handle this timing.&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21417</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21417"/>
		<updated>2012-07-03T21:31:01Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Add Application-Specific USB files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links [http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these details are handled by the USB modules themselves, and the developer seldom needs to know these things.  However, they are useful to those who are debugging hardware issues.  More information about USB can be found at the [http://www.usbmadesimple.co.uk/index.html USB Made Simple] site.&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Download===&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Include Libraries and Driver in Project===&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Add Application-Specific USB files===&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.h - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21395</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21395"/>
		<updated>2012-06-29T15:13:33Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links [http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these details are handled by the USB modules themselves, and the developer seldom needs to know these things.  However, they are useful to those who are debugging hardware issues.  More information about USB can be found at the [http://www.usbmadesimple.co.uk/index.html USB Made Simple] site.&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Download===&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Include Libraries and Driver in Project===&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Add Application-Specific USB files===&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.c - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21393</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21393"/>
		<updated>2012-06-28T05:09:11Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links [http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these details are handled by the USB modules themselves, and the developer seldom needs to know these things.  However, they are useful to those who are debugging hardware issues.  More information about USB can be found at the [http://www.usbmadesimple.co.uk/index.html USB Made Simple] site.&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Download===&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Include Libraries and Driver in Project===&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Add Application-Specific USB files===&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional. &lt;br /&gt;
&lt;br /&gt;
*usb_config.c - provides configuration details for the mode of USB operation&lt;br /&gt;
*HardwareProfile.h - gives information about the specific microcontroller we&#039;re using&lt;br /&gt;
*usb_descriptors.c - defines all the descriptors used to tell the Host PC what kind of USB device we&#039;re connecting&lt;br /&gt;
*usb_callbacks.c - defines all callback functions executed when the driver detects certain conditions in the connection&lt;br /&gt;
&lt;br /&gt;
===USB Enumeration===&lt;br /&gt;
&lt;br /&gt;
===HID Descriptors===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Processing and the HID API==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21392</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21392"/>
		<updated>2012-06-28T04:45:35Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Add Application-Specific USB files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links [http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these details are handled by the USB modules themselves, and the developer seldom needs to know these things.  However, they are useful to those who are debugging hardware issues.  More information about USB can be found at the [http://www.usbmadesimple.co.uk/index.html USB Made Simple] site.&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Download===&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Include Libraries and Driver in Project===&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Add Application-Specific USB files===&lt;br /&gt;
&lt;br /&gt;
Two additional header files and two additional source files specific to each application are required for the microcontroller to be fully USB functional.&lt;br /&gt;
&lt;br /&gt;
==USB Enumeration==&lt;br /&gt;
&lt;br /&gt;
==HID Descriptors==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21391</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21391"/>
		<updated>2012-06-27T16:10:34Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
Useful Links [http://www.usb.org/developers/devclass_docs/HID1_11.pdf HID Specification]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these details are handled by the USB modules themselves, and the developer seldom needs to know these things.  However, they are useful to those who are debugging hardware issues.  More information about USB can be found at the [http://www.usbmadesimple.co.uk/index.html USB Made Simple] site.&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Download===&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Include Libraries and Driver in Project===&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Add Application-Specific USB files===&lt;br /&gt;
&lt;br /&gt;
==USB Enumeration==&lt;br /&gt;
&lt;br /&gt;
==HID Descriptors==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21390</id>
		<title>USB Communication using PIC microcontrollers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=USB_Communication_using_PIC_microcontrollers&amp;diff=21390"/>
		<updated>2012-06-22T19:28:42Z</updated>

		<summary type="html">&lt;p&gt;DavidMeyer: /* Microchip USB Stack */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;***Under Construction***&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==USB Overview==&lt;br /&gt;
&lt;br /&gt;
Universal Serial Bus, or USB, is a very common method of communication between electronic devices.  USB is universal, meaning that it&#039;s standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses.  A simple USB connection requires a host and a device.  The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection.  For our purposes, the USB host will be the PC and the USB device will be a PIC chip.&lt;br /&gt;
&lt;br /&gt;
There are many different speeds associated with USB.  USB 1.0 introduced data rates of 1.5 Mbits/s (&#039;&#039;&#039;Low-Speed&#039;&#039;&#039;) and 12 Mbits/s (&#039;&#039;&#039;Full-Speed&#039;&#039;&#039;).  With USB 2.0 and 3.0, data rates of 480 Mbits/s (&#039;&#039;&#039;Hi-Speed&#039;&#039;&#039;) and 5 Gbits/s (&#039;&#039;&#039;Super-Speed&#039;&#039;&#039;), were added respectively.  PIC chips have hardware capable of full speed USB.&lt;br /&gt;
&lt;br /&gt;
===Hardware===&lt;br /&gt;
&lt;br /&gt;
A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white).  You may notice that many USB plugs and receptacles have 5 pins.  This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go.  OTG allows devices to also act as a host.  Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.&lt;br /&gt;
&lt;br /&gt;
The two data lines, D+ and D-, provide information about the state of the USB link.&lt;br /&gt;
&lt;br /&gt;
*Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.  &lt;br /&gt;
*Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.&lt;br /&gt;
*K State - opposite polarity from J State.  To communicate, the host or device alternates the device between the J and K states in specific patterns.&lt;br /&gt;
*Illegal State, or SE1 - both data lines are high.  If this happens, there is a hardware problem with the USB link.&lt;br /&gt;
&lt;br /&gt;
===Protocol===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these details are handled by the USB modules themselves, and the developer seldom needs to know these things.  However, they are useful to those who are debugging hardware issues.  More information about USB can be found at the [http://www.usbmadesimple.co.uk/index.html USB Made Simple] site.&lt;br /&gt;
&lt;br /&gt;
==Microchip USB Stack==&lt;br /&gt;
&lt;br /&gt;
Microchip has provided almost all of the code and examples to run USB code on PIC chips.  This code is called the USB stack, and we will use this as the main library for communicating to PCs.  Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB.  This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Download===&lt;br /&gt;
&lt;br /&gt;
We&#039;ll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code.  The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website [http://www.microchip.com/MAL/ here].&lt;br /&gt;
&lt;br /&gt;
You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (&#039;&#039;&#039;**FIND OUT WHERE**&#039;&#039;&#039;) on a Mac.  The name of the root directory should be something like &amp;quot;Microchip Solutions v2012...&amp;quot;, but from here on out, we&#039;ll refer to the root directory of that installation as &amp;lt;MAL&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Include Libraries and Driver in Project===&lt;br /&gt;
&lt;br /&gt;
Once the installer has finished running, the actual USB stack is located at &amp;lt;MAL&amp;gt;/Microchip/USB.  &amp;lt;MAL&amp;gt;/USB is not the stack, and only contains demos of the firmware for learning purposes.  We will not be using any of those files for our purposes.  After starting a project in MPLAB for your PIC chip, you must complete the following steps:&lt;br /&gt;
&lt;br /&gt;
*Include the USB libraries in your project&lt;br /&gt;
**In MPLAB X, right-click on your project and select properties.  In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips).  In the main window, find the field called &amp;quot;Include Directories&amp;quot; and click the button with &amp;quot;...&amp;quot;  Add the directories &amp;lt;MAL&amp;gt;/Microchip/USB, &amp;lt;MAL&amp;gt;/Microchip/Include, and &#039;&#039;&#039;importantly&#039;&#039;&#039; your &#039;&#039;current project directory&#039;&#039;.  This last one is important because the stack references a Hardware Profile which we will include in our own project directory.&lt;br /&gt;
*Add the HID device drivers to your project&lt;br /&gt;
**In MPLAB X, right-click on your project source files and select &amp;quot;add existing...&amp;quot;  Browse to &amp;lt;MAL&amp;gt;/Microchip/USB and add &#039;&#039;&#039;usb_device.c&#039;&#039;&#039; and &#039;&#039;&#039;./HID Device Driver/usb_function_hid.c&#039;&#039;&#039;.  These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.&lt;br /&gt;
&lt;br /&gt;
===Add Application-Specific USB files===&lt;br /&gt;
&lt;br /&gt;
==USB Enumeration==&lt;br /&gt;
&lt;br /&gt;
==HID Descriptors==&lt;/div&gt;</summary>
		<author><name>DavidMeyer</name></author>
	</entry>
</feed>