<?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=Andrew+Long</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=Andrew+Long"/>
	<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php/Special:Contributions/Andrew_Long"/>
	<updated>2026-04-10T11:27:54Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.9</generator>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:NU32v2_Encoder.c&amp;diff=20209</id>
		<title>File:NU32v2 Encoder.c</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:NU32v2_Encoder.c&amp;diff=20209"/>
		<updated>2011-02-09T17:33:35Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20208</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20208"/>
		<updated>2011-02-09T17:32:41Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Sample Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder|Rotary encoders]] create quadrature signals on two channels (A and B) corresponding to the speed and direction of a motor. Some encoders produce up to a million counts per revolution. For very fast quadrature signals there is no way that a software quadrature decoder can avoid overrun errors. Instead we may use the LS7183 chip to translate the A and B quadrature signals into brief pulses indicating up-count or down-count. The up and down counts can be configured as external input signals to increment counters on the PIC32. By reading the counters on the PIC, we can determine the angle of the motor.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LS7183.png|center|thumb|400 px]]&lt;br /&gt;
&lt;br /&gt;
The resistor bias is used to control the pulse width of the up and down counts. The mode (1X, 2X, 4X) is used to multiply the quadrature clock rate by 1, 2 or 4. The mode is selected by connecting MODE to&lt;br /&gt;
* VSS (GND)  -&amp;gt; 1X&lt;br /&gt;
* VDD (3.3V) -&amp;gt; 2X&lt;br /&gt;
* Float (don&amp;#039;t connect to anything) -&amp;gt; 4X &lt;br /&gt;
&lt;br /&gt;
The UP and DOWN count pins should be connected to external counters on the PIC32.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
The full code can be downloaded [[Media:NU32v2_Encoder.c | here]]. The code configures 2 external counters (see [[NU32v2: Counters and Timers#External Counter|Counters and Timers]]) that will increment with each pulse received from the LS7183. The example sends the encoder position via RS232 to your PC. &lt;br /&gt;
&lt;br /&gt;
The main piece of code shown below reads the UP and DOWN counters. With a variable called bigcount, it adds the the recent UP counts, subtracts the down counts and adjusts for roll-over of the counters. The full code placed this code in the main while loop, but this code can be placed in an ISR if you want to read the encoder at a given rate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
count0 = ReadTimer4();  // in your routine this must be done at least every 32000&lt;br /&gt;
	                // encoder counts to avoid rollover ambiguity&lt;br /&gt;
count1 = ReadTimer5(); 	&lt;br /&gt;
&lt;br /&gt;
bigcount += count0 - last0; // add on the recent up-counts, since the last time&lt;br /&gt;
	&lt;br /&gt;
if (count0 &amp;lt; last0){&lt;br /&gt;
  bigcount += 65536; 		// count0 only increments, so if it got lower &lt;br /&gt;
  		                //it must have rolled over&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
last0 = count0;&lt;br /&gt;
&lt;br /&gt;
bigcount -= count1 - last1; // we&amp;#039;re not worrying about rollover of the 32 bit bigcount total&lt;br /&gt;
	&lt;br /&gt;
if (count1 &amp;lt; last1){&lt;br /&gt;
  bigcount -= 65536;&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
last1 = count1; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20207</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20207"/>
		<updated>2011-02-09T17:30:42Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Sample Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder|Rotary encoders]] create quadrature signals on two channels (A and B) corresponding to the speed and direction of a motor. Some encoders produce up to a million counts per revolution. For very fast quadrature signals there is no way that a software quadrature decoder can avoid overrun errors. Instead we may use the LS7183 chip to translate the A and B quadrature signals into brief pulses indicating up-count or down-count. The up and down counts can be configured as external input signals to increment counters on the PIC32. By reading the counters on the PIC, we can determine the angle of the motor.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LS7183.png|center|thumb|400 px]]&lt;br /&gt;
&lt;br /&gt;
The resistor bias is used to control the pulse width of the up and down counts. The mode (1X, 2X, 4X) is used to multiply the quadrature clock rate by 1, 2 or 4. The mode is selected by connecting MODE to&lt;br /&gt;
* VSS (GND)  -&amp;gt; 1X&lt;br /&gt;
* VDD (3.3V) -&amp;gt; 2X&lt;br /&gt;
* Float (don&amp;#039;t connect to anything) -&amp;gt; 4X &lt;br /&gt;
&lt;br /&gt;
The UP and DOWN count pins should be connected to external counters on the PIC32.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
The full code can be downloaded [[Media:NU32v2_Encoder.c | here]]. The code configures 2 external counters (see [[NU32v2: Counters and Timers#External Counter|Counters and Timers]]) that will increment with each pulse received from the LS7183. The full code example adds the up counts, subtracts the down counts and adjusts for roll-over for a variable (bigcount) that represents the position (in relative counts) of the motor. The example sends this position via RS232 to your PC. &lt;br /&gt;
&lt;br /&gt;
The main code shown below can be placed in an ISR if you want to read the encoder at a given frequency.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
count0 = ReadTimer4();  // in your routine this must be done at least every 32000&lt;br /&gt;
	                // encoder counts to avoid rollover ambiguity&lt;br /&gt;
count1 = ReadTimer5(); 	&lt;br /&gt;
&lt;br /&gt;
bigcount += count0 - last0; // add on the recent up-counts, since the last time&lt;br /&gt;
	&lt;br /&gt;
if (count0 &amp;lt; last0){&lt;br /&gt;
  bigcount += 65536; 		// count0 only increments, so if it got lower &lt;br /&gt;
  		                //it must have rolled over&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
last0 = count0;&lt;br /&gt;
&lt;br /&gt;
bigcount -= count1 - last1; // we&amp;#039;re not worrying about rollover of the 32 bit bigcount total&lt;br /&gt;
	&lt;br /&gt;
if (count1 &amp;lt; last1){&lt;br /&gt;
  bigcount -= 65536;&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
last1 = count1; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20206</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20206"/>
		<updated>2011-02-09T17:25:09Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Sample Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder|Rotary encoders]] create quadrature signals on two channels (A and B) corresponding to the speed and direction of a motor. Some encoders produce up to a million counts per revolution. For very fast quadrature signals there is no way that a software quadrature decoder can avoid overrun errors. Instead we may use the LS7183 chip to translate the A and B quadrature signals into brief pulses indicating up-count or down-count. The up and down counts can be configured as external input signals to increment counters on the PIC32. By reading the counters on the PIC, we can determine the angle of the motor.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LS7183.png|center|thumb|400 px]]&lt;br /&gt;
&lt;br /&gt;
The resistor bias is used to control the pulse width of the up and down counts. The mode (1X, 2X, 4X) is used to multiply the quadrature clock rate by 1, 2 or 4. The mode is selected by connecting MODE to&lt;br /&gt;
* VSS (GND)  -&amp;gt; 1X&lt;br /&gt;
* VDD (3.3V) -&amp;gt; 2X&lt;br /&gt;
* Float (don&amp;#039;t connect to anything) -&amp;gt; 4X &lt;br /&gt;
&lt;br /&gt;
The UP and DOWN count pins should be connected to external counters on the PIC32.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
The full code can be downloaded [[Media:NU32v2_Encoder.c | here]]. The code configures 2 external counters (see [[NU32v2: Counters and Timers#External Counter|Counters and Timers]]).&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20205</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20205"/>
		<updated>2011-02-09T17:25:01Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Sample Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder|Rotary encoders]] create quadrature signals on two channels (A and B) corresponding to the speed and direction of a motor. Some encoders produce up to a million counts per revolution. For very fast quadrature signals there is no way that a software quadrature decoder can avoid overrun errors. Instead we may use the LS7183 chip to translate the A and B quadrature signals into brief pulses indicating up-count or down-count. The up and down counts can be configured as external input signals to increment counters on the PIC32. By reading the counters on the PIC, we can determine the angle of the motor.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LS7183.png|center|thumb|400 px]]&lt;br /&gt;
&lt;br /&gt;
The resistor bias is used to control the pulse width of the up and down counts. The mode (1X, 2X, 4X) is used to multiply the quadrature clock rate by 1, 2 or 4. The mode is selected by connecting MODE to&lt;br /&gt;
* VSS (GND)  -&amp;gt; 1X&lt;br /&gt;
* VDD (3.3V) -&amp;gt; 2X&lt;br /&gt;
* Float (don&amp;#039;t connect to anything) -&amp;gt; 4X &lt;br /&gt;
&lt;br /&gt;
The UP and DOWN count pins should be connected to external counters on the PIC32.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
The full code can be downloaded [[Media:NU32v2_Encoder.c | here]]. The code configures 2 external counters (see [[NU32v2: Counters and Timers#External Counter|Counters and Timers]] for discussion).&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20204</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20204"/>
		<updated>2011-02-09T17:24:44Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Sample Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder|Rotary encoders]] create quadrature signals on two channels (A and B) corresponding to the speed and direction of a motor. Some encoders produce up to a million counts per revolution. For very fast quadrature signals there is no way that a software quadrature decoder can avoid overrun errors. Instead we may use the LS7183 chip to translate the A and B quadrature signals into brief pulses indicating up-count or down-count. The up and down counts can be configured as external input signals to increment counters on the PIC32. By reading the counters on the PIC, we can determine the angle of the motor.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LS7183.png|center|thumb|400 px]]&lt;br /&gt;
&lt;br /&gt;
The resistor bias is used to control the pulse width of the up and down counts. The mode (1X, 2X, 4X) is used to multiply the quadrature clock rate by 1, 2 or 4. The mode is selected by connecting MODE to&lt;br /&gt;
* VSS (GND)  -&amp;gt; 1X&lt;br /&gt;
* VDD (3.3V) -&amp;gt; 2X&lt;br /&gt;
* Float (don&amp;#039;t connect to anything) -&amp;gt; 4X &lt;br /&gt;
&lt;br /&gt;
The UP and DOWN count pins should be connected to external counters on the PIC32.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
The full code can be downloaded [[Media:NU32v2_Encoder.c | here]]. The code configures 2 external counters (see [[NU32v2: Counters and Timers#External Counter|external counters]] for discussion).&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20203</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20203"/>
		<updated>2011-02-09T17:22:40Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Sample Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder|Rotary encoders]] create quadrature signals on two channels (A and B) corresponding to the speed and direction of a motor. Some encoders produce up to a million counts per revolution. For very fast quadrature signals there is no way that a software quadrature decoder can avoid overrun errors. Instead we may use the LS7183 chip to translate the A and B quadrature signals into brief pulses indicating up-count or down-count. The up and down counts can be configured as external input signals to increment counters on the PIC32. By reading the counters on the PIC, we can determine the angle of the motor.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LS7183.png|center|thumb|400 px]]&lt;br /&gt;
&lt;br /&gt;
The resistor bias is used to control the pulse width of the up and down counts. The mode (1X, 2X, 4X) is used to multiply the quadrature clock rate by 1, 2 or 4. The mode is selected by connecting MODE to&lt;br /&gt;
* VSS (GND)  -&amp;gt; 1X&lt;br /&gt;
* VDD (3.3V) -&amp;gt; 2X&lt;br /&gt;
* Float (don&amp;#039;t connect to anything) -&amp;gt; 4X &lt;br /&gt;
&lt;br /&gt;
The UP and DOWN count pins should be connected to external counters on the PIC32.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
The full code can be downloaded [[Media:NU32v2_Encoder.c | here]]. The code configures 2 external counters (see [[NU32v2: Counters and Timers:External Counter]] for discussion of external counters .&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20202</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20202"/>
		<updated>2011-02-09T17:20:10Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Details */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder|Rotary encoders]] create quadrature signals on two channels (A and B) corresponding to the speed and direction of a motor. Some encoders produce up to a million counts per revolution. For very fast quadrature signals there is no way that a software quadrature decoder can avoid overrun errors. Instead we may use the LS7183 chip to translate the A and B quadrature signals into brief pulses indicating up-count or down-count. The up and down counts can be configured as external input signals to increment counters on the PIC32. By reading the counters on the PIC, we can determine the angle of the motor.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LS7183.png|center|thumb|400 px]]&lt;br /&gt;
&lt;br /&gt;
The resistor bias is used to control the pulse width of the up and down counts. The mode (1X, 2X, 4X) is used to multiply the quadrature clock rate by 1, 2 or 4. The mode is selected by connecting MODE to&lt;br /&gt;
* VSS (GND)  -&amp;gt; 1X&lt;br /&gt;
* VDD (3.3V) -&amp;gt; 2X&lt;br /&gt;
* Float (don&amp;#039;t connect to anything) -&amp;gt; 4X &lt;br /&gt;
&lt;br /&gt;
The UP and DOWN count pins should be connected to external counters on the PIC32.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20201</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20201"/>
		<updated>2011-02-09T17:19:50Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Details */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder|Rotary encoders]] create quadrature signals on two channels (A and B) corresponding to the speed and direction of a motor. Some encoders produce up to a million counts per revolution. For very fast quadrature signals there is no way that a software quadrature decoder can avoid overrun errors. Instead we may use the LS7183 chip to translate the A and B quadrature signals into brief pulses indicating up-count or down-count. The up and down counts can be configured as external input signals to increment counters on the PIC32. By reading the counters on the PIC, we can determine the angle of the motor.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LS7183.png|center|thumb|400 px]]&lt;br /&gt;
&lt;br /&gt;
The resistor bias is used to control the pulse width of the up and down counts. The mode (1X, 2X, 4X) is used to multiply the quadrate clock rate by 1, 2 or 4. The mode is selected by connecting MODE to&lt;br /&gt;
* VSS (GND)  -&amp;gt; 1X&lt;br /&gt;
* VDD (3.3V) -&amp;gt; 2X&lt;br /&gt;
* Float (don&amp;#039;t connect to anything) -&amp;gt; 4X &lt;br /&gt;
&lt;br /&gt;
The UP and DOWN count pins should be connected to external counters on the PIC32.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20200</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20200"/>
		<updated>2011-02-09T17:19:21Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Details */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder|Rotary encoders]] create quadrature signals on two channels (A and B) corresponding to the speed and direction of a motor. Some encoders produce up to a million counts per revolution. For very fast quadrature signals there is no way that a software quadrature decoder can avoid overrun errors. Instead we may use the LS7183 chip to translate the A and B quadrature signals into brief pulses indicating up-count or down-count. The up and down counts can be configured as external input signals to increment counters on the PIC32. By reading the counters on the PIC, we can determine the angle of the motor.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LS7183.png|center|thumb|400 px]]&lt;br /&gt;
&lt;br /&gt;
The resistor bias is used to control the pulse width of the up and down counts. The mode (1X, 2X, 4X) is used to multiply the quadrate clock rate by 1, 2 or 4. The mode is selected by connecting MODE to&lt;br /&gt;
** VSS (GND)  -&amp;gt; 1X&lt;br /&gt;
** VDD (3.3V) -&amp;gt; 2X&lt;br /&gt;
** Float (don&amp;#039;t connect to anything) -&amp;gt; 4X &lt;br /&gt;
&lt;br /&gt;
The UP and DOWN count pins should be connected to external counters on the PIC32.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20199</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20199"/>
		<updated>2011-02-09T17:19:09Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Details */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder|Rotary encoders]] create quadrature signals on two channels (A and B) corresponding to the speed and direction of a motor. Some encoders produce up to a million counts per revolution. For very fast quadrature signals there is no way that a software quadrature decoder can avoid overrun errors. Instead we may use the LS7183 chip to translate the A and B quadrature signals into brief pulses indicating up-count or down-count. The up and down counts can be configured as external input signals to increment counters on the PIC32. By reading the counters on the PIC, we can determine the angle of the motor.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LS7183.png|center|thumb|300 px]]&lt;br /&gt;
&lt;br /&gt;
The resistor bias is used to control the pulse width of the up and down counts. The mode (1X, 2X, 4X) is used to multiply the quadrate clock rate by 1, 2 or 4. The mode is selected by connecting MODE to&lt;br /&gt;
** VSS (GND)  -&amp;gt; 1X&lt;br /&gt;
** VDD (3.3V) -&amp;gt; 2X&lt;br /&gt;
** Float (don&amp;#039;t connect to anything) -&amp;gt; 4X &lt;br /&gt;
&lt;br /&gt;
The UP and DOWN count pins should be connected to external counters on the PIC32.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20198</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20198"/>
		<updated>2011-02-09T17:18:29Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Details */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder|Rotary encoders]] create quadrature signals on two channels (A and B) corresponding to the speed and direction of a motor. Some encoders produce up to a million counts per revolution. For very fast quadrature signals there is no way that a software quadrature decoder can avoid overrun errors. Instead we may use the LS7183 chip to translate the A and B quadrature signals into brief pulses indicating up-count or down-count. The up and down counts can be configured as external input signals to increment counters on the PIC32. By reading the counters on the PIC, we can determine the angle of the motor.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LS7183.png|center|upright = 0.5]]&lt;br /&gt;
&lt;br /&gt;
The resistor bias is used to control the pulse width of the up and down counts. The mode (1X, 2X, 4X) is used to multiply the quadrate clock rate by 1, 2 or 4. The mode is selected by connecting MODE to&lt;br /&gt;
** VSS (GND)  -&amp;gt; 1X&lt;br /&gt;
** VDD (3.3V) -&amp;gt; 2X&lt;br /&gt;
** Float (don&amp;#039;t connect to anything) -&amp;gt; 4X &lt;br /&gt;
&lt;br /&gt;
The UP and DOWN count pins should be connected to external counters on the PIC32.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:LS7183.png&amp;diff=20197</id>
		<title>File:LS7183.png</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:LS7183.png&amp;diff=20197"/>
		<updated>2011-02-09T17:16:34Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20196</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20196"/>
		<updated>2011-02-09T17:16:21Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder|Rotary encoders]] create quadrature signals on two channels (A and B) corresponding to the speed and direction of a motor. Some encoders produce up to a million counts per revolution. For very fast quadrature signals there is no way that a software quadrature decoder can avoid overrun errors. Instead we may use the LS7183 chip to translate the A and B quadrature signals into brief pulses indicating up-count or down-count. The up and down counts can be configured as external input signals to increment counters on the PIC32. By reading the counters on the PIC, we can determine the angle of the motor.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LS7183.png|center]]&lt;br /&gt;
&lt;br /&gt;
The resistor bias is used to control the pulse width of the up and down counts. The mode (1X, 2X, 4X) is used to multiply the quadrate clock rate by 1, 2 or 4. The mode is selected by connecting MODE to&lt;br /&gt;
** VSS (GND)  -&amp;gt; 1X&lt;br /&gt;
** VDD (3.3V) -&amp;gt; 2X&lt;br /&gt;
** Float (don&amp;#039;t connect to anything) -&amp;gt; 4X &lt;br /&gt;
&lt;br /&gt;
The UP and DOWN count pins should be connected to external counters on the PIC32. &lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20195</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20195"/>
		<updated>2011-02-09T17:07:32Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder|Rotary encoders]] create quadrature signals on two channels (A and B) corresponding to the speed and direction of a motor. Some encoders produce up to a million counts per revolution. For very fast quadrature signals there is no way that a software quadrature decoder can avoid overrun errors. Instead we may use the LS7183 chip to translate the A and B quadrature signals into brief pulses indicating up-count or down-count. The up and down counts can be configured as external input signals to increment counters on the PIC32. By reading the counters on the PIC, we can determine the angle of the motor.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20194</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20194"/>
		<updated>2011-02-09T17:04:00Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder|Rotary encoders]] create quadrature signals on two channels (A and B) corresponding to the speed and direction of a motor. Some encoders produce up to a million counts per revolution. For very fast quadrature signals there is no way that a software quadrature decoder can avoid overrun errors. Instead we may use the LS7183 chip to translate the A and B quadrature signals into brief pulses indicating up-count or down-count. The up and down counts can be configured as external input signals to increment counters on the PIC32.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20193</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20193"/>
		<updated>2011-02-09T16:58:31Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The LS7183 quadrature clock converter is a chip used to convert quadrature signals from encoders into up and down counts that can be sent to a microchip. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[[Rotary Encoder||Rotary encoders]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20192</id>
		<title>NU32v2: Using the LS7183 Quadrature Clock Converter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Using_the_LS7183_Quadrature_Clock_Converter&amp;diff=20192"/>
		<updated>2011-02-09T16:50:13Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: New page:  == Overview ==  == Details ==  == Library Functions ==  == Sample Code ==&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Microchip_PICs&amp;diff=20191</id>
		<title>Microchip PICs</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Microchip_PICs&amp;diff=20191"/>
		<updated>2011-02-09T16:49:12Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Using the NU32v2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;Note:  Much of the code on this page was written using the CCS compiler.  All new code is being written using Microchip&amp;#039;s MPLAB.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://peshkin.mech.northwestern.edu/pic/code Link to all sample code here.]&amp;lt;/b&amp;gt;&lt;br /&gt;
*[[Sample code for most PIC18F4520 operations]]&lt;br /&gt;
*[http://www.ccsinfo.com/forum/ CCS user forum]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
== Using the NU32v2 ==&lt;br /&gt;
*[[NUScope]]&lt;br /&gt;
*[[NU32v2: Introduction to the PIC32]]&lt;br /&gt;
**[[NU32v2: What is in the NU32v2 Kit]]&lt;br /&gt;
**[[NU32v2: Software to Install]]&lt;br /&gt;
**[[NU32v2: Starting a New Project and Putting it on the NU32v2]]&lt;br /&gt;
**[[NU32v2: Using the Simulator]]&lt;br /&gt;
**[[NU32v2: A Detailed Look at Programming the PIC32]]&lt;br /&gt;
* Sample Code&lt;br /&gt;
**[[NU32v2: Digital Input and Output]]&lt;br /&gt;
**[[NU32v2: Counters and Timers]]&lt;br /&gt;
**[[NU32v2: Interrupts]]&lt;br /&gt;
**[[NU32v2: Output Compare, PWM, and Analog Output]]&lt;br /&gt;
**[[NU32v2: Serial Communication with the PC]]&lt;br /&gt;
**[[NU32v2: Nokia 5110 LCD]]&lt;br /&gt;
**[[NU32v2: Analog Input]]&lt;br /&gt;
**[[NU32v2: Using the LS7183 Quadrature Clock Converter]]&lt;br /&gt;
**[[NU32v2: Input Capture]]&lt;br /&gt;
**[[NU32v2: Flash Self-Programming]]&lt;br /&gt;
**[[NU32v2: UART Asynchronous Serial Communication]]&lt;br /&gt;
**[[NU32v2: I2C Synchronous Serial Communication]]&lt;br /&gt;
**[[NU32v2: SPI Synchronous Serial Communication]]&lt;br /&gt;
**[[NU32v2: Digital Signal Processing]]&lt;br /&gt;
**[[NU32v2: Watchdog Timer]]&lt;br /&gt;
**[[NU32v2: Simple Analog Input]]&lt;br /&gt;
&lt;br /&gt;
== Using the PIC18F4520 ==&lt;br /&gt;
*[[4520 Board intro]]&lt;br /&gt;
*[[4520 Board construction]]&lt;br /&gt;
*[[4520 Board use]]&lt;br /&gt;
*[[Microcontroller PIC18F4520]]&lt;br /&gt;
*[[PIC MCUs: Capabilities of PIC18F4520]]&lt;br /&gt;
**[[PIC18F4520: PWM Motor Control]]&lt;br /&gt;
**[[PIC18F4520: Digital Outputs]]&lt;br /&gt;
**[[PIC18F4520: Digital Inputs]]&lt;br /&gt;
**[[PIC18F4520: Serial Digital-to-Analog Conversion]]&lt;br /&gt;
**[[PIC18F4520: Analog Inputs]]&lt;br /&gt;
**[[PIC18F4520: Timers]]&lt;br /&gt;
**[[PIC18F4520: Comparator]]&lt;br /&gt;
&lt;br /&gt;
== Example Code in C for PIC18F4520==&lt;br /&gt;
*[[C Example: Parallel Interfacing with LCDs]]&lt;br /&gt;
*[[C Example: Digital Inputs]]&lt;br /&gt;
*[[C Example: PWM Motor Control]]&lt;br /&gt;
*[[C Example: Comparators]]&lt;br /&gt;
*[[C Example: Digital Outputs]]&lt;br /&gt;
*[[C Example: Analog Inputs]]&lt;br /&gt;
*[[C Example: Digital Outputs (Ports)]]&lt;br /&gt;
*[[C Example: Bi-Directional PWM Motor Control]]&lt;br /&gt;
*[[C Example: Serial LCD]]&lt;br /&gt;
&lt;br /&gt;
== Using the PIC32MX Series ==&lt;br /&gt;
*[[Introduction to the PIC32]]&lt;br /&gt;
*[[Getting Started with PIC32]] (includes NU32 development board)&lt;br /&gt;
** Download PIC32 Tutorial Files [[Media: PIC32_TutorialFiles.zip|here]].&lt;br /&gt;
** [[ME 333 Lab 2]] - Includes Digital IO, Analog Input, Parallel LCD and RS232 Com&lt;br /&gt;
** [[ME 333 Lab 4]] - Includes PWM and Encoder Motor Control&lt;br /&gt;
*[[Microcontroller PIC32MX460F512L]]&lt;br /&gt;
*[[Programming HID Bootloader on PIC32]] &lt;br /&gt;
*[[HelloWorld PIC32|Directions to code HelloWorld]]&lt;br /&gt;
*[[Directions to Load Files to PIC32 with HID Bootloader]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*[[PIC MCUs: Capabilities of PIC32MX]]&lt;br /&gt;
**[[Programming HID Bootloader on PIC32 |PIC32MX: Bootloader]]&lt;br /&gt;
**Simple Programs&lt;br /&gt;
***[[PIC32MX: Digital Inputs]]&lt;br /&gt;
***[[PIC32MX: Digital Outputs]]&lt;br /&gt;
***[[PIC32MX: Analog Inputs]]&lt;br /&gt;
**Communication&lt;br /&gt;
***[[PIC32MX: Parallel LCD]]&lt;br /&gt;
***[[PIC32MX: RS232]]&lt;br /&gt;
***[[PIC32MX:  USB Communication with a PC]]&lt;br /&gt;
***[[PIC32MX:  SPI External RAM]]&lt;br /&gt;
***[[PIC32MX:  I2C DAC]]&lt;br /&gt;
***[[PIC32MX:  I2C EEPROM]]&lt;br /&gt;
***[[PIC32MX:  SPI EEPROM]]&lt;br /&gt;
***[[PIC32MX:  I2C Communication between PIC32s]]&lt;br /&gt;
***[[PIC32MX:  SPI Communication between PIC32s]]&lt;br /&gt;
**Motor Control&lt;br /&gt;
***[[PIC32MX: PWM Motor Control|PIC32MX: PWM Motor Control]]&lt;br /&gt;
***[[PIC32MX: Encoder Motor Control|PIC32MX: Encoder Motor Control]]&lt;br /&gt;
***[[PIC32MX: Servo Control|PIC32MX: Servo Control]]&lt;br /&gt;
***[[PIC32MX:  Driving a Stepper Motor]]&lt;br /&gt;
***[[Using the LS7166 Quadrature Counter]]&lt;br /&gt;
***[[Using the LS7366R SPI Quadrature Counter]]&lt;br /&gt;
**Capabilities&lt;br /&gt;
***[[PIC32MX:  Benchmarking Mathematical Operations]]&lt;br /&gt;
***[[PIC32MX:  FFT of Analog Input]]&lt;br /&gt;
***[[PIC32MX:  Sinusoidal Analog Output]]&lt;br /&gt;
***[[PIC32MX:  XBee Wireless Round-trip Latency]]&lt;br /&gt;
***[[PIC32MX:  Interfacing with Force Sensors from a Scale]]&lt;br /&gt;
***[[PIC32MX:  Storing Data in Flash Memory (Run-Time Self Programming)]]&lt;br /&gt;
&lt;br /&gt;
==Other PIC Resources ==&lt;br /&gt;
&lt;br /&gt;
*[[PIC PWM Motor Driver]]&lt;br /&gt;
*[[Writing Code with the C18 Compiler]]&lt;br /&gt;
*[[Controlling a seven segment display]]&lt;br /&gt;
*[[PIC Microcontrollers with C18 Compiler]]&lt;br /&gt;
*[[PIC Motor Control and Serial Port Example]]&lt;br /&gt;
*[[PIC16F684]]&lt;br /&gt;
*[[PIC Microcontrollers with CCS Compiler]]&lt;br /&gt;
*[[Stepper motor control with the PIC]]&lt;br /&gt;
*[[PIC/C18 Compiler Tips and Troubleshooting]]&lt;br /&gt;
*[[CCS C]]&lt;br /&gt;
*[[Analog Input]]&lt;br /&gt;
*[[PIC MCUs: Hardware and Connections]]&lt;br /&gt;
*[[Storing constant data in program memory]]&lt;br /&gt;
*[[PIC Analog-Digital-Converter Example]]&lt;br /&gt;
*[[PIC Microcontroller]]&lt;br /&gt;
*[[Digital inputs &amp;amp; outputs]]&lt;br /&gt;
*[[Debugging C on a PIC]]&lt;br /&gt;
*[[Analog Output]]&lt;br /&gt;
*[[PIC MCUs: Software]]&lt;br /&gt;
*[[Interfacing to External EEPROM]]&lt;br /&gt;
*[[CCS IDE]]&lt;br /&gt;
*[[Interrupts]]&lt;br /&gt;
*[[PIC16F684 Registers]]&lt;br /&gt;
*[[Waveform Generation with AD9833]]&lt;br /&gt;
*[[Waveform Generation with AD9833, and SPI]]&lt;br /&gt;
*[[PIC computation time benchmarks]]&lt;br /&gt;
*[[PICkit 1]]&lt;br /&gt;
*[[PIC MCUs: 4520 Board]]&lt;br /&gt;
*[[More debugging tips]]&lt;br /&gt;
*[[Example Writeup: Analog Input]]&lt;br /&gt;
*[[LED Drivers]]&lt;br /&gt;
*[[Embedded Programming Tips for CCS C]]&lt;br /&gt;
*[[Wireless PIC bootloading]]&lt;br /&gt;
*[[PIC 18f4553]]&lt;br /&gt;
*[[I2C Motor Controller]]&lt;br /&gt;
*[[Driving a piezo speaker with a PIC]]&lt;br /&gt;
*[[PIC Servo Controller]]&lt;br /&gt;
*[[Watchdog timer]]&lt;br /&gt;
*[[Interfacing PIC with SPI memory]]&lt;br /&gt;
*[[Using the LS7166 Quadrature Counter]]&lt;br /&gt;
&lt;br /&gt;
==Programming the PIC with Microchip&amp;#039;s MPLAB==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=20008</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=20008"/>
		<updated>2011-01-29T22:24:54Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Sample Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The basic function of counters is quite simple:  they count the number of pulses they have received since they were last reset (or last &amp;quot;rolled over&amp;quot;).  If the pulses come from a fixed frequency clock, they can be thought of as timers, hence the words counter and timer are often used interchangeably (as in this article). I prefer to call them &amp;quot;counters,&amp;quot; but Microchip&amp;#039;s documentation mostly refers to them as timers, so we&amp;#039;ll adopt that terminology. The pulses that are being counted may come from the internal peripheral bus clock or external sources.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). A timer increments based on a clock signal, which may come from an internal source (the peripheral bus clock, 80 MHz for us) or an external source (such as an encoder).  Each timer x has an associated pin TxCK for the external source.  In addition, a &amp;#039;&amp;#039;prescaler&amp;#039;&amp;#039; determines how many clock pulses must be received before the counter increments.  If the prescaler is set to 1:1, the counter increments on each clock pulse; if it is set to 1:4, it increments every fourth pulse.  Both the clock source type (internal and external) and the prescaler value are set in software.&lt;br /&gt;
&lt;br /&gt;
Each 16-bit timer can count from 0 to 2^16 - 1 = 65535 = 0xFFFF, at which point it &amp;quot;rolls over&amp;quot; to 0 again.  The timer can also be made to roll over at any count less than 0xFFFF by setting the period of the timer roll-over in software.  We can also configure the timer to generate an interrupt when this roll-over event occurs.  In this way, by using the peripheral bus clock as the clock source, we can generate precisely timed events, such as the execution of a control law in feedback control.  &lt;br /&gt;
&lt;br /&gt;
These features are common to each of the timers.  However, we can separate the timers into two types:  Type A and Type B.  Added features of each type of timer are indicated below&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Type A Timer&amp;#039;&amp;#039;&amp;#039;:  Only Timer 1 is a Type A timer, and it can take prescaler values of 1:1, 1:8, 1:64, and 1:256.  A Type A timer is capable of counting external pulses &amp;#039;&amp;#039;asynchronously.&amp;#039;&amp;#039; This means that the counter can be configured to run independently of the peripheral bus clock---the counter can increment even in the absence of the PB clock.  (This is in contrast to &amp;#039;&amp;#039;synchronous&amp;#039;&amp;#039; counting, where even if the events being counted are external, the counter only increments on the PB clock rising edge,i.e., the external rising edge is &amp;quot;synchronized&amp;quot; to the PB clock.)  One use of asynchronous counting is to count even when the CPU is in SLEEP mode and not providing a PB clock signal.  Thus a Type A timer can be used in conjunction with the Real-Time Clock and Calendar (RTCC) to keep track of seconds, minutes, hours, days, and years, even when the PIC is in SLEEP mode.  To do this, the secondary oscillator pins SOSCO and SOSCI should be connected to a 32.768 kHz oscillator (2^15 pulses every second).  We will rarely need the asynchronous counting capability.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Type B Timer&amp;#039;&amp;#039;&amp;#039;:  Timers 2, 3, 4, and 5 are Type B, and can take prescaler values of 1:1, 1:2, 1:4, 1:8, 1:16, 1:32, 1:64, and 1:256.  Type B timers add two useful capabilities:  the ability to trigger an analog-to-digital conversion when the timer rolls over, and the ability to cascade two timers to make a single 32-bit timer (Timer 23 and Timer 45).  The former capability is useful when collecting analog input signals at fixed time intervals without CPU intervention.  The latter capability allows us to count up to 2^32 &amp;gt; 4 billion.  In this case, the even numbered timer (Timer 2 or 4) is the master timer, and its roll-over signal is the input to the slave timer (Timer 3 or 5).  Thus the even numbered timer contains the least significant 16 bits and the odd numbered timer contains the most significant 16 bits.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the timers are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, not all of the bits are relevant. Depending on whether these bits are 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR configures the behavior of the timer, including whether it is on or not, the prescaler value, the source of the clock (internal or external).  For Type A timers, it also indicates whether it will operate in synchronous or asynchronous mode.  For even-numbered Type B timers, this SFR determines whether two timers are cascaded to create a 32-bit timer.&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit count. It resets to 0 when the timer reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit &amp;quot;period register&amp;quot;:  the value where TMRx rolls over (resets to zero).&lt;br /&gt;
&lt;br /&gt;
Counter interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;IEC0&amp;#039;&amp;#039;&amp;#039;: Five bits of this SFR are relevant to the timers, one for each timer.  If the bit is 1, the interrupt is enabled and an interrupt will be generated at timer roll-over.  If the bit is 0, no interrupts will be generated.  (Other bits in this SFR are reserved for other interrupt sources.)&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;IFS0&amp;#039;&amp;#039;&amp;#039;: Five bits of this SFR are relevant to the timers, one for each timer.  If the bit is 1, the timer has generated an interrupt request.  If the bit is 0, it has not.  Your ISR must set this bit back to zero (&amp;quot;clear the flag&amp;quot;).   &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;IPCx&amp;#039;&amp;#039;&amp;#039;: Three bits of this SFR specify the priority of the interrupt associated with timer x, and two bits set the subpriority of the interrupt.  The priority can take values 1..7, and higher numbers mean higher priority.  Thus if a timer with priority 2 generates an interrupt and its interrupt service routine (ISR) is called, then a timer with priority 4 generates an interrupt, the CPU will jump to the higher priority ISR, finish its execution, and then return to the original ISR.&lt;br /&gt;
&lt;br /&gt;
If interrupts are enabled, a timer interrupt is generated when the timer rolls over.  Thus if the clock input is a fixed frequency clock (like the PB clock), then the period register PRx, together with the timer prescaler (N &amp;gt;=1), determines the frequency of the interrupts.  The period of the interrupt is&lt;br /&gt;
 Period = (PRx+1)*Tpb*N,&lt;br /&gt;
where Tpb is the period of the peripheral bus.  Thus for an 80 MHz PB clock, a maximum prescaler value of N=256, and a 16-bit timer, the maximum timer period between interrupts is 65536*(1/80 M)*256 = 0.21 seconds.  For a 32-bit timer, it increases to nearly 4 hours.&lt;br /&gt;
&lt;br /&gt;
More details can be found in Section 14 of the Reference Manual.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h.&lt;br /&gt;
&lt;br /&gt;
Each timer X (replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using bitwise OR | with the constants given in the header file. config chooses whether the time is on or off, the prescaler, the source (internal or external), and whether this timer is the master timer of a 32-bit timer pair.  period is the desired PRX value which determines the roll-over period.  &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRX value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRX register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a simple example of code to time how many cycles it takes to compute a code snippet (provided it&amp;#039;s less than 2^16 cycles; otherwise we need to cascade two timers).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  int elapsed; float time; int i;&lt;br /&gt;
  OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_8, 0xFFFF); // will overflow in 1/(80000000Hz/8/65535) = 0.0066s&lt;br /&gt;
&lt;br /&gt;
  WriteTimer2(0);&lt;br /&gt;
  for (i=0;i&amp;lt;10000;i++) {}&lt;br /&gt;
  elapsed = ReadTimer2();&lt;br /&gt;
  time = (float)elapsed / 80000000.0 * 8.0;&lt;br /&gt;
  sprintf(RS232_Out_Buffer, &amp;quot;counting to 10000 takes %d timer cycles \tor %f s\r\n&amp;quot;, elapsed, time);&lt;br /&gt;
  WriteString(UART3, RS232_Out_Buffer);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
For each interrupt, we need to write an interrupt service routine (ISR), which basically means when an interrupt is generated jump to and execute a given set of code before returning. The standard way to write an ISR is the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void __ISR(vector, iplx) yHandler(void){&lt;br /&gt;
&lt;br /&gt;
// code to execute&lt;br /&gt;
&lt;br /&gt;
// clear the interrupt flag&lt;br /&gt;
   function to clear interrupt flag&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
where vector refers to a constant defined in &amp;#039;&amp;#039;&amp;#039;(find this location)&amp;#039;&amp;#039;&amp;#039;, iplx is the interrupt priority level where x is the level. yHandler can be anything you want, for example we can call the timer2 handler as Timer2Handler. There is a function associated with each interrupt that is used to clear the flag, which allows the program to return to where it was before the interrupt was generated. Without clearing the flag, the program will never exit the interrupt. &lt;br /&gt;
&lt;br /&gt;
This example is going to toggle 2 LEDs at a given frequency using interrupts with timer 2. We are going to use the functions OpenTimer2() and ConfigIntTimer2() to initialize the timer and interrupts. You will see in the example code that there is also a function INTEnableSystemMultiVectoredInt() which is used to set up multi-vectored interrupts &amp;#039;&amp;#039;&amp;#039;(whatever that means)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* Timer_Simple.c&lt;br /&gt;
 *&lt;br /&gt;
 * This example toggles 2 LEDs on and off using a timer interrupt. &lt;br /&gt;
 * &lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
					&lt;br /&gt;
#define SYS_FREQ  	80000000		// clock frequency set by bootloader&lt;br /&gt;
#define LED_0		LATGbits.LATG12 // mnemonic constants for LEDs on NU32v2&lt;br /&gt;
#define LED_1		LATGbits.LATG13&lt;br /&gt;
&lt;br /&gt;
// Define constants for the interrupt frequency (Hz) and the prescalar&lt;br /&gt;
// Note that (SYS_FREQ / INT_FREQ / PRE_SCALER - 1) needs to be less than 2^16-1&lt;br /&gt;
#define INT_FREQ	5&lt;br /&gt;
#define PRE_SCALER	256		&lt;br /&gt;
&lt;br /&gt;
void initLEDs(void);&lt;br /&gt;
&lt;br /&gt;
//int count = 0;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
  // SYSTEMConfig() optimizes performance for our clock frequency.&lt;br /&gt;
  // in Microchip\MPLAB C32\pic32-libs\include\peripheral\system.h&lt;br /&gt;
  SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL);&lt;br /&gt;
    &lt;br /&gt;
  initLEDs(); 	// set LED pins as outputs&lt;br /&gt;
&lt;br /&gt;
  LED_0 = 1; // low output turns an LED on; see NU32v2 circuit&lt;br /&gt;
  LED_1 = 0; // high output turns an LED off&lt;br /&gt;
	&lt;br /&gt;
  // configure Timer 2 using internal clock with given prescaler&lt;br /&gt;
  OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_256, SYS_FREQ/INT_FREQ/PRE_SCALER - 1);&lt;br /&gt;
&lt;br /&gt;
  // set up the timer interrupt with a priority of 2&lt;br /&gt;
  ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2);&lt;br /&gt;
&lt;br /&gt;
  // enable multi-vector interrupts&lt;br /&gt;
  INTEnableSystemMultiVectoredInt();&lt;br /&gt;
&lt;br /&gt;
  while(1) {		// infinite loop&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
} // end main&lt;br /&gt;
&lt;br /&gt;
/* initLEDs()&lt;br /&gt;
 *&lt;br /&gt;
 * initLEDs sets RG12 and RG13 to outputs to control the LEDs on the NU32v2.&lt;br /&gt;
 */&lt;br /&gt;
void initLEDs(void) {&lt;br /&gt;
  LATG |= 0x3000;		// Initializes G12 and G13 to 1 (off for NU32v2 board)&lt;br /&gt;
  TRISG &amp;amp;= 0xCFFF;	// set the two pins to inputs&lt;br /&gt;
				&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From this example, we can see how to set up timer 2 at a frequency of 5 Hz with an interrupt priority of 2.&lt;br /&gt;
&lt;br /&gt;
Now let&amp;#039;s modify this simple time-based ISR to toggle the leds at two fixed time periods, one at period T, other at period kT, where k is an integer, by using a counter i that increments each time the ISR is called, and when i reaches k, does the less frequent code and sets i back to zero. For this example, create a global int count and initialize it to zero.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; int count = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use this ISR instead of the previous, in order to have LED0 toggle at 5 Hz and LED1 toggle at 1 Hz. &lt;br /&gt;
&amp;lt;pre&amp;gt;/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0  &lt;br /&gt;
  &lt;br /&gt;
  count++;&lt;br /&gt;
	&lt;br /&gt;
  if(count == 5){&lt;br /&gt;
    LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
		count = 0;&lt;br /&gt;
		}&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Imagine that we wanted to toggle the LEDs at 1 Hz by using a 32-bit timer. We can combine timers 2 and 3 or timers 4 and 5 to create a 32-bit timer. Let&amp;#039;s use a prescaler of 1:1, the period register then should be PR = (1 sec) (80000000 Hz) (1) -1 = 79999999 &amp;lt; 2^32 - 1. &lt;br /&gt;
&lt;br /&gt;
Replace the timer initialization with &lt;br /&gt;
&amp;lt;pre&amp;gt; // configure Timer 2 as 32-bit timer using internal clock, 1:1 prescaler&lt;br /&gt;
OpenTimer23(T23_ON | T23_SOURCE_INT | T23_PS_1_1, 79999999);	&lt;br /&gt;
&lt;br /&gt;
// set up the timer interrupt with a priority of 2&lt;br /&gt;
ConfigIntTimer23(T23_INT_ON | T23_INT_PRIOR_2);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Replace the ISR with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// ISR for Timer 23	&lt;br /&gt;
// Note that the interrupt vector needs to be _TIMER_3_VECTOR&lt;br /&gt;
void __ISR(_TIMER_3_VECTOR, ipl2) Timer23Handler(void) {&lt;br /&gt;
		&lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1; // toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT23ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In summary, this example shows you how to set up a time based interrupt with a given frequency and prescaler. It also shows you how to create a 32-bit timer.&lt;br /&gt;
&lt;br /&gt;
=== External Counter ===&lt;br /&gt;
&lt;br /&gt;
[[Media:NU32v2_external_count_example.c | This code]]  is going to set up an external counter to count the number of high pulses on the T5CK pin. The counts  will be displayed in binary on the NU32v2 board and will reset after 4 button pushes. You need a simple circuit consisting of 3.3V connected to a switch connected to a resistor (1K) connected to ground. Connect a wire from T5CK to the junction of the resistor and the switch. &lt;br /&gt;
&lt;br /&gt;
The main elements of this code are the initialization&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OpenTimer5( T5_ON | T5_PS_1_1 | T5_SOURCE_EXT, 0xFFFF);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
which sets up Timer 5 with an external clock source and a prescaler of 1:1. The PR value is the value that you want the counter to roll-over / generate an interrupt. We will not be setting up interrupts, so this number does not really matter. &lt;br /&gt;
&lt;br /&gt;
We will read the counter with the function&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 ReadTimer5()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will reset the counter to zero with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
WriteTimer5(0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19900</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19900"/>
		<updated>2011-01-26T15:54:17Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* External Counter */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Counters are quite simple:  they count the number of pulses they have received since they were last reset (or last &amp;quot;rolled over&amp;quot;).  If the pulses come from a fixed frequency clock, they can be thought of as timers, hence the words counter and timer are often used interchangeably.  The pulses may come from the internal peripheral bus clodk or external sources.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescaler that determines how many pulses the timer must receive from the clock source before adding one to the counter value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalers of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalers of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the counters are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, not all of the bits are relevant. Depending on whether these bits are 0 or 1, different actions or settings are performed for Timer (counter) x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the counter. There are many different ways to configure the counters. The main options for TxCON are to turn on or off the counter, choose the prescaler, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen in the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit count. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value where TMRx rolls over (resets to zero).&lt;br /&gt;
&lt;br /&gt;
Counter interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the counter.  An interrupt can be generated when the counter resets.&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets the priority of the interrupt associated with the counter. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescaler (1:n), the counter increments by 1 every n pulses from the clock. The counter is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the counter interrupt is generated when the counter resets. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the interrupt is given by &lt;br /&gt;
 Period = [(PRx + 1) Tpb (TMR_Prescaler_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
This means that if we want to specify the frequency of the interrupt, we need to determine PRx. Note that PRx needs to be less that 2^16 - 1 if using only 1 timer or less than 2^32 - 1 if combining two timers. See example below.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h.&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
For each interrupt, we need to write an interrupt service routine (ISR), which basically means when an interrupt is generated jump to and execute a given set of code before returning. The standard way to write an ISR is the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void __ISR(vector, iplx) yHandler(void){&lt;br /&gt;
&lt;br /&gt;
// code to execute&lt;br /&gt;
&lt;br /&gt;
// clear the interrupt flag&lt;br /&gt;
   function to clear interrupt flag&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
where vector refers to a constant defined in &amp;#039;&amp;#039;&amp;#039;(find this location)&amp;#039;&amp;#039;&amp;#039;, iplx is the interrupt priority level where x is the level. yHandler can be anything you want, for example we can call the timer2 handler as Timer2Handler. There is a function associated with each interrupt that is used to clear the flag, which allows the program to return to where it was before the interrupt was generated. Without clearing the flag, the program will never exit the interrupt. &lt;br /&gt;
&lt;br /&gt;
This example is going to toggle 2 LEDs at a given frequency using interrupts with timer 2. We are going to use the functions OpenTimer2() and ConfigIntTimer2() to initialize the timer and interrupts. You will see in the example code that there is also a function INTEnableSystemMultiVectoredInt() which is used to set up multi-vectored interrupts &amp;#039;&amp;#039;&amp;#039;(whatever that means)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* Timer_Simple.c&lt;br /&gt;
 *&lt;br /&gt;
 * This example toggles 2 LEDs on and off using a timer interrupt. &lt;br /&gt;
 * &lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
					&lt;br /&gt;
#define SYS_FREQ  	(80000000L)		// clock frequency set by bootloader&lt;br /&gt;
#define LED_0		LATGbits.LATG12 // mnemonic constants for LEDs on NU32v2&lt;br /&gt;
#define LED_1		LATGbits.LATG13&lt;br /&gt;
&lt;br /&gt;
// Define constants for the interrupt frequency (Hz) and the prescalar&lt;br /&gt;
// Note that (SYS_FREQ / INT_FREQ / PRE_SCALAR - 1) needs to be less than 2^16-1&lt;br /&gt;
#define INT_FREQ	5&lt;br /&gt;
#define PRE_SCALAR	256		&lt;br /&gt;
&lt;br /&gt;
void initLEDs(void);&lt;br /&gt;
&lt;br /&gt;
//int count = 0;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
  // SYSTEMConfig() optimizes performance for our clock frequency.&lt;br /&gt;
  // in Microchip\MPLAB C32\pic32-libs\include\peripheral\system.h&lt;br /&gt;
  SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);&lt;br /&gt;
    &lt;br /&gt;
  initLEDs(); 	// set LED pins as outputs&lt;br /&gt;
&lt;br /&gt;
  LED_0 = 1; // low output turns an LED on; see NU32v2 circuit&lt;br /&gt;
  LED_1 = 0; // high output turns an LED off&lt;br /&gt;
	&lt;br /&gt;
  // configure Timer 2 using internal clock with given prescalar&lt;br /&gt;
  OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_256, SYS_FREQ/INT_FREQ/PRE_SCALAR - 1);&lt;br /&gt;
&lt;br /&gt;
  // set up the timer interrupt with a priority of 2&lt;br /&gt;
  ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2);&lt;br /&gt;
&lt;br /&gt;
  // enable multi-vector interrupts&lt;br /&gt;
  INTEnableSystemMultiVectoredInt();&lt;br /&gt;
&lt;br /&gt;
  while(1) {		// infinite loop&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
} // end main&lt;br /&gt;
&lt;br /&gt;
/* initLEDs()&lt;br /&gt;
 *&lt;br /&gt;
 * initLEDs sets RG12 and RG13 to outputs to control the LEDs on the NU32v2.&lt;br /&gt;
 */&lt;br /&gt;
void initLEDs(void) {&lt;br /&gt;
  LATG |= 0x3000;		// Initializes G12 and G13 to 1 (off for NU32v2 board)&lt;br /&gt;
  TRISG &amp;amp;= 0xCFFF;	// set the two pins to inputs&lt;br /&gt;
				&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From this example, we can see how to set up timer 2 at a frequency of 5 Hz with an interrupt priority of 2.&lt;br /&gt;
&lt;br /&gt;
Now let&amp;#039;s modify this simple time-based ISR to toggle the leds at two fixed time periods, one at period T, other at period kT, where k is an integer, by using a counter i that increments each time the ISR is called, and when i reaches k, does the less frequent code and sets i back to zero. For this example, create a global int count and initialize it to zero.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; int count = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use this ISR instead of the previous, in order to have LED0 toggle at 5 Hz and LED1 toggle at 1 Hz. &lt;br /&gt;
&amp;lt;pre&amp;gt;/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0  &lt;br /&gt;
  &lt;br /&gt;
  count++;&lt;br /&gt;
	&lt;br /&gt;
  if(count == 5){&lt;br /&gt;
    LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
		count = 0;&lt;br /&gt;
		}&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Imagine that we wanted to toggle the LEDs at 1 Hz by using a 32-bit timer. We can combine timers 2 and 3 or timers 4 and 5 to create a 32-bit timer. Let&amp;#039;s use a prescalar of 1:1, the period register then should be PR = (1 sec) (80000000 Hz) (1) -1 = 79999999 &amp;lt; 2^32 - 1. &lt;br /&gt;
&lt;br /&gt;
Replace the timer initialization with &lt;br /&gt;
&amp;lt;pre&amp;gt; // configure Timer 2 as 32-bit timer using internal clock, 1:1 prescalar&lt;br /&gt;
OpenTimer23(T23_ON | T23_SOURCE_INT | T23_PS_1_1, 79999999);	&lt;br /&gt;
&lt;br /&gt;
// set up the timer interrupt with a priority of 2&lt;br /&gt;
ConfigIntTimer23(T23_INT_ON | T23_INT_PRIOR_2);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Replace the ISR with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// ISR for Timer 23	&lt;br /&gt;
// Note that the interrupt vector needs to be _TIMER_3_VECTOR&lt;br /&gt;
void __ISR(_TIMER_3_VECTOR, ipl2) Timer23Handler(void) {&lt;br /&gt;
		&lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1; // toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT23ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In summary, this example shows you how to set up a time based interrupt with a given frequency and prescalar. It also shows you how to create a 32-bit timer.&lt;br /&gt;
&lt;br /&gt;
=== External Counter ===&lt;br /&gt;
&lt;br /&gt;
[[Media:NU32v2_external_count_example.c | This code]]  is going to set up an external counter to count the number of high pulses on the T5CK pin. The counts  will be displayed in binary on the NU32v2 board and will reset after 4 button pushes. You need a simple circuit consisting of 3.3V connected to a switch connected to a resistor (1K) connected to ground. Connect a wire from T5CK to the junction of the resistor and the switch. &lt;br /&gt;
&lt;br /&gt;
The main elements of this code are the initialization&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OpenTimer5( T5_ON | T5_PS_1_1 | T5_SOURCE_EXT, 0xFFFF);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
which sets up Timer 5 with an external clock source and a prescaler of 1:1. The PR value is the value that you want the counter to roll-over / generate an interrupt. We will not be setting up interrupts, so this number does not really matter. &lt;br /&gt;
&lt;br /&gt;
We will read the counter with the function&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 ReadTimer5()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will reset the counter to zero with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
WriteTimer5(0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:NU32v2_external_count_example.c&amp;diff=19899</id>
		<title>File:NU32v2 external count example.c</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:NU32v2_external_count_example.c&amp;diff=19899"/>
		<updated>2011-01-26T15:53:53Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19898</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19898"/>
		<updated>2011-01-26T15:53:28Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Sample Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Counters are quite simple:  they count the number of pulses they have received since they were last reset (or last &amp;quot;rolled over&amp;quot;).  If the pulses come from a fixed frequency clock, they can be thought of as timers, hence the words counter and timer are often used interchangeably.  The pulses may come from the internal peripheral bus clodk or external sources.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescaler that determines how many pulses the timer must receive from the clock source before adding one to the counter value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalers of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalers of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers.&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the counters are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, not all of the bits are relevant. Depending on whether these bits are 0 or 1, different actions or settings are performed for Timer (counter) x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the counter. There are many different ways to configure the counters. The main options for TxCON are to turn on or off the counter, choose the prescaler, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen in the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit count. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value where TMRx rolls over (resets to zero).&lt;br /&gt;
&lt;br /&gt;
Counter interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the counter.  An interrupt can be generated when the counter resets.&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets the priority of the interrupt associated with the counter. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescaler (1:n), the counter increments by 1 every n pulses from the clock. The counter is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the counter interrupt is generated when the counter resets. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the interrupt is given by &lt;br /&gt;
 Period = [(PRx + 1) Tpb (TMR_Prescaler_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
This means that if we want to specify the frequency of the interrupt, we need to determine PRx. Note that PRx needs to be less that 2^16 - 1 if using only 1 timer or less than 2^32 - 1 if combining two timers. See example below.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h.&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
For each interrupt, we need to write an interrupt service routine (ISR), which basically means when an interrupt is generated jump to and execute a given set of code before returning. The standard way to write an ISR is the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void __ISR(vector, iplx) yHandler(void){&lt;br /&gt;
&lt;br /&gt;
// code to execute&lt;br /&gt;
&lt;br /&gt;
// clear the interrupt flag&lt;br /&gt;
   function to clear interrupt flag&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
where vector refers to a constant defined in &amp;#039;&amp;#039;&amp;#039;(find this location)&amp;#039;&amp;#039;&amp;#039;, iplx is the interrupt priority level where x is the level. yHandler can be anything you want, for example we can call the timer2 handler as Timer2Handler. There is a function associated with each interrupt that is used to clear the flag, which allows the program to return to where it was before the interrupt was generated. Without clearing the flag, the program will never exit the interrupt. &lt;br /&gt;
&lt;br /&gt;
This example is going to toggle 2 LEDs at a given frequency using interrupts with timer 2. We are going to use the functions OpenTimer2() and ConfigIntTimer2() to initialize the timer and interrupts. You will see in the example code that there is also a function INTEnableSystemMultiVectoredInt() which is used to set up multi-vectored interrupts &amp;#039;&amp;#039;&amp;#039;(whatever that means)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* Timer_Simple.c&lt;br /&gt;
 *&lt;br /&gt;
 * This example toggles 2 LEDs on and off using a timer interrupt. &lt;br /&gt;
 * &lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
					&lt;br /&gt;
#define SYS_FREQ  	(80000000L)		// clock frequency set by bootloader&lt;br /&gt;
#define LED_0		LATGbits.LATG12 // mnemonic constants for LEDs on NU32v2&lt;br /&gt;
#define LED_1		LATGbits.LATG13&lt;br /&gt;
&lt;br /&gt;
// Define constants for the interrupt frequency (Hz) and the prescalar&lt;br /&gt;
// Note that (SYS_FREQ / INT_FREQ / PRE_SCALAR - 1) needs to be less than 2^16-1&lt;br /&gt;
#define INT_FREQ	5&lt;br /&gt;
#define PRE_SCALAR	256		&lt;br /&gt;
&lt;br /&gt;
void initLEDs(void);&lt;br /&gt;
&lt;br /&gt;
//int count = 0;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
  // SYSTEMConfig() optimizes performance for our clock frequency.&lt;br /&gt;
  // in Microchip\MPLAB C32\pic32-libs\include\peripheral\system.h&lt;br /&gt;
  SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);&lt;br /&gt;
    &lt;br /&gt;
  initLEDs(); 	// set LED pins as outputs&lt;br /&gt;
&lt;br /&gt;
  LED_0 = 1; // low output turns an LED on; see NU32v2 circuit&lt;br /&gt;
  LED_1 = 0; // high output turns an LED off&lt;br /&gt;
	&lt;br /&gt;
  // configure Timer 2 using internal clock with given prescalar&lt;br /&gt;
  OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_256, SYS_FREQ/INT_FREQ/PRE_SCALAR - 1);&lt;br /&gt;
&lt;br /&gt;
  // set up the timer interrupt with a priority of 2&lt;br /&gt;
  ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2);&lt;br /&gt;
&lt;br /&gt;
  // enable multi-vector interrupts&lt;br /&gt;
  INTEnableSystemMultiVectoredInt();&lt;br /&gt;
&lt;br /&gt;
  while(1) {		// infinite loop&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
} // end main&lt;br /&gt;
&lt;br /&gt;
/* initLEDs()&lt;br /&gt;
 *&lt;br /&gt;
 * initLEDs sets RG12 and RG13 to outputs to control the LEDs on the NU32v2.&lt;br /&gt;
 */&lt;br /&gt;
void initLEDs(void) {&lt;br /&gt;
  LATG |= 0x3000;		// Initializes G12 and G13 to 1 (off for NU32v2 board)&lt;br /&gt;
  TRISG &amp;amp;= 0xCFFF;	// set the two pins to inputs&lt;br /&gt;
				&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From this example, we can see how to set up timer 2 at a frequency of 5 Hz with an interrupt priority of 2.&lt;br /&gt;
&lt;br /&gt;
Now let&amp;#039;s modify this simple time-based ISR to toggle the leds at two fixed time periods, one at period T, other at period kT, where k is an integer, by using a counter i that increments each time the ISR is called, and when i reaches k, does the less frequent code and sets i back to zero. For this example, create a global int count and initialize it to zero.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; int count = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use this ISR instead of the previous, in order to have LED0 toggle at 5 Hz and LED1 toggle at 1 Hz. &lt;br /&gt;
&amp;lt;pre&amp;gt;/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0  &lt;br /&gt;
  &lt;br /&gt;
  count++;&lt;br /&gt;
	&lt;br /&gt;
  if(count == 5){&lt;br /&gt;
    LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
		count = 0;&lt;br /&gt;
		}&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Imagine that we wanted to toggle the LEDs at 1 Hz by using a 32-bit timer. We can combine timers 2 and 3 or timers 4 and 5 to create a 32-bit timer. Let&amp;#039;s use a prescalar of 1:1, the period register then should be PR = (1 sec) (80000000 Hz) (1) -1 = 79999999 &amp;lt; 2^32 - 1. &lt;br /&gt;
&lt;br /&gt;
Replace the timer initialization with &lt;br /&gt;
&amp;lt;pre&amp;gt; // configure Timer 2 as 32-bit timer using internal clock, 1:1 prescalar&lt;br /&gt;
OpenTimer23(T23_ON | T23_SOURCE_INT | T23_PS_1_1, 79999999);	&lt;br /&gt;
&lt;br /&gt;
// set up the timer interrupt with a priority of 2&lt;br /&gt;
ConfigIntTimer23(T23_INT_ON | T23_INT_PRIOR_2);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Replace the ISR with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// ISR for Timer 23	&lt;br /&gt;
// Note that the interrupt vector needs to be _TIMER_3_VECTOR&lt;br /&gt;
void __ISR(_TIMER_3_VECTOR, ipl2) Timer23Handler(void) {&lt;br /&gt;
		&lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1; // toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT23ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In summary, this example shows you how to set up a time based interrupt with a given frequency and prescalar. It also shows you how to create a 32-bit timer.&lt;br /&gt;
&lt;br /&gt;
=== External Counter ===&lt;br /&gt;
&lt;br /&gt;
[[Media:NU32v2_external_count_example.c | This code]]  is going to set up an external counter to count the number of high pulses on the T5CK pin. The counts  will be displayed in binary on the NU32v2 board and will reset after 4 button pushes. You need a simple circuit consisting of 3.3V connected to a switch connected to a resistor (1K) connected to ground. Connect a wire from T5CK to the junction of the resistor and the switch. &lt;br /&gt;
&lt;br /&gt;
The main elements of this code are the initialization&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OpenTimer5( T5_ON | T5_PS_1_1 | T5_SOURCE_EXT, 0xFFFF);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
which sets up Timer 5 with an external clock source and a prescaler of 1:1. The PR value is the value that you want the counter to roll-over / generate an interrupt. We will not be setting up interrupts, so this number does not really matter. &lt;br /&gt;
&lt;br /&gt;
We will read the counter with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 ReadTimer5()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will reset the counter to zero with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
WriteTimer5(0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19710</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19710"/>
		<updated>2011-01-23T23:31:18Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Time-based Interrupts */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescalar (1:n), the timer increments by 1 every n pulses from the clock. The timer is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the timer interrupt is generated when the timer is reset. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the timer interrupt is given by &lt;br /&gt;
&lt;br /&gt;
Period = [(PRx + 1) Tpb (TMR_Prescalar_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
This means that if we want to specify the frequency of the interrupt, we need to determine PRx. Note that PRx needs to be less that 2^16 - 1 if using only 1 timer or less than 2^32 - 1 if combining two timers. See example below.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
For each interrupt, we need to write an interrupt service routine (ISR), which basically means when an interrupt is generated jump to and execute a given set of code before returning. The standard way to write an ISR is the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void __ISR(vector, iplx) yHandler(void){&lt;br /&gt;
&lt;br /&gt;
// code to execute&lt;br /&gt;
&lt;br /&gt;
// clear the interrupt flag&lt;br /&gt;
   function to clear interrupt flag&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
where vector refers to a constant defined in &amp;#039;&amp;#039;&amp;#039;(find this location)&amp;#039;&amp;#039;&amp;#039;, iplx is the interrupt priority level where x is the level. yHandler can be anything you want, for example we can call the timer2 handler as Timer2Handler. There is a function associated with each interrupt that is used to clear the flag, which allows the program to return to where it was before the interrupt was generated. Without clearing the flag, the program will never exit the interrupt. &lt;br /&gt;
&lt;br /&gt;
This example is going to toggle 2 LEDs at a given frequency using interrupts with timer 2. We are going to use the functions OpenTimer2() and ConfigIntTimer2() to initialize the timer and interrupts. You will see in the example code that there is also a function INTEnableSystemMultiVectoredInt() which is used to set up multi-vectored interrupts &amp;#039;&amp;#039;&amp;#039;(whatever that means)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* Timer_Simple.c&lt;br /&gt;
 *&lt;br /&gt;
 * This example toggles 2 LEDs on and off using a timer interrupt. &lt;br /&gt;
 * &lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
					&lt;br /&gt;
#define SYS_FREQ  	(80000000L)		// clock frequency set by bootloader&lt;br /&gt;
#define LED_0		LATGbits.LATG12 // mnemonic constants for LEDs on NU32v2&lt;br /&gt;
#define LED_1		LATGbits.LATG13&lt;br /&gt;
&lt;br /&gt;
// Define constants for the interrupt frequency (Hz) and the prescalar&lt;br /&gt;
// Note that (SYS_FREQ / INT_FREQ / PRE_SCALAR - 1) needs to be less than 2^16-1&lt;br /&gt;
#define INT_FREQ	5&lt;br /&gt;
#define PRE_SCALAR	256		&lt;br /&gt;
&lt;br /&gt;
void initLEDs(void);&lt;br /&gt;
&lt;br /&gt;
//int count = 0;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
  // SYSTEMConfig() optimizes performance for our clock frequency.&lt;br /&gt;
  // in Microchip\MPLAB C32\pic32-libs\include\peripheral\system.h&lt;br /&gt;
  SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);&lt;br /&gt;
    &lt;br /&gt;
  initLEDs(); 	// set LED pins as outputs&lt;br /&gt;
&lt;br /&gt;
  LED_0 = 1; // low output turns an LED on; see NU32v2 circuit&lt;br /&gt;
  LED_1 = 0; // high output turns an LED off&lt;br /&gt;
	&lt;br /&gt;
  // configure Timer 2 using internal clock with given prescalar&lt;br /&gt;
  OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_256, SYS_FREQ/INT_FREQ/PRE_SCALAR - 1);&lt;br /&gt;
&lt;br /&gt;
  // set up the timer interrupt with a priority of 2&lt;br /&gt;
  ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2);&lt;br /&gt;
&lt;br /&gt;
  // enable multi-vector interrupts&lt;br /&gt;
  INTEnableSystemMultiVectoredInt();&lt;br /&gt;
&lt;br /&gt;
  while(1) {		// infinite loop&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
} // end main&lt;br /&gt;
&lt;br /&gt;
/* initLEDs()&lt;br /&gt;
 *&lt;br /&gt;
 * initLEDs sets RG12 and RG13 to outputs to control the LEDs on the NU32v2.&lt;br /&gt;
 */&lt;br /&gt;
void initLEDs(void) {&lt;br /&gt;
  LATG |= 0x3000;		// Initializes G12 and G13 to 1 (off for NU32v2 board)&lt;br /&gt;
  TRISG &amp;amp;= 0xCFFF;	// set the two pins to inputs&lt;br /&gt;
				&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From this example, we can see how to set up timer 2 at a frequency of 5 Hz with an interrupt priority of 2.&lt;br /&gt;
&lt;br /&gt;
Now let&amp;#039;s modify this simple time-based ISR to toggle the leds at two fixed time periods, one at period T, other at period kT, where k is an integer, by using a counter i that increments each time the ISR is called, and when i reaches k, does the less frequent code and sets i back to zero. For this example, create a global int count and initialize it to zero.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; int count = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use this ISR instead of the previous, in order to have LED0 toggle at 5 Hz and LED1 toggle at 1 Hz. &lt;br /&gt;
&amp;lt;pre&amp;gt;/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0  &lt;br /&gt;
  &lt;br /&gt;
  count++;&lt;br /&gt;
	&lt;br /&gt;
  if(count == 5){&lt;br /&gt;
    LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
		count = 0;&lt;br /&gt;
		}&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Imagine that we wanted to toggle the LEDs at 1 Hz by using a 32-bit timer. We can combine timers 2 and 3 or timers 4 and 5 to create a 32-bit timer. Let&amp;#039;s use a prescalar of 1:1, the period register then should be PR = (1 sec) (80000000 Hz) (1) -1 = 79999999 &amp;lt; 2^32 - 1. &lt;br /&gt;
&lt;br /&gt;
Replace the timer initialization with &lt;br /&gt;
&amp;lt;pre&amp;gt; // configure Timer 2 as 32-bit timer using internal clock, 1:1 prescalar&lt;br /&gt;
OpenTimer23(T23_ON | T23_SOURCE_INT | T23_PS_1_1, 79999999);	&lt;br /&gt;
&lt;br /&gt;
// set up the timer interrupt with a priority of 2&lt;br /&gt;
ConfigIntTimer23(T23_INT_ON | T23_INT_PRIOR_2);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Replace the ISR with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// ISR for Timer 23	&lt;br /&gt;
// Note that the interrupt vector needs to be _TIMER_3_VECTOR&lt;br /&gt;
void __ISR(_TIMER_3_VECTOR, ipl2) Timer23Handler(void) {&lt;br /&gt;
		&lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1; // toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT23ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In summary, this example shows you how to set up a time based interrupt with a given frequency and prescalar. It also shows you how to create a 32-bit timer.&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19709</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19709"/>
		<updated>2011-01-23T23:26:22Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Time-based Interrupts */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescalar (1:n), the timer increments by 1 every n pulses from the clock. The timer is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the timer interrupt is generated when the timer is reset. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the timer interrupt is given by &lt;br /&gt;
&lt;br /&gt;
Period = [(PRx + 1) Tpb (TMR_Prescalar_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
This means that if we want to specify the frequency of the interrupt, we need to determine PRx. Note that PRx needs to be less that 2^16 - 1 if using only 1 timer or less than 2^32 - 1 if combining two timers. See example below.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
For each interrupt, we need to write an interrupt service routine (ISR), which basically means when an interrupt is generated jump to and execute a given set of code before returning. The standard way to write an ISR is the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void __ISR(vector, iplx) yHandler(void){&lt;br /&gt;
&lt;br /&gt;
// code to execute&lt;br /&gt;
&lt;br /&gt;
// clear the interrupt flag&lt;br /&gt;
   function to clear interrupt flag&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
where vector refers to a constant defined in &amp;#039;&amp;#039;&amp;#039;(find this location)&amp;#039;&amp;#039;&amp;#039;, iplx is the interrupt priority level where x is the level. yHandler can be anything you want, for example we can call the timer2 handler as Timer2Handler. There is a function associated with each interrupt that is used to clear the flag, which allows the program to return to where it was before the interrupt was generated. Without clearing the flag, the program will never exit the interrupt. &lt;br /&gt;
&lt;br /&gt;
This example is going to toggle 2 LEDs at a given frequency using interrupts with timer 2. We are going to use the functions OpenTimer2() and ConfigIntTimer2() to initialize the timer and interrupts. You will see in the example code that there is also a function INTEnableSystemMultiVectoredInt() which is used to set up multi-vectored interrupts &amp;#039;&amp;#039;&amp;#039;(whatever that means)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* Timer_Simple.c&lt;br /&gt;
 *&lt;br /&gt;
 * This example toggles 2 LEDs on and off using a timer interrupt. &lt;br /&gt;
 * &lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
					&lt;br /&gt;
#define SYS_FREQ  	(80000000L)		// clock frequency set by bootloader&lt;br /&gt;
#define LED_0		LATGbits.LATG12 // mnemonic constants for LEDs on NU32v2&lt;br /&gt;
#define LED_1		LATGbits.LATG13&lt;br /&gt;
&lt;br /&gt;
// Define constants for the interrupt frequency (Hz) and the prescalar&lt;br /&gt;
// Note that (SYS_FREQ / INT_FREQ / PRE_SCALAR - 1) needs to be less than 2^16-1&lt;br /&gt;
#define INT_FREQ	5&lt;br /&gt;
#define PRE_SCALAR	256		&lt;br /&gt;
&lt;br /&gt;
void initLEDs(void);&lt;br /&gt;
&lt;br /&gt;
//int count = 0;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
  // SYSTEMConfig() optimizes performance for our clock frequency.&lt;br /&gt;
  // in Microchip\MPLAB C32\pic32-libs\include\peripheral\system.h&lt;br /&gt;
  SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);&lt;br /&gt;
    &lt;br /&gt;
  initLEDs(); 	// set LED pins as outputs&lt;br /&gt;
&lt;br /&gt;
  LED_0 = 1; // low output turns an LED on; see NU32v2 circuit&lt;br /&gt;
  LED_1 = 0; // high output turns an LED off&lt;br /&gt;
	&lt;br /&gt;
  // configure Timer 2 using internal clock with given prescalar&lt;br /&gt;
  OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_256, SYS_FREQ/INT_FREQ/PRE_SCALAR - 1);&lt;br /&gt;
&lt;br /&gt;
  // set up the timer interrupt with a priority of 2&lt;br /&gt;
  ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2);&lt;br /&gt;
&lt;br /&gt;
  // enable multi-vector interrupts&lt;br /&gt;
  INTEnableSystemMultiVectoredInt();&lt;br /&gt;
&lt;br /&gt;
  while(1) {		// infinite loop&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
} // end main&lt;br /&gt;
&lt;br /&gt;
/* initLEDs()&lt;br /&gt;
 *&lt;br /&gt;
 * initLEDs sets RG12 and RG13 to outputs to control the LEDs on the NU32v2.&lt;br /&gt;
 */&lt;br /&gt;
void initLEDs(void) {&lt;br /&gt;
  LATG |= 0x3000;		// Initializes G12 and G13 to 1 (off for NU32v2 board)&lt;br /&gt;
  TRISG &amp;amp;= 0xCFFF;	// set the two pins to inputs&lt;br /&gt;
				&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From this example, we can see how to set up timer 2 at a frequency of 5 Hz with an interrupt priority of 2.&lt;br /&gt;
&lt;br /&gt;
Now let&amp;#039;s modify this simple time-based ISR to toggle the leds at two fixed time periods, one at period T, other at period kT, where k is an integer, by using a counter i that increments each time the ISR is called, and when i reaches k, does the less frequent code and sets i back to zero. For this example, create a global int count and initialize it to zero.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
int count = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use this ISR instead of the previous, in order to have LED0 toggle at 5 Hz and LED1 toggle at 1 Hz. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0  &lt;br /&gt;
  &lt;br /&gt;
  count++;&lt;br /&gt;
	&lt;br /&gt;
  if(count == 5){&lt;br /&gt;
    LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
		count = 0;&lt;br /&gt;
		}&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Imagine that we wanted to toggle the LEDs at 1 Hz by using a 32-bit timer. We can combine timers 2 and 3 or timers 4 and 5 to create a 32-bit timer. Let&amp;#039;s use a prescalar of 1:1, the period register then should be PR = (1 sec) (80000000 Hz) (1) -1 = 79999999 &amp;lt; 2^32 - 1. &lt;br /&gt;
&lt;br /&gt;
Replace the timer initialization with &lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
// configure Timer 2 as 32-bit timer using internal clock, 1:1 prescalar&lt;br /&gt;
OpenTimer23(T23_ON | T23_SOURCE_INT | T23_PS_1_1, 79999999);	&lt;br /&gt;
&lt;br /&gt;
// set up the timer interrupt with a priority of 2&lt;br /&gt;
ConfigIntTimer23(T23_INT_ON | T23_INT_PRIOR_2);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Replace the ISR with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// ISR for Timer 23	&lt;br /&gt;
// Note that the interrupt vector needs to be _TIMER_3_VECTOR&lt;br /&gt;
void __ISR(_TIMER_3_VECTOR, ipl2) Timer23Handler(void) {&lt;br /&gt;
		&lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1; // toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT23ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In summary, this example shows you how to set up a time based interrupt with a given frequency and prescalar. It also shows you how to create a 32-bit timer.&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19708</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19708"/>
		<updated>2011-01-23T23:18:11Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Time-based Interrupts */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescalar (1:n), the timer increments by 1 every n pulses from the clock. The timer is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the timer interrupt is generated when the timer is reset. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the timer interrupt is given by &lt;br /&gt;
&lt;br /&gt;
Period = [(PRx + 1) Tpb (TMR_Prescalar_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
This means that if we want to specify the frequency of the interrupt, we need to determine PRx. Note that PRx needs to be less that 2^16 - 1 if using only 1 timer or less than 2^32 - 1 if combining two timers. See example below.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
For each interrupt, we need to write an interrupt service routine (ISR), which basically means when an interrupt is generated jump to and execute a given set of code before returning. The standard way to write an ISR is the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void __ISR(vector, iplx) yHandler(void){&lt;br /&gt;
&lt;br /&gt;
// code to execute&lt;br /&gt;
&lt;br /&gt;
// clear the interrupt flag&lt;br /&gt;
   function to clear interrupt flag&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
where vector refers to a constant defined in &amp;#039;&amp;#039;&amp;#039;(find this location)&amp;#039;&amp;#039;&amp;#039;, iplx is the interrupt priority level where x is the level. yHandler can be anything you want, for example we can call the timer2 handler as Timer2Handler. There is a function associated with each interrupt that is used to clear the flag, which allows the program to return to where it was before the interrupt was generated. Without clearing the flag, the program will never exit the interrupt. &lt;br /&gt;
&lt;br /&gt;
This example is going to toggle 2 LEDs at a given frequency using interrupts with timer 2. We are going to use the functions OpenTimer2() and ConfigIntTimer2() to initialize the timer and interrupts. You will see in the example code that there is also a function INTEnableSystemMultiVectoredInt() which is used to set up multi-vectored interrupts &amp;#039;&amp;#039;&amp;#039;(whatever that means)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* Timer_Simple.c&lt;br /&gt;
 *&lt;br /&gt;
 * This example toggles 2 LEDs on and off using a timer interrupt. &lt;br /&gt;
 * &lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
					&lt;br /&gt;
#define SYS_FREQ  	(80000000L)		// clock frequency set by bootloader&lt;br /&gt;
#define LED_0		LATGbits.LATG12 // mnemonic constants for LEDs on NU32v2&lt;br /&gt;
#define LED_1		LATGbits.LATG13&lt;br /&gt;
&lt;br /&gt;
// Define constants for the interrupt frequency (Hz) and the prescalar&lt;br /&gt;
// Note that (SYS_FREQ / INT_FREQ / PRE_SCALAR - 1) needs to be less than 2^16-1&lt;br /&gt;
#define INT_FREQ	5&lt;br /&gt;
#define PRE_SCALAR	256		&lt;br /&gt;
&lt;br /&gt;
void initLEDs(void);&lt;br /&gt;
&lt;br /&gt;
//int count = 0;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
	// SYSTEMConfig() optimizes performance for our clock frequency.&lt;br /&gt;
	// in Microchip\MPLAB C32\pic32-libs\include\peripheral\system.h&lt;br /&gt;
	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);&lt;br /&gt;
    &lt;br /&gt;
	initLEDs(); 	// set LED pins as outputs&lt;br /&gt;
&lt;br /&gt;
	LED_0 = 1; // low output turns an LED on; see NU32v2 circuit&lt;br /&gt;
	LED_1 = 0; // high output turns an LED off&lt;br /&gt;
	&lt;br /&gt;
	// configure Timer 2 using internal clock with given prescalar&lt;br /&gt;
  OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_256, SYS_FREQ/INT_FREQ/PRE_SCALAR - 1);&lt;br /&gt;
&lt;br /&gt;
  // set up the timer interrupt with a priority of 2&lt;br /&gt;
  ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2);&lt;br /&gt;
&lt;br /&gt;
  // enable multi-vector interrupts&lt;br /&gt;
  INTEnableSystemMultiVectoredInt();&lt;br /&gt;
&lt;br /&gt;
	while(1) {		// infinite loop&lt;br /&gt;
	}&lt;br /&gt;
	return 0;&lt;br /&gt;
} // end main&lt;br /&gt;
&lt;br /&gt;
/* initLEDs()&lt;br /&gt;
 *&lt;br /&gt;
 * initLEDs sets RG12 and RG13 to outputs to control the LEDs on the NU32v2.&lt;br /&gt;
 */&lt;br /&gt;
void initLEDs(void) {&lt;br /&gt;
	LATG |= 0x3000;		// Initializes G12 and G13 to 1 (off for NU32v2 board)&lt;br /&gt;
	TRISG &amp;amp;= 0xCFFF;	// set the two pins to inputs&lt;br /&gt;
						&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From this example, we can see how to set up timer 2 at a frequency of 5 Hz with an interrupt priority of 2.&lt;br /&gt;
&lt;br /&gt;
Now let&amp;#039;s modify this simple time-based ISR to toggle the leds at two fixed time periods, one at period T, other at period kT, where k is an integer, by using a counter i that increments each time the ISR is called, and when i reaches k, does the less frequent code and sets i back to zero. For this example, create a global int count and initialize it to zero.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
int count = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use this ISR instead of the previous, in order to have LED0 toggle at 5 Hz and LED1 toggle at 1 Hz. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0  &lt;br /&gt;
  &lt;br /&gt;
  count++;&lt;br /&gt;
	&lt;br /&gt;
  if(count == 5){&lt;br /&gt;
    LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
		count = 0;&lt;br /&gt;
		}&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Imagine that we wanted to toggle the LEDs at 1 Hz by using a 32-bit timer. We can combine timers 2 and 3 or timers 4 and 5 to create a 32-bit timer. Let&amp;#039;s use a prescalar of 1:1, the period register then should be PR = (1 sec) (80000000 Hz) (1) -1 = 79999999 &amp;lt; 2^32 - 1. &lt;br /&gt;
&lt;br /&gt;
Replace the timer initialization with &lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
// configure Timer 2 as 32-bit timer using internal clock, 1:1 prescalar&lt;br /&gt;
OpenTimer23(T23_ON | T23_SOURCE_INT | T23_PS_1_1, 79999999);	&lt;br /&gt;
&lt;br /&gt;
// set up the timer interrupt with a priority of 2&lt;br /&gt;
ConfigIntTimer23(T23_INT_ON | T23_INT_PRIOR_2);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Replace the ISR with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// ISR for Timer 23	&lt;br /&gt;
// Note that the interrupt vector needs to be _TIMER_3_VECTOR&lt;br /&gt;
void __ISR(_TIMER_3_VECTOR, ipl2) Timer23Handler(void) {&lt;br /&gt;
		&lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1; // toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT23ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In summary, this example shows you how to set up a time based interrupt with a given frequency and prescalar. It also shows you how to create a 32-bit timer.&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19707</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19707"/>
		<updated>2011-01-23T23:15:29Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Time-based Interrupts */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescalar (1:n), the timer increments by 1 every n pulses from the clock. The timer is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the timer interrupt is generated when the timer is reset. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the timer interrupt is given by &lt;br /&gt;
&lt;br /&gt;
Period = [(PRx + 1) Tpb (TMR_Prescalar_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
This means that if we want to specify the frequency of the interrupt, we need to determine PRx. Note that PRx needs to be less that 2^16 - 1 if using only 1 timer or less than 2^32 - 1 if combining two timers. See example below.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
For each interrupt, we need to write an interrupt service routine (ISR), which basically means when an interrupt is generated jump to and execute a given set of code before returning. The standard way to write an ISR is the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void __ISR(vector, iplx) yHandler(void){&lt;br /&gt;
&lt;br /&gt;
// code to execute&lt;br /&gt;
&lt;br /&gt;
// clear the interrupt flag&lt;br /&gt;
   function to clear interrupt flag&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
where vector refers to a constant defined in &amp;#039;&amp;#039;&amp;#039;(find this location)&amp;#039;&amp;#039;&amp;#039;, iplx is the interrupt priority level where x is the level. yHandler can be anything you want, for example we can call the timer2 handler as Timer2Handler. There is a function associated with each interrupt that is used to clear the flag, which allows the program to return to where it was before the interrupt was generated. Without clearing the flag, the program will never exit the interrupt. &lt;br /&gt;
&lt;br /&gt;
This example is going to toggle 2 LEDs at a given frequency using interrupts with timer 2. We are going to use the functions OpenTimer2() and ConfigIntTimer2() to initialize the timer and interrupts. You will see in the example code that there is also a function INTEnableSystemMultiVectoredInt() which is used to set up multi-vectored interrupts &amp;#039;&amp;#039;&amp;#039;(whatever that means)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* Timer_Simple.c&lt;br /&gt;
 *&lt;br /&gt;
 * This example toggles 2 LEDs on and off using a timer interrupt. &lt;br /&gt;
 * &lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
					&lt;br /&gt;
#define SYS_FREQ  	(80000000L)		// clock frequency set by bootloader&lt;br /&gt;
#define LED_0		LATGbits.LATG12 // mnemonic constants for LEDs on NU32v2&lt;br /&gt;
#define LED_1		LATGbits.LATG13&lt;br /&gt;
&lt;br /&gt;
// Define constants for the interrupt frequency (Hz) and the prescalar&lt;br /&gt;
// Note that (SYS_FREQ / INT_FREQ / PRE_SCALAR - 1) needs to be less than 2^16-1&lt;br /&gt;
#define INT_FREQ	5&lt;br /&gt;
#define PRE_SCALAR	256		&lt;br /&gt;
&lt;br /&gt;
void initLEDs(void);&lt;br /&gt;
&lt;br /&gt;
//int count = 0;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
	// SYSTEMConfig() optimizes performance for our clock frequency.&lt;br /&gt;
	// in Microchip\MPLAB C32\pic32-libs\include\peripheral\system.h&lt;br /&gt;
	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);&lt;br /&gt;
    &lt;br /&gt;
	initLEDs(); 	// set LED pins as outputs&lt;br /&gt;
&lt;br /&gt;
	LED_0 = 1; // low output turns an LED on; see NU32v2 circuit&lt;br /&gt;
	LED_1 = 0; // high output turns an LED off&lt;br /&gt;
	&lt;br /&gt;
	// configure Timer 2 using internal clock with given prescalar&lt;br /&gt;
  OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_256, SYS_FREQ/INT_FREQ/PRE_SCALAR - 1);&lt;br /&gt;
&lt;br /&gt;
  // set up the timer interrupt with a priority of 2&lt;br /&gt;
  ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2);&lt;br /&gt;
&lt;br /&gt;
  // enable multi-vector interrupts&lt;br /&gt;
  INTEnableSystemMultiVectoredInt();&lt;br /&gt;
&lt;br /&gt;
	while(1) {		// infinite loop&lt;br /&gt;
	}&lt;br /&gt;
	return 0;&lt;br /&gt;
} // end main&lt;br /&gt;
&lt;br /&gt;
/* initLEDs()&lt;br /&gt;
 *&lt;br /&gt;
 * initLEDs sets RG12 and RG13 to outputs to control the LEDs on the NU32v2.&lt;br /&gt;
 */&lt;br /&gt;
void initLEDs(void) {&lt;br /&gt;
	LATG |= 0x3000;		// Initializes G12 and G13 to 1 (off for NU32v2 board)&lt;br /&gt;
	TRISG &amp;amp;= 0xCFFF;	// set the two pins to inputs&lt;br /&gt;
						&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From this example, we can see how to set up timer 2 at a frequency of 5 Hz with an interrupt priority of 2.&lt;br /&gt;
&lt;br /&gt;
Now let&amp;#039;s modify this simple time-based ISR to toggle the leds at two fixed time periods, one at period T, other at period kT, where k is an integer, by using a counter i that increments each time the ISR is called, and when i reaches k, does the less frequent code and sets i back to zero. For this example, create a global int count and initialize it to zero.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
int count = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use this ISR instead of the previous, in order to have LED0 toggle at 5 Hz and LED1 toggle at 1 Hz. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0  &lt;br /&gt;
  &lt;br /&gt;
  count++;&lt;br /&gt;
	&lt;br /&gt;
  if(count == 5){&lt;br /&gt;
    LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
		count = 0;&lt;br /&gt;
		}&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Imagine that we wanted to toggle the LEDs at 1 Hz by using a 32-bit timer. We can combine timers 2 and 3 or timers 4 and 5 to create a 32-bit timer. Let&amp;#039;s use a prescalar of 1:1, the period register then should be PR = (1 sec) (80000000 Hz) (1) -1 = 79999999 &amp;lt; 2^32 - 1. &lt;br /&gt;
&lt;br /&gt;
Replace the timer initialization with &lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
// configure Timer 2 as 32-bit timer using internal clock, 1:1 prescalar&lt;br /&gt;
OpenTimer23(T23_ON | T23_SOURCE_INT | T23_PS_1_1, 79999999);	&lt;br /&gt;
&lt;br /&gt;
// set up the timer interrupt with a priority of 2&lt;br /&gt;
ConfigIntTimer23(T23_INT_ON | T23_INT_PRIOR_2);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Replace the ISR with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// ISR for Timer 23	&lt;br /&gt;
// Note that the interrupt vector needs to be _TIMER_3_VECTOR&lt;br /&gt;
void __ISR(_TIMER_3_VECTOR, ipl2) Timer23Handler(void) {&lt;br /&gt;
		&lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1; // toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT23ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19706</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19706"/>
		<updated>2011-01-23T23:08:31Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Time-based Interrupts */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescalar (1:n), the timer increments by 1 every n pulses from the clock. The timer is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the timer interrupt is generated when the timer is reset. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the timer interrupt is given by &lt;br /&gt;
&lt;br /&gt;
Period = [(PRx + 1) Tpb (TMR_Prescalar_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
This means that if we want to specify the frequency of the interrupt, we need to determine PRx. Note that PRx needs to be less that 2^16 - 1 if using only 1 timer or less than 2^32 - 1 if combining two timers. See example below.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
For each interrupt, we need to write an interrupt service routine (ISR), which basically means when an interrupt is generated jump to and execute a given set of code before returning. The standard way to write an ISR is the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void __ISR(vector, iplx) yHandler(void){&lt;br /&gt;
&lt;br /&gt;
// code to execute&lt;br /&gt;
&lt;br /&gt;
// clear the interrupt flag&lt;br /&gt;
   function to clear interrupt flag&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
where vector refers to a constant defined in &amp;#039;&amp;#039;&amp;#039;(find this location)&amp;#039;&amp;#039;&amp;#039;, iplx is the interrupt priority level where x is the level. yHandler can be anything you want, for example we can call the timer2 handler as Timer2Handler. There is a function associated with each interrupt that is used to clear the flag, which allows the program to return to where it was before the interrupt was generated. Without clearing the flag, the program will never exit the interrupt. &lt;br /&gt;
&lt;br /&gt;
This example is going to toggle 2 LEDs at a given frequency using interrupts with timer 2. We are going to use the functions OpenTimer2() and ConfigIntTimer2() to initialize the timer and interrupts. You will see in the example code that there is also a function INTEnableSystemMultiVectoredInt() which is used to set up multi-vectored interrupts &amp;#039;&amp;#039;&amp;#039;(whatever that means)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* Timer_Simple.c&lt;br /&gt;
 *&lt;br /&gt;
 * This example toggles 2 LEDs on and off using a timer interrupt. &lt;br /&gt;
 * &lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
					&lt;br /&gt;
#define SYS_FREQ  	(80000000L)		// clock frequency set by bootloader&lt;br /&gt;
#define LED_0		LATGbits.LATG12 // mnemonic constants for LEDs on NU32v2&lt;br /&gt;
#define LED_1		LATGbits.LATG13&lt;br /&gt;
&lt;br /&gt;
// Define constants for the interrupt frequency (Hz) and the prescalar&lt;br /&gt;
// Note that (SYS_FREQ / INT_FREQ / PRE_SCALAR - 1) needs to be less than 2^16-1&lt;br /&gt;
#define INT_FREQ	5&lt;br /&gt;
#define PRE_SCALAR	256		&lt;br /&gt;
&lt;br /&gt;
void initLEDs(void);&lt;br /&gt;
&lt;br /&gt;
//int count = 0;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
	// SYSTEMConfig() optimizes performance for our clock frequency.&lt;br /&gt;
	// in Microchip\MPLAB C32\pic32-libs\include\peripheral\system.h&lt;br /&gt;
	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);&lt;br /&gt;
    &lt;br /&gt;
	initLEDs(); 	// set LED pins as outputs&lt;br /&gt;
&lt;br /&gt;
	LED_0 = 1; // low output turns an LED on; see NU32v2 circuit&lt;br /&gt;
	LED_1 = 0; // high output turns an LED off&lt;br /&gt;
	&lt;br /&gt;
	// configure Timer 2 using internal clock with given prescalar&lt;br /&gt;
  OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_256, SYS_FREQ/INT_FREQ/PRE_SCALAR - 1);&lt;br /&gt;
&lt;br /&gt;
  // set up the timer interrupt with a priority of 2&lt;br /&gt;
  ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2);&lt;br /&gt;
&lt;br /&gt;
  // enable multi-vector interrupts&lt;br /&gt;
  INTEnableSystemMultiVectoredInt();&lt;br /&gt;
&lt;br /&gt;
	while(1) {		// infinite loop&lt;br /&gt;
	}&lt;br /&gt;
	return 0;&lt;br /&gt;
} // end main&lt;br /&gt;
&lt;br /&gt;
/* initLEDs()&lt;br /&gt;
 *&lt;br /&gt;
 * initLEDs sets RG12 and RG13 to outputs to control the LEDs on the NU32v2.&lt;br /&gt;
 */&lt;br /&gt;
void initLEDs(void) {&lt;br /&gt;
	LATG |= 0x3000;		// Initializes G12 and G13 to 1 (off for NU32v2 board)&lt;br /&gt;
	TRISG &amp;amp;= 0xCFFF;	// set the two pins to inputs&lt;br /&gt;
						&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From this example, we can see how to set up timer 2 at a frequency of 5 Hz with an interrupt priority of 2.&lt;br /&gt;
&lt;br /&gt;
Now let&amp;#039;s modify this simple time-based ISR to toggle the leds at two fixed time periods, one at period T, other at period kT, where k is an integer, by using a counter i that increments each time the ISR is called, and when i reaches k, does the less frequent code and sets i back to zero. For this example, create a global int count and initialize it to zero.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
int count = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use this ISR instead of the previous, in order to have LED0 toggle at 5 Hz and LED1 toggle at 1 Hz. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0  &lt;br /&gt;
  &lt;br /&gt;
  count++;&lt;br /&gt;
	&lt;br /&gt;
  if(count == 5){&lt;br /&gt;
    LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
		count = 0;&lt;br /&gt;
		}&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19705</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19705"/>
		<updated>2011-01-23T23:02:44Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Time-based Interrupts */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescalar (1:n), the timer increments by 1 every n pulses from the clock. The timer is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the timer interrupt is generated when the timer is reset. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the timer interrupt is given by &lt;br /&gt;
&lt;br /&gt;
Period = [(PRx + 1) Tpb (TMR_Prescalar_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
This means that if we want to specify the frequency of the interrupt, we need to determine PRx. Note that PRx needs to be less that 2^16 - 1 if using only 1 timer or less than 2^32 - 1 if combining two timers. See example below.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
For each interrupt, we need to write an interrupt service routine (ISR), which basically means when an interrupt is generated jump to and execute a given set of code before returning. The standard way to write an ISR is the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void __ISR(vector, iplx) yHandler(void){&lt;br /&gt;
&lt;br /&gt;
// code to execute&lt;br /&gt;
&lt;br /&gt;
// clear the interrupt flag&lt;br /&gt;
   function to clear interrupt flag&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
where vector refers to a constant defined in &amp;#039;&amp;#039;&amp;#039;(find this location)&amp;#039;&amp;#039;&amp;#039;, iplx is the interrupt priority level where x is the level. yHandler can be anything you want, for example we can call the timer2 handler as Timer2Handler. There is a function associated with each interrupt that is used to clear the flag, which allows the program to return to where it was before the interrupt was generated. Without clearing the flag, the program will never exit the interrupt. &lt;br /&gt;
&lt;br /&gt;
This example is going to toggle 2 LEDs at a given frequency using interrupts with timer 2. We are going to use the functions OpenTimer2() and ConfigIntTimer2() to initialize the timer and interrupts. You will see in the example code that there is also a function INTEnableSystemMultiVectoredInt() which is used to set up multi-vectored interrupts &amp;#039;&amp;#039;&amp;#039;(whatever that means)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* Timer_Simple.c&lt;br /&gt;
 *&lt;br /&gt;
 * This example toggles 2 LEDs on and off using a timer interrupt. &lt;br /&gt;
 * &lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
					&lt;br /&gt;
#define SYS_FREQ  	(80000000L)		// clock frequency set by bootloader&lt;br /&gt;
#define LED_0		LATGbits.LATG12 // mnemonic constants for LEDs on NU32v2&lt;br /&gt;
#define LED_1		LATGbits.LATG13&lt;br /&gt;
&lt;br /&gt;
// Define constants for the interrupt frequency (Hz) and the prescalar&lt;br /&gt;
// Note that (SYS_FREQ / INT_FREQ / PRE_SCALAR - 1) needs to be less than 2^16-1&lt;br /&gt;
#define INT_FREQ	5&lt;br /&gt;
#define PRE_SCALAR	256		&lt;br /&gt;
&lt;br /&gt;
void initLEDs(void);&lt;br /&gt;
&lt;br /&gt;
//int count = 0;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
	// SYSTEMConfig() optimizes performance for our clock frequency.&lt;br /&gt;
	// in Microchip\MPLAB C32\pic32-libs\include\peripheral\system.h&lt;br /&gt;
	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);&lt;br /&gt;
    &lt;br /&gt;
	initLEDs(); 	// set LED pins as outputs&lt;br /&gt;
&lt;br /&gt;
	LED_0 = 1; // low output turns an LED on; see NU32v2 circuit&lt;br /&gt;
	LED_1 = 0; // high output turns an LED off&lt;br /&gt;
	&lt;br /&gt;
	// configure Timer 2 using internal clock with given prescalar&lt;br /&gt;
  OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_256, SYS_FREQ/INT_FREQ/PRE_SCALAR - 1);&lt;br /&gt;
&lt;br /&gt;
  // set up the timer interrupt with a priority of 2&lt;br /&gt;
  ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2);&lt;br /&gt;
&lt;br /&gt;
  // enable multi-vector interrupts&lt;br /&gt;
  INTEnableSystemMultiVectoredInt();&lt;br /&gt;
&lt;br /&gt;
	while(1) {		// infinite loop&lt;br /&gt;
	}&lt;br /&gt;
	return 0;&lt;br /&gt;
} // end main&lt;br /&gt;
&lt;br /&gt;
/* initLEDs()&lt;br /&gt;
 *&lt;br /&gt;
 * initLEDs sets RG12 and RG13 to outputs to control the LEDs on the NU32v2.&lt;br /&gt;
 */&lt;br /&gt;
void initLEDs(void) {&lt;br /&gt;
	LATG |= 0x3000;		// Initializes G12 and G13 to 1 (off for NU32v2 board)&lt;br /&gt;
	TRISG &amp;amp;= 0xCFFF;	// set the two pins to inputs&lt;br /&gt;
						&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
  LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From this example, we can see how to set up timer 2 at a frequency of 5 Hz with an interrupt priority of 2.&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19704</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19704"/>
		<updated>2011-01-23T23:02:08Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Time-based Interrupts */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescalar (1:n), the timer increments by 1 every n pulses from the clock. The timer is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the timer interrupt is generated when the timer is reset. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the timer interrupt is given by &lt;br /&gt;
&lt;br /&gt;
Period = [(PRx + 1) Tpb (TMR_Prescalar_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
This means that if we want to specify the frequency of the interrupt, we need to determine PRx. Note that PRx needs to be less that 2^16 - 1 if using only 1 timer or less than 2^32 - 1 if combining two timers. See example below.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
For each interrupt, we need to write an interrupt service routine (ISR), which basically means when an interrupt is generated jump to and execute a given set of code before returning. The standard way to write an ISR is the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void __ISR(vector, iplx) yHandler(void){&lt;br /&gt;
&lt;br /&gt;
// code to execute&lt;br /&gt;
&lt;br /&gt;
// clear the interrupt flag&lt;br /&gt;
   function to clear interrupt flag&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
where vector refers to a constant defined in &amp;#039;&amp;#039;&amp;#039;(find this location)&amp;#039;&amp;#039;&amp;#039;, iplx is the interrupt priority level where x is the level. yHandler can be anything you want, for example we can call the timer2 handler as Timer2Handler. There is a function associated with each interrupt that is used to clear the flag, which allows the program to return to where it was before the interrupt was generated. Without clearing the flag, the program will never exit the interrupt. &lt;br /&gt;
&lt;br /&gt;
This example is going to toggle 2 LEDs at a given frequency using interrupts with timer 2. We are going to use the functions OpenTimer2() and ConfigIntTimer2() to initialize the timer and interrupts. You will see in the example code that there is also a function INTEnableSystemMultiVectoredInt() which is used to set up multi-vectored interrupts &amp;#039;&amp;#039;&amp;#039;(whatever that means)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/* Timer_Simple.c&lt;br /&gt;
 *&lt;br /&gt;
 * This example toggles 2 LEDs on and off using a timer interrupt. &lt;br /&gt;
 * &lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
					&lt;br /&gt;
#define SYS_FREQ  	(80000000L)		// clock frequency set by bootloader&lt;br /&gt;
#define LED_0		LATGbits.LATG12 // mnemonic constants for LEDs on NU32v2&lt;br /&gt;
#define LED_1		LATGbits.LATG13&lt;br /&gt;
&lt;br /&gt;
// Define constants for the interrupt frequency (Hz) and the prescalar&lt;br /&gt;
// Note that (SYS_FREQ / INT_FREQ / PRE_SCALAR - 1) needs to be less than 2^16-1&lt;br /&gt;
#define INT_FREQ	5&lt;br /&gt;
#define PRE_SCALAR	256		&lt;br /&gt;
&lt;br /&gt;
void initLEDs(void);&lt;br /&gt;
&lt;br /&gt;
//int count = 0;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
	// SYSTEMConfig() optimizes performance for our clock frequency.&lt;br /&gt;
	// in Microchip\MPLAB C32\pic32-libs\include\peripheral\system.h&lt;br /&gt;
	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);&lt;br /&gt;
    &lt;br /&gt;
	initLEDs(); 	// set LED pins as outputs&lt;br /&gt;
&lt;br /&gt;
	LED_0 = 1; // low output turns an LED on; see NU32v2 circuit&lt;br /&gt;
	LED_1 = 0; // high output turns an LED off&lt;br /&gt;
	&lt;br /&gt;
	// configure Timer 2 using internal clock with given prescalar&lt;br /&gt;
  OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_256, SYS_FREQ/INT_FREQ/PRE_SCALAR - 1);&lt;br /&gt;
&lt;br /&gt;
  // set up the timer interrupt with a priority of 2&lt;br /&gt;
  ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2);&lt;br /&gt;
&lt;br /&gt;
  // enable multi-vector interrupts&lt;br /&gt;
  INTEnableSystemMultiVectoredInt();&lt;br /&gt;
&lt;br /&gt;
	while(1) {		// infinite loop&lt;br /&gt;
	}&lt;br /&gt;
	return 0;&lt;br /&gt;
} // end main&lt;br /&gt;
&lt;br /&gt;
/* initLEDs()&lt;br /&gt;
 *&lt;br /&gt;
 * initLEDs sets RG12 and RG13 to outputs to control the LEDs on the NU32v2.&lt;br /&gt;
 */&lt;br /&gt;
void initLEDs(void) {&lt;br /&gt;
	LATG |= 0x3000;		// Initializes G12 and G13 to 1 (off for NU32v2 board)&lt;br /&gt;
	TRISG &amp;amp;= 0xCFFF;	// set the two pins to inputs&lt;br /&gt;
						&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* ISR for Timer 2*/&lt;br /&gt;
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void){&lt;br /&gt;
    &lt;br /&gt;
  LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
	LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
&lt;br /&gt;
  // clear the interrupt flag&lt;br /&gt;
  mT2ClearIntFlag();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From this example, we can see how to set up timer 2 at a frequency of 5 Hz with an interrupt priority of 2.&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19703</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19703"/>
		<updated>2011-01-23T20:53:34Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Time-based Interrupts */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescalar (1:n), the timer increments by 1 every n pulses from the clock. The timer is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the timer interrupt is generated when the timer is reset. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the timer interrupt is given by &lt;br /&gt;
&lt;br /&gt;
Period = [(PRx + 1) Tpb (TMR_Prescalar_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
This means that if we want to specify the frequency of the interrupt, we need to determine PRx. Note that PRx needs to be less that 2^16 - 1 if using only 1 timer or less than 2^32 - 1 if combining two timers. See example below.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
For each interrupt, we need to write an interrupt service routine (ISR), which basically means when an interrupt is generated jump to and execute a given set of code before returning. The standard way to write an ISR is the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void __ISR(vector, iplx) yHandler(void){&lt;br /&gt;
&lt;br /&gt;
// code to execute&lt;br /&gt;
&lt;br /&gt;
// clear the interrupt flag&lt;br /&gt;
   function to clear interrupt flag&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
where vector refers to a constant defined in &amp;#039;&amp;#039;&amp;#039;(find this location)&amp;#039;&amp;#039;&amp;#039;, iplx is the interrupt priority level where x is the level. yHandler can be anything you want, for example we can call the timer2 handler as Timer2Handler. There is a function associated with each interrupt that is used to clear the flag, which allows the program to return to where it was before the interrupt was generated. Without clearing the flag, the program will never exit the interrupt. &lt;br /&gt;
&lt;br /&gt;
This example is going to toggle 2 LEDs at a given frequency using interrupts with timer 2. We are going to use the functions OpenTimer2() and ConfigIntTimer2() to initialize the timer and interrupts. You will see in the example code that there is also a function INTEnableSystemMultiVectoredInt() which is used to set up multi-vectored interrupts &amp;#039;&amp;#039;&amp;#039;(whatever that means)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19702</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19702"/>
		<updated>2011-01-23T20:52:35Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Sample Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescalar (1:n), the timer increments by 1 every n pulses from the clock. The timer is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the timer interrupt is generated when the timer is reset. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the timer interrupt is given by &lt;br /&gt;
&lt;br /&gt;
Period = [(PRx + 1) Tpb (TMR_Prescalar_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
This means that if we want to specify the frequency of the interrupt, we need to determine PRx. Note that PRx needs to be less that 2^16 - 1 if using only 1 timer or less than 2^32 - 1 if combining two timers. See example below.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
For each interrupt, we need to write an interrupt service routine (ISR), which basically means when an interrupt is generated jump to and execute a given set of code before returning. The standard way to write an ISR is the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void __ISR(vector, iplx) yHandler(void){&lt;br /&gt;
&lt;br /&gt;
// code to execute&lt;br /&gt;
&lt;br /&gt;
// clear the interrupt flag&lt;br /&gt;
   function to clear interrupt flag&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
where vector refers to a constant defined in &amp;#039;&amp;#039;&amp;#039;(find this location)&amp;#039;&amp;#039;&amp;#039;, iplx is the interrupt priority level where x is the level. yHandler can be anything you want, for example we can call the timer2 handler as Timer2Handler. There is a function associated with each interrupt that is used to clear the flag, which allows the program to return to where it was before the interrupt was generated. Without clearing the flag, the program will never exit the interrupt. &lt;br /&gt;
&lt;br /&gt;
This example is going to toggle 2 LEDs at a given frequency using interrupts with timer 2. We are going to use the functions OpenTimer2() and ConfigIntTimer2() to initialize the timer and interrupts. You will see in the example code that there is also a function INTEnableSystemMultiVectoredInt() which is used to set up multi-vectored interrupts (whatever that means).&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19701</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19701"/>
		<updated>2011-01-23T20:48:19Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Time-based Interrupts */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescalar (1:n), the timer increments by 1 every n pulses from the clock. The timer is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the timer interrupt is generated when the timer is reset. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the timer interrupt is given by &lt;br /&gt;
&lt;br /&gt;
Period = [(PRx + 1) Tpb (TMR_Prescalar_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
This means that if we want to specify the frequency of the interrupt, we need to determine PRx. Note that PRx needs to be less that 2^16 - 1 if using only 1 timer or less than 2^32 - 1 if combining two timers. See example below.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
This example is going to toggle 2 LEDs at a given frequency using interrupts with timer 2. We are going to use the functions OpenTimer2() and ConfigIntTimer2() to initialize the timer and interrupts. You will see in the example code that there is also a function INTEnableSystemMultiVectoredInt() which is used to set up multi-vectored interrupts (whatever that means).&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19700</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19700"/>
		<updated>2011-01-23T20:46:26Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Time-based Interrupts */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescalar (1:n), the timer increments by 1 every n pulses from the clock. The timer is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the timer interrupt is generated when the timer is reset. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the timer interrupt is given by &lt;br /&gt;
&lt;br /&gt;
Period = [(PRx + 1) Tpb (TMR_Prescalar_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
This means that if we want to specify the frequency of the interrupt, we need to determine PRx. Note that PRx needs to be less that 2^16 - 1 if using only 1 timer or less than 2^32 - 1 if combining two timers. See example below.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
This example is going to toggle 2 LEDs at a given frequency using interrupts with timer 2. We are going to use the functions OpenTimer2() and ConfigIntTimer2() to initialize the timer and interrupts. You will see in the example code that there is also a function INTEnableSystemMultiVectoredInt() which is used to set up multi-vectored interrupts (whatever that means). &lt;br /&gt;
&lt;br /&gt;
For each interrupt, we need to write an interrupt service routine (ISR), which basically means when an interrupt is generated jump to and execute a given set of code before returning. The standard way to write an ISR is the following&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void __ISR(vector, iplx) yHandler(void){&lt;br /&gt;
&lt;br /&gt;
// code to execute&lt;br /&gt;
&lt;br /&gt;
// clear the interrupt flag&lt;br /&gt;
   function to clear interrupt flag&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19699</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19699"/>
		<updated>2011-01-23T20:30:16Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Details */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescalar (1:n), the timer increments by 1 every n pulses from the clock. The timer is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the timer interrupt is generated when the timer is reset. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the timer interrupt is given by &lt;br /&gt;
&lt;br /&gt;
Period = [(PRx + 1) Tpb (TMR_Prescalar_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
This means that if we want to specify the frequency of the interrupt, we need to determine PRx. Note that PRx needs to be less that 2^16 - 1 if using only 1 timer or less than 2^32 - 1 if combining two timers. See example below.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19698</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19698"/>
		<updated>2011-01-23T20:28:40Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Details */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescalar (1:n), the timer increments by 1 every n pulses from the clock. The timer is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the timer interrupt is generated when the timer is reset. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the timer interrupt is given by &lt;br /&gt;
&lt;br /&gt;
Period = [(PRx + 1) Tpb (TMR_Prescalar_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19697</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19697"/>
		<updated>2011-01-23T20:28:26Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Details */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
Given a clock source (internal or external) and a prescalar (1:n), the timer increments by 1 every n pulses from the clock. The timer is reset to zero when the timer value equals the PRx value. If interrupts are enabled, the timer interrupt is generated when the timer is reset. This means that PRx determines the frequency of the interrupts. If the clock source is internal (peripheral bus), the period of the timer interrupt is given by &lt;br /&gt;
Period = [(PRx + 1) Tpb (TMR_Prescalar_value)],&lt;br /&gt;
where Tpb is the period of the peripheral bus.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19696</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19696"/>
		<updated>2011-01-23T20:21:46Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Sample Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
=== Accurate Timing ===&lt;br /&gt;
&lt;br /&gt;
=== Time-based Interrupts ===&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19695</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19695"/>
		<updated>2011-01-23T20:21:00Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Library Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X (ie replace X with the timer number you wish to use) has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19694</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19694"/>
		<updated>2011-01-23T20:19:55Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Library Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
The peripheral library offers a number of function calls to simplify setting up and using the Timers. The declarations of these functions can be found in pic32-libs/include/peripheral/timer.h&lt;br /&gt;
&lt;br /&gt;
Each Timer X has the following functions:&lt;br /&gt;
*OpenTimerX(config, period): where config is a value selected by using | with the constants given in the header file. period is the desired PRx value which determines the roll-over // interrupt frequency. &lt;br /&gt;
*CloseTimerX(): this disables the timer&lt;br /&gt;
*ConfigIntTimerX(config): this function configures the interrupt based on config which is a value selected by using | with the constants given in the header file. &lt;br /&gt;
*EnableIntTX(): enables the interrupt&lt;br /&gt;
*DisableIntTX(): disables the interrupt&lt;br /&gt;
*SetPriorityIntX(priority): sets the priority of the interrupt&lt;br /&gt;
*ReadTimerX(): returns the value of the timer&lt;br /&gt;
*WriteTimerX(value): loads a given value to the timer&lt;br /&gt;
*ReadPeriodX(): returns the PRx value&lt;br /&gt;
*WritePeriodX(value): loads a given value to the PRx register&lt;br /&gt;
&lt;br /&gt;
Timers 2 and 3 as well as 4 and 5 can be combined to create 32-bit timers. If using a 32-bit timer, replace the X in the functions above with 23 or 45.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19693</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19693"/>
		<updated>2011-01-23T20:11:08Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Details */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;: 16-bit value to reset TMRx. &lt;br /&gt;
&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the priority of the interrupt associated with the timer. &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;: This SFR sets up the subpriority of the interrupt.&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19692</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19692"/>
		<updated>2011-01-23T19:54:04Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Details */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed: &lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxCON&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TMRx&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;PRx&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Timer interrupts are controlled with the following SFRs:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIE&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIF&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIP&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;TxIS&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19691</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19691"/>
		<updated>2011-01-23T19:50:38Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039;: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039;: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19690</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19690"/>
		<updated>2011-01-23T19:50:07Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events. &lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal. &lt;br /&gt;
&lt;br /&gt;
In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled &amp;#039;TXCK&amp;#039; that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value. &lt;br /&gt;
&lt;br /&gt;
Each timer can be configured to generate interrupts at a given priority.&lt;br /&gt;
&lt;br /&gt;
The PIC32 has two types of timers:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer A&amp;#039;&amp;#039;&amp;#039; (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Timer B&amp;#039;&amp;#039;&amp;#039; (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers. &lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19689</id>
		<title>NU32v2: Counters and Timers</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Counters_and_Timers&amp;diff=19689"/>
		<updated>2011-01-23T19:25:21Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;NU32v2 timers&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&lt;br /&gt;
== Library Functions ==&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Starting_a_New_Project_and_Putting_it_on_the_NU32v2&amp;diff=19460</id>
		<title>NU32v2: Starting a New Project and Putting it on the NU32v2</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Starting_a_New_Project_and_Putting_it_on_the_NU32v2&amp;diff=19460"/>
		<updated>2011-01-15T21:46:07Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Return to [[Microchip PICs#Using the NU32v2|Using the NU32v2]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;**THIS PAGE IS UNDER CONSTRUCTION AND IS NOT COMPLETE**&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;**AWL 1/15/2011**&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= PIC32 Programming in C =&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Note:  Code downloaded from Microchip is constantly evolving, and it is possible that the information below will be outdated for future code releases.  This information is accurate for code downloaded from Microchip&amp;#039;s website in January 2011.  Also, sample code developed by us and others are generally modified from working code for different purposes, and therefore you may find unnecessary legacy code, programming inefficiency, or even incorrect code in some samples.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
You should complete the instructions in [[NU32v2: Software to Install]] if you have not already.  For this page specifically, you need to&lt;br /&gt;
&lt;br /&gt;
* download and install the MPLAB IDE (Integrated Development Environment) that you use for programming, compiling, debugging, and simulating your PIC code. The MPLAB C Compiler for PIC32 is included in the &amp;quot;complete&amp;quot; installation of the IDE&lt;br /&gt;
* download the Serial Bootloader for NU32v2 PC application&lt;br /&gt;
* install drivers for FTDI&lt;br /&gt;
&lt;br /&gt;
The remainder of this page describes how to create a new Project in MPLAB, compile a general &amp;quot;Hello World&amp;quot; program to blink the LEDs on the NU32v2, and use the Serial Bootloader of NU32v2 PC application to put the program on the NU32v2.&lt;br /&gt;
&lt;br /&gt;
= Create a New Project in MPLAB for the NU32v2 =&lt;br /&gt;
&lt;br /&gt;
For organizational reasons, it is recommended to have a folder on your computer to store your PIC projects. If you do not already have a space to save your projects. Create a new folder and call it PIC_Projects. Each project should have its own folder inside PIC_Projects.&lt;br /&gt;
&lt;br /&gt;
* Create a folder in PIC_Projects and call it HelloWorld&lt;br /&gt;
* Download this procdefs.ld file and place it in the new HelloWorld folder. &lt;br /&gt;
&lt;br /&gt;
When creating new projects to download to the PIC, you must include a linker file. If you have installed a bootloader (such as with the NU32v2), the linker file tells the bootloader to put the program after the bootloader memory to prevent erasing of the bootloader. The required linker file is called procdefs.ld. You will always need to add this procdefs file to your project folders in order to properly install your programs on the PIC. If you do not use this procdefs file, you potentially could overwrite the bootloader.&lt;br /&gt;
&lt;br /&gt;
* Open MPLAB&lt;br /&gt;
* Choose Project -&amp;gt; Project Wizard to create a new project.&lt;br /&gt;
* Click Next. &lt;br /&gt;
* Select the device you are using. The NU32v2 is using the PIC32MX795F512L. &lt;br /&gt;
* Choose the Microchip PIC32 C-Compiler Toolsuite from the drop down menu for Active Toolsuite. This sets up the compiler that will be used for creating the hex code that will be placed on the PIC. Click Next &lt;br /&gt;
* Create a new project called HelloWorld in the folder called HelloWorld (click browse to navigate to that folder). Click Next. &lt;br /&gt;
* Highlight the PROCDEFS.ld file under the HelloWorld folder and click Add. This adds the file to your project. For future projects, if you have existing .c and .h code, this is the step to add these files to your project. For this tutorial, we will be creating our own file, so no additional files need to be added. &lt;br /&gt;
* On this last step, it displays a summary of the previous steps. Click finish.&lt;br /&gt;
&lt;br /&gt;
You have now created a project. The project window should appear as below.&lt;br /&gt;
[[Image:PIC32_ProjectWindow.jpg|thumb|300 px|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The procdefs.ld is not the only linker file. &lt;br /&gt;
* In the project window, drag procdefs.ld from the &amp;#039;Linker Script&amp;#039; folder to the &amp;#039;Other Files&amp;#039; folder.&lt;br /&gt;
[[Image:PIC32_ProjectWindow2.jpg|thumb|300 px|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we are going to create a .c file to add to this project. &lt;br /&gt;
* Choose File -&amp;gt; New.&lt;br /&gt;
* Choose File -&amp;gt; Save As and call the new file HelloWorld.c in the HelloWorld folder. This will create a source file for your code. You need to include &amp;#039;.c&amp;#039; on the file name, otherwise MPLAB does not know that it is a source file. &lt;br /&gt;
* Right click on the &amp;#039;Source Files&amp;#039; folder and choose &amp;#039;Add files&amp;#039;. Choose the HelloWorld.c file to add it to the project. &lt;br /&gt;
[[Image:PIC32_AddFiles1.jpg|thumb|300 px|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You have now created a project and added a blank source file to your project. You are now ready to code your first program.&lt;br /&gt;
&lt;br /&gt;
= HelloWorld Code =&lt;br /&gt;
&lt;br /&gt;
This program will toggle on and off the two LEDS on the NU32v2 board.&lt;br /&gt;
&lt;br /&gt;
You are now ready to edit your new HelloWorld.c file.&lt;br /&gt;
&lt;br /&gt;
* Cut and paste the code below into your HelloWorld.c file.&lt;br /&gt;
&lt;br /&gt;
 /* HelloWorld.c&lt;br /&gt;
  *&lt;br /&gt;
  * Our first PIC program, which simply flashes LEDs on the NU32v2&lt;br /&gt;
  * board.  We have already installed a &amp;quot;bootloader&amp;quot; on our PIC32,&lt;br /&gt;
  * which specifies &amp;quot;configuration bits&amp;quot; that set the clock frequency&lt;br /&gt;
  * to 80 MHz (assuming the 8 MHz crystal on the NU32v2 board), &lt;br /&gt;
  * &lt;br /&gt;
  *&lt;br /&gt;
  * HISTORY&lt;br /&gt;
  * 1/15/2011	Created by Nick Marchuk, Andrew Long and Kevin Lynch&lt;br /&gt;
  *&lt;br /&gt;
  */&lt;br /&gt;
 &lt;br /&gt;
 // plib.h gives access to useful constant and function definitions, found in &lt;br /&gt;
 // Program Files\Microchip\MPLAB C32\pic32-libs\include&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 					&lt;br /&gt;
 #define SYS_FREQ  	(80000000L)		// clock frequency set by  bootloader&lt;br /&gt;
 #define LED_0		LATGbits.LATG12 // mnemonic constants for LEDs on NU32v2&lt;br /&gt;
 #define LED_1		LATGbits.LATG13&lt;br /&gt;
 #define WAIT_LOOPS	2000000			// number less than than 2^32 - 1, or about 4 billion&lt;br /&gt;
 &lt;br /&gt;
 void initLEDs(void);&lt;br /&gt;
 void delay(unsigned int loops);&lt;br /&gt;
 &lt;br /&gt;
 int main(void) {&lt;br /&gt;
 	int i=0;	// loop counter&lt;br /&gt;
 &lt;br /&gt;
 	// SYSTEMConfig() optimizes performance for our clock frequency.&lt;br /&gt;
 	// in Microchip\MPLAB C32\pic32-libs\include\peripheral\system.h&lt;br /&gt;
 	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);&lt;br /&gt;
      &lt;br /&gt;
 	initLEDs(); 	// set LED pins as outputs&lt;br /&gt;
 &lt;br /&gt;
 	LED_0 = 1; // low output turns an LED on; see NU32v2 circuit&lt;br /&gt;
 	LED_1 = 0; // high output turns an LED off&lt;br /&gt;
 	&lt;br /&gt;
 	while(1) {		// infinite loop&lt;br /&gt;
 			&lt;br /&gt;
 		LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
 		LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
 &lt;br /&gt;
 		delay(WAIT_LOOPS);&lt;br /&gt;
 	}&lt;br /&gt;
 	return 0;&lt;br /&gt;
 } // end main&lt;br /&gt;
 &lt;br /&gt;
 /* initLEDs()&lt;br /&gt;
  *&lt;br /&gt;
  * initLEDs sets RG12 and RG13 to outputs to control the LEDs on the NU32v2.&lt;br /&gt;
  */&lt;br /&gt;
 void initLEDs(void) {&lt;br /&gt;
 					// Read about setting digital inputs / outputs on the&lt;br /&gt;
 					// Digital input-output example	&lt;br /&gt;
 	LATG |= 0x3000;		// Initializes G12 and G13 to 1 (off for NU32v2 board)&lt;br /&gt;
 	TRISG &amp;amp;= 0xCFFF;	// set the two pins to inputs&lt;br /&gt;
 						&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 /* delay(unsigned int loops)&lt;br /&gt;
  *&lt;br /&gt;
  * This function just wastes time.  Note; it just uses an int number of loops,  &lt;br /&gt;
  * which should be less than 2^32 - 1, or about 4 billion.  If you really want&lt;br /&gt;
  * to waste a lot of time, you could use a long long.&lt;br /&gt;
  */&lt;br /&gt;
 void delay(unsigned int loops) {&lt;br /&gt;
 	int i = loops;&lt;br /&gt;
 	while(i--) {}&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To load this program to the PIC with the bootloader, we need a hex file. MPLAB creates a hex file in the project folder when you compile the program.&lt;br /&gt;
* Choose Project -&amp;gt; Build all OR F10 on PCs. &lt;br /&gt;
The output window should say &amp;#039;Build Succeeded&amp;#039;. If it doesn&amp;#039;t, fix the errors.&lt;br /&gt;
&lt;br /&gt;
= Using the Serial Bootloader for NU32v2 PC Application =&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Under Construction&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
= PIC32 Programming in C with the MPLAB X Beta IDE =&lt;br /&gt;
&lt;br /&gt;
Open &amp;#039;MPLAB X IDE beta&amp;#039;. You will see the following screen.&lt;br /&gt;
&lt;br /&gt;
Click Create New Project.&lt;br /&gt;
&lt;br /&gt;
In Categories, select the Microchip Embedded folder. In Projects, select C/ASM Standalone Project. Click Next &amp;gt;.&lt;br /&gt;
&lt;br /&gt;
In Family, select PIC32, and in Device, select PIC32MX795F512L.&lt;br /&gt;
&lt;br /&gt;
In Select Tool, select PICkit3 if you are going to program the code onto your NU32v2, or select Simulator if you are going to practice coding on your computer.&lt;br /&gt;
&lt;br /&gt;
In Select Compiler, select C32.&lt;br /&gt;
&lt;br /&gt;
In Select Project Name and Folder, create a name for your project, select a location to save it to, ab&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:PIC32_AddFiles1.jpg&amp;diff=19459</id>
		<title>File:PIC32 AddFiles1.jpg</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:PIC32_AddFiles1.jpg&amp;diff=19459"/>
		<updated>2011-01-15T21:45:25Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Starting_a_New_Project_and_Putting_it_on_the_NU32v2&amp;diff=19458</id>
		<title>NU32v2: Starting a New Project and Putting it on the NU32v2</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=NU32v2:_Starting_a_New_Project_and_Putting_it_on_the_NU32v2&amp;diff=19458"/>
		<updated>2011-01-15T21:45:15Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: /* Create a New Project in MPLAB for the NU32v2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Return to [[Microchip PICs#Using the NU32v2|Using the NU32v2]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;**THIS PAGE IS UNDER CONSTRUCTION AND IS NOT COMPLETE**&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;**AWL 1/15/2011**&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= PIC32 Programming in C =&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Note:  Code downloaded from Microchip is constantly evolving, and it is possible that the information below will be outdated for future code releases.  This information is accurate for code downloaded from Microchip&amp;#039;s website in January 2011.  Also, sample code developed by us and others are generally modified from working code for different purposes, and therefore you may find unnecessary legacy code, programming inefficiency, or even incorrect code in some samples.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
You should complete the instructions in [[NU32v2: Software to Install]] if you have not already.  For this page specifically, you need to&lt;br /&gt;
&lt;br /&gt;
* download and install the MPLAB IDE (Integrated Development Environment) that you use for programming, compiling, debugging, and simulating your PIC code. The MPLAB C Compiler for PIC32 is included in the &amp;quot;complete&amp;quot; installation of the IDE&lt;br /&gt;
* download the Serial Bootloader for NU32v2 PC application&lt;br /&gt;
* install drivers for FTDI&lt;br /&gt;
&lt;br /&gt;
The remainder of this page describes how to create a new Project in MPLAB, compile a general &amp;quot;Hello World&amp;quot; program to blink the LEDs on the NU32v2, and use the Serial Bootloader of NU32v2 PC application to put the program on the NU32v2.&lt;br /&gt;
&lt;br /&gt;
= Create a New Project in MPLAB for the NU32v2 =&lt;br /&gt;
&lt;br /&gt;
For organizational reasons, it is recommended to have a folder on your computer to store your PIC projects. If you do not already have a space to save your projects. Create a new folder and call it PIC_Projects. Each project should have its own folder inside PIC_Projects.&lt;br /&gt;
&lt;br /&gt;
* Create a folder in PIC_Projects and call it HelloWorld&lt;br /&gt;
* Download this procdefs.ld file and place it in the new HelloWorld folder. &lt;br /&gt;
&lt;br /&gt;
When creating new projects to download to the PIC, you must include a linker file. If you have installed a bootloader (such as with the NU32v2), the linker file tells the bootloader to put the program after the bootloader memory to prevent erasing of the bootloader. The required linker file is called procdefs.ld. You will always need to add this procdefs file to your project folders in order to properly install your programs on the PIC. If you do not use this procdefs file, you potentially could overwrite the bootloader.&lt;br /&gt;
&lt;br /&gt;
* Open MPLAB&lt;br /&gt;
* Choose Project -&amp;gt; Project Wizard to create a new project.&lt;br /&gt;
* Click Next. &lt;br /&gt;
* Select the device you are using. The NU32v2 is using the PIC32MX795F512L. &lt;br /&gt;
* Choose the Microchip PIC32 C-Compiler Toolsuite from the drop down menu for Active Toolsuite. This sets up the compiler that will be used for creating the hex code that will be placed on the PIC. Click Next &lt;br /&gt;
* Create a new project called HelloWorld in the folder called HelloWorld (click browse to navigate to that folder). Click Next. &lt;br /&gt;
* Highlight the PROCDEFS.ld file under the HelloWorld folder and click Add. This adds the file to your project. For future projects, if you have existing .c and .h code, this is the step to add these files to your project. For this tutorial, we will be creating our own file, so no additional files need to be added. &lt;br /&gt;
* On this last step, it displays a summary of the previous steps. Click finish.&lt;br /&gt;
&lt;br /&gt;
You have now created a project. The project window should appear as below.&lt;br /&gt;
[[Image:PIC32_ProjectWindow.jpg|thumb|300 px|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The procdefs.ld is not the only linker file. &lt;br /&gt;
* In the project window, drag procdefs.ld from the &amp;#039;Linker Script&amp;#039; folder to the &amp;#039;Other Files&amp;#039; folder.&lt;br /&gt;
[[Image:PIC32_ProjectWindow2.jpg|thumb|300 px|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we are going to create a .c file to add to this project. &lt;br /&gt;
* Choose File -&amp;gt; New.&lt;br /&gt;
* Choose File -&amp;gt; Save As and call the new file HelloWorld.c in the HelloWorld folder. This will create a source file for your code. You need to include &amp;#039;.c&amp;#039; on the file name, otherwise MPLAB does not know that it is a source file. &lt;br /&gt;
* Right click on the &amp;#039;Source Files&amp;#039; folder and choose &amp;#039;Add files&amp;#039;. Choose the HelloWorld.c file to add it to the project. &lt;br /&gt;
[[Image:PIC32_AddFiles1.jpg|thumb|300 px|left]]&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You have now created a project and added a blank source file to your project. You are now ready to code your first program.&lt;br /&gt;
&lt;br /&gt;
= HelloWorld Code =&lt;br /&gt;
&lt;br /&gt;
This program will toggle on and off the two LEDS on the NU32v2 board.&lt;br /&gt;
&lt;br /&gt;
You are now ready to edit your new HelloWorld.c file.&lt;br /&gt;
&lt;br /&gt;
* Cut and paste the code below into your HelloWorld.c file.&lt;br /&gt;
&lt;br /&gt;
 /* HelloWorld.c&lt;br /&gt;
  *&lt;br /&gt;
  * Our first PIC program, which simply flashes LEDs on the NU32v2&lt;br /&gt;
  * board.  We have already installed a &amp;quot;bootloader&amp;quot; on our PIC32,&lt;br /&gt;
  * which specifies &amp;quot;configuration bits&amp;quot; that set the clock frequency&lt;br /&gt;
  * to 80 MHz (assuming the 8 MHz crystal on the NU32v2 board), &lt;br /&gt;
  * &lt;br /&gt;
  *&lt;br /&gt;
  * HISTORY&lt;br /&gt;
  * 1/15/2011	Created by Nick Marchuk, Andrew Long and Kevin Lynch&lt;br /&gt;
  *&lt;br /&gt;
  */&lt;br /&gt;
 &lt;br /&gt;
 // plib.h gives access to useful constant and function definitions, found in &lt;br /&gt;
 // Program Files\Microchip\MPLAB C32\pic32-libs\include&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 					&lt;br /&gt;
 #define SYS_FREQ  	(80000000L)		// clock frequency set by  bootloader&lt;br /&gt;
 #define LED_0		LATGbits.LATG12 // mnemonic constants for LEDs on NU32v2&lt;br /&gt;
 #define LED_1		LATGbits.LATG13&lt;br /&gt;
 #define WAIT_LOOPS	2000000			// number less than than 2^32 - 1, or about 4 billion&lt;br /&gt;
 &lt;br /&gt;
 void initLEDs(void);&lt;br /&gt;
 void delay(unsigned int loops);&lt;br /&gt;
 &lt;br /&gt;
 int main(void) {&lt;br /&gt;
 	int i=0;	// loop counter&lt;br /&gt;
 &lt;br /&gt;
 	// SYSTEMConfig() optimizes performance for our clock frequency.&lt;br /&gt;
 	// in Microchip\MPLAB C32\pic32-libs\include\peripheral\system.h&lt;br /&gt;
 	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);&lt;br /&gt;
      &lt;br /&gt;
 	initLEDs(); 	// set LED pins as outputs&lt;br /&gt;
 &lt;br /&gt;
 	LED_0 = 1; // low output turns an LED on; see NU32v2 circuit&lt;br /&gt;
 	LED_1 = 0; // high output turns an LED off&lt;br /&gt;
 	&lt;br /&gt;
 	while(1) {		// infinite loop&lt;br /&gt;
 			&lt;br /&gt;
 		LED_0 = !LED_0; // toggle LED_0&lt;br /&gt;
 		LED_1 = !LED_1;	// toggle LED_1&lt;br /&gt;
 &lt;br /&gt;
 		delay(WAIT_LOOPS);&lt;br /&gt;
 	}&lt;br /&gt;
 	return 0;&lt;br /&gt;
 } // end main&lt;br /&gt;
 &lt;br /&gt;
 /* initLEDs()&lt;br /&gt;
  *&lt;br /&gt;
  * initLEDs sets RG12 and RG13 to outputs to control the LEDs on the NU32v2.&lt;br /&gt;
  */&lt;br /&gt;
 void initLEDs(void) {&lt;br /&gt;
 					// Read about setting digital inputs / outputs on the&lt;br /&gt;
 					// Digital input-output example	&lt;br /&gt;
 	LATG |= 0x3000;		// Initializes G12 and G13 to 1 (off for NU32v2 board)&lt;br /&gt;
 	TRISG &amp;amp;= 0xCFFF;	// set the two pins to inputs&lt;br /&gt;
 						&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 /* delay(unsigned int loops)&lt;br /&gt;
  *&lt;br /&gt;
  * This function just wastes time.  Note; it just uses an int number of loops,  &lt;br /&gt;
  * which should be less than 2^32 - 1, or about 4 billion.  If you really want&lt;br /&gt;
  * to waste a lot of time, you could use a long long.&lt;br /&gt;
  */&lt;br /&gt;
 void delay(unsigned int loops) {&lt;br /&gt;
 	int i = loops;&lt;br /&gt;
 	while(i--) {}&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To load this program to the PIC with the bootloader, we need a hex file. MPLAB creates a hex file in the project folder when you compile the program.&lt;br /&gt;
* Choose Project -&amp;gt; Build all OR F10 on PCs. &lt;br /&gt;
The output window should say &amp;#039;Build Succeeded&amp;#039;. If it doesn&amp;#039;t, fix the errors.&lt;br /&gt;
&lt;br /&gt;
= Compiling the &amp;quot;Hello World&amp;quot; example code for NU32v2 =&lt;br /&gt;
&lt;br /&gt;
= Using the Serial Bootloader for NU32v2 PC Application =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= PIC32 Programming in C with the MPLAB X Beta IDE =&lt;br /&gt;
&lt;br /&gt;
Open &amp;#039;MPLAB X IDE beta&amp;#039;. You will see the following screen.&lt;br /&gt;
&lt;br /&gt;
Click Create New Project.&lt;br /&gt;
&lt;br /&gt;
In Categories, select the Microchip Embedded folder. In Projects, select C/ASM Standalone Project. Click Next &amp;gt;.&lt;br /&gt;
&lt;br /&gt;
In Family, select PIC32, and in Device, select PIC32MX795F512L.&lt;br /&gt;
&lt;br /&gt;
In Select Tool, select PICkit3 if you are going to program the code onto your NU32v2, or select Simulator if you are going to practice coding on your computer.&lt;br /&gt;
&lt;br /&gt;
In Select Compiler, select C32.&lt;br /&gt;
&lt;br /&gt;
In Select Project Name and Folder, create a name for your project, select a location to save it to, ab&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:PIC32_AddFiles.jpg&amp;diff=19457</id>
		<title>File:PIC32 AddFiles.jpg</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:PIC32_AddFiles.jpg&amp;diff=19457"/>
		<updated>2011-01-15T21:44:54Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: uploaded a new version of &amp;quot;Image:PIC32 AddFiles.jpg&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:PIC32_AddFiles.jpg&amp;diff=19456</id>
		<title>File:PIC32 AddFiles.jpg</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:PIC32_AddFiles.jpg&amp;diff=19456"/>
		<updated>2011-01-15T21:44:36Z</updated>

		<summary type="html">&lt;p&gt;Andrew Long: uploaded a new version of &amp;quot;Image:PIC32 AddFiles.jpg&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Andrew Long</name></author>
	</entry>
</feed>