<p>// Send control and register address bytes</p><p>Wire.beginTransmission(0x20);</p><p>Wire.write(0x00);</p><p>// Write data to IODIRA register</p><p>Wire.write(0xFF);</p><p>// End session</p><p>Wire.endTransmission();</p><p>// Send control and register address bytes</p><p>Wire.beginTransmission(0x20);</p><p>Wire.write(0x12); // GPIOA</p><p>Wire.endTransmission();</p><p>// Read data from port A</p><p>Wire.requestFrom(0x20, 1); // set R/W bit to 1 (read)</p><p>portval = Wire.read(); // still points to 0x12</p><p>Since the GP I/O ports are configured as inputs by default at power-up or after a reset</p><p>(see Table 10-9), the first step could have been omitted. To configure the port as an</p><p>output the IODIRA bits would be set to 0. This is shown in the code snippet in</p><p>Example 10-8.</p><p>Example 10-8. Writing data to the output port</p><p>// Send control and register address bytes</p><p>Wire.beginTransmission(0x20);</p><p>Wire.write(0x00);</p><p>// Write data to IODIRA register</p><p>Wire.write(0x0);</p><p>// End</p><p>Wire.endTransmission();</p><p>// Send control and register address bytes</p><p>Wire.beginTransmission(0x20);</p><p>Wire.write(0x12); // GPIOA</p><p>// Write data to port A</p><p>Wire.write(portval);</p><p>Wire.endTransmission();</p>
|