<?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=JamesRein</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=JamesRein"/>
	<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php/Special:Contributions/JamesRein"/>
	<updated>2026-04-11T06:44:06Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.9</generator>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Music_from_the_Heart_--_Music_Suit&amp;diff=17799</id>
		<title>Music from the Heart -- Music Suit</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Music_from_the_Heart_--_Music_Suit&amp;diff=17799"/>
		<updated>2010-03-18T20:27:11Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
&lt;br /&gt;
This project attempted to create a natural form of musical expression by connecting sensors to the body. Six tilt switches were attached to the wrist, ankles, and shoulders, each controlling a single pitch from the [http://en.wikipedia.org/wiki/Pentatonic_scale pentatonic scale]. The heart beat was obtained using [http://en.wikipedia.org/wiki/Photoplethysmograph photoplethysmography] on the user&amp;#039;s finger, and this signal was used to strike a drum in sync with the user&amp;#039;s heart beat.&lt;br /&gt;
&lt;br /&gt;
For a video demonstration, click [http://www.youtube.com/watch?v=YyipByy7m6I here].&lt;br /&gt;
&lt;br /&gt;
  {{multiple image&lt;br /&gt;
   | width1    = 150&lt;br /&gt;
   | width2    = 269&lt;br /&gt;
   | align     = center&lt;br /&gt;
   | header    = Music from the Heart Project&lt;br /&gt;
   | image1    = Music from the heart overview.jpg&lt;br /&gt;
   | caption1  = The full &amp;quot;music suit&amp;quot; on James, with heart rate sensor on his finger. &lt;br /&gt;
   | image2    = Drum overview mfth.jpg&lt;br /&gt;
   | caption2  = A closer view of the drum mechanism&lt;br /&gt;
  }}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
[[Image:Mfth team picture.JPG|thumb|200px|From left: Thomas, Eric, James]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Thomas Peterson (Computer Engineering, 2010)&lt;br /&gt;
*James Rein (Biomedical Engineering and Music Cognition, 2010)&lt;br /&gt;
*Eric West (Mechanical Engineering, 2011)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Subsystems=&lt;br /&gt;
&lt;br /&gt;
Although intended as a single, cohesive system that would allow the user to intuitively make music, the project was easily divided into three subsystems: heart rate monitor, drum actuation, and music tones.  Below is more explanation about each subsystem. &lt;br /&gt;
&lt;br /&gt;
==Heart rate monitor - Concepts==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In brainstorming how to translate a heart beat into a drum beat, we decided that the least intrusive method would be the best.  Attaching electrodes to the user for [http://en.wikipedia.org/wiki/Electrocardiography ECG] would simplify the process of identifying a heart beat, but attaching these sensors directly to the skin would be a time-consuming and personally-invasive process.  Instead, we decided a finger-tip sensor would be much more comfortable and easy to use.  &lt;br /&gt;
&lt;br /&gt;
To make a finger tip sensor, we used the concepts of [http://en.wikipedia.org/wiki/Photoplethysmograph photoplethysmography].  Photoplethysmography is typically used in [http://en.wikipedia.org/wiki/Pulse_oximeter pulse oximeters] and finger-tip sensors in commercially-available devices.  We originally tried to hack a heart rate monitor that we had purchased, but this proved difficult, so we decided to make our own.  &lt;br /&gt;
&lt;br /&gt;
The basic concept of photoplethysmography is that blood reflects a certain amount of IR light, and the blood density in the finger changes as the heart pumps, so the IR reflectivity of the finger changes as the heart beats.  Using an IR emitter-detector pair found in the mechatronics lab  followed by amplification and filtering, we were able to obtain a decent signal with peaks when the user&amp;#039;s heart beat.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Heart rate monitor - Electrical Design===&lt;br /&gt;
&lt;br /&gt;
The signal coming directly out of the IR detector was very weak and noisy.  To obtain just the information we desired, we ran it through the following filters and amplifiers: &lt;br /&gt;
&lt;br /&gt;
# [[Passive Filters#High-Pass Filter (HPF)|Passive High-Pass Filter]]&lt;br /&gt;
# [[Operational Amplifiers (Op-Amps)#Inverting Amplifier|Inverting Amplifier]]: gain = -470&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Low-pass_filter#Active_electronic_realization Active Low-pass filter]: cutoff frequency = 4.82Hz, gain = -70&lt;br /&gt;
# [http://www.electronics-tutorials.ws/filter/filter_7.html Band-pass filter]: Frequency range = 0.72 to 3.39Hz, gain = -46.8&lt;br /&gt;
# [[Comparators|Comparator]]: output to the PIC pin A14, with pull-up resistor to 3.3V and capacitors to smooth out the signal.&lt;br /&gt;
&lt;br /&gt;
The complete signal processing circuit is shown below.  Note the potentiometers, which can be adjusted for some users if the output signal is not clear.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Heartrate monitor.PNG|thumb|center|900px|Heart rate monitor circuit diagram. Click for larger image.]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Heart rate monitor - Mechanical Design===&lt;br /&gt;
&lt;br /&gt;
[[Image:Heart rate sensor mfth.JPG|thumb|200px|Heart rate sensor with the latex band pulled away so that the emitter-detector is revealed]]&lt;br /&gt;
The sensor itself was a QRB1114 emitter-detector pair that was encased in open-cell foam insulation that served as the finger-holder.  We inserted it into the foam by drilling and filing a hole in the foam to the appropriate size, and gluing the sensor in place.  A round, finger-shaped groove was filed into the foam and the sensor sat flush with the bottom of this groove.  The user&amp;#039;s finger was held in the correct position with relatively constant pressure using a latex band around the finger and the foam enclosure.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Drum actuator - Electrical design==&lt;br /&gt;
&lt;br /&gt;
Once the signal from the heart rate sensor is sent to the PIC, the rising edge triggers an interrupt which starts the driving sequence for the motor that strikes the drum.  The drive sequence was: drive down (into drum) 100ms, drive up 75ms, wait 150ms. The interrupt does not do the entire driving sequence, it merely flags a variable to start the sequence, which is done in the main loop, as seen in the code below.&lt;br /&gt;
&lt;br /&gt;
An additional interrupt was triggered by the falling edge of the signal; this falling edge interrupt added additional wait time that prevented the PIC from being &amp;quot;fooled&amp;quot; by a noisy falling edge.  For instance, if the falling signal had some noise in it, the rising edge interrupt would be triggered again, thus causing the drum to strike on both the rising and falling edges of the heartbeat.  We only wanted one strike per heartbeat, so the falling edge interrupt was triggered by the &amp;#039;&amp;#039;first&amp;#039;&amp;#039; falling edge, and any noise in the falling edge was ignored because the PIC was told to wait for 300ms.  With a maximum delay of 150 + 300 = 450ms from the two interrupts, and 100 + 75 = 175ms of drive time, the fastest heartbeat our drum could play was 96 beats per minute, but we did not expect anyone to come play the instrument after running, so this was plenty high for our purposes.  &lt;br /&gt;
&lt;br /&gt;
Initially, we tried skipping the comparator and sending the analog signal directly into one of the PICs analog inputs. Our intention was to use the fast Fourier transform (FFT) library to try to extract the dominant frequency (see [[PIC32MX: FFT of Analog Input]] for more information). Unfortunately, we ran into various problems. First, to get the accuracy we wanted for small frequencies, the sample window had to be very long (10 seconds or more), which made the output lag changes in heart rate. Additionally, the FFT was often &amp;quot;messy&amp;quot;, which messed up our original algorithm of detecting the peak frequency between .5 and 2.5 hz. The system would sometimes randomly switch between very different frequencies, and although it was right most of the time, it was enough to make the drum beating noticeably erratic. We also tried algorithms to find the [http://en.wikipedia.org/wiki/Fundamental_frequency fundamental frequency] using the entire FFT (instead of just the .5-2.5 hz range), but this was still unreliable. We considered FFT peak-detection algorithms, but they seemed too complex for the time and computing power we had available. Because of all this, we went with the simple comparator in the end.&lt;br /&gt;
&lt;br /&gt;
The motor for driving the drumstick was driven by an [[media:L298N.pdf|L298 H-bridge]].  See [[Driving a high current DC Motor using an H-bridge]] for more information on how this works.  Our particular circuit diagram is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:T21_motorckt_png.PNG|thumb|center|600px|Motor Circuit (non-motor PIC connections not shown). Click for larger image.]]&lt;br /&gt;
&lt;br /&gt;
====Drum actuator - Mechanical Design====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Drum stick mount mfth.jpg|thumb|200px|Attachment for the drumstick to the motor shaft. Split-clamp to the motor shaft on the left, drum stick on the right|right]]&lt;br /&gt;
We wanted to use a real drum for our drum beat, so our challenge was to attach a motor and drumstick to a drum.  We chose a drumset small rack tom for its resonant tone that is reminiscent of a heart beat.  &lt;br /&gt;
&lt;br /&gt;
To attach the drumstick to the motor shaft, we used a small block of acrylic (.5&amp;quot;x.75&amp;quot;x2&amp;quot;).  The drumstick was cut down to 8&amp;quot; in length so that it would require less torque from the motor.  In one end of the block, a hole was drilled and the drumstick was sanded to the correct diameter in order to snugly fit into the hole.  To ensure that the drumstick could not slide out of the block, a screw was placed in a hole drilled through both the stick and the block.  The other end of the block was attached to the motor shaft using a split-clamp method: a hole the size of the motor shaft was drilled through the block, and then a slit was cut from the end of the block to the hole, allowing a screw to tightly clamp the block around the motor.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Motor mount mfth.jpg|thumb|200px|Mounting bracket for the motor-stick assembly.|right]]&lt;br /&gt;
To attach the motor-stick assembly to the drum, we utilized the tuning screws on the drum and holes in the face of the motor.  A sheet metal bracket with holes in the appropriate places attached the motor to two of the tuning screws.  The bracket was made by cutting and bending a piece of 16 gauge steel sheet metal to the appropriate size, with a contour fitting the curvature of the drum, and an L-shaped portion to attach the motor above the drum head.  Ideally, this part would have been made out of one continuous piece of sheet metal, but we could not find a large enough sheet available in the shop, so we used rivets to connect two smaller pieces together.  &lt;br /&gt;
&lt;br /&gt;
After testing the drum striking directly on the head of the drum, we decided it was too loud.  To dampen the sound, we taped a piece of foam core and a shop rag to the head of the drum where the stick strikes it.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Parts list for drum actuator&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*Bargain drumstick, at least .5&amp;quot; in diameter&lt;br /&gt;
*Small block of acrylic, .5&amp;quot;x.75&amp;quot;x2&amp;quot; (aluminum would work just as well, this is what was available) &lt;br /&gt;
*16 gauge steel sheet metal for bracket&lt;br /&gt;
*2 pop rivets&lt;br /&gt;
*Screws and nuts&lt;br /&gt;
*Foam core, shop rag, tape for dampening&lt;br /&gt;
&lt;br /&gt;
==Music Tones - Electrical Design==&lt;br /&gt;
The music tones were activated by tilt switches and generated using a [http://pdf1.alldatasheet.com/datasheet-pdf/view/82065/YAMAHA/YMZ284.html YMZ284] chip and were output through a 1/8&amp;quot; jack to a standard set of computer speakers.&lt;br /&gt;
&lt;br /&gt;
Each YMZ284 is capable of producing and mixing any combination of three tones.  Since we wanted to have six notes from a [http://en.wikipedia.org/wiki/Pentatonic_scale pentatonic scale], we needed two of these chips and used an [[Operational Amplifiers (Op-Amps)#Summer|summing opamp circuit]] to combine outputs from each chip.  We envisioned a possibility of adding [http://en.wikipedia.org/wiki/Overtone overtones] to each note, to make them sound better and more like a  real instrument, so we ended up having four YMZ284s attached to our board.  When we tried adding an overtone an octave above the fundamental frequency, it ended up sounding harsh and whiny.  We decided the tones sounded better with no overtones, so we are only using 2 of the 4 chips on our board. &lt;br /&gt;
&lt;br /&gt;
The YMZ284 communicates with the PIC over an 8-bit databus, plus additional pins for chip select, address, write enable, and reset.  All four of our YMZ284s shared all of their pins &amp;#039;&amp;#039;except&amp;#039;&amp;#039; the chip select line; each required their own individual chip select line so that the PIC could specify with which chips it wanted to talk.  The circuit diagram below shows how the chips were connected to the PIC and the summing circuit that followed.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  {{multiple image&lt;br /&gt;
   | align     = center&lt;br /&gt;
   | width     = 500&lt;br /&gt;
   | image1    = T21_musicckt_png.PNG&lt;br /&gt;
   | caption1  = Music circuit diagram. The summing amplifier is not shown.&lt;br /&gt;
   | image2    = Solder board mfth.jpg&lt;br /&gt;
   | alt2      = Red cartouche&lt;br /&gt;
   | caption2  = Music chips are in the center of the board. H-bridge for motor driving is on the far right, along with the power supply plug. The left side is an unsuccessful attempt at moving the heart rate circuitry to the solder board&lt;br /&gt;
  }}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Tilt switches - Electrical Design===&lt;br /&gt;
In order to detect when the limbs of the user had moved past a certain critical angle, we chose to use tilt switches.  We purchased our tilt switches for $2 each from [http://www.goldmine-elec-products.com/prodinfo.asp?number=G16881 Electronics Goldmine].  We chose to use tilt switches instead of an accelerometer because the switches were much cheaper and we figured they would be easier to work with. &lt;br /&gt;
&lt;br /&gt;
The tilt switches we chose are optically-based with an emitter and detector that are selectively blocked by a small ball.  When the critical angle is achieved, the tiny ball rolls up a ramp, allowing the emitter to shine directly on the detector, and current flows.  When the angle is neutral, the ramp forces the ball to block the light, allowing no current to pass through the phototransistor.  The &amp;quot;ramp&amp;quot; is really an inverted cone, so the switch can be activated by a change in angle in any direction away from the neutral position.  We purchased three different tilt switches to try, each with a different critical angle - 15, 30, and 45 degrees.  The configuration we ended up using was: &lt;br /&gt;
&lt;br /&gt;
*30 degrees for shoulders and ankles&lt;br /&gt;
*45 degrees for elbows&lt;br /&gt;
&lt;br /&gt;
All the tilt switches experienced unwanted inertial effects.  As they are mechanical systems, every movement, whether tilting or simply bumping the switches, could bounce the ball around inside the switch casing.  This caused the switch output voltage to oscillate in response to unsmooth movements, affecting our music tone generation.&lt;br /&gt;
&lt;br /&gt;
In hindsight, we would have used all 45-degree switches because they produced the most reliable and sharpest jump from off to on.  The 15-degree switches were practically unusable because they created a large amount of noise as they switched on and off.  &lt;br /&gt;
&lt;br /&gt;
We used the tilt switches to provide high-low inputs to the PIC to tell it when to turn on any of the six tones.  By attaching these switches to the body such that they are in the neutral position when the body is relaxed, we were able to control the six tones by moving the body in relatively natural ways.  &lt;br /&gt;
&lt;br /&gt;
===Tilt switches - Mechanical Design===&lt;br /&gt;
&lt;br /&gt;
[[Image:Wrist sensor mfth.JPG|thumb|200px|Wrist tilt sensor. Ankles are similar, with a larger strap.]]&lt;br /&gt;
The switches were attached to the user&amp;#039;s body using adjustable straps and buckles.  The wrist and ankle sensors were simply a loop of webbing with an adjustable buckle attached.  The straps could be tightened or loosened to fit any size user.  To attach the switch to the straps, we created an enclosure for each switch out of high-density foam (leftover scraps from DSGN 307).  The switch was jammed into a hole in the foam so that it could not easily be moved.  The foam was then taped to the straps using electrical tape.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The shoulder straps were a bit more involved.  Two loops with buckles, similar to the wrist and ankle straps, went over the user&amp;#039;s shoulder and underneath their armpit.  To keep these straps from sliding off the shoulders,  a large strap across the user&amp;#039;s back connected the two smaller straps together.  This large strap was also used for mounting the solder board which gathered the wires from all six tilt switches and sent them to the PIC over one ribbon cable.  Using this strap configuration, we found that the user could comfortably and securely wear the sensors.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Shoulder sensors mfth.JPG|thumb|200px|Shoulder straps containing the tilt sensors.]]&lt;br /&gt;
To attach the switches to the shoulders, we again created a foam encasement.  However, electrical tape was not secure enough to hold the foam pieces to the irregularly-shaped shoulder straps.  Instead, we riveted a piece of canvas to the webbing straps.  We chose to use rivets instead of sewing because the sewing machine could not tightly sew the foam encasement in place.  With rivets, we were able to pull the canvas tightly around the foam so that it couldn&amp;#039;t move.&lt;br /&gt;
&lt;br /&gt;
=Code=&lt;br /&gt;
To download the entire C file, click [[Media:Main_music.c|here]]. Below, the file is split into sections for comments.&lt;br /&gt;
&lt;br /&gt;
 /**********************************************************&lt;br /&gt;
  * main_music.c: Main c file for &amp;quot;Music Suit&amp;quot; project&lt;br /&gt;
  *    &lt;br /&gt;
  *    Thomas Peterson, James Rein, Eric West&lt;br /&gt;
  *    ME333 Winter 2010&lt;br /&gt;
  **********************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;HardwareProfile.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;delays.c&amp;gt;  //Delayms and Delayus functions&lt;br /&gt;
&lt;br /&gt;
The first section defines all the input and output pins used with the motor, sensor, and music circuits.&lt;br /&gt;
&lt;br /&gt;
 ////////////////////////////////////////////////////&lt;br /&gt;
 //Pin Defines&lt;br /&gt;
 &lt;br /&gt;
 //YMZ Chip selects&lt;br /&gt;
 #define CS1 LATCbits.LATC2&lt;br /&gt;
 #define CS2 LATCbits.LATC3&lt;br /&gt;
 #define CS3 LATCbits.LATC4&lt;br /&gt;
 #define CS4 LATGbits.LATG6&lt;br /&gt;
 //YMZ communication pins&lt;br /&gt;
 #define NWR LATBbits.LATB0 //&amp;quot;Not WRite&amp;quot;: active low write line&lt;br /&gt;
 #define A0  LATBbits.LATB6 //Address 0&lt;br /&gt;
 #define NIC LATCbits.LATC1 //&amp;quot;Not Input Clear&amp;quot;: active low reset line&lt;br /&gt;
 //YMZ data pins&lt;br /&gt;
 #define D0 LATGbits.LATG9&lt;br /&gt;
 #define D1 LATEbits.LATE8&lt;br /&gt;
 #define D2 LATEbits.LATE9&lt;br /&gt;
 #define D3 LATBbits.LATB5&lt;br /&gt;
 #define D4 LATBbits.LATB4&lt;br /&gt;
 #define D5 LATBbits.LATB3&lt;br /&gt;
 #define D6 LATBbits.LATB2&lt;br /&gt;
 #define D7 LATBbits.LATB1&lt;br /&gt;
 &lt;br /&gt;
 //Tilt inputs&lt;br /&gt;
 #define ENC PORTDbits.RD3&lt;br /&gt;
 #define END PORTDbits.RD2&lt;br /&gt;
 #define ENE PORTDbits.RD1&lt;br /&gt;
 #define ENG PORTCbits.RC14&lt;br /&gt;
 #define ENA PORTCbits.RC13&lt;br /&gt;
 #define ENC5 PORTDbits.RD0&lt;br /&gt;
 &lt;br /&gt;
 //Motor outputs&lt;br /&gt;
 #define IN1 LATBbits.LATB7&lt;br /&gt;
 #define IN2 LATAbits.LATA9&lt;br /&gt;
 #define EN   LATAbits.LATA10&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////&lt;br /&gt;
 //Other defines&lt;br /&gt;
 //Motor times&lt;br /&gt;
 #define MS(x) x*312.5&lt;br /&gt;
 #define DRIVETIME MS(100)&lt;br /&gt;
 #define BACKTIME MS(75)&lt;br /&gt;
 #define WAITTIME MS(150)&lt;br /&gt;
 //Channels&lt;br /&gt;
 #define CHNA 1&lt;br /&gt;
 #define CHNB 2&lt;br /&gt;
 #define CHNC 4&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //////////////////////////////////////////&lt;br /&gt;
 //Function definitions&lt;br /&gt;
 void write_chip(int address, int data);&lt;br /&gt;
 void portout(int byte);&lt;br /&gt;
 void channelSw(int chip, int chn, int *mixer, int state);&lt;br /&gt;
 //Global variables&lt;br /&gt;
 int mixer1, mixer2;&lt;br /&gt;
 int motorflag = 0;&lt;br /&gt;
The initialization function sets up various parts of the system.&lt;br /&gt;
 /* init() : This function runs initializations, setting up the pic and music chips */&lt;br /&gt;
 void init() {&lt;br /&gt;
    int pbClk = SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
    /////////////////////////////////////////////&lt;br /&gt;
    //Setup inputs / outputs&lt;br /&gt;
    AD1PCFG = 0xFFFF; //All digital inputs&lt;br /&gt;
    TRISG &amp;amp;= 0xFDBF;  //sets G9,G6 to 0 (output); all else remains the same&lt;br /&gt;
    TRISC &amp;amp;= 0xFFE1;  //sets C4,C3,C2,C1 to 0s (outputs); all else remains the same&lt;br /&gt;
    TRISB &amp;amp;= 0xFF80;  //sets B0-B6 0 (output)&lt;br /&gt;
    TRISE &amp;amp;= 0xFCFF;  //sets E8,9 to output&lt;br /&gt;
    TRISB |= 0xF000;  //sets B12,B13,B14,B15 to 1s (inputs); all else remains the same&lt;br /&gt;
    TRISC |= 0x6000;  //sets C14,13 to inputs&lt;br /&gt;
    TRISD |= 0x000F;  //Sets D0,D1,D2,D3 to inputs&lt;br /&gt;
    TRISDbits.TRISD4 = 0; //Output for clock&lt;br /&gt;
    //Motor outputs&lt;br /&gt;
    TRISAbits.TRISA9 = 0;&lt;br /&gt;
    TRISAbits.TRISA10 = 0;&lt;br /&gt;
    TRISBbits.TRISB7 = 0;&lt;br /&gt;
    //Pulse inputs   &lt;br /&gt;
    TRISAbits.TRISA14 = 1;&lt;br /&gt;
    TRISAbits.TRISA15 = 1;&lt;br /&gt;
    //ODC setup&lt;br /&gt;
    ODCCbits.ODCC1 = 1; //open drain control; can only float high (when set high) or pull low &lt;br /&gt;
    &lt;br /&gt;
    /* Initialize outputs */&lt;br /&gt;
    //YMZs&lt;br /&gt;
    CS1 = 1;&lt;br /&gt;
    CS2 = 1;&lt;br /&gt;
    CS3 = 1;&lt;br /&gt;
    CS4 = 1;&lt;br /&gt;
    NWR = 1;&lt;br /&gt;
    A0 = 0;&lt;br /&gt;
    NIC = 1;&lt;br /&gt;
    //Motor&lt;br /&gt;
    IN2 = 0;&lt;br /&gt;
    IN1 = 0;&lt;br /&gt;
    EN = 0;&lt;br /&gt;
    &lt;br /&gt;
    Delayms(100);  //wait for power supply to ramp up&lt;br /&gt;
    NIC = 0;       //resets YMZ284s&lt;br /&gt;
    Delayms(10);&lt;br /&gt;
    NIC = 1;&lt;br /&gt;
    &lt;br /&gt;
In the init() function, the PIC writes all the setup data to the music chips. This includes the frequency registers, which determine the frequency of each output, the mixer, which determines which channels are on or off (1 = off), and the volume registers, which determine the volume of each channel. After the setup, the only register that is changed in normal operation is the mixer register, which merely turns channels on and off.&lt;br /&gt;
    //////////////////////////////////////////////////&lt;br /&gt;
    //Setup music chips&lt;br /&gt;
    //Setup Chip 1//&lt;br /&gt;
    CS1 = 0;&lt;br /&gt;
    write_chip(0x00,0xC7); //a low               // C4&lt;br /&gt;
    write_chip(0x01,0x01); //a high&lt;br /&gt;
    write_chip(0x02,0x98); //b low               // D&lt;br /&gt;
    write_chip(0x03,0x01); //b high&lt;br /&gt;
    write_chip(0x04,0x6E); //c low&lt;br /&gt;
    write_chip(0x05,0x01); //c high               // E&lt;br /&gt;
    write_chip(0x07,0xFF); //mixer (0 enables)&lt;br /&gt;
    write_chip(0x08, 0x0A);  //level (volume) A&lt;br /&gt;
    write_chip(0x09, 0x0A);  //level (volume) B&lt;br /&gt;
    write_chip(0x0A, 0x0A);  //level (volume) C&lt;br /&gt;
    CS1=1;&lt;br /&gt;
    //Setup Chip 2//&lt;br /&gt;
    CS2 = 0;&lt;br /&gt;
    write_chip(0x00,0x2C); //a low               //G 392&lt;br /&gt;
    write_chip(0x01,0x01); //a high&lt;br /&gt;
    write_chip(0x02,0x0D); //b low               //A 440&lt;br /&gt;
    write_chip(0x03,0x01); //b high&lt;br /&gt;
    write_chip(0x04,0xE3); //c low               //C5 523.25&lt;br /&gt;
    write_chip(0x05,0x00); //c high               &lt;br /&gt;
    write_chip(0x07,0xFF); //mixer (0 enables)&lt;br /&gt;
    write_chip(0x08, 0x0A);  //level (volume) A&lt;br /&gt;
    write_chip(0x09, 0x0A);  //level (volume) B&lt;br /&gt;
    write_chip(0x0A, 0x0A);  //level (volume) C&lt;br /&gt;
    CS2=1;&lt;br /&gt;
 &lt;br /&gt;
    mixer1=0xFF;&lt;br /&gt;
    mixer2=0xFF;&lt;br /&gt;
OC5 is used to create a PWM signal with 50% duty at 4Mhz, in effect creating a 4Mhz square wave. This is used by the music chips as a master clock.&lt;br /&gt;
    //////////////////////////////////////////////&lt;br /&gt;
    //Setup OC to create master clock for music chips (~4 Mhz)&lt;br /&gt;
    OpenOC5( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);&lt;br /&gt;
    OpenTimer2( T2_ON | T2_PS_1_1 | T2_SOURCE_INT, 20);&lt;br /&gt;
    SetDCOC5PWM(10);&lt;br /&gt;
The interrupts are used, as mentioned above, to control when the motor is driven. The motor should drive once every rising edge.&lt;br /&gt;
    /////////////////////////////////////////////&lt;br /&gt;
    //External interrupt setups (for motor) - INT3 (A14)&lt;br /&gt;
    mINT3ClearIntFlag();&lt;br /&gt;
    mINT3IntEnable(1);&lt;br /&gt;
    mINT3SetIntPriority(6);&lt;br /&gt;
    mINT3SetEdgeMode(1); //Rising edge&lt;br /&gt;
    /////////////////////////////////////////////&lt;br /&gt;
    //External interrupt setups (for motor) - INT4 (A15)&lt;br /&gt;
    mINT4ClearIntFlag();&lt;br /&gt;
    mINT4IntEnable(1);&lt;br /&gt;
    mINT4SetIntPriority(5);&lt;br /&gt;
    mINT4SetEdgeMode(0); //Falling edge&lt;br /&gt;
    INTEnableSystemMultiVectoredInt();&lt;br /&gt;
 &lt;br /&gt;
    /////////////////////////////////////////////&lt;br /&gt;
    //Setup motor timer (used for timing delays)&lt;br /&gt;
    OpenTimer3(T3_ON | T3_PS_1_256 | T3_SOURCE_INT, 0xFFFF);&lt;br /&gt;
    ConfigIntTimer3(T3_INT_OFF);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Main function&lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
    init();      /* Run initialization functions */&lt;br /&gt;
    Delayms(1);&lt;br /&gt;
 &lt;br /&gt;
    /* Main while loop: loop indefinietly */&lt;br /&gt;
    while (1) {&lt;br /&gt;
In the main while loop, the PIC checks every cycle to see if an input has changed since the last cycle. Since the &amp;quot;mixer&amp;quot; variables store the current state, if an input does not match up with its valid mixer bit then it has changed recently. In this case, the channelSw() function is called to switch on or off the music channel controlled by that bit.&lt;br /&gt;
       //Check for state changes in music notes&lt;br /&gt;
       if (ENC != (mixer1 &amp;amp; 0x01)) { channelSw(1,CHNA,&amp;amp;mixer1,!ENC); }   //If input ENC (enable C) is not equal to the state of channel A1 (note c), change the state&lt;br /&gt;
       if (END != (mixer1 &amp;amp; 0x02)) { channelSw(1,CHNB,&amp;amp;mixer1,!END); }&lt;br /&gt;
       if (ENE != (mixer1 &amp;amp; 0x04)) { channelSw(1,CHNC,&amp;amp;mixer1,!ENE); }&lt;br /&gt;
       if (ENG != (mixer2 &amp;amp; 0x01)) { channelSw(2,CHNA,&amp;amp;mixer2,!ENG); }&lt;br /&gt;
       if (ENA != (mixer2 &amp;amp; 0x02)) { channelSw(2,CHNB,&amp;amp;mixer2,!ENA); }&lt;br /&gt;
       if (ENC5 != (mixer2 &amp;amp; 0x04)) { channelSw(2,CHNC,&amp;amp;mixer2,!ENC5); }&lt;br /&gt;
During the main while loop, the motor flag variable is checked to see if the motor should be driven. If so, it enters a sequence where it uses timer3 to determine the current stage in the motor cycle, changing the outputs as needed.&lt;br /&gt;
       //Check for motor state changes&lt;br /&gt;
       switch (motorflag) {&lt;br /&gt;
          case 0: break;   //Do nothing (motor off)&lt;br /&gt;
          case 1:   //Start motor cycle&lt;br /&gt;
                WriteTimer3(0);   //Reset timer to 0&lt;br /&gt;
                IN1 = 0;          //Set H bridge to forward drive&lt;br /&gt;
                IN2 = 1;&lt;br /&gt;
                EN = 1;&lt;br /&gt;
                motorflag = 2;    //In drivedown cycle&lt;br /&gt;
                break;&lt;br /&gt;
          case 2:   //Drivedown cycle&lt;br /&gt;
                if (ReadTimer3() &amp;gt; DRIVETIME) {  //Wait for DRIVETIME to pass&lt;br /&gt;
                   WriteTimer3(0); //Reset timer to 0&lt;br /&gt;
                   motorflag = 3; //In driveup phase&lt;br /&gt;
                   IN2 = 0;       //Switch H bridge&lt;br /&gt;
                   IN1 = 1;&lt;br /&gt;
                }&lt;br /&gt;
                break;&lt;br /&gt;
          case 3:   //Driveup cycle&lt;br /&gt;
                if (ReadTimer3() &amp;gt; BACKTIME) {   //Wait for BACKTIME to pass&lt;br /&gt;
                   WriteTimer3(0);   //Reset timer to 0&lt;br /&gt;
                   motorflag = 4;    //In coast/wait phase&lt;br /&gt;
                   EN = 0;           //Turn off motor&lt;br /&gt;
                }&lt;br /&gt;
                break;&lt;br /&gt;
          case 4: //coast / wait cycle&lt;br /&gt;
                if (ReadTimer3() &amp;gt; WAITTIME) {&lt;br /&gt;
                   motorflag = 5; //more wait&lt;br /&gt;
                   WriteTimer3(0);&lt;br /&gt;
                }&lt;br /&gt;
                break;&lt;br /&gt;
          case 5:  //Additional wait cycle&lt;br /&gt;
                if (ReadTimer3() &amp;gt; WAITTIME) {&lt;br /&gt;
                   //Done!&lt;br /&gt;
                   motorflag = 0; //Motor off (can now be triggered again)&lt;br /&gt;
                }&lt;br /&gt;
                break;&lt;br /&gt;
          //Static wait cycles&lt;br /&gt;
          case 6:&lt;br /&gt;
                if (ReadTimer3() &amp;gt; WAITTIME) {&lt;br /&gt;
                   motorflag = 7; &lt;br /&gt;
                   WriteTimer3(0);&lt;br /&gt;
                }&lt;br /&gt;
                break;&lt;br /&gt;
          case 7:&lt;br /&gt;
                if (ReadTimer3() &amp;gt; WAITTIME) {&lt;br /&gt;
                   WriteTimer3(0);&lt;br /&gt;
                   motorflag = 8; &lt;br /&gt;
                }&lt;br /&gt;
                break;&lt;br /&gt;
          case 8:&lt;br /&gt;
                if (ReadTimer3() &amp;gt; WAITTIME) {&lt;br /&gt;
                   motorflag = 0; &lt;br /&gt;
                }&lt;br /&gt;
                break;&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
       Delayms(10);&lt;br /&gt;
    }&lt;br /&gt;
    return 0;&lt;br /&gt;
 }//end main&lt;br /&gt;
 &lt;br /&gt;
Write chip function, which is a support function that writes data to the music chips.&lt;br /&gt;
 /* write_chip: This writes the given byte &amp;quot;data&amp;quot; to the &amp;quot;address&amp;quot; (0 or 1)&lt;br /&gt;
                on a YMZ284. It is assumed that the chip select is already low */&lt;br /&gt;
 void write_chip(int address,int data) {&lt;br /&gt;
    //Write address&lt;br /&gt;
    portout(address);&lt;br /&gt;
    A0 = 0;     //select address mode&lt;br /&gt;
    Delayus(1); //setup time&lt;br /&gt;
    NWR = 0;    //Write cycle begin&lt;br /&gt;
    Delayus(1); //hold time&lt;br /&gt;
    NWR = 1;    //Write cycle end&lt;br /&gt;
   &lt;br /&gt;
    //Write  data&lt;br /&gt;
    portout(data);&lt;br /&gt;
    A0 = 1;     //select data mode&lt;br /&gt;
    Delayus(1); //setup time&lt;br /&gt;
    NWR = 0;    //write cycle begin&lt;br /&gt;
    Delayus(1); //hold time&lt;br /&gt;
    NWR = 1;    //write cycle end&lt;br /&gt;
 }&lt;br /&gt;
Port out function&lt;br /&gt;
 /* port_out: This function outputs a byte to the D0-D7 port */&lt;br /&gt;
 void portout(int byte) {&lt;br /&gt;
    // Note:  !! converts an integer expression to a boolean (1 or 0).&lt;br /&gt;
     D0 = !!(byte  &amp;amp; 1);&lt;br /&gt;
     D1 = !!(byte  &amp;amp; 2); &lt;br /&gt;
     D2 = !!(byte  &amp;amp; 4);   &lt;br /&gt;
     D3 = !!(byte  &amp;amp; 8);&lt;br /&gt;
     D4 = !!(byte  &amp;amp; 16);&lt;br /&gt;
     D5 = !!(byte  &amp;amp; 32); &lt;br /&gt;
     D6 = !!(byte  &amp;amp; 64);   &lt;br /&gt;
     D7 = !!(byte  &amp;amp; 128);&lt;br /&gt;
 }&lt;br /&gt;
Channel switch function, which writes a new mixer value to turn a channel on or off.&lt;br /&gt;
 /* channelSw: Generic channel switch function &lt;br /&gt;
               This writes a new mixer value to chip &amp;quot;chip&amp;quot;. The mixer value&lt;br /&gt;
               depends on &amp;quot;chn&amp;quot;, the channel turning on or off, &amp;quot;state&amp;quot; whether&lt;br /&gt;
               the channel is turning on or off, and &amp;quot;*mixer&amp;quot;, a pointer to&lt;br /&gt;
               the old mixer value. */&lt;br /&gt;
 void channelSw(int chip, int chn, int *mixer, int state) {&lt;br /&gt;
    /* Enable music chip */&lt;br /&gt;
    switch(chip) {&lt;br /&gt;
       case 1: CS1 = 0; break;&lt;br /&gt;
       case 2: CS2 = 0; break;&lt;br /&gt;
       case 3: CS3 = 0; break;&lt;br /&gt;
       case 4: CS4 = 0; break;&lt;br /&gt;
    }&lt;br /&gt;
    /* Write mixer value */&lt;br /&gt;
    /* This writes to channel 7 (mixer channel) the _new_ mixer value, which is&lt;br /&gt;
       the old mixer value with a particular bit flipped based on &amp;quot;chn&amp;quot; and &amp;quot;state&amp;quot; */&lt;br /&gt;
    write_chip(0x07, (*mixer) = state ? (*mixer) &amp;amp; (0xFF &amp;amp; ~chn) : (*mixer) | chn);&lt;br /&gt;
    /* Disable chip */&lt;br /&gt;
    switch(chip) {&lt;br /&gt;
       case 1: CS1 = 1; break;&lt;br /&gt;
       case 2: CS2 = 1; break;&lt;br /&gt;
       case 3: CS3 = 1; break;&lt;br /&gt;
       case 4: CS4 = 1; break;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
Interrupt functions&lt;br /&gt;
 //External interrupt for motor driving&lt;br /&gt;
 void __ISR(_EXTERNAL_3_VECTOR, ipl6) _SingleVectorHandler(void) {&lt;br /&gt;
    if (motorflag == 0) {&lt;br /&gt;
       motorflag = 1;   //Start motor cycle&lt;br /&gt;
    }&lt;br /&gt;
    //If on already, do nothing&lt;br /&gt;
    mINT3ClearIntFlag();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 //External interrupt for motor waiting&lt;br /&gt;
 void __ISR(_EXTERNAL_4_VECTOR, ipl5) _SingleVectorHandler2(void) {&lt;br /&gt;
    if (motorflag == 0) {&lt;br /&gt;
       WriteTimer3(0);&lt;br /&gt;
       motorflag = 6;   //Start wait cycle&lt;br /&gt;
    }&lt;br /&gt;
    //If on already, do nothing&lt;br /&gt;
    mINT4ClearIntFlag();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Results= &lt;br /&gt;
&lt;br /&gt;
The project turned out to be a decent success overall.  The tilt switches worked very well for activating the music tones, but the heart rate monitor was less successful.  One of the biggest issues was getting rid of very low frequency drift in the signal that made the peaks sometimes as high as 10V, but other times less 1V.  Dealing with this fluctuation in peak size was the most difficult part of signal processing.  As mentioned above, we tried FFT in order to establish a steady heart rate, but could not get the resolution we desired, thus we abandoned the idea.  Also, for optimum functioning, the system had to be tuned for some users, adjusting the potentiometers (shown in the circuit diagram above) until a robust signal was attained.&lt;br /&gt;
&lt;br /&gt;
Furthermore, using the heart rate monitor was complicated by its sensitivity to movement artifact.  The system worked best when one user provided the heartbeat, and another user wore the music suit.  The system was decent at accounting for larger movements initiated from the shoulder or elbow, as long as the wrist and fingers stayed relatively rigid along with the arm.  However, movement initiated from the wrist or finger caused a large influx of noise into the system, requiring at least 5 seconds to smooth out once the user stopped those movements.&lt;br /&gt;
&lt;br /&gt;
The best result was that using the device was just plain fun.  We each had a great time moving around and hearing the fun sounds that resulted, or if we were feeling ambitious, we would try to play a simple melody like &amp;quot;Mary Had a Little Lamb.&amp;quot;  It was rather difficult to play anything more complicated than that because it required lots of body control and precise movements, and we were also constrained by having only the notes of the pentatonic scale at our disposal.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Reflections=&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Successes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*tilt switches activated each of the six notes when they were supposed to&lt;br /&gt;
**the YMZ284 chips were able to produce all six notes at the same time, allowing cool chords&lt;br /&gt;
*tones were in tune and formed fun melodies and chords as the body moved&lt;br /&gt;
*drum actuator hit the drum reliably and with good tone&lt;br /&gt;
*heart rate monitor usually showed clear peaks on a scope, but was finnicky in practice&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Room for improvements&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*tilt switches sometimes bounced, so the beginning and particularly the ends of the notes sounded jagged&lt;br /&gt;
**&amp;#039;&amp;#039;Possible Solution:&amp;#039;&amp;#039; add a low pass filter to the switch signal so that the signal ramps up and down.  &lt;br /&gt;
*the shoulder straps sometimes needed adjustments for each individual in order for the sensors to be in the correct position&lt;br /&gt;
**&amp;#039;&amp;#039;Possible Solution:&amp;#039;&amp;#039; create straps with more constraints, such as a strap across the chest as well as the back, to ensure that the sensors are in the correct positions and orientations. &lt;br /&gt;
*the straps could be easier to put on&lt;br /&gt;
**&amp;#039;&amp;#039;Possible Solution:&amp;#039;&amp;#039; maybe integrate them into a single garment, like a onesie&lt;br /&gt;
*the heart rate monitor was not reliable.  Low frequency drift made setting a comparison level nearly impossible, especially for more than one specific user.&lt;br /&gt;
**&amp;#039;&amp;#039;Possible Solution:&amp;#039;&amp;#039; improve FFT method so that desired resolution could be achieved in a short sample period (around 10 seconds).  Or implement a complicated peak detection algorithm on the PIC.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We believe this project, if expanded and refined, could have some really fun and useful applications.  The music suit idea is not new, but it is not widespread.  A device like this could be useful in music therapy, where therapists use music to help patients heal from a variety of maladies.  Or, this could be an engaging interactive exhibit in a children&amp;#039;s museum.  A suit could also be used to help improve proprioception, or movement awareness, in developmentally disable patients who have difficulty coordinating certain movements and can lack feedback to help them learn.  Using a suit like this would make movement fun in a whole way.&lt;br /&gt;
&lt;br /&gt;
Making music with this device is just plain fun, and it gives a whole new meaning to the idea of &amp;#039;playing with your heart.&amp;#039;&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Music_from_the_Heart_--_Music_Suit&amp;diff=17797</id>
		<title>Music from the Heart -- Music Suit</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Music_from_the_Heart_--_Music_Suit&amp;diff=17797"/>
		<updated>2010-03-18T20:25:03Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
&lt;br /&gt;
This project attempted to create a natural form of musical expression by connecting sensors to the body. Six tilt switches were attached to the wrist, ankles, and shoulders, each controlling a single pitch from the [http://en.wikipedia.org/wiki/Pentatonic_scale pentatonic scale]. The heart beat was obtained using [http://en.wikipedia.org/wiki/Photoplethysmograph photoplethysmography] on the user&amp;#039;s finger, and this signal was used to strike a drum in sync with the user&amp;#039;s heart beat.&lt;br /&gt;
&lt;br /&gt;
For a video demonstration, click [http://www.youtube.com/watch?v=YyipByy7m6I here].&lt;br /&gt;
&lt;br /&gt;
  {{multiple image&lt;br /&gt;
   | width1    = 150&lt;br /&gt;
   | width2    = 269&lt;br /&gt;
   | align     = center&lt;br /&gt;
   | header    = Music from the Heart Project&lt;br /&gt;
   | image1    = Music from the heart overview.jpg&lt;br /&gt;
   | caption1  = The full &amp;quot;music suit&amp;quot; on James, with heart rate sensor on his finger. &lt;br /&gt;
   | image2    = Drum overview mfth.jpg&lt;br /&gt;
   | caption2  = A closer view of the drum mechanism&lt;br /&gt;
  }}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Team Members==&lt;br /&gt;
&lt;br /&gt;
[[Image:Mfth team picture.JPG|thumb|200px|From left: Thomas, Eric, James]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Thomas Peterson (Computer Engineering, 2010)&lt;br /&gt;
*James Rein (Biomedical Engineering and Music Cognition, 2010)&lt;br /&gt;
*Eric West (Mechanical Engineering, 2011)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Subsystems=&lt;br /&gt;
&lt;br /&gt;
Although intended as a single, cohesive system that would allow the user to intuitively make music, the project was easily divided into three subsystems: heart rate monitor, drum actuation, and music tones.  Below is more explanation about each subsystem. &lt;br /&gt;
&lt;br /&gt;
==Heart rate monitor - Concepts==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In brainstorming how to translate a heart beat into a drum beat, we decided that the least intrusive method would be the best.  Attaching electrodes to the user for [http://en.wikipedia.org/wiki/Electrocardiography ECG] would simplify the process of identifying a heart beat, but attaching these sensors directly to the skin would be a time-consuming and personally-invasive process.  Instead, we decided a finger-tip sensor would be much more comfortable and easy to use.  &lt;br /&gt;
&lt;br /&gt;
To make a finger tip sensor, we used the concepts of [http://en.wikipedia.org/wiki/Photoplethysmograph photoplethysmography].  Photoplethysmography is typically used in [http://en.wikipedia.org/wiki/Pulse_oximeter pulse oximeters] and finger-tip sensors in commercially-available devices.  We originally tried to hack a heart rate monitor that we had purchased, but this proved difficult, so we decided to make our own.  &lt;br /&gt;
&lt;br /&gt;
The basic concept of photoplethysmography is that blood reflects a certain amount of IR light, and the blood density in the finger changes as the heart pumps, so the IR reflectivity of the finger changes as the heart beats.  Using an IR emitter-detector pair found in the mechatronics lab  followed by amplification and filtering, we were able to obtain a decent signal with peaks when the user&amp;#039;s heart beat.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Heart rate monitor - Electrical Design===&lt;br /&gt;
&lt;br /&gt;
The signal coming directly out of the IR detector was very weak and noisy.  To obtain just the information we desired, we ran it through the following filters and amplifiers: &lt;br /&gt;
&lt;br /&gt;
# [[Passive Filters#High-Pass Filter (HPF)|Passive High-Pass Filter]]&lt;br /&gt;
# [[Operational Amplifiers (Op-Amps)#Inverting Amplifier|Inverting Amplifier]]: gain = -470&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Low-pass_filter#Active_electronic_realization Active Low-pass filter]: cutoff frequency = 4.82Hz, gain = -70&lt;br /&gt;
# [http://www.electronics-tutorials.ws/filter/filter_7.html Band-pass filter]: Frequency range = 0.72 to 3.39Hz, gain = -46.8&lt;br /&gt;
# [[Comparators|Comparator]]: output to the PIC pin A14, with pull-up resistor to 3.3V and capacitors to smooth out the signal.&lt;br /&gt;
&lt;br /&gt;
The complete signal processing circuit is shown below.  Note the potentiometers, which can be adjusted for some users if the output signal is not clear.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Heartrate monitor.PNG|thumb|center|900px|Heart rate monitor circuit diagram. Click for larger image.]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Heart rate monitor - Mechanical Design===&lt;br /&gt;
&lt;br /&gt;
[[Image:Heart rate sensor mfth.JPG|thumb|200px|Heart rate sensor with the latex band pulled away so that the emitter-detector is revealed]]&lt;br /&gt;
The sensor itself was a QRB1114 emitter-detector pair that was encased in open-cell foam insulation that served as the finger-holder.  We inserted it into the foam by drilling and filing a hole in the foam to the appropriate size, and gluing the sensor in place.  A round, finger-shaped groove was filed into the foam and the sensor sat flush with the bottom of this groove.  The user&amp;#039;s finger was held in the correct position with relatively constant pressure using a latex band around the finger and the foam enclosure.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Drum actuator - Electrical design==&lt;br /&gt;
&lt;br /&gt;
Once the signal from the heart rate sensor is sent to the PIC, the rising edge triggers an interrupt which starts the driving sequence for the motor that strikes the drum.  The drive sequence was: drive down (into drum) 100ms, drive up 75ms, wait 150ms. The interrupt does not do the entire driving sequence, it merely flags a variable to start the sequence, which is done in the main loop, as seen in the code below.&lt;br /&gt;
&lt;br /&gt;
An additional interrupt was triggered by the falling edge of the signal; this falling edge interrupt added additional wait time that prevented the PIC from being &amp;quot;fooled&amp;quot; by a noisy falling edge.  For instance, if the falling signal had some noise in it, the rising edge interrupt would be triggered again, thus causing the drum to strike on both the rising and falling edges of the heartbeat.  We only wanted one strike per heartbeat, so the falling edge interrupt was triggered by the &amp;#039;&amp;#039;first&amp;#039;&amp;#039; falling edge, and any noise in the falling edge was ignored because the PIC was told to wait for 300ms.  With a maximum delay of 150 + 300 = 450ms from the two interrupts, and 100 + 75 = 175ms of drive time, the fastest heartbeat our drum could play was 96 beats per minute, but we did not expect anyone to come play the instrument after running, so this was plenty high for our purposes.  &lt;br /&gt;
&lt;br /&gt;
Initially, we tried skipping the comparator and sending the analog signal directly into one of the PICs analog inputs. Our intention was to use the fast Fourier transform (FFT) library to try to extract the dominant frequency (see [[PIC32MX: FFT of Analog Input]] for more information). Unfortunately, we ran into various problems. First, to get the accuracy we wanted for small frequencies, the sample window had to be very long (10 seconds or more), which made the output lag changes in heart rate. Additionally, the FFT was often &amp;quot;messy&amp;quot;, which messed up our original algorithm of detecting the peak frequency between .5 and 2.5 hz. The system would sometimes randomly switch between very different frequencies, and although it was right most of the time, it was enough to make the drum beating noticeably erratic. We also tried algorithms to find the [http://en.wikipedia.org/wiki/Fundamental_frequency fundamental frequency] using the entire FFT (instead of just the .5-2.5 hz range), but this was still unreliable. We considered FFT peak-detection algorithms, but they seemed too complex for the time and computing power we had available. Because of all this, we went with the simple comparator in the end.&lt;br /&gt;
&lt;br /&gt;
The motor for driving the drumstick was driven by an [[media:L298N.pdf|L298 H-bridge]].  See [[Driving a high current DC Motor using an H-bridge]] for more information on how this works.  Our particular circuit diagram is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:T21_motorckt_png.PNG|thumb|center|600px|Motor Circuit (non-motor PIC connections not shown). Click for larger image.]]&lt;br /&gt;
&lt;br /&gt;
====Drum actuator - Mechanical Design====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Drum stick mount mfth.jpg|thumb|200px|Attachment for the drumstick to the motor shaft. Split-clamp to the motor shaft on the left, drum stick on the right|right]]&lt;br /&gt;
We wanted to use a real drum for our drum beat, so our challenge was to attach a motor and drumstick to a drum.  We chose a drumset small rack tom for its resonant tone that is reminiscent of a heart beat.  &lt;br /&gt;
&lt;br /&gt;
To attach the drumstick to the motor shaft, we used a small block of acrylic (.5&amp;quot;x.75&amp;quot;x2&amp;quot;).  The drumstick was cut down to 8&amp;quot; in length so that it would require less torque from the motor.  In one end of the block, a hole was drilled and the drumstick was sanded to the correct diameter in order to snugly fit into the hole.  To ensure that the drumstick could not slide out of the block, a screw was placed in a hole drilled through both the stick and the block.  The other end of the block was attached to the motor shaft using a split-clamp method: a hole the size of the motor shaft was drilled through the block, and then a slit was cut from the end of the block to the hole, allowing a screw to tightly clamp the block around the motor.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Motor mount mfth.jpg|thumb|200px|Mounting bracket for the motor-stick assembly.|right]]&lt;br /&gt;
To attach the motor-stick assembly to the drum, we utilized the tuning screws on the drum and holes in the face of the motor.  A sheet metal bracket with holes in the appropriate places attached the motor to two of the tuning screws.  The bracket was made by cutting and bending a piece of 16 gauge steel sheet metal to the appropriate size, with a contour fitting the curvature of the drum, and an L-shaped portion to attach the motor above the drum head.  Ideally, this part would have been made out of one continuous piece of sheet metal, but we could not find a large enough sheet available in the shop, so we used rivets to connect two smaller pieces together.  &lt;br /&gt;
&lt;br /&gt;
After testing the drum striking directly on the head of the drum, we decided it was too loud.  To dampen the sound, we taped a piece of foam core and a shop rag to the head of the drum where the stick strikes it.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Parts list for drum actuator&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*Bargain drumstick, at least .5&amp;quot; in diameter&lt;br /&gt;
*Small block of acrylic, .5&amp;quot;x.75&amp;quot;x2&amp;quot; (aluminum would work just as well, this is what was available) &lt;br /&gt;
*16 gauge steel sheet metal for bracket&lt;br /&gt;
*2 pop rivets&lt;br /&gt;
*Screws and nuts&lt;br /&gt;
*Foam core, shop rag, tape for dampening&lt;br /&gt;
&lt;br /&gt;
==Music Tones - Electrical Design==&lt;br /&gt;
The music tones were activated by tilt switches and generated using a [http://pdf1.alldatasheet.com/datasheet-pdf/view/82065/YAMAHA/YMZ284.html YMZ284] chip and were output through a 1/8&amp;quot; jack to a standard set of computer speakers.&lt;br /&gt;
&lt;br /&gt;
Each YMZ284 is capable of producing and mixing any combination of three tones.  Since we wanted to have six notes from a [http://en.wikipedia.org/wiki/Pentatonic_scale pentatonic scale], we needed two of these chips and used an [[Operational Amplifiers (Op-Amps)#Summer|summing opamp circuit]] to combine outputs from each chip.  We envisioned a possibility of adding [http://en.wikipedia.org/wiki/Overtone overtones] to each note, to make them sound better and more like a  real instrument, so we ended up having four YMZ284s attached to our board.  When we tried adding an overtone an octave above the fundamental frequency, it ended up sounding harsh and whiny.  We decided the tones sounded better with no overtones, so we are only using 2 of the 4 chips on our board. &lt;br /&gt;
&lt;br /&gt;
The YMZ284 communicates with the PIC over an 8-bit databus, plus additional pins for chip select, address, write enable, and reset.  All four of our YMZ284s shared all of their pins &amp;#039;&amp;#039;except&amp;#039;&amp;#039; the chip select line; each required their own individual chip select line so that the PIC could specify with which chips it wanted to talk.  The circuit diagram below shows how the chips were connected to the PIC and the summing circuit that followed.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  {{multiple image&lt;br /&gt;
   | align     = center&lt;br /&gt;
   | width     = 500&lt;br /&gt;
   | image1    = T21_musicckt_png.PNG&lt;br /&gt;
   | caption1  = Music circuit diagram. The summing amplifier is not shown.&lt;br /&gt;
   | image2    = Solder board mfth.jpg&lt;br /&gt;
   | alt2      = Red cartouche&lt;br /&gt;
   | caption2  = Music chips are in the center of the board. H-bridge for motor driving is on the far right, along with the power supply plug. The left side is an unsuccessful attempt at moving the heart rate circuitry to the solder board&lt;br /&gt;
  }}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Tilt switches - Electrical Design===&lt;br /&gt;
In order to detect when the limbs of the user had moved past a certain critical angle, we chose to use tilt switches.  We purchased our tilt switches for $2 each from [http://www.goldmine-elec-products.com/prodinfo.asp?number=G16881 Electronics Goldmine].  We chose to use tilt switches instead of an accelerometer because the switches were much cheaper and we figured they would be easier to work with. &lt;br /&gt;
&lt;br /&gt;
The tilt switches we chose are optically-based with an emitter and detector that are selectively blocked by a small ball.  When the critical angle is achieved, the tiny ball rolls up a ramp, allowing the emitter to shine directly on the detector, and current flows.  When the angle is neutral, the ramp forces the ball to block the light, allowing no current to pass through the phototransistor.  The &amp;quot;ramp&amp;quot; is really an inverted cone, so the switch can be activated by a change in angle in any direction away from the neutral position.  We purchased three different tilt switches to try, each with a different critical angle - 15, 30, and 45 degrees.  The configuration we ended up using was: &lt;br /&gt;
&lt;br /&gt;
*30 degrees for shoulders and ankles&lt;br /&gt;
*45 degrees for elbows&lt;br /&gt;
&lt;br /&gt;
All the tilt switches experienced unwanted inertial effects.  As they are mechanical systems, every movement, whether tilting or simply bumping the switches, could bounce the ball around inside the switch casing.  This caused the switch output voltage to oscillate in response to unsmooth movements, affecting our music tone generation.&lt;br /&gt;
&lt;br /&gt;
In hindsight, we would have used all 45-degree switches because they produced the most reliable and sharpest jump from off to on.  The 15-degree switches were practically unusable because they created a large amount of noise as they switched on and off.  &lt;br /&gt;
&lt;br /&gt;
We used the tilt switches to provide high-low inputs to the PIC to tell it when to turn on any of the six tones.  By attaching these switches to the body such that they are in the neutral position when the body is relaxed, we were able to control the six tones by moving the body in relatively natural ways.  &lt;br /&gt;
&lt;br /&gt;
===Tilt switches - Mechanical Design===&lt;br /&gt;
&lt;br /&gt;
[[Image:Wrist sensor mfth.JPG|thumb|200px|Wrist tilt sensor. Ankles are similar, with a larger strap.]]&lt;br /&gt;
The switches were attached to the user&amp;#039;s body using adjustable straps and buckles.  The wrist and ankle sensors were simply a loop of webbing with an adjustable buckle attached.  The straps could be tightened or loosened to fit any size user.  To attach the switch to the straps, we created an enclosure for each switch out of high-density foam (leftover scraps from DSGN 307).  The switch was jammed into a hole in the foam so that it could not easily be moved.  The foam was then taped to the straps using electrical tape.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The shoulder straps were a bit more involved.  Two loops with buckles, similar to the wrist and ankle straps, went over the user&amp;#039;s shoulder and underneath their armpit.  To keep these straps from sliding off the shoulders,  a large strap across the user&amp;#039;s back connected the two smaller straps together.  This large strap was also used for mounting the solder board which gathered the wires from all six tilt switches and sent them to the PIC over one ribbon cable.  Using this strap configuration, we found that the user could comfortably and securely wear the sensors.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Shoulder sensors mfth.JPG|thumb|200px|Shoulder straps containing the tilt sensors.]]&lt;br /&gt;
To attach the switches to the shoulders, we again created a foam encasement.  However, electrical tape was not secure enough to hold the foam pieces to the irregularly-shaped shoulder straps.  Instead, we riveted a piece of canvas to the webbing straps.  We chose to use rivets instead of sewing because the sewing machine could not tightly sew the foam encasement in place.  With rivets, we were able to pull the canvas tightly around the foam so that it couldn&amp;#039;t move.&lt;br /&gt;
&lt;br /&gt;
=Code=&lt;br /&gt;
To download the entire C file, click [[Media:Main_music.c|here]]. Below, the file is split into sections for comments.&lt;br /&gt;
&lt;br /&gt;
 /**********************************************************&lt;br /&gt;
  * main_music.c: Main c file for &amp;quot;Music Suit&amp;quot; project&lt;br /&gt;
  *    &lt;br /&gt;
  *    Thomas Peterson, James Rein, Eric West&lt;br /&gt;
  *    ME333 Winter 2010&lt;br /&gt;
  **********************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;HardwareProfile.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;delays.c&amp;gt;  //Delayms and Delayus functions&lt;br /&gt;
&lt;br /&gt;
The first section defines all the input and output pins used with the motor, sensor, and music circuits.&lt;br /&gt;
&lt;br /&gt;
 ////////////////////////////////////////////////////&lt;br /&gt;
 //Pin Defines&lt;br /&gt;
 &lt;br /&gt;
 //YMZ Chip selects&lt;br /&gt;
 #define CS1 LATCbits.LATC2&lt;br /&gt;
 #define CS2 LATCbits.LATC3&lt;br /&gt;
 #define CS3 LATCbits.LATC4&lt;br /&gt;
 #define CS4 LATGbits.LATG6&lt;br /&gt;
 //YMZ communication pins&lt;br /&gt;
 #define NWR LATBbits.LATB0 //&amp;quot;Not WRite&amp;quot;: active low write line&lt;br /&gt;
 #define A0  LATBbits.LATB6 //Address 0&lt;br /&gt;
 #define NIC LATCbits.LATC1 //&amp;quot;Not Input Clear&amp;quot;: active low reset line&lt;br /&gt;
 //YMZ data pins&lt;br /&gt;
 #define D0 LATGbits.LATG9&lt;br /&gt;
 #define D1 LATEbits.LATE8&lt;br /&gt;
 #define D2 LATEbits.LATE9&lt;br /&gt;
 #define D3 LATBbits.LATB5&lt;br /&gt;
 #define D4 LATBbits.LATB4&lt;br /&gt;
 #define D5 LATBbits.LATB3&lt;br /&gt;
 #define D6 LATBbits.LATB2&lt;br /&gt;
 #define D7 LATBbits.LATB1&lt;br /&gt;
 &lt;br /&gt;
 //Tilt inputs&lt;br /&gt;
 #define ENC PORTDbits.RD3&lt;br /&gt;
 #define END PORTDbits.RD2&lt;br /&gt;
 #define ENE PORTDbits.RD1&lt;br /&gt;
 #define ENG PORTCbits.RC14&lt;br /&gt;
 #define ENA PORTCbits.RC13&lt;br /&gt;
 #define ENC5 PORTDbits.RD0&lt;br /&gt;
 &lt;br /&gt;
 //Motor outputs&lt;br /&gt;
 #define IN1 LATBbits.LATB7&lt;br /&gt;
 #define IN2 LATAbits.LATA9&lt;br /&gt;
 #define EN   LATAbits.LATA10&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////&lt;br /&gt;
 //Other defines&lt;br /&gt;
 //Motor times&lt;br /&gt;
 #define MS(x) x*312.5&lt;br /&gt;
 #define DRIVETIME MS(100)&lt;br /&gt;
 #define BACKTIME MS(75)&lt;br /&gt;
 #define WAITTIME MS(150)&lt;br /&gt;
 //Channels&lt;br /&gt;
 #define CHNA 1&lt;br /&gt;
 #define CHNB 2&lt;br /&gt;
 #define CHNC 4&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //////////////////////////////////////////&lt;br /&gt;
 //Function definitions&lt;br /&gt;
 void write_chip(int address, int data);&lt;br /&gt;
 void portout(int byte);&lt;br /&gt;
 void channelSw(int chip, int chn, int *mixer, int state);&lt;br /&gt;
 //Global variables&lt;br /&gt;
 int mixer1, mixer2;&lt;br /&gt;
 int motorflag = 0;&lt;br /&gt;
The initialization function sets up various parts of the system.&lt;br /&gt;
 /* init() : This function runs initializations, setting up the pic and music chips */&lt;br /&gt;
 void init() {&lt;br /&gt;
    int pbClk = SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
    /////////////////////////////////////////////&lt;br /&gt;
    //Setup inputs / outputs&lt;br /&gt;
    AD1PCFG = 0xFFFF; //All digital inputs&lt;br /&gt;
    TRISG &amp;amp;= 0xFDBF;  //sets G9,G6 to 0 (output); all else remains the same&lt;br /&gt;
    TRISC &amp;amp;= 0xFFE1;  //sets C4,C3,C2,C1 to 0s (outputs); all else remains the same&lt;br /&gt;
    TRISB &amp;amp;= 0xFF80;  //sets B0-B6 0 (output)&lt;br /&gt;
    TRISE &amp;amp;= 0xFCFF;  //sets E8,9 to output&lt;br /&gt;
    TRISB |= 0xF000;  //sets B12,B13,B14,B15 to 1s (inputs); all else remains the same&lt;br /&gt;
    TRISC |= 0x6000;  //sets C14,13 to inputs&lt;br /&gt;
    TRISD |= 0x000F;  //Sets D0,D1,D2,D3 to inputs&lt;br /&gt;
    TRISDbits.TRISD4 = 0; //Output for clock&lt;br /&gt;
    //Motor outputs&lt;br /&gt;
    TRISAbits.TRISA9 = 0;&lt;br /&gt;
    TRISAbits.TRISA10 = 0;&lt;br /&gt;
    TRISBbits.TRISB7 = 0;&lt;br /&gt;
    //Pulse inputs   &lt;br /&gt;
    TRISAbits.TRISA14 = 1;&lt;br /&gt;
    TRISAbits.TRISA15 = 1;&lt;br /&gt;
    //ODC setup&lt;br /&gt;
    ODCCbits.ODCC1 = 1; //open drain control; can only float high (when set high) or pull low &lt;br /&gt;
    &lt;br /&gt;
    /* Initialize outputs */&lt;br /&gt;
    //YMZs&lt;br /&gt;
    CS1 = 1;&lt;br /&gt;
    CS2 = 1;&lt;br /&gt;
    CS3 = 1;&lt;br /&gt;
    CS4 = 1;&lt;br /&gt;
    NWR = 1;&lt;br /&gt;
    A0 = 0;&lt;br /&gt;
    NIC = 1;&lt;br /&gt;
    //Motor&lt;br /&gt;
    IN2 = 0;&lt;br /&gt;
    IN1 = 0;&lt;br /&gt;
    EN = 0;&lt;br /&gt;
    &lt;br /&gt;
    Delayms(100);  //wait for power supply to ramp up&lt;br /&gt;
    NIC = 0;       //resets YMZ284s&lt;br /&gt;
    Delayms(10);&lt;br /&gt;
    NIC = 1;&lt;br /&gt;
    &lt;br /&gt;
In the init() function, the PIC writes all the setup data to the music chips. This includes the frequency registers, which determine the frequency of each output, the mixer, which determines which channels are on or off (1 = off), and the volume registers, which determine the volume of each channel. After the setup, the only register that is changed in normal operation is the mixer register, which merely turns channels on and off.&lt;br /&gt;
    //////////////////////////////////////////////////&lt;br /&gt;
    //Setup music chips&lt;br /&gt;
    //Setup Chip 1//&lt;br /&gt;
    CS1 = 0;&lt;br /&gt;
    write_chip(0x00,0xC7); //a low               // C4&lt;br /&gt;
    write_chip(0x01,0x01); //a high&lt;br /&gt;
    write_chip(0x02,0x98); //b low               // D&lt;br /&gt;
    write_chip(0x03,0x01); //b high&lt;br /&gt;
    write_chip(0x04,0x6E); //c low&lt;br /&gt;
    write_chip(0x05,0x01); //c high               // E&lt;br /&gt;
    write_chip(0x07,0xFF); //mixer (0 enables)&lt;br /&gt;
    write_chip(0x08, 0x0A);  //level (volume) A&lt;br /&gt;
    write_chip(0x09, 0x0A);  //level (volume) B&lt;br /&gt;
    write_chip(0x0A, 0x0A);  //level (volume) C&lt;br /&gt;
    CS1=1;&lt;br /&gt;
    //Setup Chip 2//&lt;br /&gt;
    CS2 = 0;&lt;br /&gt;
    write_chip(0x00,0x2C); //a low               //G 392&lt;br /&gt;
    write_chip(0x01,0x01); //a high&lt;br /&gt;
    write_chip(0x02,0x0D); //b low               //A 440&lt;br /&gt;
    write_chip(0x03,0x01); //b high&lt;br /&gt;
    write_chip(0x04,0xE3); //c low               //C5 523.25&lt;br /&gt;
    write_chip(0x05,0x00); //c high               &lt;br /&gt;
    write_chip(0x07,0xFF); //mixer (0 enables)&lt;br /&gt;
    write_chip(0x08, 0x0A);  //level (volume) A&lt;br /&gt;
    write_chip(0x09, 0x0A);  //level (volume) B&lt;br /&gt;
    write_chip(0x0A, 0x0A);  //level (volume) C&lt;br /&gt;
    CS2=1;&lt;br /&gt;
 &lt;br /&gt;
    mixer1=0xFF;&lt;br /&gt;
    mixer2=0xFF;&lt;br /&gt;
OC5 is used to create a PWM signal with 50% duty at 4Mhz, in effect creating a 4Mhz square wave. This is used by the music chips as a master clock.&lt;br /&gt;
    //////////////////////////////////////////////&lt;br /&gt;
    //Setup OC to create master clock for music chips (~4 Mhz)&lt;br /&gt;
    OpenOC5( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);&lt;br /&gt;
    OpenTimer2( T2_ON | T2_PS_1_1 | T2_SOURCE_INT, 20);&lt;br /&gt;
    SetDCOC5PWM(10);&lt;br /&gt;
The interrupts are used, as mentioned above, to control when the motor is driven. The motor should drive once every rising edge.&lt;br /&gt;
    /////////////////////////////////////////////&lt;br /&gt;
    //External interrupt setups (for motor) - INT3 (A14)&lt;br /&gt;
    mINT3ClearIntFlag();&lt;br /&gt;
    mINT3IntEnable(1);&lt;br /&gt;
    mINT3SetIntPriority(6);&lt;br /&gt;
    mINT3SetEdgeMode(1); //Rising edge&lt;br /&gt;
    /////////////////////////////////////////////&lt;br /&gt;
    //External interrupt setups (for motor) - INT4 (A15)&lt;br /&gt;
    mINT4ClearIntFlag();&lt;br /&gt;
    mINT4IntEnable(1);&lt;br /&gt;
    mINT4SetIntPriority(5);&lt;br /&gt;
    mINT4SetEdgeMode(0); //Falling edge&lt;br /&gt;
    INTEnableSystemMultiVectoredInt();&lt;br /&gt;
 &lt;br /&gt;
    /////////////////////////////////////////////&lt;br /&gt;
    //Setup motor timer (used for timing delays)&lt;br /&gt;
    OpenTimer3(T3_ON | T3_PS_1_256 | T3_SOURCE_INT, 0xFFFF);&lt;br /&gt;
    ConfigIntTimer3(T3_INT_OFF);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Main function&lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
    init();      /* Run initialization functions */&lt;br /&gt;
    Delayms(1);&lt;br /&gt;
 &lt;br /&gt;
    /* Main while loop: loop indefinietly */&lt;br /&gt;
    while (1) {&lt;br /&gt;
In the main while loop, the PIC checks every cycle to see if an input has changed since the last cycle. Since the &amp;quot;mixer&amp;quot; variables store the current state, if an input does not match up with its valid mixer bit then it has changed recently. In this case, the channelSw() function is called to switch on or off the music channel controlled by that bit.&lt;br /&gt;
       //Check for state changes in music notes&lt;br /&gt;
       if (ENC != (mixer1 &amp;amp; 0x01)) { channelSw(1,CHNA,&amp;amp;mixer1,!ENC); }   //If input ENC (enable C) is not equal to the state of channel A1 (note c), change the state&lt;br /&gt;
       if (END != (mixer1 &amp;amp; 0x02)) { channelSw(1,CHNB,&amp;amp;mixer1,!END); }&lt;br /&gt;
       if (ENE != (mixer1 &amp;amp; 0x04)) { channelSw(1,CHNC,&amp;amp;mixer1,!ENE); }&lt;br /&gt;
       if (ENG != (mixer2 &amp;amp; 0x01)) { channelSw(2,CHNA,&amp;amp;mixer2,!ENG); }&lt;br /&gt;
       if (ENA != (mixer2 &amp;amp; 0x02)) { channelSw(2,CHNB,&amp;amp;mixer2,!ENA); }&lt;br /&gt;
       if (ENC5 != (mixer2 &amp;amp; 0x04)) { channelSw(2,CHNC,&amp;amp;mixer2,!ENC5); }&lt;br /&gt;
During the main while loop, the motor flag variable is checked to see if the motor should be driven. If so, it enters a sequence where it uses timer3 to determine the current stage in the motor cycle, changing the outputs as needed.&lt;br /&gt;
       //Check for motor state changes&lt;br /&gt;
       switch (motorflag) {&lt;br /&gt;
          case 0: break;   //Do nothing (motor off)&lt;br /&gt;
          case 1:   //Start motor cycle&lt;br /&gt;
                WriteTimer3(0);   //Reset timer to 0&lt;br /&gt;
                IN1 = 0;          //Set H bridge to forward drive&lt;br /&gt;
                IN2 = 1;&lt;br /&gt;
                EN = 1;&lt;br /&gt;
                motorflag = 2;    //In drivedown cycle&lt;br /&gt;
                break;&lt;br /&gt;
          case 2:   //Drivedown cycle&lt;br /&gt;
                if (ReadTimer3() &amp;gt; DRIVETIME) {  //Wait for DRIVETIME to pass&lt;br /&gt;
                   WriteTimer3(0); //Reset timer to 0&lt;br /&gt;
                   motorflag = 3; //In driveup phase&lt;br /&gt;
                   IN2 = 0;       //Switch H bridge&lt;br /&gt;
                   IN1 = 1;&lt;br /&gt;
                }&lt;br /&gt;
                break;&lt;br /&gt;
          case 3:   //Driveup cycle&lt;br /&gt;
                if (ReadTimer3() &amp;gt; BACKTIME) {   //Wait for BACKTIME to pass&lt;br /&gt;
                   WriteTimer3(0);   //Reset timer to 0&lt;br /&gt;
                   motorflag = 4;    //In coast/wait phase&lt;br /&gt;
                   EN = 0;           //Turn off motor&lt;br /&gt;
                }&lt;br /&gt;
                break;&lt;br /&gt;
          case 4: //coast / wait cycle&lt;br /&gt;
                if (ReadTimer3() &amp;gt; WAITTIME) {&lt;br /&gt;
                   motorflag = 5; //more wait&lt;br /&gt;
                   WriteTimer3(0);&lt;br /&gt;
                }&lt;br /&gt;
                break;&lt;br /&gt;
          case 5:  //Additional wait cycle&lt;br /&gt;
                if (ReadTimer3() &amp;gt; WAITTIME) {&lt;br /&gt;
                   //Done!&lt;br /&gt;
                   motorflag = 0; //Motor off (can now be triggered again)&lt;br /&gt;
                }&lt;br /&gt;
                break;&lt;br /&gt;
          //Static wait cycles&lt;br /&gt;
          case 6:&lt;br /&gt;
                if (ReadTimer3() &amp;gt; WAITTIME) {&lt;br /&gt;
                   motorflag = 7; &lt;br /&gt;
                   WriteTimer3(0);&lt;br /&gt;
                }&lt;br /&gt;
                break;&lt;br /&gt;
          case 7:&lt;br /&gt;
                if (ReadTimer3() &amp;gt; WAITTIME) {&lt;br /&gt;
                   WriteTimer3(0);&lt;br /&gt;
                   motorflag = 8; &lt;br /&gt;
                }&lt;br /&gt;
                break;&lt;br /&gt;
          case 8:&lt;br /&gt;
                if (ReadTimer3() &amp;gt; WAITTIME) {&lt;br /&gt;
                   motorflag = 0; &lt;br /&gt;
                }&lt;br /&gt;
                break;&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
       Delayms(10);&lt;br /&gt;
    }&lt;br /&gt;
    return 0;&lt;br /&gt;
 }//end main&lt;br /&gt;
 &lt;br /&gt;
Write chip function, which is a support function that writes data to the music chips.&lt;br /&gt;
 /* write_chip: This writes the given byte &amp;quot;data&amp;quot; to the &amp;quot;address&amp;quot; (0 or 1)&lt;br /&gt;
                on a YMZ284. It is assumed that the chip select is already low */&lt;br /&gt;
 void write_chip(int address,int data) {&lt;br /&gt;
    //Write address&lt;br /&gt;
    portout(address);&lt;br /&gt;
    A0 = 0;     //select address mode&lt;br /&gt;
    Delayus(1); //setup time&lt;br /&gt;
    NWR = 0;    //Write cycle begin&lt;br /&gt;
    Delayus(1); //hold time&lt;br /&gt;
    NWR = 1;    //Write cycle end&lt;br /&gt;
   &lt;br /&gt;
    //Write  data&lt;br /&gt;
    portout(data);&lt;br /&gt;
    A0 = 1;     //select data mode&lt;br /&gt;
    Delayus(1); //setup time&lt;br /&gt;
    NWR = 0;    //write cycle begin&lt;br /&gt;
    Delayus(1); //hold time&lt;br /&gt;
    NWR = 1;    //write cycle end&lt;br /&gt;
 }&lt;br /&gt;
Port out function&lt;br /&gt;
 /* port_out: This function outputs a byte to the D0-D7 port */&lt;br /&gt;
 void portout(int byte) {&lt;br /&gt;
    // Note:  !! converts an integer expression to a boolean (1 or 0).&lt;br /&gt;
     D0 = !!(byte  &amp;amp; 1);&lt;br /&gt;
     D1 = !!(byte  &amp;amp; 2); &lt;br /&gt;
     D2 = !!(byte  &amp;amp; 4);   &lt;br /&gt;
     D3 = !!(byte  &amp;amp; 8);&lt;br /&gt;
     D4 = !!(byte  &amp;amp; 16);&lt;br /&gt;
     D5 = !!(byte  &amp;amp; 32); &lt;br /&gt;
     D6 = !!(byte  &amp;amp; 64);   &lt;br /&gt;
     D7 = !!(byte  &amp;amp; 128);&lt;br /&gt;
 }&lt;br /&gt;
Channel switch function, which writes a new mixer value to turn a channel on or off.&lt;br /&gt;
 /* channelSw: Generic channel switch function &lt;br /&gt;
               This writes a new mixer value to chip &amp;quot;chip&amp;quot;. The mixer value&lt;br /&gt;
               depends on &amp;quot;chn&amp;quot;, the channel turning on or off, &amp;quot;state&amp;quot; whether&lt;br /&gt;
               the channel is turning on or off, and &amp;quot;*mixer&amp;quot;, a pointer to&lt;br /&gt;
               the old mixer value. */&lt;br /&gt;
 void channelSw(int chip, int chn, int *mixer, int state) {&lt;br /&gt;
    /* Enable music chip */&lt;br /&gt;
    switch(chip) {&lt;br /&gt;
       case 1: CS1 = 0; break;&lt;br /&gt;
       case 2: CS2 = 0; break;&lt;br /&gt;
       case 3: CS3 = 0; break;&lt;br /&gt;
       case 4: CS4 = 0; break;&lt;br /&gt;
    }&lt;br /&gt;
    /* Write mixer value */&lt;br /&gt;
    /* This writes to channel 7 (mixer channel) the _new_ mixer value, which is&lt;br /&gt;
       the old mixer value with a particular bit flipped based on &amp;quot;chn&amp;quot; and &amp;quot;state&amp;quot; */&lt;br /&gt;
    write_chip(0x07, (*mixer) = state ? (*mixer) &amp;amp; (0xFF &amp;amp; ~chn) : (*mixer) | chn);&lt;br /&gt;
    /* Disable chip */&lt;br /&gt;
    switch(chip) {&lt;br /&gt;
       case 1: CS1 = 1; break;&lt;br /&gt;
       case 2: CS2 = 1; break;&lt;br /&gt;
       case 3: CS3 = 1; break;&lt;br /&gt;
       case 4: CS4 = 1; break;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
Interrupt functions&lt;br /&gt;
 //External interrupt for motor driving&lt;br /&gt;
 void __ISR(_EXTERNAL_3_VECTOR, ipl6) _SingleVectorHandler(void) {&lt;br /&gt;
    if (motorflag == 0) {&lt;br /&gt;
       motorflag = 1;   //Start motor cycle&lt;br /&gt;
    }&lt;br /&gt;
    //If on already, do nothing&lt;br /&gt;
    mINT3ClearIntFlag();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 //External interrupt for motor waiting&lt;br /&gt;
 void __ISR(_EXTERNAL_4_VECTOR, ipl5) _SingleVectorHandler2(void) {&lt;br /&gt;
    if (motorflag == 0) {&lt;br /&gt;
       WriteTimer3(0);&lt;br /&gt;
       motorflag = 6;   //Start wait cycle&lt;br /&gt;
    }&lt;br /&gt;
    //If on already, do nothing&lt;br /&gt;
    mINT4ClearIntFlag();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Results= &lt;br /&gt;
&lt;br /&gt;
The project turned out to be a decent success overall.  The tilt switches worked very well for activating the music tones, but the heart rate monitor was less successful.  One of the biggest issues was getting rid of very low frequency drift in the signal that made the peaks sometimes as high as 10V, but other times less 1V.  Dealing with this fluctuation in peak size was the most difficult part of signal processing.  As mentioned above, we tried FFT in order to establish a steady heart rate, but could not get the resolution we desired, thus we abandoned the idea.  Also, for optimum functioning, the system had to be tuned for some users, adjusting the potentiometers (shown in the circuit diagram above) until a robust signal was attained.&lt;br /&gt;
&lt;br /&gt;
Furthermore, using the heart rate monitor was complicated by its sensitivity to movement artifact.  The system worked best when one user provided the heartbeat, and another user wore the music suit.  The system was decent at accounting for larger movements initiated from the shoulder or elbow, as long as the wrist and fingers stayed relatively rigid along with the arm.  However, movement initiated from the wrist or finger caused a large influx of noise into the system, requiring at least 5 seconds to smooth out once the user stopped those movements.&lt;br /&gt;
&lt;br /&gt;
The best result was that using the device was just plain fun.  We each had a great time moving around and hearing the fun sounds that resulted, or if we were feeling ambitious, we would try to play a simple melody like &amp;quot;Mary Had a Little Lamb.&amp;quot;  It was rather difficult to play anything more complicated than that because it required lots of body control and precise movements, and we were also constrained by having only the notes of the pentatonic scale at our disposal.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Reflections=&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Successes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*tilt switches activated each of the six notes when they were supposed to&lt;br /&gt;
**the YMZ284 chips were able to produce all six notes at the same time, allowing cool chords&lt;br /&gt;
*tones were in tune and formed fun melodies and chords as the body moved&lt;br /&gt;
*drum actuator hit the drum reliably and with good tone&lt;br /&gt;
*heart rate monitor usually showed clear peaks on a scope, but was finnicky in practice&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Room for improvements&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*tilt switches sometimes bounced, so the beginning and particularly the ends of the notes sounded jagged&lt;br /&gt;
**&amp;#039;&amp;#039;Possible Solution:&amp;#039;&amp;#039; add a low pass filter to the switch signal so that the signal ramps up and down.  &lt;br /&gt;
*the shoulder straps sometimes needed adjustments for each individual in order for the sensors to be in the correct position&lt;br /&gt;
**&amp;#039;&amp;#039;Possible Solution:&amp;#039;&amp;#039; create straps with more constraints, such as a strap across the chest as well as the back, to ensure that the sensors are in the correct positions and orientations. &lt;br /&gt;
*the straps could be easier to put on&lt;br /&gt;
**&amp;#039;&amp;#039;Possible Solution:&amp;#039;&amp;#039; maybe integrate them into a single garment, like a onesie&lt;br /&gt;
*the heart rate monitor was not reliable.  Low frequency drift made setting a comparison level nearly impossible, especially for more than one specific user.&lt;br /&gt;
**&amp;#039;&amp;#039;Possible Solution:&amp;#039;&amp;#039; improve FFT method so that desired resolution could be achieved in a short sample period (around 10 seconds).  Or implement a complicated peak detection algorithm on the PIC.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We believe this project, if expanded and refined, could have some really fun and useful applications.  The music suit idea is not new, but it is not widespread.  A device like this could be useful in music therapy, where therapists use music to help patients heal from a variety of maladies.  This could be an engaging interactive exhibit in a children&amp;#039;s museum.  A suit could also be used to help improve proprioception, or movement awareness, in developmentally disable patients who have difficulty coordinating certain movements and can lack feedback to help them learn.&lt;br /&gt;
&lt;br /&gt;
Making music with this device is just plain fun, and it gives a whole new meaning to the idea of &amp;#039;playing with your heart.&amp;#039;&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16126</id>
		<title>PIC32MX: I2C Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16126"/>
		<updated>2010-02-16T04:45:55Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Slave Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32s to communicate via I2C.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf I2C] is pronounced &amp;quot;I squared C&amp;quot; and stands for Inter-Integrated Circuit.  This protocol was designed by Phillips Semiconductors around 1992 to allow easy communication between components on the same circuit board and can achieve transfer rates of up to 400 kbit/sec.  I2C is a 2 line (plus common ground) communication method for one master device to control up to 112 slave devices.  While it is possible to have multiple masters on the same I2C bus, this page will only deal with a one master configuration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Basic Operation&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two lines are named SCL1 and SDA1 where SCL1 is the CLock line and SDA1 is the DAta line.  I2C requires the lines to be high unless the master or the slave is pulling the line down, so you will need to use pull up resistors on both the clock and data lines.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;2&amp;quot; cellpadding=&amp;quot;3&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Line!! Name!! Description&lt;br /&gt;
|-&lt;br /&gt;
|SCL1||Serial Clock Line||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDA1||Data Line||The single data line for sending/receiving&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The data line is read each time the clock line goes high, as shown in the diagram below:&lt;br /&gt;
&lt;br /&gt;
[[image:I2C_Data_Transfer.jpg|center]]&lt;br /&gt;
&lt;br /&gt;
* S - As the master pulls down the data line, the falling edge of the data line signifies the start condition.&lt;br /&gt;
* P - As the master allows the data line to rise, the rising edge of the data line signifies the stop condition.&lt;br /&gt;
* B - Individual bits being transferred.&lt;br /&gt;
&lt;br /&gt;
The data line can only change while the clock line is low.  The data line is only read when the clock line is high and therefore 1 bit can be transferred per cycle of the clock line.&lt;br /&gt;
&lt;br /&gt;
The master initially sends a start bit followed by the 7-bit address of the slave it wishes to communicate with. The next bit represents whether it wishes to write(0) to or read(1) from the slave.&lt;br /&gt;
&lt;br /&gt;
The slave then responds with an acknowledge bit. The transmission continues according to whether the master is attempting to read to or write from the device.&lt;br /&gt;
&lt;br /&gt;
The master then ends the transmission with a stop bit.  Another option is to send another start bit to continue the transfer in a combined message.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
&lt;br /&gt;
[[Image:I2C_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
In the code below, the master sends one of two bytes (0xAA or 0x23) to the slave at address 0x40. The master then receives a byte back from the slave. If the slave receives a 0xAA, it lights its LEDs.&lt;br /&gt;
When the master begins sending, the slave triggers an interrupt (if the slave address is a match), which does one of four actions based on whether the master is reading or writing and whether the last byte was a data or address byte. As mentioned, when the master is writing the slave stores the byte, and when the master is reading the slave sends back the last stored byte.&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /********************************************************&lt;br /&gt;
  * I2C_Master.c: Master code for I2C communication.	*&lt;br /&gt;
  * 	Both PICS use I2C1 module to send/ receive data.*&lt;br /&gt;
  *	The master sends different values to a slave,   *&lt;br /&gt;
  *	which uses an interrupt to respond accordingly.	*&lt;br /&gt;
  *							*&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards	*&lt;br /&gt;
  ********************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West		*&lt;br /&gt;
  * ME333 Winter 2010					*&lt;br /&gt;
  * File Created: 	05-FEB-2010			*&lt;br /&gt;
  * Last Modified: 	14-FEB-2010			*&lt;br /&gt;
  *******************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 #pragma config FPBDIV = DIV_1				//Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000      					//Frequency of (I2C) serial clock&lt;br /&gt;
  &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )                 //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 #define INPUT_A9       PORTAbits.RA9&lt;br /&gt;
 #define INPUT_A10       PORTAbits.RA10&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //function declaration for sending data and selecting slave address&lt;br /&gt;
 void SendData(int,unsigned int);&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
 This function is a delay function, causing the program to wait for approximately 4 * cnt cycles&lt;br /&gt;
 1 cycle is 1/SYSCLK seconds.&lt;br /&gt;
 */&lt;br /&gt;
 void i2c_wait(unsigned int cnt)&lt;br /&gt;
 {&lt;br /&gt;
 	while(--cnt)&lt;br /&gt;
 	{&lt;br /&gt;
 		Nop();&lt;br /&gt;
 		Nop();&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
     // Configure the proper PB frequency and the number of wait states.&lt;br /&gt;
 	SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
 	// Set all analog pins to be digital I/O&lt;br /&gt;
    	AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
 	//Setup TRIS bits for switches and I2C pins&lt;br /&gt;
 	TRISAbits.TRISA14=0;&lt;br /&gt;
 	TRISAbits.TRISA15=0;&lt;br /&gt;
 &lt;br /&gt;
 	//Initialize all of the LED pins&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
        mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
 	unsigned char SlaveAddress;   //Slave address variable to tell the master where to send the data.  &lt;br /&gt;
                                      //Will be re-assigned for multiple slaves.&lt;br /&gt;
 &lt;br /&gt;
 	//Enable I2C channel and set the baud rate to BRG_VAL)&lt;br /&gt;
 	OpenI2C1( I2C_EN, BRG_VAL );&lt;br /&gt;
 &lt;br /&gt;
 	int rcv;			//For received data&lt;br /&gt;
 &lt;br /&gt;
 	//While loop to test LED functionality &lt;br /&gt;
 	while(1) {&lt;br /&gt;
 		if (swProgram) {				//First button pressed&lt;br /&gt;
 			while(swProgram) { Nop(); }	//Wait for release&lt;br /&gt;
 			mLED_2_Toggle();			//Toggle LED2&lt;br /&gt;
 			SendData(0xAA,0x40);  			//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);			//Receives data from address 0x40				&lt;br /&gt;
 			Delayms(100);&lt;br /&gt;
 		}&lt;br /&gt;
 		if (swUser) {				//Second button pressed&lt;br /&gt;
 			while(swUser) { Nop(); }&lt;br /&gt;
 			mLED_1_Toggle();			//Toggle LED1&lt;br /&gt;
 			SendData(0x23,0x40);    		//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);		        //Receives data from address 0x40			&lt;br /&gt;
 			Delayms(100);		&lt;br /&gt;
 		}&lt;br /&gt;
 	}//while loop ending&lt;br /&gt;
 &lt;br /&gt;
 	return 0;&lt;br /&gt;
 }  //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /*****************************************************&lt;br /&gt;
  * RcvData(unsigned int address)		     *&lt;br /&gt;
  *					  	     *&lt;br /&gt;
  * Gets a byte of data from I2C slave device at      *&lt;br /&gt;
  *  ADDRESS.					     *&lt;br /&gt;
  *						     *&lt;br /&gt;
  * Returns: Received data			     *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 int RcvData(unsigned int address) {&lt;br /&gt;
 	StartI2C1();				//Send line start condition&lt;br /&gt;
 	IdleI2C1();			        //Wait to complete&lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 1);	//Write out slave address OR 1 (read command)&lt;br /&gt;
 	IdleI2C1();				//Wait to complete&lt;br /&gt;
 	int rcv = MasterReadI2C1();		//Read in a value&lt;br /&gt;
 	StopI2C1();				//Send line stop condition&lt;br /&gt;
 	IdleI2C1();				//Wait co complete&lt;br /&gt;
 	return rcv;				//Return read value&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /***************************************************&lt;br /&gt;
  * SendData(int data, unsigned int address)        *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Sends a byte of data (DATA) over the I2C line   *&lt;br /&gt;
  *	to I2C address ADDRESS			    *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Returns: nothing				    *&lt;br /&gt;
  ***************************************************/&lt;br /&gt;
 void SendData (int data, unsigned int address){&lt;br /&gt;
 	StartI2C1();	        //Send the Start Bit&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 0);  //Sends the slave address over the I2C line.  This must happen first so the &lt;br /&gt;
                                              //proper slave is selected to receive data.&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1(data);  //Sends data byte over I2C line&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	StopI2C1();	        //Send the Stop condition&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 } //end function&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
 	}&lt;br /&gt;
 	CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
 /***********************************************************************&lt;br /&gt;
  * PIC32 I2C Slave Code                &lt;br /&gt;
  ***********************************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;GenericTypeDefs.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;Compiler.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000&lt;br /&gt;
 #define BRG_VAL 	((PBCLK/2/Fsck)-2)&lt;br /&gt;
 &lt;br /&gt;
 // this is the modules Slave Address&lt;br /&gt;
 #define SLAVE_ADDRESS 0x40&lt;br /&gt;
 &lt;br /&gt;
 // volatile variables to hold the switch and led states&lt;br /&gt;
 volatile unsigned char dataRead = 0;&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	InitI2C&lt;br /&gt;
 //&lt;br /&gt;
 // 	Perform initialisation of the I2C module to operate as a slave&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void InitI2C(void)&lt;br /&gt;
 {&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	&lt;br /&gt;
 	// Enable the I2C module with clock stretching enabled&lt;br /&gt;
 	OpenI2C1(I2C_ON | I2C_7BIT_ADD | I2C_STR_EN, BRG_VAL);&lt;br /&gt;
 	&lt;br /&gt;
 	// set the address of the slave module, address matching is with bits&lt;br /&gt;
 	// 7:1 of the message compared with bits 6:0 of the ADD SFR so we&lt;br /&gt;
 	// need to shift the desired address 1 bit. &lt;br /&gt;
 	I2C1ADD = SLAVE_ADDRESS; // &amp;gt;&amp;gt; 1;&lt;br /&gt;
 	I2C1MSK = 0;&lt;br /&gt;
 	&lt;br /&gt;
 	// configure the interrupt priority for the I2C peripheral&lt;br /&gt;
 	mI2C1SetIntPriority(I2C_INT_PRI_3 | I2C_INT_SLAVE);&lt;br /&gt;
 &lt;br /&gt;
 	// clear pending interrupts and enable I2C interrupts&lt;br /&gt;
 	mI2C1SClearIntFlag();&lt;br /&gt;
 	EnableIntSI2C1;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	main routine&lt;br /&gt;
 // &lt;br /&gt;
 //	This code example demonstrates using the PIC32 as an I2C slave&lt;br /&gt;
 //	&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 int main (void)&lt;br /&gt;
 {&lt;br /&gt;
 	// set for 80MHz operation&lt;br /&gt;
 	SYSTEMConfigPerformance(SYSCLK);&lt;br /&gt;
 	// set the Pbus to be 40000000&lt;br /&gt;
 	mOSCSetPBDIV(OSC_PB_DIV_2);&lt;br /&gt;
 	// disable the JTAG port&lt;br /&gt;
 	mJTAGPortEnable(0);&lt;br /&gt;
 	// enable interrupts&lt;br /&gt;
 	INTEnableSystemMultiVectoredInt();&lt;br /&gt;
 	&lt;br /&gt;
 	InitI2C();&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
 &lt;br /&gt;
 	// main loop&lt;br /&gt;
 	while (1) {&lt;br /&gt;
 		/* If global variable &amp;quot;dataRead&amp;quot; is set high during interrupt, turn on all LEDs */&lt;br /&gt;
 		if (dataRead == 0xAA)&lt;br /&gt;
 		{&lt;br /&gt;
 			mLED_0_On();&lt;br /&gt;
 			mLED_1_On();&lt;br /&gt;
 			mLED_2_On();			&lt;br /&gt;
 			mLED_3_On();			&lt;br /&gt;
 			&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 // Slave I2C interrupt handler&lt;br /&gt;
 // This handler is called when a qualifying I2C events occurs&lt;br /&gt;
 // this means that as well as Slave events &lt;br /&gt;
 // Master and Bus Collision events will also trigger this handler.&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void __ISR(_I2C_1_VECTOR, ipl3) _SlaveI2CHandler(void)&lt;br /&gt;
 {&lt;br /&gt;
 	mLED_1_On();&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	static unsigned int dIndex;&lt;br /&gt;
 	&lt;br /&gt;
 	// check for MASTER and Bus events and respond accordingly&lt;br /&gt;
 	if (IFS0bits.I2C1MIF == 1) {&lt;br /&gt;
 		mI2C1MClearIntFlag();&lt;br /&gt;
 		return;		&lt;br /&gt;
 	}&lt;br /&gt;
 	if (IFS0bits.I2C1BIF == 1) {&lt;br /&gt;
 		mI2C1BClearIntFlag();&lt;br /&gt;
 		return;&lt;br /&gt;
 	}&lt;br /&gt;
 	mLED_1_Off();&lt;br /&gt;
 	mLED_2_On();&lt;br /&gt;
 	&lt;br /&gt;
 	// handle the incoming message&lt;br /&gt;
 	if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 0 --&amp;gt; indicates last byte was address  &lt;br /&gt;
 		&lt;br /&gt;
 		// reset any state variables needed by a message sequence	&lt;br /&gt;
 		// perform a dummy read of the address&lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release the clock&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		// writing data to our module, just store it in adcSample&lt;br /&gt;
 		dataRead = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release clock stretch bit&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is output from slave&lt;br /&gt;
 		// D/A bit = 0 --&amp;gt; indicates last byte was address&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// read of the slave device, read the address &lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		dIndex = 0;&lt;br /&gt;
 		SlaveWriteI2C1(dataRead);&lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is output from slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		&lt;br /&gt;
 		// output the data until the MASTER terminates the&lt;br /&gt;
 		// transfer with a NACK, continuing reads return 0&lt;br /&gt;
 		if (dIndex == 0) {&lt;br /&gt;
 			SlaveWriteI2C1(dataRead);&lt;br /&gt;
 			dIndex++;&lt;br /&gt;
 		} else&lt;br /&gt;
 			SlaveWriteI2C1(0);&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	// finally clear the slave interrupt flag&lt;br /&gt;
 	mI2C1SClearIntFlag();		&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16125</id>
		<title>PIC32MX: I2C Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16125"/>
		<updated>2010-02-16T04:44:53Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Slave Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32s to communicate via I2C.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf I2C] is pronounced &amp;quot;I squared C&amp;quot; and stands for Inter-Integrated Circuit.  This protocol was designed by Phillips Semiconductors around 1992 to allow easy communication between components on the same circuit board and can achieve transfer rates of up to 400 kbit/sec.  I2C is a 2 line (plus common ground) communication method for one master device to control up to 112 slave devices.  While it is possible to have multiple masters on the same I2C bus, this page will only deal with a one master configuration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Basic Operation&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two lines are named SCL1 and SDA1 where SCL1 is the CLock line and SDA1 is the DAta line.  I2C requires the lines to be high unless the master or the slave is pulling the line down, so you will need to use pull up resistors on both the clock and data lines.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;2&amp;quot; cellpadding=&amp;quot;3&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Line!! Name!! Description&lt;br /&gt;
|-&lt;br /&gt;
|SCL1||Serial Clock Line||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDA1||Data Line||The single data line for sending/receiving&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The data line is read each time the clock line goes high, as shown in the diagram below:&lt;br /&gt;
&lt;br /&gt;
[[image:I2C_Data_Transfer.jpg|center]]&lt;br /&gt;
&lt;br /&gt;
* S - As the master pulls down the data line, the falling edge of the data line signifies the start condition.&lt;br /&gt;
* P - As the master allows the data line to rise, the rising edge of the data line signifies the stop condition.&lt;br /&gt;
* B - Individual bits being transferred.&lt;br /&gt;
&lt;br /&gt;
The data line can only change while the clock line is low.  The data line is only read when the clock line is high and therefore 1 bit can be transferred per cycle of the clock line.&lt;br /&gt;
&lt;br /&gt;
The master initially sends a start bit followed by the 7-bit address of the slave it wishes to communicate with. The next bit represents whether it wishes to write(0) to or read(1) from the slave.&lt;br /&gt;
&lt;br /&gt;
The slave then responds with an acknowledge bit. The transmission continues according to whether the master is attempting to read to or write from the device.&lt;br /&gt;
&lt;br /&gt;
The master then ends the transmission with a stop bit.  Another option is to send another start bit to continue the transfer in a combined message.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
&lt;br /&gt;
[[Image:I2C_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
In the code below, the master sends one of two bytes (0xAA or 0x23) to the slave at address 0x40. The master then receives a byte back from the slave. If the slave receives a 0xAA, it lights its LEDs.&lt;br /&gt;
When the master begins sending, the slave triggers an interrupt (if the slave address is a match), which does one of four actions based on whether the master is reading or writing and whether the last byte was a data or address byte. As mentioned, when the master is writing the slave stores the byte, and when the master is reading the slave sends back the last stored byte.&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /********************************************************&lt;br /&gt;
  * I2C_Master.c: Master code for I2C communication.	*&lt;br /&gt;
  * 	Both PICS use I2C1 module to send/ receive data.*&lt;br /&gt;
  *	The master sends different values to a slave,   *&lt;br /&gt;
  *	which uses an interrupt to respond accordingly.	*&lt;br /&gt;
  *							*&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards	*&lt;br /&gt;
  ********************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West		*&lt;br /&gt;
  * ME333 Winter 2010					*&lt;br /&gt;
  * File Created: 	05-FEB-2010			*&lt;br /&gt;
  * Last Modified: 	14-FEB-2010			*&lt;br /&gt;
  *******************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 #pragma config FPBDIV = DIV_1				//Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000      					//Frequency of (I2C) serial clock&lt;br /&gt;
  &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )                 //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 #define INPUT_A9       PORTAbits.RA9&lt;br /&gt;
 #define INPUT_A10       PORTAbits.RA10&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //function declaration for sending data and selecting slave address&lt;br /&gt;
 void SendData(int,unsigned int);&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
 This function is a delay function, causing the program to wait for approximately 4 * cnt cycles&lt;br /&gt;
 1 cycle is 1/SYSCLK seconds.&lt;br /&gt;
 */&lt;br /&gt;
 void i2c_wait(unsigned int cnt)&lt;br /&gt;
 {&lt;br /&gt;
 	while(--cnt)&lt;br /&gt;
 	{&lt;br /&gt;
 		Nop();&lt;br /&gt;
 		Nop();&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
     // Configure the proper PB frequency and the number of wait states.&lt;br /&gt;
 	SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
 	// Set all analog pins to be digital I/O&lt;br /&gt;
    	AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
 	//Setup TRIS bits for switches and I2C pins&lt;br /&gt;
 	TRISAbits.TRISA14=0;&lt;br /&gt;
 	TRISAbits.TRISA15=0;&lt;br /&gt;
 &lt;br /&gt;
 	//Initialize all of the LED pins&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
        mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
 	unsigned char SlaveAddress;   //Slave address variable to tell the master where to send the data.  &lt;br /&gt;
                                      //Will be re-assigned for multiple slaves.&lt;br /&gt;
 &lt;br /&gt;
 	//Enable I2C channel and set the baud rate to BRG_VAL)&lt;br /&gt;
 	OpenI2C1( I2C_EN, BRG_VAL );&lt;br /&gt;
 &lt;br /&gt;
 	int rcv;			//For received data&lt;br /&gt;
 &lt;br /&gt;
 	//While loop to test LED functionality &lt;br /&gt;
 	while(1) {&lt;br /&gt;
 		if (swProgram) {				//First button pressed&lt;br /&gt;
 			while(swProgram) { Nop(); }	//Wait for release&lt;br /&gt;
 			mLED_2_Toggle();			//Toggle LED2&lt;br /&gt;
 			SendData(0xAA,0x40);  			//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);			//Receives data from address 0x40				&lt;br /&gt;
 			Delayms(100);&lt;br /&gt;
 		}&lt;br /&gt;
 		if (swUser) {				//Second button pressed&lt;br /&gt;
 			while(swUser) { Nop(); }&lt;br /&gt;
 			mLED_1_Toggle();			//Toggle LED1&lt;br /&gt;
 			SendData(0x23,0x40);    		//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);		        //Receives data from address 0x40			&lt;br /&gt;
 			Delayms(100);		&lt;br /&gt;
 		}&lt;br /&gt;
 	}//while loop ending&lt;br /&gt;
 &lt;br /&gt;
 	return 0;&lt;br /&gt;
 }  //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /*****************************************************&lt;br /&gt;
  * RcvData(unsigned int address)		     *&lt;br /&gt;
  *					  	     *&lt;br /&gt;
  * Gets a byte of data from I2C slave device at      *&lt;br /&gt;
  *  ADDRESS.					     *&lt;br /&gt;
  *						     *&lt;br /&gt;
  * Returns: Received data			     *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 int RcvData(unsigned int address) {&lt;br /&gt;
 	StartI2C1();				//Send line start condition&lt;br /&gt;
 	IdleI2C1();			        //Wait to complete&lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 1);	//Write out slave address OR 1 (read command)&lt;br /&gt;
 	IdleI2C1();				//Wait to complete&lt;br /&gt;
 	int rcv = MasterReadI2C1();		//Read in a value&lt;br /&gt;
 	StopI2C1();				//Send line stop condition&lt;br /&gt;
 	IdleI2C1();				//Wait co complete&lt;br /&gt;
 	return rcv;				//Return read value&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /***************************************************&lt;br /&gt;
  * SendData(int data, unsigned int address)        *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Sends a byte of data (DATA) over the I2C line   *&lt;br /&gt;
  *	to I2C address ADDRESS			    *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Returns: nothing				    *&lt;br /&gt;
  ***************************************************/&lt;br /&gt;
 void SendData (int data, unsigned int address){&lt;br /&gt;
 	StartI2C1();	        //Send the Start Bit&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 0);  //Sends the slave address over the I2C line.  This must happen first so the &lt;br /&gt;
                                              //proper slave is selected to receive data.&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1(data);  //Sends data byte over I2C line&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	StopI2C1();	        //Send the Stop condition&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 } //end function&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
 	}&lt;br /&gt;
 	CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
 /***********************************************************************&lt;br /&gt;
  * PIC32 I2C Slave Code                &lt;br /&gt;
  ***********************************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;GenericTypeDefs.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;Compiler.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000&lt;br /&gt;
 #define BRG_VAL 	((PBCLK/2/Fsck)-2)&lt;br /&gt;
 &lt;br /&gt;
 // this is the modules Slave Address&lt;br /&gt;
 #define SLAVE_ADDRESS 0x40&lt;br /&gt;
 &lt;br /&gt;
 // volatile variables to hold the switch and led states&lt;br /&gt;
 volatile unsigned char dataRead = 0;&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	InitI2C&lt;br /&gt;
 //&lt;br /&gt;
 // 	Perform initialisation of the I2C module to operate as a slave&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void InitI2C(void)&lt;br /&gt;
 {&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	&lt;br /&gt;
 	// Enable the I2C module with clock stretching enabled&lt;br /&gt;
 	OpenI2C1(I2C_ON | I2C_7BIT_ADD | I2C_STR_EN, BRG_VAL);&lt;br /&gt;
 	&lt;br /&gt;
 	// set the address of the slave module, address matching is with bits&lt;br /&gt;
 	// 7:1 of the message compared with bits 6:0 of the ADD SFR so we&lt;br /&gt;
 	// need to shift the desired address 1 bit. &lt;br /&gt;
 	I2C1ADD = SLAVE_ADDRESS; // &amp;gt;&amp;gt; 1;&lt;br /&gt;
 	I2C1MSK = 0;&lt;br /&gt;
 	&lt;br /&gt;
 	// configure the interrupt priority for the I2C peripheral&lt;br /&gt;
 	mI2C1SetIntPriority(I2C_INT_PRI_3 | I2C_INT_SLAVE);&lt;br /&gt;
 &lt;br /&gt;
 	// clear pending interrupts and enable I2C interrupts&lt;br /&gt;
 	mI2C1SClearIntFlag();&lt;br /&gt;
 	EnableIntSI2C1;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	main routine&lt;br /&gt;
 // &lt;br /&gt;
 //	This code example demonstrates using the PIC32 as an I2C slave&lt;br /&gt;
 //	&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 int main (void)&lt;br /&gt;
 {&lt;br /&gt;
 	// set for 80MHz operation&lt;br /&gt;
 	SYSTEMConfigPerformance(SYSCLK);&lt;br /&gt;
 	// set the Pbus to be 40000000&lt;br /&gt;
 	mOSCSetPBDIV(OSC_PB_DIV_2);&lt;br /&gt;
 	// disable the JTAG port&lt;br /&gt;
 	mJTAGPortEnable(0);&lt;br /&gt;
 	// enable interrupts&lt;br /&gt;
 	INTEnableSystemMultiVectoredInt();&lt;br /&gt;
 	&lt;br /&gt;
 	InitI2C();&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
 &lt;br /&gt;
 	// main loop&lt;br /&gt;
 	while (1) {&lt;br /&gt;
 		/* If global variable &amp;quot;dataRead&amp;quot; is set high during interrupt, turn on all LEDs */&lt;br /&gt;
 		if (dataRead == 0xAA)&lt;br /&gt;
 		{&lt;br /&gt;
 			mLED_0_On();&lt;br /&gt;
 			mLED_1_On();&lt;br /&gt;
 			mLED_2_On();			&lt;br /&gt;
 			mLED_3_On();			&lt;br /&gt;
 			&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 // Slave I2C interrupt handler&lt;br /&gt;
 // This handler is called when a qualifying I2C events occurs&lt;br /&gt;
 // this means that as well as Slave events &lt;br /&gt;
 // Master and Bus Collision events will also trigger this handler.&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void __ISR(_I2C_1_VECTOR, ipl3) _SlaveI2CHandler(void)&lt;br /&gt;
 {&lt;br /&gt;
 	mLED_1_On();&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	static unsigned int dIndex;&lt;br /&gt;
 	&lt;br /&gt;
 	// check for MASTER and Bus events and respond accordingly&lt;br /&gt;
 	if (IFS0bits.I2C1MIF == 1) {&lt;br /&gt;
 		mI2C1MClearIntFlag();&lt;br /&gt;
 		return;		&lt;br /&gt;
 	}&lt;br /&gt;
 	if (IFS0bits.I2C1BIF == 1) {&lt;br /&gt;
 		mI2C1BClearIntFlag();&lt;br /&gt;
 		return;&lt;br /&gt;
 	}&lt;br /&gt;
 	mLED_1_Off();&lt;br /&gt;
 	mLED_2_On();&lt;br /&gt;
 	&lt;br /&gt;
 	// handle the incoming message&lt;br /&gt;
 	if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 0 --&amp;gt; indicates last byte was address  &lt;br /&gt;
 		&lt;br /&gt;
 		// reset any state variables needed by a message sequence	&lt;br /&gt;
 		// perform a dummy read of the address&lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release the clock&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		// writing data to our module, just store it in adcSample&lt;br /&gt;
 		dataRead = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release clock stretch bit&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is output from slave&lt;br /&gt;
 		// D/A bit = 0 --&amp;gt; indicates last byte was address&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// read of the slave device, read the address &lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		dIndex = 0;&lt;br /&gt;
 		SlaveWriteI2C1(dataRead);&lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		&lt;br /&gt;
 		// output the data until the MASTER terminates the&lt;br /&gt;
 		// transfer with a NACK, continuing reads return 0&lt;br /&gt;
 		if (dIndex == 0) {&lt;br /&gt;
 			SlaveWriteI2C1(dataRead);&lt;br /&gt;
 			dIndex++;&lt;br /&gt;
 		} else&lt;br /&gt;
 			SlaveWriteI2C1(0);&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	// finally clear the slave interrupt flag&lt;br /&gt;
 	mI2C1SClearIntFlag();		&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16124</id>
		<title>PIC32MX: I2C Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16124"/>
		<updated>2010-02-16T04:39:26Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32s to communicate via I2C.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf I2C] is pronounced &amp;quot;I squared C&amp;quot; and stands for Inter-Integrated Circuit.  This protocol was designed by Phillips Semiconductors around 1992 to allow easy communication between components on the same circuit board and can achieve transfer rates of up to 400 kbit/sec.  I2C is a 2 line (plus common ground) communication method for one master device to control up to 112 slave devices.  While it is possible to have multiple masters on the same I2C bus, this page will only deal with a one master configuration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Basic Operation&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two lines are named SCL1 and SDA1 where SCL1 is the CLock line and SDA1 is the DAta line.  I2C requires the lines to be high unless the master or the slave is pulling the line down, so you will need to use pull up resistors on both the clock and data lines.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;2&amp;quot; cellpadding=&amp;quot;3&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Line!! Name!! Description&lt;br /&gt;
|-&lt;br /&gt;
|SCL1||Serial Clock Line||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDA1||Data Line||The single data line for sending/receiving&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The data line is read each time the clock line goes high, as shown in the diagram below:&lt;br /&gt;
&lt;br /&gt;
[[image:I2C_Data_Transfer.jpg|center]]&lt;br /&gt;
&lt;br /&gt;
* S - As the master pulls down the data line, the falling edge of the data line signifies the start condition.&lt;br /&gt;
* P - As the master allows the data line to rise, the rising edge of the data line signifies the stop condition.&lt;br /&gt;
* B - Individual bits being transferred.&lt;br /&gt;
&lt;br /&gt;
The data line can only change while the clock line is low.  The data line is only read when the clock line is high and therefore 1 bit can be transferred per cycle of the clock line.&lt;br /&gt;
&lt;br /&gt;
The master initially sends a start bit followed by the 7-bit address of the slave it wishes to communicate with. The next bit represents whether it wishes to write(0) to or read(1) from the slave.&lt;br /&gt;
&lt;br /&gt;
The slave then responds with an acknowledge bit. The transmission continues according to whether the master is attempting to read to or write from the device.&lt;br /&gt;
&lt;br /&gt;
The master then ends the transmission with a stop bit.  Another option is to send another start bit to continue the transfer in a combined message.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
&lt;br /&gt;
[[Image:I2C_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
In the code below, the master sends one of two bytes (0xAA or 0x23) to the slave at address 0x40. The master then receives a byte back from the slave. If the slave receives a 0xAA, it lights its LEDs.&lt;br /&gt;
When the master begins sending, the slave triggers an interrupt (if the slave address is a match), which does one of four actions based on whether the master is reading or writing and whether the last byte was a data or address byte. As mentioned, when the master is writing the slave stores the byte, and when the master is reading the slave sends back the last stored byte.&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /********************************************************&lt;br /&gt;
  * I2C_Master.c: Master code for I2C communication.	*&lt;br /&gt;
  * 	Both PICS use I2C1 module to send/ receive data.*&lt;br /&gt;
  *	The master sends different values to a slave,   *&lt;br /&gt;
  *	which uses an interrupt to respond accordingly.	*&lt;br /&gt;
  *							*&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards	*&lt;br /&gt;
  ********************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West		*&lt;br /&gt;
  * ME333 Winter 2010					*&lt;br /&gt;
  * File Created: 	05-FEB-2010			*&lt;br /&gt;
  * Last Modified: 	14-FEB-2010			*&lt;br /&gt;
  *******************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 #pragma config FPBDIV = DIV_1				//Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000      					//Frequency of (I2C) serial clock&lt;br /&gt;
  &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )                 //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 #define INPUT_A9       PORTAbits.RA9&lt;br /&gt;
 #define INPUT_A10       PORTAbits.RA10&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //function declaration for sending data and selecting slave address&lt;br /&gt;
 void SendData(int,unsigned int);&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
 This function is a delay function, causing the program to wait for approximately 4 * cnt cycles&lt;br /&gt;
 1 cycle is 1/SYSCLK seconds.&lt;br /&gt;
 */&lt;br /&gt;
 void i2c_wait(unsigned int cnt)&lt;br /&gt;
 {&lt;br /&gt;
 	while(--cnt)&lt;br /&gt;
 	{&lt;br /&gt;
 		Nop();&lt;br /&gt;
 		Nop();&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
     // Configure the proper PB frequency and the number of wait states.&lt;br /&gt;
 	SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
 	// Set all analog pins to be digital I/O&lt;br /&gt;
    	AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
 	//Setup TRIS bits for switches and I2C pins&lt;br /&gt;
 	TRISAbits.TRISA14=0;&lt;br /&gt;
 	TRISAbits.TRISA15=0;&lt;br /&gt;
 &lt;br /&gt;
 	//Initialize all of the LED pins&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
        mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
 	unsigned char SlaveAddress;   //Slave address variable to tell the master where to send the data.  &lt;br /&gt;
                                      //Will be re-assigned for multiple slaves.&lt;br /&gt;
 &lt;br /&gt;
 	//Enable I2C channel and set the baud rate to BRG_VAL)&lt;br /&gt;
 	OpenI2C1( I2C_EN, BRG_VAL );&lt;br /&gt;
 &lt;br /&gt;
 	int rcv;			//For received data&lt;br /&gt;
 &lt;br /&gt;
 	//While loop to test LED functionality &lt;br /&gt;
 	while(1) {&lt;br /&gt;
 		if (swProgram) {				//First button pressed&lt;br /&gt;
 			while(swProgram) { Nop(); }	//Wait for release&lt;br /&gt;
 			mLED_2_Toggle();			//Toggle LED2&lt;br /&gt;
 			SendData(0xAA,0x40);  			//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);			//Receives data from address 0x40				&lt;br /&gt;
 			Delayms(100);&lt;br /&gt;
 		}&lt;br /&gt;
 		if (swUser) {				//Second button pressed&lt;br /&gt;
 			while(swUser) { Nop(); }&lt;br /&gt;
 			mLED_1_Toggle();			//Toggle LED1&lt;br /&gt;
 			SendData(0x23,0x40);    		//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);		        //Receives data from address 0x40			&lt;br /&gt;
 			Delayms(100);		&lt;br /&gt;
 		}&lt;br /&gt;
 	}//while loop ending&lt;br /&gt;
 &lt;br /&gt;
 	return 0;&lt;br /&gt;
 }  //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /*****************************************************&lt;br /&gt;
  * RcvData(unsigned int address)		     *&lt;br /&gt;
  *					  	     *&lt;br /&gt;
  * Gets a byte of data from I2C slave device at      *&lt;br /&gt;
  *  ADDRESS.					     *&lt;br /&gt;
  *						     *&lt;br /&gt;
  * Returns: Received data			     *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 int RcvData(unsigned int address) {&lt;br /&gt;
 	StartI2C1();				//Send line start condition&lt;br /&gt;
 	IdleI2C1();			        //Wait to complete&lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 1);	//Write out slave address OR 1 (read command)&lt;br /&gt;
 	IdleI2C1();				//Wait to complete&lt;br /&gt;
 	int rcv = MasterReadI2C1();		//Read in a value&lt;br /&gt;
 	StopI2C1();				//Send line stop condition&lt;br /&gt;
 	IdleI2C1();				//Wait co complete&lt;br /&gt;
 	return rcv;				//Return read value&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /***************************************************&lt;br /&gt;
  * SendData(int data, unsigned int address)        *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Sends a byte of data (DATA) over the I2C line   *&lt;br /&gt;
  *	to I2C address ADDRESS			    *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Returns: nothing				    *&lt;br /&gt;
  ***************************************************/&lt;br /&gt;
 void SendData (int data, unsigned int address){&lt;br /&gt;
 	StartI2C1();	        //Send the Start Bit&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 0);  //Sends the slave address over the I2C line.  This must happen first so the &lt;br /&gt;
                                              //proper slave is selected to receive data.&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1(data);  //Sends data byte over I2C line&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	StopI2C1();	        //Send the Stop condition&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 } //end function&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
 	}&lt;br /&gt;
 	CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
 /***********************************************************************&lt;br /&gt;
  * PIC32 I2C Slave Code                &lt;br /&gt;
  ***********************************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;GenericTypeDefs.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;Compiler.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000&lt;br /&gt;
 #define BRG_VAL 	((PBCLK/2/Fsck)-2)&lt;br /&gt;
 &lt;br /&gt;
 // this is the modules Slave Address&lt;br /&gt;
 #define SLAVE_ADDRESS 0x40&lt;br /&gt;
 &lt;br /&gt;
 // volatile variables to hold the switch and led states&lt;br /&gt;
 volatile unsigned char dataRead = 0;&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	InitI2C&lt;br /&gt;
 //&lt;br /&gt;
 // 	Perform initialisation of the I2C module to operate as a slave&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void InitI2C(void)&lt;br /&gt;
 {&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	&lt;br /&gt;
 	// Enable the I2C module with clock stretching enabled&lt;br /&gt;
 	OpenI2C1(I2C_ON | I2C_7BIT_ADD | I2C_STR_EN, BRG_VAL);&lt;br /&gt;
 	&lt;br /&gt;
 	// set the address of the slave module, address matching is with bits&lt;br /&gt;
 	// 7:1 of the message compared with bits 6:0 of the ADD SFR so we&lt;br /&gt;
 	// need to shift the desired address 1 bit. &lt;br /&gt;
 	I2C1ADD = SLAVE_ADDRESS; // &amp;gt;&amp;gt; 1;&lt;br /&gt;
 	I2C1MSK = 0;&lt;br /&gt;
 	&lt;br /&gt;
 	// configure the interrupt priority for the I2C peripheral&lt;br /&gt;
 	mI2C1SetIntPriority(I2C_INT_PRI_3 | I2C_INT_SLAVE);&lt;br /&gt;
 &lt;br /&gt;
 	// clear pending interrupts and enable I2C interrupts&lt;br /&gt;
 	mI2C1SClearIntFlag();&lt;br /&gt;
 	EnableIntSI2C1;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	main routine&lt;br /&gt;
 // &lt;br /&gt;
 //	This code example demonstrates using the PIC32 as an I2C slave&lt;br /&gt;
 //	&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 int main (void)&lt;br /&gt;
 {&lt;br /&gt;
 	// set for 80MHz operation&lt;br /&gt;
 	SYSTEMConfigPerformance(SYSCLK);&lt;br /&gt;
 	// set the Pbus to be 40000000&lt;br /&gt;
 	mOSCSetPBDIV(OSC_PB_DIV_2);&lt;br /&gt;
 	// disable the JTAG port&lt;br /&gt;
 	mJTAGPortEnable(0);&lt;br /&gt;
 	// enable interrupts&lt;br /&gt;
 	INTEnableSystemMultiVectoredInt();&lt;br /&gt;
 	&lt;br /&gt;
 	InitI2C();&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
 &lt;br /&gt;
 	// main loop&lt;br /&gt;
 	while (1) {&lt;br /&gt;
 		/* If global variable &amp;quot;dataRead&amp;quot; is set high during interrupt, turn on all LEDs */&lt;br /&gt;
 		if (dataRead == 0xAA)&lt;br /&gt;
 		{&lt;br /&gt;
 			mLED_0_On();&lt;br /&gt;
 			mLED_1_On();&lt;br /&gt;
 			mLED_2_On();			&lt;br /&gt;
 			mLED_3_On();			&lt;br /&gt;
 			&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 // Slave I2C interrupt handler&lt;br /&gt;
 // This handler is called when a qualifying I2C events occurs&lt;br /&gt;
 // this means that as well as Slave events &lt;br /&gt;
 // Master and Bus Collision events will also trigger this handler.&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void __ISR(_I2C_1_VECTOR, ipl3) _SlaveI2CHandler(void)&lt;br /&gt;
 {&lt;br /&gt;
 	mLED_1_On();&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	static unsigned int dIndex;&lt;br /&gt;
 	&lt;br /&gt;
 	// check for MASTER and Bus events and respond accordingly&lt;br /&gt;
 	if (IFS0bits.I2C1MIF == 1) {&lt;br /&gt;
 		mI2C1MClearIntFlag();&lt;br /&gt;
 		return;		&lt;br /&gt;
 	}&lt;br /&gt;
 	if (IFS0bits.I2C1BIF == 1) {&lt;br /&gt;
 		mI2C1BClearIntFlag();&lt;br /&gt;
 		return;&lt;br /&gt;
 	}&lt;br /&gt;
 	mLED_1_Off();&lt;br /&gt;
 	mLED_2_On();&lt;br /&gt;
 	&lt;br /&gt;
 	// handle the incoming message&lt;br /&gt;
 	if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 0 --&amp;gt; indicates last byte was address  &lt;br /&gt;
 		&lt;br /&gt;
 		// reset any state variables needed by a message sequence	&lt;br /&gt;
 		// perform a dummy read of the address&lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release the clock&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		// writing data to our module, just store it in adcSample&lt;br /&gt;
 		dataRead = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release clock stretch bit&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is output from slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was address&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// read of the slave device, read the address &lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		dIndex = 0;&lt;br /&gt;
 		SlaveWriteI2C1(dataRead);&lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		&lt;br /&gt;
 		// output the data until the MASTER terminates the&lt;br /&gt;
 		// transfer with a NACK, continuing reads return 0&lt;br /&gt;
 		if (dIndex == 0) {&lt;br /&gt;
 			SlaveWriteI2C1(dataRead);&lt;br /&gt;
 			dIndex++;&lt;br /&gt;
 		} else&lt;br /&gt;
 			SlaveWriteI2C1(0);&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	// finally clear the slave interrupt flag&lt;br /&gt;
 	mI2C1SClearIntFlag();		&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_SPI_Communication_between_PIC32s&amp;diff=16119</id>
		<title>PIC32MX: SPI Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_SPI_Communication_between_PIC32s&amp;diff=16119"/>
		<updated>2010-02-16T04:31:44Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32 boards to communicate with each other via SPI.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
SPI or [http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus Serial Peripheral Interface] is a communication method that was once used to connect devices such as printers, cameras, scanners, etc. to a desktop computer. This function has largely  been taken over by USB, but SPI can still be a useful communication tool for some applications. SPI runs using a master/slave set-up and can run in full duplex mode, meaning that signals can be transmitted between the master and the slave simultaneously. There is no standard communication protocol for SPI.&lt;br /&gt;
&lt;br /&gt;
SPI is still used to control some peripheral devices and has some advantages over [[PIC32MX:_I2C_Communication_between_PIC32s|I2C]] (another type of serial data communication). SPI can communicate at much higher data rates than I2C. Furthermore, when multiple slaves are present, SPI requires no addressing to differentiate between these slaves. Compared to parallel buses, SPI has the additional benefit of requiring only simple wiring. However, one disadvantage when compared to I2C is that each slave must have a dedicated slave line.  A slave line is not necessary when only one slave is present.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Peripheral devices that still use SPI:&lt;br /&gt;
&lt;br /&gt;
•	Converters (ADC and DAC)&lt;br /&gt;
&lt;br /&gt;
•	Memories (EEPROM and FLASH)&lt;br /&gt;
&lt;br /&gt;
•	Real Time Clocks (RTC)&lt;br /&gt;
&lt;br /&gt;
•	Sensors (temperature, pressure, etc.)&lt;br /&gt;
&lt;br /&gt;
•	Others (signal mixer, potentiometer, LCD controller, UART, CAN controller, USB controller, amplifier)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Basic Operation&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SPI requires three lines, and the optional slave select, and is therefore often termed the “four wire” serial bus. These four lines are described in the table below.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;2&amp;quot; cellpadding=&amp;quot;3&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Line!! Name!! Description&lt;br /&gt;
|-&lt;br /&gt;
|SCK1||Serial Clock||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDO1||Master Output, Slave Input||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDI1||Master Input, Slave Output||Output from slave&lt;br /&gt;
|-&lt;br /&gt;
|SS1||Slave Select||Output from master (active low)&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master, as its name suggests, controls all communication. By controlling the clock, the master decides when data is sent and received. Within each clock cycle a full duplex communication is carried out; each side sends and receives one bit of information. Because there is no standard communication protocol, the master can either send data or both send and receive data, depending on the needs of the application. Likewise, the slave can either receive data or both receive and send data back to the master.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;Slave Select&amp;quot; line is not required, if only one slave is used.  However, using the SS line, the master can choose which slave with which to communicate. Note that more than one slave may be selected, simply by applying a logic low to the desired SS lines.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;References&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.totalphase.com/support/articles/article03/ SPI Background](www.totalphase.com)&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus SPI Wikipedia Article] (www.wikipedia.org)&lt;br /&gt;
&lt;br /&gt;
[http://www.mct.net/faq/spi.html More Information] (www.mct.net)&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
[[Image:SPI_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
[[Image:Pic32_spi.png||thumb|right|x500px| PIC32MX SPI Module Block Diagram (click to expand) ]]&lt;br /&gt;
&lt;br /&gt;
This code, coupled with the circuit diagram above, sends one of two bytes of data from the master to the slave, and back. The data bytes are 0xAA and 0x23 which are sent on different button presses (PRG and USER).  Slave-LED0 toggles when 0xAA is received from the master, and slave-LED1 toggles when anything else (for example, 0x23) is received from the master.&lt;br /&gt;
No slave select line is used since there is only one slave, but if there were multiple slaves it would have to be used.&lt;br /&gt;
The SPI module sends and receives at the same time, so whenever a byte is written to SPI1BUF the module immediately sends out that byte on SDO which clocking with SCK and a byte is clocked in at the same time and written back to SPI1BUF. The send and receive buffers, SPI1TXB and SPI1RXB, cannot be written to directly, but instead are memory mapped to SPI1BUF. If the slave is not sending a value back when the master sends a byte, a 0x00 will be written into SPI1BUF. Because of this, in the code below the master reads from SPI1BUF after sending to clear the buffer and prevent overflow. To actually receive a byte, the master &amp;#039;&amp;#039;must&amp;#039;&amp;#039; send a byte as well. In the code below, by writing a zero to SPI1BUF, the master clocks out zero and in the slave message at the same time, at which point the slave message can be used. Since SPI is slower than the system clock, reading from SPI1BUF immediately would not result in the correct byte, instead the pic must wait for the module to finish sending, which the function getcSPI1() does automatically. Similarly, the slave sends a byte back by writing a value into SPI1BUF, but since the module is in slave mode the clock is not generated immediately. Instead the value waits in SPI1TXB until the clock is generated by the master. At this point the slave clocks out that value and in another value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /****************************************************&lt;br /&gt;
  * SPI_master_btwnPIC32s.c: Master code for Master- *&lt;br /&gt;
  *    Slave SPI communication. Both PICs are set to *&lt;br /&gt;
  *   use the SPI1 module and no SS line. A SS line  *&lt;br /&gt;
  *   would be used if there were more than two PICs.*&lt;br /&gt;
  *                                                  *&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards  *&lt;br /&gt;
  ****************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West           *&lt;br /&gt;
  * ME333 Winter 2010                                *&lt;br /&gt;
  * File Created:    05-FEB-2010                     *&lt;br /&gt;
  * Last Modified:    10-FEB-2010                    *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 //No-operation; asm stands for assembly&lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )&lt;br /&gt;
 &lt;br /&gt;
 #pragma config FPBDIV = DIV_1            //Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 //function definitions&lt;br /&gt;
 void SendData(int);         //Sends data&lt;br /&gt;
 void Delayms( unsigned t);   //Delay fcn&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main Function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
    // Configure the proper PB frequency and the number of wait states&lt;br /&gt;
    SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
    &lt;br /&gt;
    // Set all analog pins to be digital I/O&lt;br /&gt;
    AD1PCFG = 0xFFFF;&lt;br /&gt;
    //Initialize all of the LED pins&lt;br /&gt;
    mInitAllLEDs();&lt;br /&gt;
    mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
    //SPI setup&lt;br /&gt;
    int rData = SPI1BUF;    //Clears receive buffer&lt;br /&gt;
    IFS0CLR = 0x03800000;   //Clears any existing event (rx / tx/ fault interrupt)&lt;br /&gt;
    SPI1STATCLR = 0x40;      //Clears overflow&lt;br /&gt;
    //Enables the SPI channel (channel, master mode enable | use 8 bit mode | turn on, clock divider)&lt;br /&gt;
    SpiChnOpen(1, SPI_CON_MSTEN | SPI_CON_MODE8 | SPI_CON_ON, 1024);   // divide fpb by 1024, configure the I/O ports.&lt;br /&gt;
    &lt;br /&gt;
    /* Main while loop: Waits for button press to send/ receive data */&lt;br /&gt;
    while(1)&lt;br /&gt;
    {&lt;br /&gt;
       if (swProgram)   //If button 1 depressed&lt;br /&gt;
       {&lt;br /&gt;
          while(swProgram) { Nop(); }      //Wait for release&lt;br /&gt;
          mLED_2_Toggle();               //Toggle LED&lt;br /&gt;
          putcSPI1(0xAA);                 //Sends hex data 0xAA to slave&lt;br /&gt;
          Delayms(50);                  //delay&lt;br /&gt;
          SpiChnClrIntFlags(1);            //Clear interrupt flags (Tx / Rx buffers empty)&lt;br /&gt;
          int receive = SPI1BUF;            //Read SP1BUF (dummy read)&lt;br /&gt;
          SPI1BUF = 0x0;                  //Write SP1BUF- sets Tx flag, if not done read will not clock&lt;br /&gt;
          receive = getcSPI1();            //Generates clock and reads SDO&lt;br /&gt;
          Delayms(100);&lt;br /&gt;
       }//if loop ending&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
       if (swUser)&lt;br /&gt;
        {&lt;br /&gt;
          while(swUser) { Nop(); }&lt;br /&gt;
          mLED_1_Toggle();//Toggle LED&lt;br /&gt;
          putcSPI1(0x23);               //Sends hex data 0x23 to slave&lt;br /&gt;
          Delayms(50);               //delay&lt;br /&gt;
          SpiChnClrIntFlags(1);         //Clear interrupt flags (Tx / Rx buffers empty)&lt;br /&gt;
          int receive = SPI1BUF;         //Read SP1BUF (dummy read)&lt;br /&gt;
          SPI1BUF = 0x0;               //Write SP1BUF- sets Tx flag, if not done read will not clock&lt;br /&gt;
          receive = getcSPI1();         //Generates clock and reads SDO&lt;br /&gt;
          Delayms(100);&lt;br /&gt;
       }//if loop ending&lt;br /&gt;
    }//while loop ending&lt;br /&gt;
 &lt;br /&gt;
    return 0;&lt;br /&gt;
 } //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
    }&lt;br /&gt;
    CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
&lt;br /&gt;
 /****************************************************&lt;br /&gt;
  * SPI_slave_btwnPIC32s.c: Slave code for Master-   *&lt;br /&gt;
  *    Slave SPI communication. Both PICs are set to *&lt;br /&gt;
  *   use the SPI1 module and no SS line. A SS line  *&lt;br /&gt;
  *   would be used if there were more than two PICs.*&lt;br /&gt;
  *                                                  *&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards  *&lt;br /&gt;
  ****************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West           *&lt;br /&gt;
  * ME333 Winter 2010                                *&lt;br /&gt;
  * File Created:    05-FEB-2010                     *&lt;br /&gt;
  * Last Modified:    10-FEB-2010                    *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )   //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //Function definition for Delay function&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
    // Configure the proper PB frequency and the number of wait states. &lt;br /&gt;
    SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
    // Set all analog pins to be digital I/O&lt;br /&gt;
    AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
    /* Set TRIS bits for SPI lines (this may not be necessary */&lt;br /&gt;
    TRISDbits.TRISD10=1;&lt;br /&gt;
    TRISDbits.TRISD0=0;&lt;br /&gt;
    TRISCbits.TRISC4=1;&lt;br /&gt;
 &lt;br /&gt;
    //Initialize all of the LED pins&lt;br /&gt;
    mInitAllLEDs();&lt;br /&gt;
    mLED_3_Off();&lt;br /&gt;
 &lt;br /&gt;
    //SPI setup&lt;br /&gt;
    SPI1CON = 0;         //Clears config register&lt;br /&gt;
    int rData = SPI1BUF;   //Clears receive buffer&lt;br /&gt;
    IFS0CLR = 0x03800000;   //Clears any existing event (rx / tx/ fault interrupt)&lt;br /&gt;
    SPI1STATCLR = 0x40;      //Clears overflow&lt;br /&gt;
    //Enables the SPI channel (channel, master mode enable | use 8 bit mode | turn on, clock divider)&lt;br /&gt;
    SpiChnOpen(1, SPI_CON_SLVEN | SPI_CON_MODE8 | SPI_CON_ON, 1024);   // divide fpb by 1024, configure the I/O ports.&lt;br /&gt;
 &lt;br /&gt;
    //While loop to test LED functionality &lt;br /&gt;
    while(1)&lt;br /&gt;
    {&lt;br /&gt;
       rData = SpiChnGetC(1);               //Wait and read character when aviliable&lt;br /&gt;
       if (rData == 0xAA){mLED_0_Toggle();}   //Toggle LED0 if 0xAA&lt;br /&gt;
       else {mLED_1_Toggle();}               //Toggle LED1 if not&lt;br /&gt;
       putcSPI1(rData);                  //Send character back (when clock arrives)&lt;br /&gt;
       rData = SPI1BUF;                  //Clear recieve buffer (brevents overflow)&lt;br /&gt;
       Delayms(5);&lt;br /&gt;
    }//while loop ending&lt;br /&gt;
 &lt;br /&gt;
    return 0;&lt;br /&gt;
 } //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
    }&lt;br /&gt;
    CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_SPI_Communication_between_PIC32s&amp;diff=16117</id>
		<title>PIC32MX: SPI Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_SPI_Communication_between_PIC32s&amp;diff=16117"/>
		<updated>2010-02-16T04:28:56Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32 boards to communicate with each other via SPI.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
SPI or [http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus Serial Peripheral Interface] is a communication method that was once used to connect devices such as printers, cameras, scanners, etc. to a desktop computer. This function has largely  been taken over by USB, but SPI can still be a useful communication tool for some applications. SPI runs using a master/slave set-up and can run in full duplex mode, meaning that signals can be transmitted between the master and the slave simultaneously. There is no standard communication protocol for SPI.&lt;br /&gt;
&lt;br /&gt;
SPI is still used to control some peripheral devices and has some advantages over [[I2C communication between PICs|I2C]]&amp;#039;&amp;#039;&amp;#039;CHANGE THIS TO OUR I2C PAGE!!&amp;#039;&amp;#039;&amp;#039; (another type of serial data communication). SPI can communicate at much higher data rates than I2C. Furthermore, when multiple slaves are present, SPI requires no addressing to differentiate between these slaves. Compared to parallel buses, SPI has the additional benefit of requiring only simple wiring. However, one disadvantage when compared to I2C is that each slave must have a dedicated slave line.  A slave line is not necessary when only one slave is present.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Peripheral devices that still use SPI:&lt;br /&gt;
&lt;br /&gt;
•	Converters (ADC and DAC)&lt;br /&gt;
&lt;br /&gt;
•	Memories (EEPROM and FLASH)&lt;br /&gt;
&lt;br /&gt;
•	Real Time Clocks (RTC)&lt;br /&gt;
&lt;br /&gt;
•	Sensors (temperature, pressure, etc.)&lt;br /&gt;
&lt;br /&gt;
•	Others (signal mixer, potentiometer, LCD controller, UART, CAN controller, USB controller, amplifier)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Basic Operation&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SPI requires three lines, and the optional slave select, and is therefore often termed the “four wire” serial bus. These four lines are described in the table below.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;2&amp;quot; cellpadding=&amp;quot;3&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Line!! Name!! Description&lt;br /&gt;
|-&lt;br /&gt;
|SCK1||Serial Clock||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDO1||Master Output, Slave Input||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDI1||Master Input, Slave Output||Output from slave&lt;br /&gt;
|-&lt;br /&gt;
|SS1||Slave Select||Output from master (active low)&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master, as its name suggests, controls all communication. By controlling the clock, the master decides when data is sent and received. Within each clock cycle a full duplex communication is carried out; each side sends and receives one bit of information. Because there is no standard communication protocol, the master can either send data or both send and receive data, depending on the needs of the application. Likewise, the slave can either receive data or both receive and send data back to the master.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;Slave Select&amp;quot; line is not required, if only one slave is used.  However, using the SS line, the master can choose which slave with which to communicate. Note that more than one slave may be selected, simply by applying a logic low to the desired SS lines.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;References&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.totalphase.com/support/articles/article03/ SPI Background](www.totalphase.com)&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus SPI Wikipedia Article] (www.wikipedia.org)&lt;br /&gt;
&lt;br /&gt;
[http://www.mct.net/faq/spi.html More Information] (www.mct.net)&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
[[Image:SPI_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
[[Image:Pic32_spi.png||thumb|right|x500px| PIC32MX SPI Module Block Diagram (click to expand) ]]&lt;br /&gt;
&lt;br /&gt;
This code, coupled with the circuit diagram above, sends one of two bytes of data from the master to the slave, and back. The data bytes are 0xAA and 0x23 which are sent on different button presses (PRG and USER).  Slave-LED0 toggles when 0xAA is received from the master, and slave-LED1 toggles when anything else (for example, 0x23) is received from the master.&lt;br /&gt;
No slave select line is used since there is only one slave, but if there were multiple slaves it would have to be used.&lt;br /&gt;
The SPI module sends and receives at the same time, so whenever a byte is written to SPI1BUF the module immediately sends out that byte on SDO which clocking with SCK and a byte is clocked in at the same time and written back to SPI1BUF. The send and receive buffers, SPI1TXB and SPI1RXB, cannot be written to directly, but instead are memory mapped to SPI1BUF. If the slave is not sending a value back when the master sends a byte, a 0x00 will be written into SPI1BUF. Because of this, in the code below the master reads from SPI1BUF after sending to clear the buffer and prevent overflow. To actually receive a byte, the master &amp;#039;&amp;#039;must&amp;#039;&amp;#039; send a byte as well. In the code below, by writing a zero to SPI1BUF, the master clocks out zero and in the slave message at the same time, at which point the slave message can be used. Since SPI is slower than the system clock, reading from SPI1BUF immediately would not result in the correct byte, instead the pic must wait for the module to finish sending, which the function getcSPI1() does automatically. Similarly, the slave sends a byte back by writing a value into SPI1BUF, but since the module is in slave mode the clock is not generated immediately. Instead the value waits in SPI1TXB until the clock is generated by the master. At this point the slave clocks out that value and in another value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /****************************************************&lt;br /&gt;
  * SPI_master_btwnPIC32s.c: Master code for Master- *&lt;br /&gt;
  *    Slave SPI communication. Both PICs are set to *&lt;br /&gt;
  *   use the SPI1 module and no SS line. A SS line  *&lt;br /&gt;
  *   would be used if there were more than two PICs.*&lt;br /&gt;
  *                                                  *&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards  *&lt;br /&gt;
  ****************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West           *&lt;br /&gt;
  * ME333 Winter 2010                                *&lt;br /&gt;
  * File Created:    05-FEB-2010                     *&lt;br /&gt;
  * Last Modified:    10-FEB-2010                    *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 //No-operation; asm stands for assembly&lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )&lt;br /&gt;
 &lt;br /&gt;
 #pragma config FPBDIV = DIV_1            //Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 //function definitions&lt;br /&gt;
 void SendData(int);         //Sends data&lt;br /&gt;
 void Delayms( unsigned t);   //Delay fcn&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main Function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
    // Configure the proper PB frequency and the number of wait states&lt;br /&gt;
    SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
    &lt;br /&gt;
    // Set all analog pins to be digital I/O&lt;br /&gt;
    AD1PCFG = 0xFFFF;&lt;br /&gt;
    //Initialize all of the LED pins&lt;br /&gt;
    mInitAllLEDs();&lt;br /&gt;
    mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
    //SPI setup&lt;br /&gt;
    int rData = SPI1BUF;    //Clears receive buffer&lt;br /&gt;
    IFS0CLR = 0x03800000;   //Clears any existing event (rx / tx/ fault interrupt)&lt;br /&gt;
    SPI1STATCLR = 0x40;      //Clears overflow&lt;br /&gt;
    //Enables the SPI channel (channel, master mode enable | use 8 bit mode | turn on, clock divider)&lt;br /&gt;
    SpiChnOpen(1, SPI_CON_MSTEN | SPI_CON_MODE8 | SPI_CON_ON, 1024);   // divide fpb by 1024, configure the I/O ports.&lt;br /&gt;
    &lt;br /&gt;
    /* Main while loop: Waits for button press to send/ receive data */&lt;br /&gt;
    while(1)&lt;br /&gt;
    {&lt;br /&gt;
       if (swProgram)   //If button 1 depressed&lt;br /&gt;
       {&lt;br /&gt;
          while(swProgram) { Nop(); }      //Wait for release&lt;br /&gt;
          mLED_2_Toggle();               //Toggle LED&lt;br /&gt;
          putcSPI1(0xAA);                 //Sends hex data 0xAA to slave&lt;br /&gt;
          Delayms(50);                  //delay&lt;br /&gt;
          SpiChnClrIntFlags(1);            //Clear interrupt flags (Tx / Rx buffers empty)&lt;br /&gt;
          int receive = SPI1BUF;            //Read SP1BUF (dummy read)&lt;br /&gt;
          SPI1BUF = 0x0;                  //Write SP1BUF- sets Tx flag, if not done read will not clock&lt;br /&gt;
          receive = getcSPI1();            //Generates clock and reads SDO&lt;br /&gt;
          Delayms(100);&lt;br /&gt;
       }//if loop ending&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
       if (swUser)&lt;br /&gt;
        {&lt;br /&gt;
          while(swUser) { Nop(); }&lt;br /&gt;
          mLED_1_Toggle();//Toggle LED&lt;br /&gt;
          putcSPI1(0x23);               //Sends hex data 0x23 to slave&lt;br /&gt;
          Delayms(50);               //delay&lt;br /&gt;
          SpiChnClrIntFlags(1);         //Clear interrupt flags (Tx / Rx buffers empty)&lt;br /&gt;
          int receive = SPI1BUF;         //Read SP1BUF (dummy read)&lt;br /&gt;
          SPI1BUF = 0x0;               //Write SP1BUF- sets Tx flag, if not done read will not clock&lt;br /&gt;
          receive = getcSPI1();         //Generates clock and reads SDO&lt;br /&gt;
          Delayms(100);&lt;br /&gt;
       }//if loop ending&lt;br /&gt;
    }//while loop ending&lt;br /&gt;
 &lt;br /&gt;
    return 0;&lt;br /&gt;
 } //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
    }&lt;br /&gt;
    CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
&lt;br /&gt;
 /****************************************************&lt;br /&gt;
  * SPI_slave_btwnPIC32s.c: Slave code for Master-   *&lt;br /&gt;
  *    Slave SPI communication. Both PICs are set to *&lt;br /&gt;
  *   use the SPI1 module and no SS line. A SS line  *&lt;br /&gt;
  *   would be used if there were more than two PICs.*&lt;br /&gt;
  *                                                  *&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards  *&lt;br /&gt;
  ****************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West           *&lt;br /&gt;
  * ME333 Winter 2010                                *&lt;br /&gt;
  * File Created:    05-FEB-2010                     *&lt;br /&gt;
  * Last Modified:    10-FEB-2010                    *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )   //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //Function definition for Delay function&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
    // Configure the proper PB frequency and the number of wait states. &lt;br /&gt;
    SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
    // Set all analog pins to be digital I/O&lt;br /&gt;
    AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
    /* Set TRIS bits for SPI lines (this may not be necessary */&lt;br /&gt;
    TRISDbits.TRISD10=1;&lt;br /&gt;
    TRISDbits.TRISD0=0;&lt;br /&gt;
    TRISCbits.TRISC4=1;&lt;br /&gt;
 &lt;br /&gt;
    //Initialize all of the LED pins&lt;br /&gt;
    mInitAllLEDs();&lt;br /&gt;
    mLED_3_Off();&lt;br /&gt;
 &lt;br /&gt;
    //SPI setup&lt;br /&gt;
    SPI1CON = 0;         //Clears config register&lt;br /&gt;
    int rData = SPI1BUF;   //Clears receive buffer&lt;br /&gt;
    IFS0CLR = 0x03800000;   //Clears any existing event (rx / tx/ fault interrupt)&lt;br /&gt;
    SPI1STATCLR = 0x40;      //Clears overflow&lt;br /&gt;
    //Enables the SPI channel (channel, master mode enable | use 8 bit mode | turn on, clock divider)&lt;br /&gt;
    SpiChnOpen(1, SPI_CON_SLVEN | SPI_CON_MODE8 | SPI_CON_ON, 1024);   // divide fpb by 1024, configure the I/O ports.&lt;br /&gt;
 &lt;br /&gt;
    //While loop to test LED functionality &lt;br /&gt;
    while(1)&lt;br /&gt;
    {&lt;br /&gt;
       rData = SpiChnGetC(1);               //Wait and read character when aviliable&lt;br /&gt;
       if (rData == 0xAA){mLED_0_Toggle();}   //Toggle LED0 if 0xAA&lt;br /&gt;
       else {mLED_1_Toggle();}               //Toggle LED1 if not&lt;br /&gt;
       putcSPI1(rData);                  //Send character back (when clock arrives)&lt;br /&gt;
       rData = SPI1BUF;                  //Clear recieve buffer (brevents overflow)&lt;br /&gt;
       Delayms(5);&lt;br /&gt;
    }//while loop ending&lt;br /&gt;
 &lt;br /&gt;
    return 0;&lt;br /&gt;
 } //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
    }&lt;br /&gt;
    CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:SPI_circuit.PNG&amp;diff=16115</id>
		<title>File:SPI circuit.PNG</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:SPI_circuit.PNG&amp;diff=16115"/>
		<updated>2010-02-16T04:27:19Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: uploaded a new version of &amp;quot;Image:SPI circuit.PNG&amp;quot;: Removed the external switches because we changed our code so that it uses the internal switches, USER and PRG instead.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This is the circuit used for SPI communication between two PIC32s.  It uses two digital inputs from push button switches to control what byte of data is sent from the master to the slave.&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:I2C_circuit.PNG&amp;diff=16114</id>
		<title>File:I2C circuit.PNG</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:I2C_circuit.PNG&amp;diff=16114"/>
		<updated>2010-02-16T04:25:18Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: uploaded a new version of &amp;quot;Image:I2C circuit.PNG&amp;quot;: Updated I2C circuit without the external switches because we changed our code to use the PRG and USER switches internal to the NU32 board.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This circuit is for I2C communication between 2 PIC32s.  Two push button switches create digital inputs to the master that indicate which byte of data should be sent from the master to the slave.  The slave then sends this byte back to master.&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16113</id>
		<title>PIC32MX: I2C Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16113"/>
		<updated>2010-02-16T04:18:45Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32s to communicate via I2C.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf I2C] is pronounced &amp;quot;I squared C&amp;quot; and stands for Inter-Integrated Circuit.  This protocol was designed by Phillips Semiconductors around 1992 to allow easy communication between components on the same circuit board and can achieve transfer rates of up to 400 kbit/sec.  Is is a 2 line (plus common ground) communication method for one master device to control up to 112 slave devices.  While it is possible to have multiple masters on the same I2c bus, this page will only deal with a one master configuration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Basic Operation&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two lines are named SCL1 and SDA1 where SCL1 is the CLock line and SDA1 is the DAta line.  I2C requires the lines to be high unless the master or the slave is pulling the line down, so you will need to use pull up resistors on both the clock and data lines.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;2&amp;quot; cellpadding=&amp;quot;3&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Line!! Name!! Description&lt;br /&gt;
|-&lt;br /&gt;
|SCL1||Serial Clock Line||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDA1||Data Line||The single data line for sending/receiving&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The data line is read each time the clock line goes high, as shown in the diagram below:&lt;br /&gt;
&lt;br /&gt;
[[image:I2C_Data_Transfer.jpg|center]]&lt;br /&gt;
&lt;br /&gt;
* S - As the master pulls down the data line, the falling edge of the data line signifies the start condition.&lt;br /&gt;
* P - As the master allows the data line to rise, the rising edge of the data line signifies the stop condition.&lt;br /&gt;
* B - Individual bits being transferred.&lt;br /&gt;
&lt;br /&gt;
The data line can only change while the clock line is low.  The data line is only read when the clock line is high and therefore 1 bit can be transferred per cycle of the clock line.&lt;br /&gt;
&lt;br /&gt;
The master is initially sends a start bit followed by the 7-bit address of the slave it wishes to communicate with. The next bit represents whether it wishes to write(0) to or read(1) from the slave.&lt;br /&gt;
&lt;br /&gt;
The slave then responds with an acknowledge bit. The transmission continues according to whether the master is attempting to read to or write from the device.&lt;br /&gt;
&lt;br /&gt;
The master then ends the transmission with a stop bit.  Another option is to send another start bit to continue the transfer in a combined message.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
&lt;br /&gt;
[[Image:I2C_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
In the code below, the master sends one of two bytes (0xAA or 0x23) to the slave at address 0x40. The master then receives a byte back from the slave. If the slave receives a 0xAA, it lights its LEDs.&lt;br /&gt;
When the master begins sending, the slave triggers an interrupt (if the slave address is a match), which does one of four actions based on whether the master is reading or writing and whether the last byte was a data or address byte. As mentioned, when the master is writing the slave stores the byte, and when the master is reading the slave sends back the last stored byte.&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /********************************************************&lt;br /&gt;
  * I2C_Master.c: Master code for I2C communication.	*&lt;br /&gt;
  * 	Both PICS use I2C1 module to send/ receive data.*&lt;br /&gt;
  *	The master sends different values to a slave,   *&lt;br /&gt;
  *	which uses an interrupt to respond accordingly.	*&lt;br /&gt;
  *							*&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards	*&lt;br /&gt;
  ********************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West		*&lt;br /&gt;
  * ME333 Winter 2010					*&lt;br /&gt;
  * File Created: 	05-FEB-2010			*&lt;br /&gt;
  * Last Modified: 	14-FEB-2010			*&lt;br /&gt;
  *******************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 #pragma config FPBDIV = DIV_1				//Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000      					//Frequency of (I2C) serial clock&lt;br /&gt;
  &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )                 //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 #define INPUT_A9       PORTAbits.RA9&lt;br /&gt;
 #define INPUT_A10       PORTAbits.RA10&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //function declaration for sending data and selecting slave address&lt;br /&gt;
 void SendData(int,unsigned int);&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
 This function is a delay function, causing the program to wait for approximately 4 * cnt cycles&lt;br /&gt;
 1 cycle is 1/SYSCLK seconds.&lt;br /&gt;
 */&lt;br /&gt;
 void i2c_wait(unsigned int cnt)&lt;br /&gt;
 {&lt;br /&gt;
 	while(--cnt)&lt;br /&gt;
 	{&lt;br /&gt;
 		Nop();&lt;br /&gt;
 		Nop();&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
     // Configure the proper PB frequency and the number of wait states.&lt;br /&gt;
 	SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
 	// Set all analog pins to be digital I/O&lt;br /&gt;
    	AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
 	//Setup TRIS bits for switches and I2C pins&lt;br /&gt;
 	TRISAbits.TRISA14=0;&lt;br /&gt;
 	TRISAbits.TRISA15=0;&lt;br /&gt;
 &lt;br /&gt;
 	//Initialize all of the LED pins&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
        mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
 	unsigned char SlaveAddress;   //Slave address variable to tell the master where to send the data.  &lt;br /&gt;
                                      //Will be re-assigned for multiple slaves.&lt;br /&gt;
 &lt;br /&gt;
 	//Enable I2C channel and set the baud rate to BRG_VAL)&lt;br /&gt;
 	OpenI2C1( I2C_EN, BRG_VAL );&lt;br /&gt;
 &lt;br /&gt;
 	int rcv;			//For received data&lt;br /&gt;
 &lt;br /&gt;
 	//While loop to test LED functionality &lt;br /&gt;
 	while(1) {&lt;br /&gt;
 		if (swProgram) {				//First button pressed&lt;br /&gt;
 			while(swProgram) { Nop(); }	//Wait for release&lt;br /&gt;
 			mLED_2_Toggle();			//Toggle LED2&lt;br /&gt;
 			SendData(0xAA,0x40);  			//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);			//Receives data from address 0x40				&lt;br /&gt;
 			Delayms(100);&lt;br /&gt;
 		}&lt;br /&gt;
 		if (swUser) {				//Second button pressed&lt;br /&gt;
 			while(swUser) { Nop(); }&lt;br /&gt;
 			mLED_1_Toggle();			//Toggle LED1&lt;br /&gt;
 			SendData(0x23,0x40);    		//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);		        //Receives data from address 0x40			&lt;br /&gt;
 			Delayms(100);		&lt;br /&gt;
 		}&lt;br /&gt;
 	}//while loop ending&lt;br /&gt;
 &lt;br /&gt;
 	return 0;&lt;br /&gt;
 }  //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /*****************************************************&lt;br /&gt;
  * RcvData(unsigned int address)		     *&lt;br /&gt;
  *					  	     *&lt;br /&gt;
  * Gets a byte of data from I2C slave device at      *&lt;br /&gt;
  *  ADDRESS.					     *&lt;br /&gt;
  *						     *&lt;br /&gt;
  * Returns: Received data			     *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 int RcvData(unsigned int address) {&lt;br /&gt;
 	StartI2C1();				//Send line start condition&lt;br /&gt;
 	IdleI2C1();			        //Wait to complete&lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 1);	//Write out slave address OR 1 (read command)&lt;br /&gt;
 	IdleI2C1();				//Wait to complete&lt;br /&gt;
 	int rcv = MasterReadI2C1();		//Read in a value&lt;br /&gt;
 	StopI2C1();				//Send line stop condition&lt;br /&gt;
 	IdleI2C1();				//Wait co complete&lt;br /&gt;
 	return rcv;				//Return read value&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /***************************************************&lt;br /&gt;
  * SendData(int data, unsigned int address)        *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Sends a byte of data (DATA) over the I2C line   *&lt;br /&gt;
  *	to I2C address ADDRESS			    *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Returns: nothing				    *&lt;br /&gt;
  ***************************************************/&lt;br /&gt;
 void SendData (int data, unsigned int address){&lt;br /&gt;
 	StartI2C1();	        //Send the Start Bit&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 0);  //Sends the slave address over the I2C line.  This must happen first so the &lt;br /&gt;
                                              //proper slave is selected to receive data.&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1(data);  //Sends data byte over I2C line&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	StopI2C1();	        //Send the Stop condition&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 } //end function&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
 	}&lt;br /&gt;
 	CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
 /***********************************************************************&lt;br /&gt;
  * PIC32 I2C Slave Code                &lt;br /&gt;
  ***********************************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;GenericTypeDefs.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;Compiler.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000&lt;br /&gt;
 #define BRG_VAL 	((PBCLK/2/Fsck)-2)&lt;br /&gt;
 &lt;br /&gt;
 // this is the modules Slave Address&lt;br /&gt;
 #define SLAVE_ADDRESS 0x40&lt;br /&gt;
 &lt;br /&gt;
 // volatile variables to hold the switch and led states&lt;br /&gt;
 volatile unsigned char dataRead = 0;&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	InitI2C&lt;br /&gt;
 //&lt;br /&gt;
 // 	Perform initialisation of the I2C module to operate as a slave&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void InitI2C(void)&lt;br /&gt;
 {&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	&lt;br /&gt;
 	// Enable the I2C module with clock stretching enabled&lt;br /&gt;
 	OpenI2C1(I2C_ON | I2C_7BIT_ADD | I2C_STR_EN, BRG_VAL);&lt;br /&gt;
 	&lt;br /&gt;
 	// set the address of the slave module, address matching is with bits&lt;br /&gt;
 	// 7:1 of the message compared with bits 6:0 of the ADD SFR so we&lt;br /&gt;
 	// need to shift the desired address 1 bit. &lt;br /&gt;
 	I2C1ADD = SLAVE_ADDRESS; // &amp;gt;&amp;gt; 1;&lt;br /&gt;
 	I2C1MSK = 0;&lt;br /&gt;
 	&lt;br /&gt;
 	// configure the interrupt priority for the I2C peripheral&lt;br /&gt;
 	mI2C1SetIntPriority(I2C_INT_PRI_3 | I2C_INT_SLAVE);&lt;br /&gt;
 &lt;br /&gt;
 	// clear pending interrupts and enable I2C interrupts&lt;br /&gt;
 	mI2C1SClearIntFlag();&lt;br /&gt;
 	EnableIntSI2C1;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	main routine&lt;br /&gt;
 // &lt;br /&gt;
 //	This code example demonstrates using the PIC32 as an I2C slave&lt;br /&gt;
 //	&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 int main (void)&lt;br /&gt;
 {&lt;br /&gt;
 	// set for 80MHz operation&lt;br /&gt;
 	SYSTEMConfigPerformance(SYSCLK);&lt;br /&gt;
 	// set the Pbus to be 40000000&lt;br /&gt;
 	mOSCSetPBDIV(OSC_PB_DIV_2);&lt;br /&gt;
 	// disable the JTAG port&lt;br /&gt;
 	mJTAGPortEnable(0);&lt;br /&gt;
 	// enable interrupts&lt;br /&gt;
 	INTEnableSystemMultiVectoredInt();&lt;br /&gt;
 	&lt;br /&gt;
 	InitI2C();&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
 &lt;br /&gt;
 	// main loop&lt;br /&gt;
 	while (1) {&lt;br /&gt;
 		/* If global variable &amp;quot;dataRead&amp;quot; is set high during interrupt, turn on all LEDs */&lt;br /&gt;
 		if (dataRead == 0xAA)&lt;br /&gt;
 		{&lt;br /&gt;
 			mLED_0_On();&lt;br /&gt;
 			mLED_1_On();&lt;br /&gt;
 			mLED_2_On();			&lt;br /&gt;
 			mLED_3_On();			&lt;br /&gt;
 			&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 // Slave I2C interrupt handler&lt;br /&gt;
 // This handler is called when a qualifying I2C events occurs&lt;br /&gt;
 // this means that as well as Slave events &lt;br /&gt;
 // Master and Bus Collision events will also trigger this handler.&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void __ISR(_I2C_1_VECTOR, ipl3) _SlaveI2CHandler(void)&lt;br /&gt;
 {&lt;br /&gt;
 	mLED_1_On();&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	static unsigned int dIndex;&lt;br /&gt;
 	&lt;br /&gt;
 	// check for MASTER and Bus events and respond accordingly&lt;br /&gt;
 	if (IFS0bits.I2C1MIF == 1) {&lt;br /&gt;
 		mI2C1MClearIntFlag();&lt;br /&gt;
 		return;		&lt;br /&gt;
 	}&lt;br /&gt;
 	if (IFS0bits.I2C1BIF == 1) {&lt;br /&gt;
 		mI2C1BClearIntFlag();&lt;br /&gt;
 		return;&lt;br /&gt;
 	}&lt;br /&gt;
 	mLED_1_Off();&lt;br /&gt;
 	mLED_2_On();&lt;br /&gt;
 	&lt;br /&gt;
 	// handle the incoming message&lt;br /&gt;
 	if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 0 --&amp;gt; indicates last byte was address  &lt;br /&gt;
 		&lt;br /&gt;
 		// reset any state variables needed by a message sequence	&lt;br /&gt;
 		// perform a dummy read of the address&lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release the clock&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		// writing data to our module, just store it in adcSample&lt;br /&gt;
 		dataRead = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release clock stretch bit&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is output from slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was address&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// read of the slave device, read the address &lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		dIndex = 0;&lt;br /&gt;
 		SlaveWriteI2C1(dataRead);&lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		&lt;br /&gt;
 		// output the data until the MASTER terminates the&lt;br /&gt;
 		// transfer with a NACK, continuing reads return 0&lt;br /&gt;
 		if (dIndex == 0) {&lt;br /&gt;
 			SlaveWriteI2C1(dataRead);&lt;br /&gt;
 			dIndex++;&lt;br /&gt;
 		} else&lt;br /&gt;
 			SlaveWriteI2C1(0);&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	// finally clear the slave interrupt flag&lt;br /&gt;
 	mI2C1SClearIntFlag();		&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16111</id>
		<title>PIC32MX: I2C Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16111"/>
		<updated>2010-02-16T04:12:07Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Master Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32s to communicate via I2C.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf I2C] is pronounced &amp;quot;I squared C&amp;quot; and stands for Inter-Integrated Circuit.  This protocol was designed by Phillips Semiconductors around 1992 to allow easy communication between components on the same circuit board and can achieve transfer rates of up to 400 kbit/sec.  Is is a 2 line (plus common ground) communication method for one master device to control up to 112 slave devices.  While it is possible to have multiple masters on the same I2c bus, this page will only deal with a one master configuration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Basic Operation&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two lines are named SCL1 and SDA1 where SCL1 is the CLock line and SDA1 is the DAta line.  I2C requires the lines to be high unless the master or the slave is pulling the line down, so you will need to use pull up resistors on both the clock and data lines.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;2&amp;quot; cellpadding=&amp;quot;3&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Line!! Name!! Description&lt;br /&gt;
|-&lt;br /&gt;
|SCL1||Serial Clock Line||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDA1||Data Line||The single data line for sending/receiving&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The data line is read each time the clock line goes high, as shown in the diagram below:&lt;br /&gt;
&lt;br /&gt;
[[image:I2C_Data_Transfer.jpg|center]]&lt;br /&gt;
&lt;br /&gt;
* S - As the master pulls down the data line, the falling edge of the data line signifies the start condition.&lt;br /&gt;
* P - As the master allows the data line to rise, the rising edge of the data line signifies the stop condition.&lt;br /&gt;
* B - Individual bits being transferred.&lt;br /&gt;
&lt;br /&gt;
The data line can only change while the clock line is low.  The data line is only read when the clock line is high and therefore 1 bit can be transferred per cycle of the clock line.&lt;br /&gt;
&lt;br /&gt;
The master is initially sends a start bit followed by the 7-bit address of the slave it wishes to communicate with. The next bit represents whether it wishes to write(0) to or read(1) from the slave.&lt;br /&gt;
&lt;br /&gt;
The slave then responds with an acknowledge bit. The transmission continues according to whether the master is attempting to read to or write from the device.&lt;br /&gt;
&lt;br /&gt;
The master then ends the transmission with a stop bit.  Another option is to send another start bit to continue the transfer in a combined message.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
&lt;br /&gt;
[[Image:I2C_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
In the code below, the master sends one of two bytes (0xAA or 0x23) to the slave at address 0x40. The slave then sends the byte back. If the slave receives a 0xAA, it lights its LEDs.&lt;br /&gt;
&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /********************************************************&lt;br /&gt;
  * I2C_Master.c: Master code for I2C communication.	*&lt;br /&gt;
  * 	Both PICS use I2C1 module to send/ receive data.*&lt;br /&gt;
  *	The master sends different values to a slave,   *&lt;br /&gt;
  *	which uses an interrupt to respond accordingly.	*&lt;br /&gt;
  *							*&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards	*&lt;br /&gt;
  ********************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West		*&lt;br /&gt;
  * ME333 Winter 2010					*&lt;br /&gt;
  * File Created: 	05-FEB-2010			*&lt;br /&gt;
  * Last Modified: 	14-FEB-2010			*&lt;br /&gt;
  *******************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 #pragma config FPBDIV = DIV_1				//Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000      					//Frequency of (I2C) serial clock&lt;br /&gt;
  &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )                 //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 #define INPUT_A9       PORTAbits.RA9&lt;br /&gt;
 #define INPUT_A10       PORTAbits.RA10&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //function declaration for sending data and selecting slave address&lt;br /&gt;
 void SendData(int,unsigned int);&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
 This function is a delay function, causing the program to wait for approximately 4 * cnt cycles&lt;br /&gt;
 1 cycle is 1/SYSCLK seconds.&lt;br /&gt;
 */&lt;br /&gt;
 void i2c_wait(unsigned int cnt)&lt;br /&gt;
 {&lt;br /&gt;
 	while(--cnt)&lt;br /&gt;
 	{&lt;br /&gt;
 		Nop();&lt;br /&gt;
 		Nop();&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
     // Configure the proper PB frequency and the number of wait states.&lt;br /&gt;
 	SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
 	// Set all analog pins to be digital I/O&lt;br /&gt;
    	AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
 	//Setup TRIS bits for switches and I2C pins&lt;br /&gt;
 	TRISAbits.TRISA14=0;&lt;br /&gt;
 	TRISAbits.TRISA15=0;&lt;br /&gt;
 &lt;br /&gt;
 	//Initialize all of the LED pins&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
        mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
 	unsigned char SlaveAddress;   //Slave address variable to tell the master where to send the data.  &lt;br /&gt;
                                      //Will be re-assigned for multiple slaves.&lt;br /&gt;
 &lt;br /&gt;
 	//Enable I2C channel and set the baud rate to BRG_VAL)&lt;br /&gt;
 	OpenI2C1( I2C_EN, BRG_VAL );&lt;br /&gt;
 &lt;br /&gt;
 	int rcv;			//For received data&lt;br /&gt;
 &lt;br /&gt;
 	//While loop to test LED functionality &lt;br /&gt;
 	while(1) {&lt;br /&gt;
 		if (swProgram) {				//First button pressed&lt;br /&gt;
 			while(swProgram) { Nop(); }	//Wait for release&lt;br /&gt;
 			mLED_2_Toggle();			//Toggle LED2&lt;br /&gt;
 			SendData(0xAA,0x40);  			//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);			//Receives data from address 0x40				&lt;br /&gt;
 			Delayms(100);&lt;br /&gt;
 		}&lt;br /&gt;
 		if (swUser) {				//Second button pressed&lt;br /&gt;
 			while(swUser) { Nop(); }&lt;br /&gt;
 			mLED_1_Toggle();			//Toggle LED1&lt;br /&gt;
 			SendData(0x23,0x40);    		//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);		        //Receives data from address 0x40			&lt;br /&gt;
 			Delayms(100);		&lt;br /&gt;
 		}&lt;br /&gt;
 	}//while loop ending&lt;br /&gt;
 &lt;br /&gt;
 	return 0;&lt;br /&gt;
 }  //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /*****************************************************&lt;br /&gt;
  * RcvData(unsigned int address)		     *&lt;br /&gt;
  *					  	     *&lt;br /&gt;
  * Gets a byte of data from I2C slave device at      *&lt;br /&gt;
  *  ADDRESS.					     *&lt;br /&gt;
  *						     *&lt;br /&gt;
  * Returns: Received data			     *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 int RcvData(unsigned int address) {&lt;br /&gt;
 	StartI2C1();				//Send line start condition&lt;br /&gt;
 	IdleI2C1();			        //Wait to complete&lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 1);	//Write out slave address OR 1 (read command)&lt;br /&gt;
 	IdleI2C1();				//Wait to complete&lt;br /&gt;
 	int rcv = MasterReadI2C1();		//Read in a value&lt;br /&gt;
 	StopI2C1();				//Send line stop condition&lt;br /&gt;
 	IdleI2C1();				//Wait co complete&lt;br /&gt;
 	return rcv;				//Return read value&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /***************************************************&lt;br /&gt;
  * SendData(int data, unsigned int address)        *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Sends a byte of data (DATA) over the I2C line   *&lt;br /&gt;
  *	to I2C address ADDRESS			    *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Returns: nothing				    *&lt;br /&gt;
  ***************************************************/&lt;br /&gt;
 void SendData (int data, unsigned int address){&lt;br /&gt;
 	StartI2C1();	        //Send the Start Bit&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 0);  //Sends the slave address over the I2C line.  This must happen first so the &lt;br /&gt;
                                              //proper slave is selected to receive data.&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1(data);  //Sends data byte over I2C line&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	StopI2C1();	        //Send the Stop condition&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 } //end function&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
 	}&lt;br /&gt;
 	CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
 /***********************************************************************&lt;br /&gt;
  * PIC32 I2C Slave Code                &lt;br /&gt;
  ***********************************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;GenericTypeDefs.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;Compiler.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000&lt;br /&gt;
 #define BRG_VAL 	((PBCLK/2/Fsck)-2)&lt;br /&gt;
 &lt;br /&gt;
 // this is the modules Slave Address&lt;br /&gt;
 #define SLAVE_ADDRESS 0x40&lt;br /&gt;
 &lt;br /&gt;
 // volatile variables to hold the switch and led states&lt;br /&gt;
 volatile unsigned char dataRead = 0;&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	InitI2C&lt;br /&gt;
 //&lt;br /&gt;
 // 	Perform initialisation of the I2C module to operate as a slave&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void InitI2C(void)&lt;br /&gt;
 {&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	&lt;br /&gt;
 	// Enable the I2C module with clock stretching enabled&lt;br /&gt;
 	OpenI2C1(I2C_ON | I2C_7BIT_ADD | I2C_STR_EN, BRG_VAL);&lt;br /&gt;
 	&lt;br /&gt;
 	// set the address of the slave module, address matching is with bits&lt;br /&gt;
 	// 7:1 of the message compared with bits 6:0 of the ADD SFR so we&lt;br /&gt;
 	// need to shift the desired address 1 bit. &lt;br /&gt;
 	I2C1ADD = SLAVE_ADDRESS; // &amp;gt;&amp;gt; 1;&lt;br /&gt;
 	I2C1MSK = 0;&lt;br /&gt;
 	&lt;br /&gt;
 	// configure the interrupt priority for the I2C peripheral&lt;br /&gt;
 	mI2C1SetIntPriority(I2C_INT_PRI_3 | I2C_INT_SLAVE);&lt;br /&gt;
 &lt;br /&gt;
 	// clear pending interrupts and enable I2C interrupts&lt;br /&gt;
 	mI2C1SClearIntFlag();&lt;br /&gt;
 	EnableIntSI2C1;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	main routine&lt;br /&gt;
 // &lt;br /&gt;
 //	This code example demonstrates using the PIC32 as an I2C slave&lt;br /&gt;
 //	&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 int main (void)&lt;br /&gt;
 {&lt;br /&gt;
 	// set for 80MHz operation&lt;br /&gt;
 	SYSTEMConfigPerformance(SYSCLK);&lt;br /&gt;
 	// set the Pbus to be 40000000&lt;br /&gt;
 	mOSCSetPBDIV(OSC_PB_DIV_2);&lt;br /&gt;
 	// disable the JTAG port&lt;br /&gt;
 	mJTAGPortEnable(0);&lt;br /&gt;
 	// enable interrupts&lt;br /&gt;
 	INTEnableSystemMultiVectoredInt();&lt;br /&gt;
 	&lt;br /&gt;
 	InitI2C();&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
 &lt;br /&gt;
 	// main loop&lt;br /&gt;
 	while (1) {&lt;br /&gt;
 		/* If global variable &amp;quot;dataRead&amp;quot; is set high during interrupt, turn on all LEDs */&lt;br /&gt;
 		if (dataRead == 0xAA)&lt;br /&gt;
 		{&lt;br /&gt;
 			mLED_0_On();&lt;br /&gt;
 			mLED_1_On();&lt;br /&gt;
 			mLED_2_On();			&lt;br /&gt;
 			mLED_3_On();			&lt;br /&gt;
 			&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 // Slave I2C interrupt handler&lt;br /&gt;
 // This handler is called when a qualifying I2C events occurs&lt;br /&gt;
 // this means that as well as Slave events &lt;br /&gt;
 // Master and Bus Collision events will also trigger this handler.&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void __ISR(_I2C_1_VECTOR, ipl3) _SlaveI2CHandler(void)&lt;br /&gt;
 {&lt;br /&gt;
 	mLED_1_On();&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	static unsigned int dIndex;&lt;br /&gt;
 	&lt;br /&gt;
 	// check for MASTER and Bus events and respond accordingly&lt;br /&gt;
 	if (IFS0bits.I2C1MIF == 1) {&lt;br /&gt;
 		mI2C1MClearIntFlag();&lt;br /&gt;
 		return;		&lt;br /&gt;
 	}&lt;br /&gt;
 	if (IFS0bits.I2C1BIF == 1) {&lt;br /&gt;
 		mI2C1BClearIntFlag();&lt;br /&gt;
 		return;&lt;br /&gt;
 	}&lt;br /&gt;
 	mLED_1_Off();&lt;br /&gt;
 	mLED_2_On();&lt;br /&gt;
 	&lt;br /&gt;
 	// handle the incoming message&lt;br /&gt;
 	if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 0 --&amp;gt; indicates last byte was address  &lt;br /&gt;
 		&lt;br /&gt;
 		// reset any state variables needed by a message sequence	&lt;br /&gt;
 		// perform a dummy read of the address&lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release the clock&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		// writing data to our module, just store it in adcSample&lt;br /&gt;
 		dataRead = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release clock stretch bit&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is output from slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was address&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// read of the slave device, read the address &lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		dIndex = 0;&lt;br /&gt;
 		SlaveWriteI2C1(dataRead);&lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		&lt;br /&gt;
 		// output the data until the MASTER terminates the&lt;br /&gt;
 		// transfer with a NACK, continuing reads return 0&lt;br /&gt;
 		if (dIndex == 0) {&lt;br /&gt;
 			SlaveWriteI2C1(dataRead);&lt;br /&gt;
 			dIndex++;&lt;br /&gt;
 		} else&lt;br /&gt;
 			SlaveWriteI2C1(0);&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	// finally clear the slave interrupt flag&lt;br /&gt;
 	mI2C1SClearIntFlag();		&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16110</id>
		<title>PIC32MX: I2C Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16110"/>
		<updated>2010-02-16T04:10:40Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32s to communicate via I2C.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf I2C] is pronounced &amp;quot;I squared C&amp;quot; and stands for Inter-Integrated Circuit.  This protocol was designed by Phillips Semiconductors around 1992 to allow easy communication between components on the same circuit board and can achieve transfer rates of up to 400 kbit/sec.  Is is a 2 line (plus common ground) communication method for one master device to control up to 112 slave devices.  While it is possible to have multiple masters on the same I2c bus, this page will only deal with a one master configuration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Basic Operation&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two lines are named SCL1 and SDA1 where SCL1 is the CLock line and SDA1 is the DAta line.  I2C requires the lines to be high unless the master or the slave is pulling the line down, so you will need to use pull up resistors on both the clock and data lines.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;2&amp;quot; cellpadding=&amp;quot;3&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Line!! Name!! Description&lt;br /&gt;
|-&lt;br /&gt;
|SCL1||Serial Clock Line||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDA1||Data Line||The single data line for sending/receiving&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The data line is read each time the clock line goes high, as shown in the diagram below:&lt;br /&gt;
&lt;br /&gt;
[[image:I2C_Data_Transfer.jpg|center]]&lt;br /&gt;
&lt;br /&gt;
* S - As the master pulls down the data line, the falling edge of the data line signifies the start condition.&lt;br /&gt;
* P - As the master allows the data line to rise, the rising edge of the data line signifies the stop condition.&lt;br /&gt;
* B - Individual bits being transferred.&lt;br /&gt;
&lt;br /&gt;
The data line can only change while the clock line is low.  The data line is only read when the clock line is high and therefore 1 bit can be transferred per cycle of the clock line.&lt;br /&gt;
&lt;br /&gt;
The master is initially sends a start bit followed by the 7-bit address of the slave it wishes to communicate with. The next bit represents whether it wishes to write(0) to or read(1) from the slave.&lt;br /&gt;
&lt;br /&gt;
The slave then responds with an acknowledge bit. The transmission continues according to whether the master is attempting to read to or write from the device.&lt;br /&gt;
&lt;br /&gt;
The master then ends the transmission with a stop bit.  Another option is to send another start bit to continue the transfer in a combined message.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
&lt;br /&gt;
[[Image:I2C_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
In the code below, the master sends one of two bytes (0xAA or 0x23) to the slave at address 0x40. The slave then sends the byte back. If the slave receives a 0xAA, it lights its LEDs.&lt;br /&gt;
&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /*******************************************************&lt;br /&gt;
  * I2C_Master.c: Master code for I2C communication.	*&lt;br /&gt;
  * 	Both PICS use I2C1 module to send/ receive data.*&lt;br /&gt;
  *	The master sends different values to a slave,   *&lt;br /&gt;
  *	which uses an interrupt to respond accordingly.	*&lt;br /&gt;
  *							*&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards	*&lt;br /&gt;
  *******************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West		*&lt;br /&gt;
  * ME333 Winter 2010					*&lt;br /&gt;
  * File Created: 	05-FEB-2010			*&lt;br /&gt;
  * Last Modified: 	14-FEB-2010			*&lt;br /&gt;
  *******************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 #pragma config FPBDIV = DIV_1				//Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000      					//Frequency of (I2C) serial clock&lt;br /&gt;
  &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )                 //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 #define INPUT_A9       PORTAbits.RA9&lt;br /&gt;
 #define INPUT_A10       PORTAbits.RA10&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //function declaration for sending data and selecting slave address&lt;br /&gt;
 void SendData(int,unsigned int);&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
 This function is a delay function, causing the program to wait for approximately 4 * cnt cycles&lt;br /&gt;
 1 cycle is 1/SYSCLK seconds.&lt;br /&gt;
 */&lt;br /&gt;
 void i2c_wait(unsigned int cnt)&lt;br /&gt;
 {&lt;br /&gt;
 	while(--cnt)&lt;br /&gt;
 	{&lt;br /&gt;
 		Nop();&lt;br /&gt;
 		Nop();&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
     // Configure the proper PB frequency and the number of wait states.&lt;br /&gt;
 	SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
 	// Set all analog pins to be digital I/O&lt;br /&gt;
    	AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
 	//Setup TRIS bits for switches and I2C pins&lt;br /&gt;
 	TRISAbits.TRISA14=0;&lt;br /&gt;
 	TRISAbits.TRISA15=0;&lt;br /&gt;
 &lt;br /&gt;
 	//Initialize all of the LED pins&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
        mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
 	unsigned char SlaveAddress;   //Slave address variable to tell the master where to send the data.  &lt;br /&gt;
                                      //Will be re-assigned for multiple slaves.&lt;br /&gt;
 &lt;br /&gt;
 	//Enable I2C channel and set the baud rate to BRG_VAL)&lt;br /&gt;
 	OpenI2C1( I2C_EN, BRG_VAL );&lt;br /&gt;
 &lt;br /&gt;
 	int rcv;			//For received data&lt;br /&gt;
 &lt;br /&gt;
 	//While loop to test LED functionality &lt;br /&gt;
 	while(1) {&lt;br /&gt;
 		if (swProgram) {				//First button pressed&lt;br /&gt;
 			while(swProgram) { Nop(); }	//Wait for release&lt;br /&gt;
 			mLED_2_Toggle();			//Toggle LED2&lt;br /&gt;
 			SendData(0xAA,0x40);  			//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);			//Receives data from address 0x40				&lt;br /&gt;
 			Delayms(100);&lt;br /&gt;
 		}&lt;br /&gt;
 		if (swUser) {				//Second button pressed&lt;br /&gt;
 			while(swUser) { Nop(); }&lt;br /&gt;
 			mLED_1_Toggle();			//Toggle LED1&lt;br /&gt;
 			SendData(0x23,0x40);    		//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);		        //Receives data from address 0x40			&lt;br /&gt;
 			Delayms(100);		&lt;br /&gt;
 		}&lt;br /&gt;
 	}//while loop ending&lt;br /&gt;
 &lt;br /&gt;
 	return 0;&lt;br /&gt;
 }  //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /*****************************************************&lt;br /&gt;
  * RcvData(unsigned int address)		     *&lt;br /&gt;
  *					  	     *&lt;br /&gt;
  * Gets a byte of data from I2C slave device at      *&lt;br /&gt;
  *  ADDRESS.					     *&lt;br /&gt;
  *						     *&lt;br /&gt;
  * Returns: Received data			     *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 int RcvData(unsigned int address) {&lt;br /&gt;
 	StartI2C1();				//Send line start condition&lt;br /&gt;
 	IdleI2C1();			        //Wait to complete&lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 1);	//Write out slave address OR 1 (read command)&lt;br /&gt;
 	IdleI2C1();				//Wait to complete&lt;br /&gt;
 	int rcv = MasterReadI2C1();		//Read in a value&lt;br /&gt;
 	StopI2C1();				//Send line stop condition&lt;br /&gt;
 	IdleI2C1();				//Wait co complete&lt;br /&gt;
 	return rcv;				//Return read value&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /***************************************************&lt;br /&gt;
  * SendData(int data, unsigned int address)        *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Sends a byte of data (DATA) over the I2C line   *&lt;br /&gt;
  *	to I2C address ADDRESS			    *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Returns: nothing				    *&lt;br /&gt;
  ***************************************************/&lt;br /&gt;
 void SendData (int data, unsigned int address){&lt;br /&gt;
 	StartI2C1();	        //Send the Start Bit&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 0);  //Sends the slave address over the I2C line.  This must happen first so the &lt;br /&gt;
                                              //proper slave is selected to receive data.&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1(data);  //Sends data byte over I2C line&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	StopI2C1();	        //Send the Stop condition&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 } //end function&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
 	}&lt;br /&gt;
 	CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
 /***********************************************************************&lt;br /&gt;
  * PIC32 I2C Slave Code                &lt;br /&gt;
  ***********************************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;GenericTypeDefs.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;Compiler.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000&lt;br /&gt;
 #define BRG_VAL 	((PBCLK/2/Fsck)-2)&lt;br /&gt;
 &lt;br /&gt;
 // this is the modules Slave Address&lt;br /&gt;
 #define SLAVE_ADDRESS 0x40&lt;br /&gt;
 &lt;br /&gt;
 // volatile variables to hold the switch and led states&lt;br /&gt;
 volatile unsigned char dataRead = 0;&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	InitI2C&lt;br /&gt;
 //&lt;br /&gt;
 // 	Perform initialisation of the I2C module to operate as a slave&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void InitI2C(void)&lt;br /&gt;
 {&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	&lt;br /&gt;
 	// Enable the I2C module with clock stretching enabled&lt;br /&gt;
 	OpenI2C1(I2C_ON | I2C_7BIT_ADD | I2C_STR_EN, BRG_VAL);&lt;br /&gt;
 	&lt;br /&gt;
 	// set the address of the slave module, address matching is with bits&lt;br /&gt;
 	// 7:1 of the message compared with bits 6:0 of the ADD SFR so we&lt;br /&gt;
 	// need to shift the desired address 1 bit. &lt;br /&gt;
 	I2C1ADD = SLAVE_ADDRESS; // &amp;gt;&amp;gt; 1;&lt;br /&gt;
 	I2C1MSK = 0;&lt;br /&gt;
 	&lt;br /&gt;
 	// configure the interrupt priority for the I2C peripheral&lt;br /&gt;
 	mI2C1SetIntPriority(I2C_INT_PRI_3 | I2C_INT_SLAVE);&lt;br /&gt;
 &lt;br /&gt;
 	// clear pending interrupts and enable I2C interrupts&lt;br /&gt;
 	mI2C1SClearIntFlag();&lt;br /&gt;
 	EnableIntSI2C1;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	main routine&lt;br /&gt;
 // &lt;br /&gt;
 //	This code example demonstrates using the PIC32 as an I2C slave&lt;br /&gt;
 //	&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 int main (void)&lt;br /&gt;
 {&lt;br /&gt;
 	// set for 80MHz operation&lt;br /&gt;
 	SYSTEMConfigPerformance(SYSCLK);&lt;br /&gt;
 	// set the Pbus to be 40000000&lt;br /&gt;
 	mOSCSetPBDIV(OSC_PB_DIV_2);&lt;br /&gt;
 	// disable the JTAG port&lt;br /&gt;
 	mJTAGPortEnable(0);&lt;br /&gt;
 	// enable interrupts&lt;br /&gt;
 	INTEnableSystemMultiVectoredInt();&lt;br /&gt;
 	&lt;br /&gt;
 	InitI2C();&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
 &lt;br /&gt;
 	// main loop&lt;br /&gt;
 	while (1) {&lt;br /&gt;
 		/* If global variable &amp;quot;dataRead&amp;quot; is set high during interrupt, turn on all LEDs */&lt;br /&gt;
 		if (dataRead == 0xAA)&lt;br /&gt;
 		{&lt;br /&gt;
 			mLED_0_On();&lt;br /&gt;
 			mLED_1_On();&lt;br /&gt;
 			mLED_2_On();			&lt;br /&gt;
 			mLED_3_On();			&lt;br /&gt;
 			&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 // Slave I2C interrupt handler&lt;br /&gt;
 // This handler is called when a qualifying I2C events occurs&lt;br /&gt;
 // this means that as well as Slave events &lt;br /&gt;
 // Master and Bus Collision events will also trigger this handler.&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void __ISR(_I2C_1_VECTOR, ipl3) _SlaveI2CHandler(void)&lt;br /&gt;
 {&lt;br /&gt;
 	mLED_1_On();&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	static unsigned int dIndex;&lt;br /&gt;
 	&lt;br /&gt;
 	// check for MASTER and Bus events and respond accordingly&lt;br /&gt;
 	if (IFS0bits.I2C1MIF == 1) {&lt;br /&gt;
 		mI2C1MClearIntFlag();&lt;br /&gt;
 		return;		&lt;br /&gt;
 	}&lt;br /&gt;
 	if (IFS0bits.I2C1BIF == 1) {&lt;br /&gt;
 		mI2C1BClearIntFlag();&lt;br /&gt;
 		return;&lt;br /&gt;
 	}&lt;br /&gt;
 	mLED_1_Off();&lt;br /&gt;
 	mLED_2_On();&lt;br /&gt;
 	&lt;br /&gt;
 	// handle the incoming message&lt;br /&gt;
 	if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 0 --&amp;gt; indicates last byte was address  &lt;br /&gt;
 		&lt;br /&gt;
 		// reset any state variables needed by a message sequence	&lt;br /&gt;
 		// perform a dummy read of the address&lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release the clock&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		// writing data to our module, just store it in adcSample&lt;br /&gt;
 		dataRead = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release clock stretch bit&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is output from slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was address&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// read of the slave device, read the address &lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		dIndex = 0;&lt;br /&gt;
 		SlaveWriteI2C1(dataRead);&lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		&lt;br /&gt;
 		// output the data until the MASTER terminates the&lt;br /&gt;
 		// transfer with a NACK, continuing reads return 0&lt;br /&gt;
 		if (dIndex == 0) {&lt;br /&gt;
 			SlaveWriteI2C1(dataRead);&lt;br /&gt;
 			dIndex++;&lt;br /&gt;
 		} else&lt;br /&gt;
 			SlaveWriteI2C1(0);&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	// finally clear the slave interrupt flag&lt;br /&gt;
 	mI2C1SClearIntFlag();		&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16097</id>
		<title>PIC32MX: I2C Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16097"/>
		<updated>2010-02-16T03:20:55Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32s to communicate via I2C.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf I2C] is pronounced &amp;quot;I squared C&amp;quot; and stands for Inter-Integrated Circuit.  This protocol was designed by Phillips Semiconductors around 1992 to allow easy communication between components on the same circuit board and can achieve transfer rates of up to 400 kbit/sec.  Is is a 2 line (plus common ground) communication method for one master device to control up to 112 slave devices.  While it is possible to have multiple masters on the same I2c bus, this page will only deal with a one master configuration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Basic Operation&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two lines are named SCL1 and SDA1 where SCL1 is the CLock line and SDA1 is the DAta line.  I2C requires the lines to be high unless the master or the slave is pulling the line down, so you will need to use pull up resistors on both the clock and data lines.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;2&amp;quot; cellpadding=&amp;quot;3&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Line!! Name!! Description&lt;br /&gt;
|-&lt;br /&gt;
|SCL1||Serial Clock Line||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDA1||Data Line||The single data line for sending/receiving&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The data line is read each time the clock line goes high, as shown in the diagram below:&lt;br /&gt;
&lt;br /&gt;
[[image:I2C_Data_Transfer.jpg|center]]&lt;br /&gt;
&lt;br /&gt;
* S - As the master pulls down the data line, the falling edge of the data line signifies the start condition.&lt;br /&gt;
* P - As the master allows the data line to rise, the rising edge of the data line signifies the stop condition.&lt;br /&gt;
* B - Individual bits being transferred.&lt;br /&gt;
&lt;br /&gt;
The data line can only change while the clock line is low.  The data line is only read when the clock line is high and therefore 1 bit can be transferred per cycle of the clock line.&lt;br /&gt;
&lt;br /&gt;
The master is initially sends a start bit followed by the 7-bit address of the slave it wishes to communicate with. The next bit represents whether it wishes to write(0) to or read(1) from the slave.&lt;br /&gt;
&lt;br /&gt;
The slave then responds with an acknowledge bit. The transmission continues according to whether the master is attempting to read to or write from the device.&lt;br /&gt;
&lt;br /&gt;
The master then ends the transmission with a stop bit.  Another option is to send another start bit to continue the transfer in a combined message.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
&lt;br /&gt;
[[Image:I2C_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
(code description here)&lt;br /&gt;
&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /*******************************************************&lt;br /&gt;
  * I2C_Master.c: Master code for I2C communication.	*&lt;br /&gt;
  * 	Both PICS use I2C1 module to send/ recieve data.*&lt;br /&gt;
  *	The master sends different values to a slave,   *&lt;br /&gt;
  *	which uses an interrupt to respond accordingly.	*&lt;br /&gt;
  *							*&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards	*&lt;br /&gt;
  *******************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West		*&lt;br /&gt;
  * ME333 Winter 2010					*&lt;br /&gt;
  * File Created: 	05-FEB-2010			*&lt;br /&gt;
  * Last Modified: 	14-FEB-2010			*&lt;br /&gt;
  *******************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 #pragma config FPBDIV = DIV_1				//Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000      					//Frequency of (I2C) serial clock&lt;br /&gt;
  &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )                 //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 #define INPUT_A9       PORTAbits.RA9&lt;br /&gt;
 #define INPUT_A10       PORTAbits.RA10&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //function declaration for sending data and selecting slave address&lt;br /&gt;
 void SendData(int,unsigned int);&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
 This function is a delay function, causing the program to wait for approximately 4 * cnt cycles&lt;br /&gt;
 1 cycle is 1/SYSCLK seconds.&lt;br /&gt;
 */&lt;br /&gt;
 void i2c_wait(unsigned int cnt)&lt;br /&gt;
 {&lt;br /&gt;
 	while(--cnt)&lt;br /&gt;
 	{&lt;br /&gt;
 		Nop();&lt;br /&gt;
 		Nop();&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
     // Configure the proper PB frequency and the number of wait states.&lt;br /&gt;
 	SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
 	// Set all analog pins to be digital I/O&lt;br /&gt;
    	AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
 	//Setup TRIS bits for switches and I2C pins&lt;br /&gt;
 	TRISAbits.TRISA14=0;&lt;br /&gt;
 	TRISAbits.TRISA15=0;&lt;br /&gt;
 &lt;br /&gt;
 	//Initialize all of the LED pins&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
        mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
 	unsigned char SlaveAddress;   //Slave address variable to tell the master where to send the data.  &lt;br /&gt;
                                      //Will be re-assigned for multiple slaves.&lt;br /&gt;
 &lt;br /&gt;
 	//Enable I2C channel and set the baud rate to BRG_VAL)&lt;br /&gt;
 	OpenI2C1( I2C_EN, BRG_VAL );&lt;br /&gt;
 &lt;br /&gt;
 	int rcv;			//For received data&lt;br /&gt;
 &lt;br /&gt;
 	//While loop to test LED functionality &lt;br /&gt;
 	while(1) {&lt;br /&gt;
 		if (swProgram) {				//First button pressed&lt;br /&gt;
 			while(swProgram) { Nop(); }	//Wait for release&lt;br /&gt;
 			mLED_2_Toggle();			//Toggle LED2&lt;br /&gt;
 			SendData(0xAA,0x40);  			//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);			//Receives data from address 0x40				&lt;br /&gt;
 			Delayms(100);&lt;br /&gt;
 		}&lt;br /&gt;
 		if (swUser) {				//Second button pressed&lt;br /&gt;
 			while(swUser) { Nop(); }&lt;br /&gt;
 			mLED_1_Toggle();			//Toggle LED1&lt;br /&gt;
 			SendData(0x23,0x40);    		//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);		        //Receives data from address 0x40			&lt;br /&gt;
 			Delayms(100);		&lt;br /&gt;
 		}&lt;br /&gt;
 	}//while loop ending&lt;br /&gt;
 &lt;br /&gt;
 	return 0;&lt;br /&gt;
 }  //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /*****************************************************&lt;br /&gt;
  * RcvData(unsigned int address)		     *&lt;br /&gt;
  *					  	     *&lt;br /&gt;
  * Gets a byte of data from I2C slave device at      *&lt;br /&gt;
  *  ADDRESS.					     *&lt;br /&gt;
  *						     *&lt;br /&gt;
  * Returns: Received data			     *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 int RcvData(unsigned int address) {&lt;br /&gt;
 	StartI2C1();				//Send line start condition&lt;br /&gt;
 	IdleI2C1();			        //Wait to complete&lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 1);	//Write out slave address OR 1 (read command)&lt;br /&gt;
 	IdleI2C1();				//Wait to complete&lt;br /&gt;
 	int rcv = MasterReadI2C1();		//Read in a value&lt;br /&gt;
 	StopI2C1();				//Send line stop condition&lt;br /&gt;
 	IdleI2C1();				//Wait co complete&lt;br /&gt;
 	return rcv;				//Return read value&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /***************************************************&lt;br /&gt;
  * SendData(int data, unsigned int address)        *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Sends a byte of data (DATA) over the I2C line   *&lt;br /&gt;
  *	to I2C address ADDRESS			    *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Returns: nothing				    *&lt;br /&gt;
  ***************************************************/&lt;br /&gt;
 void SendData (int data, unsigned int address){&lt;br /&gt;
 	StartI2C1();	        //Send the Start Bit&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 0);  //Sends the slave address over the I2C line.  This must happen first so the &lt;br /&gt;
                                              //proper slave is selected to receive data.&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1(data);  //Sends data byte over I2C line&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	StopI2C1();	        //Send the Stop condition&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 } //end function&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
 	}&lt;br /&gt;
 	CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
 /***********************************************************************&lt;br /&gt;
  * PIC32 I2C Slave Code                &lt;br /&gt;
  ***********************************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;GenericTypeDefs.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;Compiler.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000&lt;br /&gt;
 #define BRG_VAL 	((PBCLK/2/Fsck)-2)&lt;br /&gt;
 &lt;br /&gt;
 // this is the modules Slave Address&lt;br /&gt;
 #define SLAVE_ADDRESS 0x40&lt;br /&gt;
 &lt;br /&gt;
 // volatile variables to hold the switch and led states&lt;br /&gt;
 volatile unsigned char dataRead = 0;&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	InitI2C&lt;br /&gt;
 //&lt;br /&gt;
 // 	Perform initialisation of the I2C module to operate as a slave&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void InitI2C(void)&lt;br /&gt;
 {&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	&lt;br /&gt;
 	// Enable the I2C module with clock stretching enabled&lt;br /&gt;
 	OpenI2C1(I2C_ON | I2C_7BIT_ADD | I2C_STR_EN, BRG_VAL);&lt;br /&gt;
 	&lt;br /&gt;
 	// set the address of the slave module, address matching is with bits&lt;br /&gt;
 	// 7:1 of the message compared with bits 6:0 of the ADD SFR so we&lt;br /&gt;
 	// need to shift the desired address 1 bit. &lt;br /&gt;
 	I2C1ADD = SLAVE_ADDRESS; // &amp;gt;&amp;gt; 1;&lt;br /&gt;
 	I2C1MSK = 0;&lt;br /&gt;
 	&lt;br /&gt;
 	// configure the interrupt priority for the I2C peripheral&lt;br /&gt;
 	mI2C1SetIntPriority(I2C_INT_PRI_3 | I2C_INT_SLAVE);&lt;br /&gt;
 &lt;br /&gt;
 	// clear pending interrupts and enable I2C interrupts&lt;br /&gt;
 	mI2C1SClearIntFlag();&lt;br /&gt;
 	EnableIntSI2C1;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	main routine&lt;br /&gt;
 // &lt;br /&gt;
 //	This code example demonstrates using the PIC32 as an I2C slave&lt;br /&gt;
 //	&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 int main (void)&lt;br /&gt;
 {&lt;br /&gt;
 	// set for 80MHz operation&lt;br /&gt;
 	SYSTEMConfigPerformance(SYSCLK);&lt;br /&gt;
 	// set the Pbus to be 40000000&lt;br /&gt;
 	mOSCSetPBDIV(OSC_PB_DIV_2);&lt;br /&gt;
 	// disable the JTAG port&lt;br /&gt;
 	mJTAGPortEnable(0);&lt;br /&gt;
 	// enable interrupts&lt;br /&gt;
 	INTEnableSystemMultiVectoredInt();&lt;br /&gt;
 	&lt;br /&gt;
 	InitI2C();&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
 &lt;br /&gt;
 	// main loop&lt;br /&gt;
 	while (1) {&lt;br /&gt;
 		/* If global variable &amp;quot;dataRead&amp;quot; is set high during interrupt, turn on all LEDs */&lt;br /&gt;
 		if (dataRead == 0xAA)&lt;br /&gt;
 		{&lt;br /&gt;
 			mLED_0_On();&lt;br /&gt;
 			mLED_1_On();&lt;br /&gt;
 			mLED_2_On();			&lt;br /&gt;
 			mLED_3_On();			&lt;br /&gt;
 			&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 // Slave I2C interrupt handler&lt;br /&gt;
 // This handler is called when a qualifying I2C events occurs&lt;br /&gt;
 // this means that as well as Slave events &lt;br /&gt;
 // Master and Bus Collision events will also trigger this handler.&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void __ISR(_I2C_1_VECTOR, ipl3) _SlaveI2CHandler(void)&lt;br /&gt;
 {&lt;br /&gt;
 	mLED_1_On();&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	static unsigned int dIndex;&lt;br /&gt;
 	&lt;br /&gt;
 	// check for MASTER and Bus events and respond accordingly&lt;br /&gt;
 	if (IFS0bits.I2C1MIF == 1) {&lt;br /&gt;
 		mI2C1MClearIntFlag();&lt;br /&gt;
 		return;		&lt;br /&gt;
 	}&lt;br /&gt;
 	if (IFS0bits.I2C1BIF == 1) {&lt;br /&gt;
 		mI2C1BClearIntFlag();&lt;br /&gt;
 		return;&lt;br /&gt;
 	}&lt;br /&gt;
 	mLED_1_Off();&lt;br /&gt;
 	mLED_2_On();&lt;br /&gt;
 	&lt;br /&gt;
 	// handle the incoming message&lt;br /&gt;
 	if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 0 --&amp;gt; indicates last byte was address  &lt;br /&gt;
 		&lt;br /&gt;
 		// reset any state variables needed by a message sequence	&lt;br /&gt;
 		// perform a dummy read of the address&lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release the clock&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		// writing data to our module, just store it in adcSample&lt;br /&gt;
 		dataRead = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release clock stretch bit&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is output from slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was address&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// read of the slave device, read the address &lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		dIndex = 0;&lt;br /&gt;
 		SlaveWriteI2C1(dataRead);&lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		&lt;br /&gt;
 		// output the data until the MASTER terminates the&lt;br /&gt;
 		// transfer with a NACK, continuing reads return 0&lt;br /&gt;
 		if (dIndex == 0) {&lt;br /&gt;
 			SlaveWriteI2C1(dataRead);&lt;br /&gt;
 			dIndex++;&lt;br /&gt;
 		} else&lt;br /&gt;
 			SlaveWriteI2C1(0);&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	// finally clear the slave interrupt flag&lt;br /&gt;
 	mI2C1SClearIntFlag();		&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16095</id>
		<title>PIC32MX: I2C Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16095"/>
		<updated>2010-02-16T03:19:54Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32s to communicate via I2C.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf I2C] is pronounced &amp;quot;I squared C&amp;quot; and stands for Inter-Integrated Circuit.  This protocol was designed by Phillips Semiconductors around 1992 to allow easy communication between components on the same circuit board and can achieve transfer rates of up to 400 kbit/sec.  Is is a 2 line (plus common ground) communication method for one master device to control up to 112 slave devices.  While it is possible to have multiple masters on the same I2c bus, this page will only deal with a one master configuration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Basic Operation&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two lines are named SCL1 and SDA1 where SCL1 is the CLock line and SDA1 is the DAta line.  I2C requires the lines to be high unless the master or the slave is pulling the line down, so you will need to use pull up resistors on both the clock and data lines.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;2&amp;quot; cellpadding=&amp;quot;3&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Line!! Name!! Description&lt;br /&gt;
|-&lt;br /&gt;
|SCL1||Serial Clock Line||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDA1||Data Line||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The data line is read each time the clock line goes high, as shown in the diagram below:&lt;br /&gt;
&lt;br /&gt;
[[image:I2C_Data_Transfer.jpg|center]]&lt;br /&gt;
&lt;br /&gt;
* S - As the master pulls down the data line, the falling edge of the data line signifies the start condition.&lt;br /&gt;
* P - As the master allows the data line to rise, the rising edge of the data line signifies the stop condition.&lt;br /&gt;
* B - Individual bits being transferred.&lt;br /&gt;
&lt;br /&gt;
The data line can only change while the clock line is low.  The data line is only read when the clock line is high and therefore 1 bit can be transferred per cycle of the clock line.&lt;br /&gt;
&lt;br /&gt;
The master is initially sends a start bit followed by the 7-bit address of the slave it wishes to communicate with. The next bit represents whether it wishes to write(0) to or read(1) from the slave.&lt;br /&gt;
&lt;br /&gt;
The slave then responds with an acknowledge bit. The transmission continues according to whether the master is attempting to read to or write from the device.&lt;br /&gt;
&lt;br /&gt;
The master then ends the transmission with a stop bit.  Another option is to send another start bit to continue the transfer in a combined message.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
&lt;br /&gt;
[[Image:I2C_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
(code description here)&lt;br /&gt;
&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /*******************************************************&lt;br /&gt;
  * I2C_Master.c: Master code for I2C communication.	*&lt;br /&gt;
  * 	Both PICS use I2C1 module to send/ recieve data.*&lt;br /&gt;
  *	The master sends different values to a slave,   *&lt;br /&gt;
  *	which uses an interrupt to respond accordingly.	*&lt;br /&gt;
  *							*&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards	*&lt;br /&gt;
  *******************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West		*&lt;br /&gt;
  * ME333 Winter 2010					*&lt;br /&gt;
  * File Created: 	05-FEB-2010			*&lt;br /&gt;
  * Last Modified: 	14-FEB-2010			*&lt;br /&gt;
  *******************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 #pragma config FPBDIV = DIV_1				//Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000      					//Frequency of (I2C) serial clock&lt;br /&gt;
  &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )                 //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 #define INPUT_A9       PORTAbits.RA9&lt;br /&gt;
 #define INPUT_A10       PORTAbits.RA10&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //function declaration for sending data and selecting slave address&lt;br /&gt;
 void SendData(int,unsigned int);&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
 This function is a delay function, causing the program to wait for approximately 4 * cnt cycles&lt;br /&gt;
 1 cycle is 1/SYSCLK seconds.&lt;br /&gt;
 */&lt;br /&gt;
 void i2c_wait(unsigned int cnt)&lt;br /&gt;
 {&lt;br /&gt;
 	while(--cnt)&lt;br /&gt;
 	{&lt;br /&gt;
 		Nop();&lt;br /&gt;
 		Nop();&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
     // Configure the proper PB frequency and the number of wait states.&lt;br /&gt;
 	SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
 	// Set all analog pins to be digital I/O&lt;br /&gt;
    	AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
 	//Setup TRIS bits for switches and I2C pins&lt;br /&gt;
 	TRISAbits.TRISA14=0;&lt;br /&gt;
 	TRISAbits.TRISA15=0;&lt;br /&gt;
 &lt;br /&gt;
 	//Initialize all of the LED pins&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
        mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
 	unsigned char SlaveAddress;   //Slave address variable to tell the master where to send the data.  &lt;br /&gt;
                                      //Will be re-assigned for multiple slaves.&lt;br /&gt;
 &lt;br /&gt;
 	//Enable I2C channel and set the baud rate to BRG_VAL)&lt;br /&gt;
 	OpenI2C1( I2C_EN, BRG_VAL );&lt;br /&gt;
 &lt;br /&gt;
 	int rcv;			//For received data&lt;br /&gt;
 &lt;br /&gt;
 	//While loop to test LED functionality &lt;br /&gt;
 	while(1) {&lt;br /&gt;
 		if (swProgram) {				//First button pressed&lt;br /&gt;
 			while(swProgram) { Nop(); }	//Wait for release&lt;br /&gt;
 			mLED_2_Toggle();			//Toggle LED2&lt;br /&gt;
 			SendData(0xAA,0x40);  			//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);			//Receives data from address 0x40				&lt;br /&gt;
 			Delayms(100);&lt;br /&gt;
 		}&lt;br /&gt;
 		if (swUser) {				//Second button pressed&lt;br /&gt;
 			while(swUser) { Nop(); }&lt;br /&gt;
 			mLED_1_Toggle();			//Toggle LED1&lt;br /&gt;
 			SendData(0x23,0x40);    		//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);		        //Receives data from address 0x40			&lt;br /&gt;
 			Delayms(100);		&lt;br /&gt;
 		}&lt;br /&gt;
 	}//while loop ending&lt;br /&gt;
 &lt;br /&gt;
 	return 0;&lt;br /&gt;
 }  //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /*****************************************************&lt;br /&gt;
  * RcvData(unsigned int address)		     *&lt;br /&gt;
  *					  	     *&lt;br /&gt;
  * Gets a byte of data from I2C slave device at      *&lt;br /&gt;
  *  ADDRESS.					     *&lt;br /&gt;
  *						     *&lt;br /&gt;
  * Returns: Received data			     *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 int RcvData(unsigned int address) {&lt;br /&gt;
 	StartI2C1();				//Send line start condition&lt;br /&gt;
 	IdleI2C1();			        //Wait to complete&lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 1);	//Write out slave address OR 1 (read command)&lt;br /&gt;
 	IdleI2C1();				//Wait to complete&lt;br /&gt;
 	int rcv = MasterReadI2C1();		//Read in a value&lt;br /&gt;
 	StopI2C1();				//Send line stop condition&lt;br /&gt;
 	IdleI2C1();				//Wait co complete&lt;br /&gt;
 	return rcv;				//Return read value&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /***************************************************&lt;br /&gt;
  * SendData(int data, unsigned int address)        *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Sends a byte of data (DATA) over the I2C line   *&lt;br /&gt;
  *	to I2C address ADDRESS			    *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Returns: nothing				    *&lt;br /&gt;
  ***************************************************/&lt;br /&gt;
 void SendData (int data, unsigned int address){&lt;br /&gt;
 	StartI2C1();	        //Send the Start Bit&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 0);  //Sends the slave address over the I2C line.  This must happen first so the &lt;br /&gt;
                                              //proper slave is selected to receive data.&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1(data);  //Sends data byte over I2C line&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	StopI2C1();	        //Send the Stop condition&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 } //end function&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
 	}&lt;br /&gt;
 	CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
 /***********************************************************************&lt;br /&gt;
  * PIC32 I2C Slave Code                &lt;br /&gt;
  ***********************************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;GenericTypeDefs.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;Compiler.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000&lt;br /&gt;
 #define BRG_VAL 	((PBCLK/2/Fsck)-2)&lt;br /&gt;
 &lt;br /&gt;
 // this is the modules Slave Address&lt;br /&gt;
 #define SLAVE_ADDRESS 0x40&lt;br /&gt;
 &lt;br /&gt;
 // volatile variables to hold the switch and led states&lt;br /&gt;
 volatile unsigned char dataRead = 0;&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	InitI2C&lt;br /&gt;
 //&lt;br /&gt;
 // 	Perform initialisation of the I2C module to operate as a slave&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void InitI2C(void)&lt;br /&gt;
 {&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	&lt;br /&gt;
 	// Enable the I2C module with clock stretching enabled&lt;br /&gt;
 	OpenI2C1(I2C_ON | I2C_7BIT_ADD | I2C_STR_EN, BRG_VAL);&lt;br /&gt;
 	&lt;br /&gt;
 	// set the address of the slave module, address matching is with bits&lt;br /&gt;
 	// 7:1 of the message compared with bits 6:0 of the ADD SFR so we&lt;br /&gt;
 	// need to shift the desired address 1 bit. &lt;br /&gt;
 	I2C1ADD = SLAVE_ADDRESS; // &amp;gt;&amp;gt; 1;&lt;br /&gt;
 	I2C1MSK = 0;&lt;br /&gt;
 	&lt;br /&gt;
 	// configure the interrupt priority for the I2C peripheral&lt;br /&gt;
 	mI2C1SetIntPriority(I2C_INT_PRI_3 | I2C_INT_SLAVE);&lt;br /&gt;
 &lt;br /&gt;
 	// clear pending interrupts and enable I2C interrupts&lt;br /&gt;
 	mI2C1SClearIntFlag();&lt;br /&gt;
 	EnableIntSI2C1;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	main routine&lt;br /&gt;
 // &lt;br /&gt;
 //	This code example demonstrates using the PIC32 as an I2C slave&lt;br /&gt;
 //	&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 int main (void)&lt;br /&gt;
 {&lt;br /&gt;
 	// set for 80MHz operation&lt;br /&gt;
 	SYSTEMConfigPerformance(SYSCLK);&lt;br /&gt;
 	// set the Pbus to be 40000000&lt;br /&gt;
 	mOSCSetPBDIV(OSC_PB_DIV_2);&lt;br /&gt;
 	// disable the JTAG port&lt;br /&gt;
 	mJTAGPortEnable(0);&lt;br /&gt;
 	// enable interrupts&lt;br /&gt;
 	INTEnableSystemMultiVectoredInt();&lt;br /&gt;
 	&lt;br /&gt;
 	InitI2C();&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
 &lt;br /&gt;
 	// main loop&lt;br /&gt;
 	while (1) {&lt;br /&gt;
 		/* If global variable &amp;quot;dataRead&amp;quot; is set high during interrupt, turn on all LEDs */&lt;br /&gt;
 		if (dataRead == 0xAA)&lt;br /&gt;
 		{&lt;br /&gt;
 			mLED_0_On();&lt;br /&gt;
 			mLED_1_On();&lt;br /&gt;
 			mLED_2_On();			&lt;br /&gt;
 			mLED_3_On();			&lt;br /&gt;
 			&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 // Slave I2C interrupt handler&lt;br /&gt;
 // This handler is called when a qualifying I2C events occurs&lt;br /&gt;
 // this means that as well as Slave events &lt;br /&gt;
 // Master and Bus Collision events will also trigger this handler.&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void __ISR(_I2C_1_VECTOR, ipl3) _SlaveI2CHandler(void)&lt;br /&gt;
 {&lt;br /&gt;
 	mLED_1_On();&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	static unsigned int dIndex;&lt;br /&gt;
 	&lt;br /&gt;
 	// check for MASTER and Bus events and respond accordingly&lt;br /&gt;
 	if (IFS0bits.I2C1MIF == 1) {&lt;br /&gt;
 		mI2C1MClearIntFlag();&lt;br /&gt;
 		return;		&lt;br /&gt;
 	}&lt;br /&gt;
 	if (IFS0bits.I2C1BIF == 1) {&lt;br /&gt;
 		mI2C1BClearIntFlag();&lt;br /&gt;
 		return;&lt;br /&gt;
 	}&lt;br /&gt;
 	mLED_1_Off();&lt;br /&gt;
 	mLED_2_On();&lt;br /&gt;
 	&lt;br /&gt;
 	// handle the incoming message&lt;br /&gt;
 	if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 0 --&amp;gt; indicates last byte was address  &lt;br /&gt;
 		&lt;br /&gt;
 		// reset any state variables needed by a message sequence	&lt;br /&gt;
 		// perform a dummy read of the address&lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release the clock&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		// writing data to our module, just store it in adcSample&lt;br /&gt;
 		dataRead = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release clock stretch bit&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is output from slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was address&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// read of the slave device, read the address &lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		dIndex = 0;&lt;br /&gt;
 		SlaveWriteI2C1(dataRead);&lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		&lt;br /&gt;
 		// output the data until the MASTER terminates the&lt;br /&gt;
 		// transfer with a NACK, continuing reads return 0&lt;br /&gt;
 		if (dIndex == 0) {&lt;br /&gt;
 			SlaveWriteI2C1(dataRead);&lt;br /&gt;
 			dIndex++;&lt;br /&gt;
 		} else&lt;br /&gt;
 			SlaveWriteI2C1(0);&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	// finally clear the slave interrupt flag&lt;br /&gt;
 	mI2C1SClearIntFlag();		&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16094</id>
		<title>PIC32MX: I2C Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16094"/>
		<updated>2010-02-16T03:19:25Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32s to communicate via I2C.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf I2C] is pronounced &amp;quot;I squared C&amp;quot; and stands for Inter-Integrated Circuit.  This protocol was designed by Phillips Semiconductors around 1992 to allow easy communication between components on the same circuit board and can achieve transfer rates of up to 400 kbit/sec.  Is is a 2 line (plus common ground) communication method for one master device to control up to 112 slave devices.  While it is possible to have multiple masters on the same I2c bus, this page will only deal with a one master configuration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Basic Operation&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two lines are named SCL1 and SDA1 where SCL1 is the CLock line and SDA1 is the DAta line.  I2C requires the lines to be high unless the master or the slave is pulling the line down, so you will need to use pull up resistors on both the clock and data lines.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;2&amp;quot; cellpadding=&amp;quot;3&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Line!! Name!! Description&lt;br /&gt;
|-&lt;br /&gt;
|SCL1||Serial Clock Line||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDA1||Data Line||Output from master&lt;br /&gt;
|-&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The data line is read each time the clock line goes high, as shown in the diagram below:&lt;br /&gt;
&lt;br /&gt;
[[image:I2C_Data_Transfer.jpg|center]]&lt;br /&gt;
&lt;br /&gt;
* S - As the master pulls down the data line, the falling edge of the data line signifies the start condition.&lt;br /&gt;
* P - As the master allows the data line to rise, the rising edge of the data line signifies the stop condition.&lt;br /&gt;
* B - Individual bits being transferred.&lt;br /&gt;
&lt;br /&gt;
The data line can only change while the clock line is low.  The data line is only read when the clock line is high and therefore 1 bit can be transferred per cycle of the clock line.&lt;br /&gt;
&lt;br /&gt;
The master is initially sends a start bit followed by the 7-bit address of the slave it wishes to communicate with. The next bit represents whether it wishes to write(0) to or read(1) from the slave.&lt;br /&gt;
&lt;br /&gt;
The slave then responds with an acknowledge bit. The transmission continues according to whether the master is attempting to read to or write from the device.&lt;br /&gt;
&lt;br /&gt;
The master then ends the transmission with a stop bit.  Another option is to send another start bit to continue the transfer in a combined message.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
&lt;br /&gt;
[[Image:I2C_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
(code description here)&lt;br /&gt;
&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /*******************************************************&lt;br /&gt;
  * I2C_Master.c: Master code for I2C communication.	*&lt;br /&gt;
  * 	Both PICS use I2C1 module to send/ recieve data.*&lt;br /&gt;
  *	The master sends different values to a slave,   *&lt;br /&gt;
  *	which uses an interrupt to respond accordingly.	*&lt;br /&gt;
  *							*&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards	*&lt;br /&gt;
  *******************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West		*&lt;br /&gt;
  * ME333 Winter 2010					*&lt;br /&gt;
  * File Created: 	05-FEB-2010			*&lt;br /&gt;
  * Last Modified: 	14-FEB-2010			*&lt;br /&gt;
  *******************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 #pragma config FPBDIV = DIV_1				//Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000      					//Frequency of (I2C) serial clock&lt;br /&gt;
  &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )                 //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 #define INPUT_A9       PORTAbits.RA9&lt;br /&gt;
 #define INPUT_A10       PORTAbits.RA10&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //function declaration for sending data and selecting slave address&lt;br /&gt;
 void SendData(int,unsigned int);&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
 This function is a delay function, causing the program to wait for approximately 4 * cnt cycles&lt;br /&gt;
 1 cycle is 1/SYSCLK seconds.&lt;br /&gt;
 */&lt;br /&gt;
 void i2c_wait(unsigned int cnt)&lt;br /&gt;
 {&lt;br /&gt;
 	while(--cnt)&lt;br /&gt;
 	{&lt;br /&gt;
 		Nop();&lt;br /&gt;
 		Nop();&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
     // Configure the proper PB frequency and the number of wait states.&lt;br /&gt;
 	SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
 	// Set all analog pins to be digital I/O&lt;br /&gt;
    	AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
 	//Setup TRIS bits for switches and I2C pins&lt;br /&gt;
 	TRISAbits.TRISA14=0;&lt;br /&gt;
 	TRISAbits.TRISA15=0;&lt;br /&gt;
 &lt;br /&gt;
 	//Initialize all of the LED pins&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
        mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
 	unsigned char SlaveAddress;   //Slave address variable to tell the master where to send the data.  &lt;br /&gt;
                                      //Will be re-assigned for multiple slaves.&lt;br /&gt;
 &lt;br /&gt;
 	//Enable I2C channel and set the baud rate to BRG_VAL)&lt;br /&gt;
 	OpenI2C1( I2C_EN, BRG_VAL );&lt;br /&gt;
 &lt;br /&gt;
 	int rcv;			//For received data&lt;br /&gt;
 &lt;br /&gt;
 	//While loop to test LED functionality &lt;br /&gt;
 	while(1) {&lt;br /&gt;
 		if (swProgram) {				//First button pressed&lt;br /&gt;
 			while(swProgram) { Nop(); }	//Wait for release&lt;br /&gt;
 			mLED_2_Toggle();			//Toggle LED2&lt;br /&gt;
 			SendData(0xAA,0x40);  			//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);			//Receives data from address 0x40				&lt;br /&gt;
 			Delayms(100);&lt;br /&gt;
 		}&lt;br /&gt;
 		if (swUser) {				//Second button pressed&lt;br /&gt;
 			while(swUser) { Nop(); }&lt;br /&gt;
 			mLED_1_Toggle();			//Toggle LED1&lt;br /&gt;
 			SendData(0x23,0x40);    		//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);		        //Receives data from address 0x40			&lt;br /&gt;
 			Delayms(100);		&lt;br /&gt;
 		}&lt;br /&gt;
 	}//while loop ending&lt;br /&gt;
 &lt;br /&gt;
 	return 0;&lt;br /&gt;
 }  //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /*****************************************************&lt;br /&gt;
  * RcvData(unsigned int address)		     *&lt;br /&gt;
  *					  	     *&lt;br /&gt;
  * Gets a byte of data from I2C slave device at      *&lt;br /&gt;
  *  ADDRESS.					     *&lt;br /&gt;
  *						     *&lt;br /&gt;
  * Returns: Received data			     *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 int RcvData(unsigned int address) {&lt;br /&gt;
 	StartI2C1();				//Send line start condition&lt;br /&gt;
 	IdleI2C1();			        //Wait to complete&lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 1);	//Write out slave address OR 1 (read command)&lt;br /&gt;
 	IdleI2C1();				//Wait to complete&lt;br /&gt;
 	int rcv = MasterReadI2C1();		//Read in a value&lt;br /&gt;
 	StopI2C1();				//Send line stop condition&lt;br /&gt;
 	IdleI2C1();				//Wait co complete&lt;br /&gt;
 	return rcv;				//Return read value&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /***************************************************&lt;br /&gt;
  * SendData(int data, unsigned int address)        *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Sends a byte of data (DATA) over the I2C line   *&lt;br /&gt;
  *	to I2C address ADDRESS			    *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Returns: nothing				    *&lt;br /&gt;
  ***************************************************/&lt;br /&gt;
 void SendData (int data, unsigned int address){&lt;br /&gt;
 	StartI2C1();	        //Send the Start Bit&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 0);  //Sends the slave address over the I2C line.  This must happen first so the &lt;br /&gt;
                                              //proper slave is selected to receive data.&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1(data);  //Sends data byte over I2C line&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	StopI2C1();	        //Send the Stop condition&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 } //end function&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
 	}&lt;br /&gt;
 	CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
 /***********************************************************************&lt;br /&gt;
  * PIC32 I2C Slave Code                &lt;br /&gt;
  ***********************************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;GenericTypeDefs.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;Compiler.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000&lt;br /&gt;
 #define BRG_VAL 	((PBCLK/2/Fsck)-2)&lt;br /&gt;
 &lt;br /&gt;
 // this is the modules Slave Address&lt;br /&gt;
 #define SLAVE_ADDRESS 0x40&lt;br /&gt;
 &lt;br /&gt;
 // volatile variables to hold the switch and led states&lt;br /&gt;
 volatile unsigned char dataRead = 0;&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	InitI2C&lt;br /&gt;
 //&lt;br /&gt;
 // 	Perform initialisation of the I2C module to operate as a slave&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void InitI2C(void)&lt;br /&gt;
 {&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	&lt;br /&gt;
 	// Enable the I2C module with clock stretching enabled&lt;br /&gt;
 	OpenI2C1(I2C_ON | I2C_7BIT_ADD | I2C_STR_EN, BRG_VAL);&lt;br /&gt;
 	&lt;br /&gt;
 	// set the address of the slave module, address matching is with bits&lt;br /&gt;
 	// 7:1 of the message compared with bits 6:0 of the ADD SFR so we&lt;br /&gt;
 	// need to shift the desired address 1 bit. &lt;br /&gt;
 	I2C1ADD = SLAVE_ADDRESS; // &amp;gt;&amp;gt; 1;&lt;br /&gt;
 	I2C1MSK = 0;&lt;br /&gt;
 	&lt;br /&gt;
 	// configure the interrupt priority for the I2C peripheral&lt;br /&gt;
 	mI2C1SetIntPriority(I2C_INT_PRI_3 | I2C_INT_SLAVE);&lt;br /&gt;
 &lt;br /&gt;
 	// clear pending interrupts and enable I2C interrupts&lt;br /&gt;
 	mI2C1SClearIntFlag();&lt;br /&gt;
 	EnableIntSI2C1;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	main routine&lt;br /&gt;
 // &lt;br /&gt;
 //	This code example demonstrates using the PIC32 as an I2C slave&lt;br /&gt;
 //	&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 int main (void)&lt;br /&gt;
 {&lt;br /&gt;
 	// set for 80MHz operation&lt;br /&gt;
 	SYSTEMConfigPerformance(SYSCLK);&lt;br /&gt;
 	// set the Pbus to be 40000000&lt;br /&gt;
 	mOSCSetPBDIV(OSC_PB_DIV_2);&lt;br /&gt;
 	// disable the JTAG port&lt;br /&gt;
 	mJTAGPortEnable(0);&lt;br /&gt;
 	// enable interrupts&lt;br /&gt;
 	INTEnableSystemMultiVectoredInt();&lt;br /&gt;
 	&lt;br /&gt;
 	InitI2C();&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
 &lt;br /&gt;
 	// main loop&lt;br /&gt;
 	while (1) {&lt;br /&gt;
 		/* If global variable &amp;quot;dataRead&amp;quot; is set high during interrupt, turn on all LEDs */&lt;br /&gt;
 		if (dataRead == 0xAA)&lt;br /&gt;
 		{&lt;br /&gt;
 			mLED_0_On();&lt;br /&gt;
 			mLED_1_On();&lt;br /&gt;
 			mLED_2_On();			&lt;br /&gt;
 			mLED_3_On();			&lt;br /&gt;
 			&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 // Slave I2C interrupt handler&lt;br /&gt;
 // This handler is called when a qualifying I2C events occurs&lt;br /&gt;
 // this means that as well as Slave events &lt;br /&gt;
 // Master and Bus Collision events will also trigger this handler.&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void __ISR(_I2C_1_VECTOR, ipl3) _SlaveI2CHandler(void)&lt;br /&gt;
 {&lt;br /&gt;
 	mLED_1_On();&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	static unsigned int dIndex;&lt;br /&gt;
 	&lt;br /&gt;
 	// check for MASTER and Bus events and respond accordingly&lt;br /&gt;
 	if (IFS0bits.I2C1MIF == 1) {&lt;br /&gt;
 		mI2C1MClearIntFlag();&lt;br /&gt;
 		return;		&lt;br /&gt;
 	}&lt;br /&gt;
 	if (IFS0bits.I2C1BIF == 1) {&lt;br /&gt;
 		mI2C1BClearIntFlag();&lt;br /&gt;
 		return;&lt;br /&gt;
 	}&lt;br /&gt;
 	mLED_1_Off();&lt;br /&gt;
 	mLED_2_On();&lt;br /&gt;
 	&lt;br /&gt;
 	// handle the incoming message&lt;br /&gt;
 	if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 0 --&amp;gt; indicates last byte was address  &lt;br /&gt;
 		&lt;br /&gt;
 		// reset any state variables needed by a message sequence	&lt;br /&gt;
 		// perform a dummy read of the address&lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release the clock&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		// writing data to our module, just store it in adcSample&lt;br /&gt;
 		dataRead = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release clock stretch bit&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is output from slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was address&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// read of the slave device, read the address &lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		dIndex = 0;&lt;br /&gt;
 		SlaveWriteI2C1(dataRead);&lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		&lt;br /&gt;
 		// output the data until the MASTER terminates the&lt;br /&gt;
 		// transfer with a NACK, continuing reads return 0&lt;br /&gt;
 		if (dIndex == 0) {&lt;br /&gt;
 			SlaveWriteI2C1(dataRead);&lt;br /&gt;
 			dIndex++;&lt;br /&gt;
 		} else&lt;br /&gt;
 			SlaveWriteI2C1(0);&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	// finally clear the slave interrupt flag&lt;br /&gt;
 	mI2C1SClearIntFlag();		&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16089</id>
		<title>PIC32MX: I2C Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16089"/>
		<updated>2010-02-16T03:15:44Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32s to communicate via I2C.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf I2C] is pronounced &amp;quot;I squared C&amp;quot; and stands for Inter-Integrated Circuit.  This protocol was designed by Phillips Semiconductors around 1992 to allow easy communication between components on the same circuit board and can achieve transfer rates of up to 400 kbit/sec.  Is is a 2 line (plus common ground) communication method for one master device to control up to 112 slave devices.  While it is possible to have multiple masters on the same I2c bus, this page will only deal with a one master configuration.&lt;br /&gt;
&lt;br /&gt;
The two lines are named SCL and SDA where SCL is the CLock line and SDA is the DAta line.  The PIC 4520 is designed to use pin 18 as SCL and pin 23 as SDA for hardware I2C.  Note that pin 18 is one of the only pins not accessible on the prototyping board.  I2C requires the lines to be high unless the master or the slave is pulling the line down, so you will need to use pull up resistors on both the clock and data lines.&lt;br /&gt;
&lt;br /&gt;
The data line is read each time the clock line goes high, as shown in the diagram below:&lt;br /&gt;
&lt;br /&gt;
[[image:I2C_Data_Transfer.jpg|center]]&lt;br /&gt;
&lt;br /&gt;
* S - As the master pulls down the data line, the falling edge of the data line signifies the start condition.&lt;br /&gt;
* P - As the master allows the data line to rise, the rising edge of the data line signifies the stop condition.&lt;br /&gt;
* B - Individual bits being transferred.&lt;br /&gt;
&lt;br /&gt;
The data line can only change while the clock line is low.  The data line is only read when the clock line is high and therefore 1 bit can be transferred per cycle of the clock line.&lt;br /&gt;
&lt;br /&gt;
The master is initially sends a start bit followed by the 7-bit address of the slave it wishes to communicate with. The next bit represents whether it wishes to write(0) to or read(1) from the slave.&lt;br /&gt;
&lt;br /&gt;
The slave then responds with an acknowledge bit. The transmission continues according to whether the master is attempting to read to or write from the device.&lt;br /&gt;
&lt;br /&gt;
The master then ends the transmission with a stop bit.  Another option is to send another start bit to continue the transfer in a combined message.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
&lt;br /&gt;
[[Image:I2C_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
(code description here)&lt;br /&gt;
&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /*******************************************************&lt;br /&gt;
  * I2C_Master.c: Master code for I2C communication.	*&lt;br /&gt;
  * 	Both PICS use I2C1 module to send/ recieve data.*&lt;br /&gt;
  *	The master sends different values to a slave,   *&lt;br /&gt;
  *	which uses an interrupt to respond accordingly.	*&lt;br /&gt;
  *							*&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards	*&lt;br /&gt;
  *******************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West		*&lt;br /&gt;
  * ME333 Winter 2010					*&lt;br /&gt;
  * File Created: 	05-FEB-2010			*&lt;br /&gt;
  * Last Modified: 	14-FEB-2010			*&lt;br /&gt;
  *******************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 #pragma config FPBDIV = DIV_1				//Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000      					//Frequency of (I2C) serial clock&lt;br /&gt;
  &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )                 //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 #define INPUT_A9       PORTAbits.RA9&lt;br /&gt;
 #define INPUT_A10       PORTAbits.RA10&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //function declaration for sending data and selecting slave address&lt;br /&gt;
 void SendData(int,unsigned int);&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
 This function is a delay function, causing the program to wait for approximately 4 * cnt cycles&lt;br /&gt;
 1 cycle is 1/SYSCLK seconds.&lt;br /&gt;
 */&lt;br /&gt;
 void i2c_wait(unsigned int cnt)&lt;br /&gt;
 {&lt;br /&gt;
 	while(--cnt)&lt;br /&gt;
 	{&lt;br /&gt;
 		Nop();&lt;br /&gt;
 		Nop();&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
     // Configure the proper PB frequency and the number of wait states.&lt;br /&gt;
 	SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
 	// Set all analog pins to be digital I/O&lt;br /&gt;
    	AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
 	//Setup TRIS bits for switches and I2C pins&lt;br /&gt;
 	TRISAbits.TRISA14=0;&lt;br /&gt;
 	TRISAbits.TRISA15=0;&lt;br /&gt;
 &lt;br /&gt;
 	//Initialize all of the LED pins&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
        mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
 	unsigned char SlaveAddress;   //Slave address variable to tell the master where to send the data.  &lt;br /&gt;
                                      //Will be re-assigned for multiple slaves.&lt;br /&gt;
 &lt;br /&gt;
 	//Enable I2C channel and set the baud rate to BRG_VAL)&lt;br /&gt;
 	OpenI2C1( I2C_EN, BRG_VAL );&lt;br /&gt;
 &lt;br /&gt;
 	int rcv;			//For received data&lt;br /&gt;
 &lt;br /&gt;
 	//While loop to test LED functionality &lt;br /&gt;
 	while(1) {&lt;br /&gt;
 		if (swProgram) {				//First button pressed&lt;br /&gt;
 			while(swProgram) { Nop(); }	//Wait for release&lt;br /&gt;
 			mLED_2_Toggle();			//Toggle LED2&lt;br /&gt;
 			SendData(0xAA,0x40);  			//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);			//Receives data from address 0x40				&lt;br /&gt;
 			Delayms(100);&lt;br /&gt;
 		}&lt;br /&gt;
 		if (swUser) {				//Second button pressed&lt;br /&gt;
 			while(swUser) { Nop(); }&lt;br /&gt;
 			mLED_1_Toggle();			//Toggle LED1&lt;br /&gt;
 			SendData(0x23,0x40);    		//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);		        //Receives data from address 0x40			&lt;br /&gt;
 			Delayms(100);		&lt;br /&gt;
 		}&lt;br /&gt;
 	}//while loop ending&lt;br /&gt;
 &lt;br /&gt;
 	return 0;&lt;br /&gt;
 }  //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /*****************************************************&lt;br /&gt;
  * RcvData(unsigned int address)		     *&lt;br /&gt;
  *					  	     *&lt;br /&gt;
  * Gets a byte of data from I2C slave device at      *&lt;br /&gt;
  *  ADDRESS.					     *&lt;br /&gt;
  *						     *&lt;br /&gt;
  * Returns: Received data			     *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 int RcvData(unsigned int address) {&lt;br /&gt;
 	StartI2C1();				//Send line start condition&lt;br /&gt;
 	IdleI2C1();			        //Wait to complete&lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 1);	//Write out slave address OR 1 (read command)&lt;br /&gt;
 	IdleI2C1();				//Wait to complete&lt;br /&gt;
 	int rcv = MasterReadI2C1();		//Read in a value&lt;br /&gt;
 	StopI2C1();				//Send line stop condition&lt;br /&gt;
 	IdleI2C1();				//Wait co complete&lt;br /&gt;
 	return rcv;				//Return read value&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /***************************************************&lt;br /&gt;
  * SendData(int data, unsigned int address)        *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Sends a byte of data (DATA) over the I2C line   *&lt;br /&gt;
  *	to I2C address ADDRESS			    *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Returns: nothing				    *&lt;br /&gt;
  ***************************************************/&lt;br /&gt;
 void SendData (int data, unsigned int address){&lt;br /&gt;
 	StartI2C1();	        //Send the Start Bit&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 0);  //Sends the slave address over the I2C line.  This must happen first so the &lt;br /&gt;
                                              //proper slave is selected to receive data.&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1(data);  //Sends data byte over I2C line&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	StopI2C1();	        //Send the Stop condition&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 } //end function&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
 	}&lt;br /&gt;
 	CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
 /***********************************************************************&lt;br /&gt;
  * PIC32 I2C Slave Code                &lt;br /&gt;
  ***********************************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;GenericTypeDefs.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;Compiler.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000&lt;br /&gt;
 #define BRG_VAL 	((PBCLK/2/Fsck)-2)&lt;br /&gt;
 &lt;br /&gt;
 // this is the modules Slave Address&lt;br /&gt;
 #define SLAVE_ADDRESS 0x40&lt;br /&gt;
 &lt;br /&gt;
 // volatile variables to hold the switch and led states&lt;br /&gt;
 volatile unsigned char dataRead = 0;&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	InitI2C&lt;br /&gt;
 //&lt;br /&gt;
 // 	Perform initialisation of the I2C module to operate as a slave&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void InitI2C(void)&lt;br /&gt;
 {&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	&lt;br /&gt;
 	// Enable the I2C module with clock stretching enabled&lt;br /&gt;
 	OpenI2C1(I2C_ON | I2C_7BIT_ADD | I2C_STR_EN, BRG_VAL);&lt;br /&gt;
 	&lt;br /&gt;
 	// set the address of the slave module, address matching is with bits&lt;br /&gt;
 	// 7:1 of the message compared with bits 6:0 of the ADD SFR so we&lt;br /&gt;
 	// need to shift the desired address 1 bit. &lt;br /&gt;
 	I2C1ADD = SLAVE_ADDRESS; // &amp;gt;&amp;gt; 1;&lt;br /&gt;
 	I2C1MSK = 0;&lt;br /&gt;
 	&lt;br /&gt;
 	// configure the interrupt priority for the I2C peripheral&lt;br /&gt;
 	mI2C1SetIntPriority(I2C_INT_PRI_3 | I2C_INT_SLAVE);&lt;br /&gt;
 &lt;br /&gt;
 	// clear pending interrupts and enable I2C interrupts&lt;br /&gt;
 	mI2C1SClearIntFlag();&lt;br /&gt;
 	EnableIntSI2C1;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	main routine&lt;br /&gt;
 // &lt;br /&gt;
 //	This code example demonstrates using the PIC32 as an I2C slave&lt;br /&gt;
 //	&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 int main (void)&lt;br /&gt;
 {&lt;br /&gt;
 	// set for 80MHz operation&lt;br /&gt;
 	SYSTEMConfigPerformance(SYSCLK);&lt;br /&gt;
 	// set the Pbus to be 40000000&lt;br /&gt;
 	mOSCSetPBDIV(OSC_PB_DIV_2);&lt;br /&gt;
 	// disable the JTAG port&lt;br /&gt;
 	mJTAGPortEnable(0);&lt;br /&gt;
 	// enable interrupts&lt;br /&gt;
 	INTEnableSystemMultiVectoredInt();&lt;br /&gt;
 	&lt;br /&gt;
 	InitI2C();&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
 &lt;br /&gt;
 	// main loop&lt;br /&gt;
 	while (1) {&lt;br /&gt;
 		/* If global variable &amp;quot;dataRead&amp;quot; is set high during interrupt, turn on all LEDs */&lt;br /&gt;
 		if (dataRead == 0xAA)&lt;br /&gt;
 		{&lt;br /&gt;
 			mLED_0_On();&lt;br /&gt;
 			mLED_1_On();&lt;br /&gt;
 			mLED_2_On();			&lt;br /&gt;
 			mLED_3_On();			&lt;br /&gt;
 			&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 // Slave I2C interrupt handler&lt;br /&gt;
 // This handler is called when a qualifying I2C events occurs&lt;br /&gt;
 // this means that as well as Slave events &lt;br /&gt;
 // Master and Bus Collision events will also trigger this handler.&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void __ISR(_I2C_1_VECTOR, ipl3) _SlaveI2CHandler(void)&lt;br /&gt;
 {&lt;br /&gt;
 	mLED_1_On();&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	static unsigned int dIndex;&lt;br /&gt;
 	&lt;br /&gt;
 	// check for MASTER and Bus events and respond accordingly&lt;br /&gt;
 	if (IFS0bits.I2C1MIF == 1) {&lt;br /&gt;
 		mI2C1MClearIntFlag();&lt;br /&gt;
 		return;		&lt;br /&gt;
 	}&lt;br /&gt;
 	if (IFS0bits.I2C1BIF == 1) {&lt;br /&gt;
 		mI2C1BClearIntFlag();&lt;br /&gt;
 		return;&lt;br /&gt;
 	}&lt;br /&gt;
 	mLED_1_Off();&lt;br /&gt;
 	mLED_2_On();&lt;br /&gt;
 	&lt;br /&gt;
 	// handle the incoming message&lt;br /&gt;
 	if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 0 --&amp;gt; indicates last byte was address  &lt;br /&gt;
 		&lt;br /&gt;
 		// reset any state variables needed by a message sequence	&lt;br /&gt;
 		// perform a dummy read of the address&lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release the clock&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		// writing data to our module, just store it in adcSample&lt;br /&gt;
 		dataRead = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release clock stretch bit&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is output from slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was address&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// read of the slave device, read the address &lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		dIndex = 0;&lt;br /&gt;
 		SlaveWriteI2C1(dataRead);&lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		&lt;br /&gt;
 		// output the data until the MASTER terminates the&lt;br /&gt;
 		// transfer with a NACK, continuing reads return 0&lt;br /&gt;
 		if (dIndex == 0) {&lt;br /&gt;
 			SlaveWriteI2C1(dataRead);&lt;br /&gt;
 			dIndex++;&lt;br /&gt;
 		} else&lt;br /&gt;
 			SlaveWriteI2C1(0);&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	// finally clear the slave interrupt flag&lt;br /&gt;
 	mI2C1SClearIntFlag();		&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16086</id>
		<title>PIC32MX: I2C Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16086"/>
		<updated>2010-02-16T03:11:58Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Overview */  - copied from previous I2C page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32s to communicate via I2C.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
[http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf I2C] is pronounced &amp;quot;I squared C&amp;quot; and stands for Inter-Integrated Circuit.  This protocol was designed by Phillips Semiconductors around 1992 to allow easy communication between components on the same circuit board and can achieve transfer rates of up to 400 kbit/sec.  Is is a 2 line (plus common ground) communication method for one master device to control up to 112 slave devices.  While it is possible to have multiple masters on the same I2c bus, this page will only deal with a one master configuration.&lt;br /&gt;
&lt;br /&gt;
The two lines are named SCL and SDA where SCL is the CLock line and SDA is the DAta line.  The PIC 4520 is designed to use pin 18 as SCL and pin 23 as SDA for hardware I2C.  Note that pin 18 is one of the only pins not accessible on the prototyping board.  An additional wire can be added as shown in the example image.  It is possible to use lines accessible from the prototyping board to communicate via software I2C, but this has data rates of less than one fourth those of hardware I2C and will therefore be omitted.  I2C requires the lines to be high unless the master or the slave is pulling the line down, so you will need to use pull up resistors on both the clock and data lines.&lt;br /&gt;
&lt;br /&gt;
An communication diagram is shown:&lt;br /&gt;
&lt;br /&gt;
[[image:I2C_Data_Transfer.jpg|center]]&lt;br /&gt;
&lt;br /&gt;
* S - As the master pulls down the data line, the falling edge of the data line signifies the start condition.&lt;br /&gt;
* P - As the master allows the data line to rise, the rising edge of the data line signifies the stop condition.&lt;br /&gt;
* B - Individual bits being transferred.&lt;br /&gt;
&lt;br /&gt;
The data line can only change while the clock line is low.  The data line is only read when the clock line is high and therefore 1 bit can be transferred per cycle of the clock line.&lt;br /&gt;
&lt;br /&gt;
The master is initially sends a start bit followed by the 7-bit address of the slave it wishes to communicate with. The next bit represents whether it wishes to write(0) to or read(1) from the slave.&lt;br /&gt;
&lt;br /&gt;
The slave then responds with an acknowledge bit. The transmission continues according to whether the master is attempting to read to or write from the device.&lt;br /&gt;
&lt;br /&gt;
The master then ends the transmission with a stop bit.  Another option is to send another start bit to continue the transfer in a combined message.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
&lt;br /&gt;
[[Image:I2C_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
(code description here)&lt;br /&gt;
&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /*******************************************************&lt;br /&gt;
  * I2C_Master.c: Master code for I2C communication.	*&lt;br /&gt;
  * 	Both PICS use I2C1 module to send/ recieve data.*&lt;br /&gt;
  *	The master sends different values to a slave,   *&lt;br /&gt;
  *	which uses an interrupt to respond accordingly.	*&lt;br /&gt;
  *							*&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards	*&lt;br /&gt;
  *******************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West		*&lt;br /&gt;
  * ME333 Winter 2010					*&lt;br /&gt;
  * File Created: 	05-FEB-2010			*&lt;br /&gt;
  * Last Modified: 	14-FEB-2010			*&lt;br /&gt;
  *******************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 #pragma config FPBDIV = DIV_1				//Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000      					//Frequency of (I2C) serial clock&lt;br /&gt;
  &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )                 //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 #define INPUT_A9       PORTAbits.RA9&lt;br /&gt;
 #define INPUT_A10       PORTAbits.RA10&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //function declaration for sending data and selecting slave address&lt;br /&gt;
 void SendData(int,unsigned int);&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
 This function is a delay function, causing the program to wait for approximately 4 * cnt cycles&lt;br /&gt;
 1 cycle is 1/SYSCLK seconds.&lt;br /&gt;
 */&lt;br /&gt;
 void i2c_wait(unsigned int cnt)&lt;br /&gt;
 {&lt;br /&gt;
 	while(--cnt)&lt;br /&gt;
 	{&lt;br /&gt;
 		Nop();&lt;br /&gt;
 		Nop();&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
     // Configure the proper PB frequency and the number of wait states.&lt;br /&gt;
 	SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
 	// Set all analog pins to be digital I/O&lt;br /&gt;
    	AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
 	//Setup TRIS bits for switches and I2C pins&lt;br /&gt;
 	TRISAbits.TRISA14=0;&lt;br /&gt;
 	TRISAbits.TRISA15=0;&lt;br /&gt;
 &lt;br /&gt;
 	//Initialize all of the LED pins&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
        mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
 	unsigned char SlaveAddress;   //Slave address variable to tell the master where to send the data.  &lt;br /&gt;
                                      //Will be re-assigned for multiple slaves.&lt;br /&gt;
 &lt;br /&gt;
 	//Enable I2C channel and set the baud rate to BRG_VAL)&lt;br /&gt;
 	OpenI2C1( I2C_EN, BRG_VAL );&lt;br /&gt;
 &lt;br /&gt;
 	int rcv;			//For received data&lt;br /&gt;
 &lt;br /&gt;
 	//While loop to test LED functionality &lt;br /&gt;
 	while(1) {&lt;br /&gt;
 		if (swProgram) {				//First button pressed&lt;br /&gt;
 			while(swProgram) { Nop(); }	//Wait for release&lt;br /&gt;
 			mLED_2_Toggle();			//Toggle LED2&lt;br /&gt;
 			SendData(0xAA,0x40);  			//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);			//Receives data from address 0x40				&lt;br /&gt;
 			Delayms(100);&lt;br /&gt;
 		}&lt;br /&gt;
 		if (swUser) {				//Second button pressed&lt;br /&gt;
 			while(swUser) { Nop(); }&lt;br /&gt;
 			mLED_1_Toggle();			//Toggle LED1&lt;br /&gt;
 			SendData(0x23,0x40);    		//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);		        //Receives data from address 0x40			&lt;br /&gt;
 			Delayms(100);		&lt;br /&gt;
 		}&lt;br /&gt;
 	}//while loop ending&lt;br /&gt;
 &lt;br /&gt;
 	return 0;&lt;br /&gt;
 }  //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /*****************************************************&lt;br /&gt;
  * RcvData(unsigned int address)		     *&lt;br /&gt;
  *					  	     *&lt;br /&gt;
  * Gets a byte of data from I2C slave device at      *&lt;br /&gt;
  *  ADDRESS.					     *&lt;br /&gt;
  *						     *&lt;br /&gt;
  * Returns: Received data			     *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 int RcvData(unsigned int address) {&lt;br /&gt;
 	StartI2C1();				//Send line start condition&lt;br /&gt;
 	IdleI2C1();			        //Wait to complete&lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 1);	//Write out slave address OR 1 (read command)&lt;br /&gt;
 	IdleI2C1();				//Wait to complete&lt;br /&gt;
 	int rcv = MasterReadI2C1();		//Read in a value&lt;br /&gt;
 	StopI2C1();				//Send line stop condition&lt;br /&gt;
 	IdleI2C1();				//Wait co complete&lt;br /&gt;
 	return rcv;				//Return read value&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /***************************************************&lt;br /&gt;
  * SendData(int data, unsigned int address)        *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Sends a byte of data (DATA) over the I2C line   *&lt;br /&gt;
  *	to I2C address ADDRESS			    *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Returns: nothing				    *&lt;br /&gt;
  ***************************************************/&lt;br /&gt;
 void SendData (int data, unsigned int address){&lt;br /&gt;
 	StartI2C1();	        //Send the Start Bit&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 0);  //Sends the slave address over the I2C line.  This must happen first so the &lt;br /&gt;
                                              //proper slave is selected to receive data.&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1(data);  //Sends data byte over I2C line&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	StopI2C1();	        //Send the Stop condition&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 } //end function&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
 	}&lt;br /&gt;
 	CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
 /***********************************************************************&lt;br /&gt;
  * PIC32 I2C Slave Code                &lt;br /&gt;
  ***********************************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;GenericTypeDefs.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;Compiler.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000&lt;br /&gt;
 #define BRG_VAL 	((PBCLK/2/Fsck)-2)&lt;br /&gt;
 &lt;br /&gt;
 // this is the modules Slave Address&lt;br /&gt;
 #define SLAVE_ADDRESS 0x40&lt;br /&gt;
 &lt;br /&gt;
 // volatile variables to hold the switch and led states&lt;br /&gt;
 volatile unsigned char dataRead = 0;&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	InitI2C&lt;br /&gt;
 //&lt;br /&gt;
 // 	Perform initialisation of the I2C module to operate as a slave&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void InitI2C(void)&lt;br /&gt;
 {&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	&lt;br /&gt;
 	// Enable the I2C module with clock stretching enabled&lt;br /&gt;
 	OpenI2C1(I2C_ON | I2C_7BIT_ADD | I2C_STR_EN, BRG_VAL);&lt;br /&gt;
 	&lt;br /&gt;
 	// set the address of the slave module, address matching is with bits&lt;br /&gt;
 	// 7:1 of the message compared with bits 6:0 of the ADD SFR so we&lt;br /&gt;
 	// need to shift the desired address 1 bit. &lt;br /&gt;
 	I2C1ADD = SLAVE_ADDRESS; // &amp;gt;&amp;gt; 1;&lt;br /&gt;
 	I2C1MSK = 0;&lt;br /&gt;
 	&lt;br /&gt;
 	// configure the interrupt priority for the I2C peripheral&lt;br /&gt;
 	mI2C1SetIntPriority(I2C_INT_PRI_3 | I2C_INT_SLAVE);&lt;br /&gt;
 &lt;br /&gt;
 	// clear pending interrupts and enable I2C interrupts&lt;br /&gt;
 	mI2C1SClearIntFlag();&lt;br /&gt;
 	EnableIntSI2C1;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	main routine&lt;br /&gt;
 // &lt;br /&gt;
 //	This code example demonstrates using the PIC32 as an I2C slave&lt;br /&gt;
 //	&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 int main (void)&lt;br /&gt;
 {&lt;br /&gt;
 	// set for 80MHz operation&lt;br /&gt;
 	SYSTEMConfigPerformance(SYSCLK);&lt;br /&gt;
 	// set the Pbus to be 40000000&lt;br /&gt;
 	mOSCSetPBDIV(OSC_PB_DIV_2);&lt;br /&gt;
 	// disable the JTAG port&lt;br /&gt;
 	mJTAGPortEnable(0);&lt;br /&gt;
 	// enable interrupts&lt;br /&gt;
 	INTEnableSystemMultiVectoredInt();&lt;br /&gt;
 	&lt;br /&gt;
 	InitI2C();&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
 &lt;br /&gt;
 	// main loop&lt;br /&gt;
 	while (1) {&lt;br /&gt;
 		/* If global variable &amp;quot;dataRead&amp;quot; is set high during interrupt, turn on all LEDs */&lt;br /&gt;
 		if (dataRead == 0xAA)&lt;br /&gt;
 		{&lt;br /&gt;
 			mLED_0_On();&lt;br /&gt;
 			mLED_1_On();&lt;br /&gt;
 			mLED_2_On();			&lt;br /&gt;
 			mLED_3_On();			&lt;br /&gt;
 			&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 // Slave I2C interrupt handler&lt;br /&gt;
 // This handler is called when a qualifying I2C events occurs&lt;br /&gt;
 // this means that as well as Slave events &lt;br /&gt;
 // Master and Bus Collision events will also trigger this handler.&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void __ISR(_I2C_1_VECTOR, ipl3) _SlaveI2CHandler(void)&lt;br /&gt;
 {&lt;br /&gt;
 	mLED_1_On();&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	static unsigned int dIndex;&lt;br /&gt;
 	&lt;br /&gt;
 	// check for MASTER and Bus events and respond accordingly&lt;br /&gt;
 	if (IFS0bits.I2C1MIF == 1) {&lt;br /&gt;
 		mI2C1MClearIntFlag();&lt;br /&gt;
 		return;		&lt;br /&gt;
 	}&lt;br /&gt;
 	if (IFS0bits.I2C1BIF == 1) {&lt;br /&gt;
 		mI2C1BClearIntFlag();&lt;br /&gt;
 		return;&lt;br /&gt;
 	}&lt;br /&gt;
 	mLED_1_Off();&lt;br /&gt;
 	mLED_2_On();&lt;br /&gt;
 	&lt;br /&gt;
 	// handle the incoming message&lt;br /&gt;
 	if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 0 --&amp;gt; indicates last byte was address  &lt;br /&gt;
 		&lt;br /&gt;
 		// reset any state variables needed by a message sequence	&lt;br /&gt;
 		// perform a dummy read of the address&lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release the clock&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		// writing data to our module, just store it in adcSample&lt;br /&gt;
 		dataRead = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release clock stretch bit&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is output from slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was address&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// read of the slave device, read the address &lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		dIndex = 0;&lt;br /&gt;
 		SlaveWriteI2C1(dataRead);&lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		&lt;br /&gt;
 		// output the data until the MASTER terminates the&lt;br /&gt;
 		// transfer with a NACK, continuing reads return 0&lt;br /&gt;
 		if (dIndex == 0) {&lt;br /&gt;
 			SlaveWriteI2C1(dataRead);&lt;br /&gt;
 			dIndex++;&lt;br /&gt;
 		} else&lt;br /&gt;
 			SlaveWriteI2C1(0);&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	// finally clear the slave interrupt flag&lt;br /&gt;
 	mI2C1SClearIntFlag();		&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16085</id>
		<title>PIC32MX: I2C Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_I2C_Communication_between_PIC32s&amp;diff=16085"/>
		<updated>2010-02-16T03:10:38Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Circuit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32s to communicate via I2C.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
Summarize briefly what the page is about.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
&lt;br /&gt;
[[Image:I2C_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
(code description here)&lt;br /&gt;
&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /*******************************************************&lt;br /&gt;
  * I2C_Master.c: Master code for I2C communication.	*&lt;br /&gt;
  * 	Both PICS use I2C1 module to send/ recieve data.*&lt;br /&gt;
  *	The master sends different values to a slave,   *&lt;br /&gt;
  *	which uses an interrupt to respond accordingly.	*&lt;br /&gt;
  *							*&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards	*&lt;br /&gt;
  *******************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West		*&lt;br /&gt;
  * ME333 Winter 2010					*&lt;br /&gt;
  * File Created: 	05-FEB-2010			*&lt;br /&gt;
  * Last Modified: 	14-FEB-2010			*&lt;br /&gt;
  *******************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 #pragma config FPBDIV = DIV_1				//Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000      					//Frequency of (I2C) serial clock&lt;br /&gt;
  &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )                 //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 #define INPUT_A9       PORTAbits.RA9&lt;br /&gt;
 #define INPUT_A10       PORTAbits.RA10&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //function declaration for sending data and selecting slave address&lt;br /&gt;
 void SendData(int,unsigned int);&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 /*&lt;br /&gt;
 This function is a delay function, causing the program to wait for approximately 4 * cnt cycles&lt;br /&gt;
 1 cycle is 1/SYSCLK seconds.&lt;br /&gt;
 */&lt;br /&gt;
 void i2c_wait(unsigned int cnt)&lt;br /&gt;
 {&lt;br /&gt;
 	while(--cnt)&lt;br /&gt;
 	{&lt;br /&gt;
 		Nop();&lt;br /&gt;
 		Nop();&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
     // Configure the proper PB frequency and the number of wait states.&lt;br /&gt;
 	SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
 	// Set all analog pins to be digital I/O&lt;br /&gt;
    	AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
 	//Setup TRIS bits for switches and I2C pins&lt;br /&gt;
 	TRISAbits.TRISA14=0;&lt;br /&gt;
 	TRISAbits.TRISA15=0;&lt;br /&gt;
 &lt;br /&gt;
 	//Initialize all of the LED pins&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
        mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
 	unsigned char SlaveAddress;   //Slave address variable to tell the master where to send the data.  &lt;br /&gt;
                                      //Will be re-assigned for multiple slaves.&lt;br /&gt;
 &lt;br /&gt;
 	//Enable I2C channel and set the baud rate to BRG_VAL)&lt;br /&gt;
 	OpenI2C1( I2C_EN, BRG_VAL );&lt;br /&gt;
 &lt;br /&gt;
 	int rcv;			//For received data&lt;br /&gt;
 &lt;br /&gt;
 	//While loop to test LED functionality &lt;br /&gt;
 	while(1) {&lt;br /&gt;
 		if (swProgram) {				//First button pressed&lt;br /&gt;
 			while(swProgram) { Nop(); }	//Wait for release&lt;br /&gt;
 			mLED_2_Toggle();			//Toggle LED2&lt;br /&gt;
 			SendData(0xAA,0x40);  			//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);			//Receives data from address 0x40				&lt;br /&gt;
 			Delayms(100);&lt;br /&gt;
 		}&lt;br /&gt;
 		if (swUser) {				//Second button pressed&lt;br /&gt;
 			while(swUser) { Nop(); }&lt;br /&gt;
 			mLED_1_Toggle();			//Toggle LED1&lt;br /&gt;
 			SendData(0x23,0x40);    		//Sends hex data 0xAA to slave address 0x40&lt;br /&gt;
 			rcv = RcvData(0x40);		        //Receives data from address 0x40			&lt;br /&gt;
 			Delayms(100);		&lt;br /&gt;
 		}&lt;br /&gt;
 	}//while loop ending&lt;br /&gt;
 &lt;br /&gt;
 	return 0;&lt;br /&gt;
 }  //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /*****************************************************&lt;br /&gt;
  * RcvData(unsigned int address)		     *&lt;br /&gt;
  *					  	     *&lt;br /&gt;
  * Gets a byte of data from I2C slave device at      *&lt;br /&gt;
  *  ADDRESS.					     *&lt;br /&gt;
  *						     *&lt;br /&gt;
  * Returns: Received data			     *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 int RcvData(unsigned int address) {&lt;br /&gt;
 	StartI2C1();				//Send line start condition&lt;br /&gt;
 	IdleI2C1();			        //Wait to complete&lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 1);	//Write out slave address OR 1 (read command)&lt;br /&gt;
 	IdleI2C1();				//Wait to complete&lt;br /&gt;
 	int rcv = MasterReadI2C1();		//Read in a value&lt;br /&gt;
 	StopI2C1();				//Send line stop condition&lt;br /&gt;
 	IdleI2C1();				//Wait co complete&lt;br /&gt;
 	return rcv;				//Return read value&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /***************************************************&lt;br /&gt;
  * SendData(int data, unsigned int address)        *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Sends a byte of data (DATA) over the I2C line   *&lt;br /&gt;
  *	to I2C address ADDRESS			    *&lt;br /&gt;
  *						    *&lt;br /&gt;
  * Returns: nothing				    *&lt;br /&gt;
  ***************************************************/&lt;br /&gt;
 void SendData (int data, unsigned int address){&lt;br /&gt;
 	StartI2C1();	        //Send the Start Bit&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1((address &amp;lt;&amp;lt; 1) | 0);  //Sends the slave address over the I2C line.  This must happen first so the &lt;br /&gt;
                                              //proper slave is selected to receive data.&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	MasterWriteI2C1(data);  //Sends data byte over I2C line&lt;br /&gt;
 	IdleI2C1();		//Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 	StopI2C1();	        //Send the Stop condition&lt;br /&gt;
 	IdleI2C1();	        //Wait to complete&lt;br /&gt;
 &lt;br /&gt;
 } //end function&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
 	}&lt;br /&gt;
 	CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
 /***********************************************************************&lt;br /&gt;
  * PIC32 I2C Slave Code                &lt;br /&gt;
  ***********************************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;GenericTypeDefs.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;Compiler.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #define SYSCLK	(80000000)&lt;br /&gt;
 #define PBCLK  (SYSCLK)&lt;br /&gt;
 &lt;br /&gt;
 #define Fsck	50000&lt;br /&gt;
 #define BRG_VAL 	((PBCLK/2/Fsck)-2)&lt;br /&gt;
 &lt;br /&gt;
 // this is the modules Slave Address&lt;br /&gt;
 #define SLAVE_ADDRESS 0x40&lt;br /&gt;
 &lt;br /&gt;
 // volatile variables to hold the switch and led states&lt;br /&gt;
 volatile unsigned char dataRead = 0;&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	InitI2C&lt;br /&gt;
 //&lt;br /&gt;
 // 	Perform initialisation of the I2C module to operate as a slave&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void InitI2C(void)&lt;br /&gt;
 {&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	&lt;br /&gt;
 	// Enable the I2C module with clock stretching enabled&lt;br /&gt;
 	OpenI2C1(I2C_ON | I2C_7BIT_ADD | I2C_STR_EN, BRG_VAL);&lt;br /&gt;
 	&lt;br /&gt;
 	// set the address of the slave module, address matching is with bits&lt;br /&gt;
 	// 7:1 of the message compared with bits 6:0 of the ADD SFR so we&lt;br /&gt;
 	// need to shift the desired address 1 bit. &lt;br /&gt;
 	I2C1ADD = SLAVE_ADDRESS; // &amp;gt;&amp;gt; 1;&lt;br /&gt;
 	I2C1MSK = 0;&lt;br /&gt;
 	&lt;br /&gt;
 	// configure the interrupt priority for the I2C peripheral&lt;br /&gt;
 	mI2C1SetIntPriority(I2C_INT_PRI_3 | I2C_INT_SLAVE);&lt;br /&gt;
 &lt;br /&gt;
 	// clear pending interrupts and enable I2C interrupts&lt;br /&gt;
 	mI2C1SClearIntFlag();&lt;br /&gt;
 	EnableIntSI2C1;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 //	main routine&lt;br /&gt;
 // &lt;br /&gt;
 //	This code example demonstrates using the PIC32 as an I2C slave&lt;br /&gt;
 //	&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 int main (void)&lt;br /&gt;
 {&lt;br /&gt;
 	// set for 80MHz operation&lt;br /&gt;
 	SYSTEMConfigPerformance(SYSCLK);&lt;br /&gt;
 	// set the Pbus to be 40000000&lt;br /&gt;
 	mOSCSetPBDIV(OSC_PB_DIV_2);&lt;br /&gt;
 	// disable the JTAG port&lt;br /&gt;
 	mJTAGPortEnable(0);&lt;br /&gt;
 	// enable interrupts&lt;br /&gt;
 	INTEnableSystemMultiVectoredInt();&lt;br /&gt;
 	&lt;br /&gt;
 	InitI2C();&lt;br /&gt;
 	mInitAllLEDs();&lt;br /&gt;
 &lt;br /&gt;
 	// main loop&lt;br /&gt;
 	while (1) {&lt;br /&gt;
 		/* If global variable &amp;quot;dataRead&amp;quot; is set high during interrupt, turn on all LEDs */&lt;br /&gt;
 		if (dataRead == 0xAA)&lt;br /&gt;
 		{&lt;br /&gt;
 			mLED_0_On();&lt;br /&gt;
 			mLED_1_On();&lt;br /&gt;
 			mLED_2_On();			&lt;br /&gt;
 			mLED_3_On();			&lt;br /&gt;
 			&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 //&lt;br /&gt;
 // Slave I2C interrupt handler&lt;br /&gt;
 // This handler is called when a qualifying I2C events occurs&lt;br /&gt;
 // this means that as well as Slave events &lt;br /&gt;
 // Master and Bus Collision events will also trigger this handler.&lt;br /&gt;
 //&lt;br /&gt;
 ///////////////////////////////////////////////////////////////////&lt;br /&gt;
 void __ISR(_I2C_1_VECTOR, ipl3) _SlaveI2CHandler(void)&lt;br /&gt;
 {&lt;br /&gt;
 	mLED_1_On();&lt;br /&gt;
 	unsigned char temp;&lt;br /&gt;
 	static unsigned int dIndex;&lt;br /&gt;
 	&lt;br /&gt;
 	// check for MASTER and Bus events and respond accordingly&lt;br /&gt;
 	if (IFS0bits.I2C1MIF == 1) {&lt;br /&gt;
 		mI2C1MClearIntFlag();&lt;br /&gt;
 		return;		&lt;br /&gt;
 	}&lt;br /&gt;
 	if (IFS0bits.I2C1BIF == 1) {&lt;br /&gt;
 		mI2C1BClearIntFlag();&lt;br /&gt;
 		return;&lt;br /&gt;
 	}&lt;br /&gt;
 	mLED_1_Off();&lt;br /&gt;
 	mLED_2_On();&lt;br /&gt;
 	&lt;br /&gt;
 	// handle the incoming message&lt;br /&gt;
 	if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 0 --&amp;gt; indicates last byte was address  &lt;br /&gt;
 		&lt;br /&gt;
 		// reset any state variables needed by a message sequence	&lt;br /&gt;
 		// perform a dummy read of the address&lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release the clock&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 0) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 0 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		&lt;br /&gt;
 		mLED_3_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		// writing data to our module, just store it in adcSample&lt;br /&gt;
 		dataRead = SlaveReadI2C1();&lt;br /&gt;
 		&lt;br /&gt;
 		// release the clock to restart I2C&lt;br /&gt;
 		I2C1CONbits.SCLREL = 1; // release clock stretch bit&lt;br /&gt;
 &lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 0)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is output from slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was address&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_Off();&lt;br /&gt;
 		// read of the slave device, read the address &lt;br /&gt;
 		temp = SlaveReadI2C1();&lt;br /&gt;
 		dIndex = 0;&lt;br /&gt;
 		SlaveWriteI2C1(dataRead);&lt;br /&gt;
 	} else if ((I2C1STATbits.R_W == 1) &amp;amp;&amp;amp; (I2C1STATbits.D_A == 1)) {&lt;br /&gt;
 		// R/W bit = 1 --&amp;gt; indicates data transfer is input to slave&lt;br /&gt;
 		// D/A bit = 1 --&amp;gt; indicates last byte was data&lt;br /&gt;
 		mLED_0_On();&lt;br /&gt;
 		mLED_2_On();&lt;br /&gt;
 		&lt;br /&gt;
 		// output the data until the MASTER terminates the&lt;br /&gt;
 		// transfer with a NACK, continuing reads return 0&lt;br /&gt;
 		if (dIndex == 0) {&lt;br /&gt;
 			SlaveWriteI2C1(dataRead);&lt;br /&gt;
 			dIndex++;&lt;br /&gt;
 		} else&lt;br /&gt;
 			SlaveWriteI2C1(0);&lt;br /&gt;
 	}&lt;br /&gt;
 	&lt;br /&gt;
 	// finally clear the slave interrupt flag&lt;br /&gt;
 	mI2C1SClearIntFlag();		&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_SPI_Communication_between_PIC32s&amp;diff=16082</id>
		<title>PIC32MX: SPI Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_SPI_Communication_between_PIC32s&amp;diff=16082"/>
		<updated>2010-02-16T03:07:30Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32 boards to communicate with each other via SPI.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
SPI or [http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus Serial Peripheral Interface] is a communication method that was once used to connect devices such as printers, cameras, scanners, etc. to a desktop computer. This function has largely  been taken over by USB, but SPI can still be a useful communication tool for some applications. SPI runs using a master/slave set-up and can run in full duplex mode, meaning that signals can be transmitted between the master and the slave simultaneously. There is no standard communication protocol for SPI.&lt;br /&gt;
&lt;br /&gt;
SPI is still used to control some peripheral devices and has some advantages over [[I2C communication between PICs|I2C]]&amp;#039;&amp;#039;&amp;#039;CHANGE THIS TO OUR I2C PAGE!!&amp;#039;&amp;#039;&amp;#039; (another type of serial data communication). SPI can communicate at much higher data rates than I2C. Furthermore, when multiple slaves are present, SPI requires no addressing to differentiate between these slaves. Compared to parallel buses, SPI has the additional benefit of requiring only simple wiring. However, one disadvantage when compared to I2C is that each slave must have a dedicated slave line.  A slave line is not necessary when only one slave is present.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Peripheral devices that still use SPI:&lt;br /&gt;
&lt;br /&gt;
•	Converters (ADC and DAC)&lt;br /&gt;
&lt;br /&gt;
•	Memories (EEPROM and FLASH)&lt;br /&gt;
&lt;br /&gt;
•	Real Time Clocks (RTC)&lt;br /&gt;
&lt;br /&gt;
•	Sensors (temperature, pressure, etc.)&lt;br /&gt;
&lt;br /&gt;
•	Others (signal mixer, potentiometer, LCD controller, UART, CAN controller, USB controller, amplifier)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Basic Operation&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SPI requires three lines, and the optional slave select, and is therefore often termed the “four wire” serial bus. These four lines are described in the table below.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;2&amp;quot; cellpadding=&amp;quot;3&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Line!! Name!! Description&lt;br /&gt;
|-&lt;br /&gt;
|SCK1||Serial Clock||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDO1||Master Output, Slave Input||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDI1||Master Input, Slave Output||Output from slave&lt;br /&gt;
|-&lt;br /&gt;
|SS1||Slave Select||Output from master (active low)&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master, as its name suggests, controls all communication. By controlling the clock, the master decides when data is sent and received. Within each clock cycle a full duplex communication is carried out; each side sends and receives one bit of information. Because there is no standard communication protocol, the master can either send data or both send and receive data, depending on the needs of the application. Likewise, the slave can either receive data or both receive and send data back to the master.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;Slave Select&amp;quot; line is not required, if only one slave is used.  However, using the SS line, the master can choose which slave with which to communicate. Note that more than one slave may be selected, simply by applying a logic low to the desired SS lines.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;References&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.totalphase.com/support/articles/article03/ SPI Background](www.totalphase.com)&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus SPI Wikipedia Article] (www.wikipedia.org)&lt;br /&gt;
&lt;br /&gt;
[http://www.mct.net/faq/spi.html More Information] (www.mct.net)&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
[[Image:SPI_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
[[Image:Pic32_spi.png||thumb|right|x500px| PIC32MX SPI Module Block Diagram (click to expand) ]]&lt;br /&gt;
&lt;br /&gt;
This code, coupled with the circuit diagram above, sends one of two bytes of data from the master to the slave, and back. The data bytes are 0xAA and 0x23 which are sent on different button presses (PRG and USER).  Slave-LED0 toggles when 0xAA is received from the master, and slave-LED1 toggles when anything else (for example, 0x23) is received from the master.&lt;br /&gt;
No slave select line is used since there is only one slave, but if there were multiple slaves it would have to be used.&lt;br /&gt;
The SPI module sends and receives at the same time, so whenever a byte is written to SPI1BUF the module immediately sends out that byte on SDO which clocking with SCK and a byte is clocked in at the same time and written back to SPI1BUF. The send and receive buffers, SPI1TXB and SPI1RXB, cannot be written to directly, but instead are memory mapped to SPI1BUF. If the slave is not sending a value back when the master sends a byte, a 0x00 will be written into SPI1BUF. Because of this, in the code below the master reads from SPI1BUF after sending to clear the buffer and prevent overflow. To actually receive a byte, the master &amp;#039;&amp;#039;must&amp;#039;&amp;#039; send a byte as well. In the code below, by writing a zero to SPI1BUF, the master clocks out zero and in the slave message at the same time, at which point the slave message can be used. Since SPI is slower than the system clock, reading from SPI1BUF immediately would not result in the correct byte, instead the pic must wait for the module to finish sending, which the function getcSPI1() does automatically. Similarly, the slave sends a byte back by writing a value into SPI1BUF, but since the module is in slave mode the clock is not generated immediately. Instead the value waits in SPI1TXB until the clock is generated by the master. At this point the slave clocks out that value and in another value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /****************************************************&lt;br /&gt;
  * SPI_master_btwnPIC32s.c: Master code for Master- *&lt;br /&gt;
  *    Slave SPI communication. Both PICs are set to *&lt;br /&gt;
  *   use the SPI1 module and no SS line. A SS line  *&lt;br /&gt;
  *   would be used if there were more than two PICs.*&lt;br /&gt;
  *                                                  *&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards  *&lt;br /&gt;
  ****************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West           *&lt;br /&gt;
  * ME333 Winter 2010                                *&lt;br /&gt;
  * File Created:    05-FEB-2010                     *&lt;br /&gt;
  * Last Modified:    10-FEB-2010                    *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 //No-operation; asm stands for assembly&lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )&lt;br /&gt;
 &lt;br /&gt;
 #pragma config FPBDIV = DIV_1            //Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 //function definitions&lt;br /&gt;
 void SendData(int);         //Sends data&lt;br /&gt;
 void Delayms( unsigned t);   //Delay fcn&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main Function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
    // Configure the proper PB frequency and the number of wait states&lt;br /&gt;
    SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
    &lt;br /&gt;
    // Set all analog pins to be digital I/O&lt;br /&gt;
    AD1PCFG = 0xFFFF;&lt;br /&gt;
    //Initialize all of the LED pins&lt;br /&gt;
    mInitAllLEDs();&lt;br /&gt;
    mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
    //SPI setup&lt;br /&gt;
    int rData = SPI1BUF;    //Clears receive buffer&lt;br /&gt;
    IFS0CLR = 0x03800000;   //Clears any existing event (rx / tx/ fault interrupt)&lt;br /&gt;
    SPI1STATCLR = 0x40;      //Clears overflow&lt;br /&gt;
    //Enables the SPI channel (channel, master mode enable | use 8 bit mode | turn on, clock divider)&lt;br /&gt;
    SpiChnOpen(1, SPI_CON_MSTEN | SPI_CON_MODE8 | SPI_CON_ON, 1024);   // divide fpb by 1024, configure the I/O ports.&lt;br /&gt;
    &lt;br /&gt;
    /* Main while loop: Waits for button press to send/ receive data */&lt;br /&gt;
    while(1)&lt;br /&gt;
    {&lt;br /&gt;
       if (swProgram)   //If button 1 depressed&lt;br /&gt;
       {&lt;br /&gt;
          while(swProgram) { Nop(); }      //Wait for release&lt;br /&gt;
          mLED_2_Toggle();               //Toggle LED&lt;br /&gt;
          putcSPI1(0xAA);                 //Sends hex data 0xAA to slave&lt;br /&gt;
          Delayms(50);                  //delay&lt;br /&gt;
          SpiChnClrIntFlags(1);            //Clear interrupt flags (Tx / Rx buffers empty)&lt;br /&gt;
          int receive = SPI1BUF;            //Read SP1BUF (dummy read)&lt;br /&gt;
          SPI1BUF = 0x0;                  //Write SP1BUF- sets Tx flag, if not done read will not clock&lt;br /&gt;
          receive = getcSPI1();            //Generates clock and reads SDO&lt;br /&gt;
          Delayms(100);&lt;br /&gt;
       }//if loop ending&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
       if (swUser)&lt;br /&gt;
        {&lt;br /&gt;
          while(swUser) { Nop(); }&lt;br /&gt;
          mLED_1_Toggle();//Toggle LED&lt;br /&gt;
          putcSPI1(0x23);               //Sends hex data 0x23 to slave&lt;br /&gt;
          Delayms(50);               //delay&lt;br /&gt;
          SpiChnClrIntFlags(1);         //Clear interrupt flags (Tx / Rx buffers empty)&lt;br /&gt;
          int receive = SPI1BUF;         //Read SP1BUF (dummy read)&lt;br /&gt;
          SPI1BUF = 0x0;               //Write SP1BUF- sets Tx flag, if not done read will not clock&lt;br /&gt;
          receive = getcSPI1();         //Generates clock and reads SDO&lt;br /&gt;
          Delayms(100);&lt;br /&gt;
       }//if loop ending&lt;br /&gt;
    }//while loop ending&lt;br /&gt;
 &lt;br /&gt;
    return 0;&lt;br /&gt;
 } //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
    }&lt;br /&gt;
    CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
&lt;br /&gt;
 /****************************************************&lt;br /&gt;
  * SPI_slave_btwnPIC32s.c: Slave code for Master-   *&lt;br /&gt;
  *    Slave SPI communication. Both PICs are set to *&lt;br /&gt;
  *   use the SPI1 module and no SS line. A SS line  *&lt;br /&gt;
  *   would be used if there were more than two PICs.*&lt;br /&gt;
  *                                                  *&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards  *&lt;br /&gt;
  ****************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West           *&lt;br /&gt;
  * ME333 Winter 2010                                *&lt;br /&gt;
  * File Created:    05-FEB-2010                     *&lt;br /&gt;
  * Last Modified:    10-FEB-2010                    *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )   //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //Function definition for Delay function&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
    // Configure the proper PB frequency and the number of wait states. &lt;br /&gt;
    SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
    // Set all analog pins to be digital I/O&lt;br /&gt;
    AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
    /* Set TRIS bits for SPI lines (this may not be necessary */&lt;br /&gt;
    TRISDbits.TRISD10=1;&lt;br /&gt;
    TRISDbits.TRISD0=0;&lt;br /&gt;
    TRISCbits.TRISC4=1;&lt;br /&gt;
 &lt;br /&gt;
    //Initialize all of the LED pins&lt;br /&gt;
    mInitAllLEDs();&lt;br /&gt;
    mLED_3_Off();&lt;br /&gt;
 &lt;br /&gt;
    //SPI setup&lt;br /&gt;
    SPI1CON = 0;         //Clears config register&lt;br /&gt;
    int rData = SPI1BUF;   //Clears receive buffer&lt;br /&gt;
    IFS0CLR = 0x03800000;   //Clears any existing event (rx / tx/ fault interrupt)&lt;br /&gt;
    SPI1STATCLR = 0x40;      //Clears overflow&lt;br /&gt;
    //Enables the SPI channel (channel, master mode enable | use 8 bit mode | turn on, clock divider)&lt;br /&gt;
    SpiChnOpen(1, SPI_CON_SLVEN | SPI_CON_MODE8 | SPI_CON_ON, 1024);   // divide fpb by 1024, configure the I/O ports.&lt;br /&gt;
 &lt;br /&gt;
    //While loop to test LED functionality &lt;br /&gt;
    while(1)&lt;br /&gt;
    {&lt;br /&gt;
       rData = SpiChnGetC(1);               //Wait and read character when aviliable&lt;br /&gt;
       if (rData == 0xAA){mLED_0_Toggle();}   //Toggle LED0 if 0xAA&lt;br /&gt;
       else {mLED_1_Toggle();}               //Toggle LED1 if not&lt;br /&gt;
       putcSPI1(rData);                  //Send character back (when clock arrives)&lt;br /&gt;
       rData = SPI1BUF;                  //Clear recieve buffer (brevents overflow)&lt;br /&gt;
       Delayms(5);&lt;br /&gt;
    }//while loop ending&lt;br /&gt;
 &lt;br /&gt;
    return 0;&lt;br /&gt;
 } //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
    }&lt;br /&gt;
    CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_SPI_Communication_between_PIC32s&amp;diff=16081</id>
		<title>PIC32MX: SPI Communication between PIC32s</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=PIC32MX:_SPI_Communication_between_PIC32s&amp;diff=16081"/>
		<updated>2010-02-16T03:07:16Z</updated>

		<summary type="html">&lt;p&gt;JamesRein: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not erase this section!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Your assignment is to create code that allows two PIC32 boards to communicate with each other via SPI.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
SPI or [http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus Serial Peripheral Interface] is a communication method that was once used to connect devices such as printers, cameras, scanners, etc. to a desktop computer. This function has largely  been taken over by USB, but SPI can still be a useful communication tool for some applications. SPI runs using a master/slave set-up and can run in full duplex mode, meaning that signals can be transmitted between the master and the slave simultaneously. There is no standard communication protocol for SPI.&lt;br /&gt;
&lt;br /&gt;
SPI is still used to control some peripheral devices and has some advantages over [[I2C communication between PICs|I2C]]&amp;#039;&amp;#039;&amp;#039;CHANGE THIS TO OUR I2C PAGE!!&amp;#039;&amp;#039;&amp;#039; (another type of serial data communication). SPI can communicate at much higher data rates than I2C. Furthermore, when multiple slaves are present, SPI requires no addressing to differentiate between these slaves. Compared to parallel buses, SPI has the additional benefit of requiring only simple wiring. However, one disadvantage when compared to I2C is that each slave must have a dedicated slave line.  A slave line is not necessary when only one slave is present.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Peripheral devices that still use SPI:&lt;br /&gt;
&lt;br /&gt;
•	Converters (ADC and DAC)&lt;br /&gt;
&lt;br /&gt;
•	Memories (EEPROM and FLASH)&lt;br /&gt;
&lt;br /&gt;
•	Real Time Clocks (RTC)&lt;br /&gt;
&lt;br /&gt;
•	Sensors (temperature, pressure, etc.)&lt;br /&gt;
&lt;br /&gt;
•	Others (signal mixer, potentiometer, LCD controller, UART, CAN controller, USB controller, amplifier)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Basic Operation&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SPI requires three lines, and the optional slave select, and is therefore often termed the “four wire” serial bus. These four lines are described in the table below.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;2&amp;quot; cellpadding=&amp;quot;3&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Line!! Name!! Description&lt;br /&gt;
|-&lt;br /&gt;
|SCK1||Serial Clock||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDO1||Master Output, Slave Input||Output from master&lt;br /&gt;
|-&lt;br /&gt;
|SDI1||Master Input, Slave Output||Output from slave&lt;br /&gt;
|-&lt;br /&gt;
|SS1||Slave Select||Output from master (active low)&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master, as its name suggests, controls all communication. By controlling the clock, the master decides when data is sent and received. Within each clock cycle a full duplex communication is carried out; each side sends and receives one bit of information. Because there is no standard communication protocol, the master can either send data or both send and receive data, depending on the needs of the application. Likewise, the slave can either receive data or both receive and send data back to the master.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;Slave Select&amp;quot; line is not required, if only one slave is used.  However, using the “Slave Select” line, the master can choose which slave with which to communicate. Note that more than one slave may be selected, simply by applying a logic low to the desired SS lines.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;References&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.totalphase.com/support/articles/article03/ SPI Background](www.totalphase.com)&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus SPI Wikipedia Article] (www.wikipedia.org)&lt;br /&gt;
&lt;br /&gt;
[http://www.mct.net/faq/spi.html More Information] (www.mct.net)&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
[[Image:SPI_circuit.PNG]]&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
[[Image:Pic32_spi.png||thumb|right|x500px| PIC32MX SPI Module Block Diagram (click to expand) ]]&lt;br /&gt;
&lt;br /&gt;
This code, coupled with the circuit diagram above, sends one of two bytes of data from the master to the slave, and back. The data bytes are 0xAA and 0x23 which are sent on different button presses (PRG and USER).  Slave-LED0 toggles when 0xAA is received from the master, and slave-LED1 toggles when anything else (for example, 0x23) is received from the master.&lt;br /&gt;
No slave select line is used since there is only one slave, but if there were multiple slaves it would have to be used.&lt;br /&gt;
The SPI module sends and receives at the same time, so whenever a byte is written to SPI1BUF the module immediately sends out that byte on SDO which clocking with SCK and a byte is clocked in at the same time and written back to SPI1BUF. The send and receive buffers, SPI1TXB and SPI1RXB, cannot be written to directly, but instead are memory mapped to SPI1BUF. If the slave is not sending a value back when the master sends a byte, a 0x00 will be written into SPI1BUF. Because of this, in the code below the master reads from SPI1BUF after sending to clear the buffer and prevent overflow. To actually receive a byte, the master &amp;#039;&amp;#039;must&amp;#039;&amp;#039; send a byte as well. In the code below, by writing a zero to SPI1BUF, the master clocks out zero and in the slave message at the same time, at which point the slave message can be used. Since SPI is slower than the system clock, reading from SPI1BUF immediately would not result in the correct byte, instead the pic must wait for the module to finish sending, which the function getcSPI1() does automatically. Similarly, the slave sends a byte back by writing a value into SPI1BUF, but since the module is in slave mode the clock is not generated immediately. Instead the value waits in SPI1TXB until the clock is generated by the master. At this point the slave clocks out that value and in another value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Master Code===&lt;br /&gt;
 /****************************************************&lt;br /&gt;
  * SPI_master_btwnPIC32s.c: Master code for Master- *&lt;br /&gt;
  *    Slave SPI communication. Both PICs are set to *&lt;br /&gt;
  *   use the SPI1 module and no SS line. A SS line  *&lt;br /&gt;
  *   would be used if there were more than two PICs.*&lt;br /&gt;
  *                                                  *&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards  *&lt;br /&gt;
  ****************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West           *&lt;br /&gt;
  * ME333 Winter 2010                                *&lt;br /&gt;
  * File Created:    05-FEB-2010                     *&lt;br /&gt;
  * Last Modified:    10-FEB-2010                    *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 //No-operation; asm stands for assembly&lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )&lt;br /&gt;
 &lt;br /&gt;
 #pragma config FPBDIV = DIV_1            //Sets PBCLK to SYSCLK&lt;br /&gt;
 &lt;br /&gt;
 //function definitions&lt;br /&gt;
 void SendData(int);         //Sends data&lt;br /&gt;
 void Delayms( unsigned t);   //Delay fcn&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main Function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
    // Configure the proper PB frequency and the number of wait states&lt;br /&gt;
    SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
    &lt;br /&gt;
    // Set all analog pins to be digital I/O&lt;br /&gt;
    AD1PCFG = 0xFFFF;&lt;br /&gt;
    //Initialize all of the LED pins&lt;br /&gt;
    mInitAllLEDs();&lt;br /&gt;
    mInitAllSwitches()&lt;br /&gt;
 &lt;br /&gt;
    //SPI setup&lt;br /&gt;
    int rData = SPI1BUF;    //Clears receive buffer&lt;br /&gt;
    IFS0CLR = 0x03800000;   //Clears any existing event (rx / tx/ fault interrupt)&lt;br /&gt;
    SPI1STATCLR = 0x40;      //Clears overflow&lt;br /&gt;
    //Enables the SPI channel (channel, master mode enable | use 8 bit mode | turn on, clock divider)&lt;br /&gt;
    SpiChnOpen(1, SPI_CON_MSTEN | SPI_CON_MODE8 | SPI_CON_ON, 1024);   // divide fpb by 1024, configure the I/O ports.&lt;br /&gt;
    &lt;br /&gt;
    /* Main while loop: Waits for button press to send/ receive data */&lt;br /&gt;
    while(1)&lt;br /&gt;
    {&lt;br /&gt;
       if (swProgram)   //If button 1 depressed&lt;br /&gt;
       {&lt;br /&gt;
          while(swProgram) { Nop(); }      //Wait for release&lt;br /&gt;
          mLED_2_Toggle();               //Toggle LED&lt;br /&gt;
          putcSPI1(0xAA);                 //Sends hex data 0xAA to slave&lt;br /&gt;
          Delayms(50);                  //delay&lt;br /&gt;
          SpiChnClrIntFlags(1);            //Clear interrupt flags (Tx / Rx buffers empty)&lt;br /&gt;
          int receive = SPI1BUF;            //Read SP1BUF (dummy read)&lt;br /&gt;
          SPI1BUF = 0x0;                  //Write SP1BUF- sets Tx flag, if not done read will not clock&lt;br /&gt;
          receive = getcSPI1();            //Generates clock and reads SDO&lt;br /&gt;
          Delayms(100);&lt;br /&gt;
       }//if loop ending&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
       if (swUser)&lt;br /&gt;
        {&lt;br /&gt;
          while(swUser) { Nop(); }&lt;br /&gt;
          mLED_1_Toggle();//Toggle LED&lt;br /&gt;
          putcSPI1(0x23);               //Sends hex data 0x23 to slave&lt;br /&gt;
          Delayms(50);               //delay&lt;br /&gt;
          SpiChnClrIntFlags(1);         //Clear interrupt flags (Tx / Rx buffers empty)&lt;br /&gt;
          int receive = SPI1BUF;         //Read SP1BUF (dummy read)&lt;br /&gt;
          SPI1BUF = 0x0;               //Write SP1BUF- sets Tx flag, if not done read will not clock&lt;br /&gt;
          receive = getcSPI1();         //Generates clock and reads SDO&lt;br /&gt;
          Delayms(100);&lt;br /&gt;
       }//if loop ending&lt;br /&gt;
    }//while loop ending&lt;br /&gt;
 &lt;br /&gt;
    return 0;&lt;br /&gt;
 } //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
    }&lt;br /&gt;
    CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;br /&gt;
&lt;br /&gt;
===Slave Code===&lt;br /&gt;
&lt;br /&gt;
 /****************************************************&lt;br /&gt;
  * SPI_slave_btwnPIC32s.c: Slave code for Master-   *&lt;br /&gt;
  *    Slave SPI communication. Both PICs are set to *&lt;br /&gt;
  *   use the SPI1 module and no SS line. A SS line  *&lt;br /&gt;
  *   would be used if there were more than two PICs.*&lt;br /&gt;
  *                                                  *&lt;br /&gt;
  * Hardware: 2 PIC32MX460F512L PICs on NU32 boards  *&lt;br /&gt;
  ****************************************************&lt;br /&gt;
  * Thomas Peterson, James Rein, Eric West           *&lt;br /&gt;
  * ME333 Winter 2010                                *&lt;br /&gt;
  * File Created:    05-FEB-2010                     *&lt;br /&gt;
  * Last Modified:    10-FEB-2010                    *&lt;br /&gt;
  ****************************************************/&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;quot;HardwareProfile.h&amp;quot;&lt;br /&gt;
 #include &amp;lt;plib.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 #define Nop() asm( &amp;quot;nop&amp;quot; )   //No-operation; asm stands for assembly, using an assembly command in C.  Cool!&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 //Function definition for Delay function&lt;br /&gt;
 void Delayms( unsigned t);&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 /* Main function */&lt;br /&gt;
 int main(void)&lt;br /&gt;
    // Configure the proper PB frequency and the number of wait states. &lt;br /&gt;
    SYSTEMConfigPerformance(SYS_FREQ);&lt;br /&gt;
 &lt;br /&gt;
    // Set all analog pins to be digital I/O&lt;br /&gt;
    AD1PCFG = 0xFFFF;&lt;br /&gt;
 &lt;br /&gt;
    /* Set TRIS bits for SPI lines (this may not be necessary */&lt;br /&gt;
    TRISDbits.TRISD10=1;&lt;br /&gt;
    TRISDbits.TRISD0=0;&lt;br /&gt;
    TRISCbits.TRISC4=1;&lt;br /&gt;
 &lt;br /&gt;
    //Initialize all of the LED pins&lt;br /&gt;
    mInitAllLEDs();&lt;br /&gt;
    mLED_3_Off();&lt;br /&gt;
 &lt;br /&gt;
    //SPI setup&lt;br /&gt;
    SPI1CON = 0;         //Clears config register&lt;br /&gt;
    int rData = SPI1BUF;   //Clears receive buffer&lt;br /&gt;
    IFS0CLR = 0x03800000;   //Clears any existing event (rx / tx/ fault interrupt)&lt;br /&gt;
    SPI1STATCLR = 0x40;      //Clears overflow&lt;br /&gt;
    //Enables the SPI channel (channel, master mode enable | use 8 bit mode | turn on, clock divider)&lt;br /&gt;
    SpiChnOpen(1, SPI_CON_SLVEN | SPI_CON_MODE8 | SPI_CON_ON, 1024);   // divide fpb by 1024, configure the I/O ports.&lt;br /&gt;
 &lt;br /&gt;
    //While loop to test LED functionality &lt;br /&gt;
    while(1)&lt;br /&gt;
    {&lt;br /&gt;
       rData = SpiChnGetC(1);               //Wait and read character when aviliable&lt;br /&gt;
       if (rData == 0xAA){mLED_0_Toggle();}   //Toggle LED0 if 0xAA&lt;br /&gt;
       else {mLED_1_Toggle();}               //Toggle LED1 if not&lt;br /&gt;
       putcSPI1(rData);                  //Send character back (when clock arrives)&lt;br /&gt;
       rData = SPI1BUF;                  //Clear recieve buffer (brevents overflow)&lt;br /&gt;
       Delayms(5);&lt;br /&gt;
    }//while loop ending&lt;br /&gt;
 &lt;br /&gt;
    return 0;&lt;br /&gt;
 } //ending main &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 void Delayms( unsigned t)&lt;br /&gt;
 // This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ&lt;br /&gt;
 {&lt;br /&gt;
     OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);&lt;br /&gt;
     while (t--)&lt;br /&gt;
     {  // t x 1ms loop&lt;br /&gt;
         WriteTimer1(0);&lt;br /&gt;
         while (ReadTimer1() &amp;lt; SYS_FREQ/256/1000);&lt;br /&gt;
    }&lt;br /&gt;
    CloseTimer1();&lt;br /&gt;
 } // Delayms&lt;/div&gt;</summary>
		<author><name>JamesRein</name></author>
	</entry>
</feed>