title: I2C C Programming Nothing Works
body: <p>Here is my program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;err.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;linux/types.h&gt;
  #include &lt;linux/i2c.h&gt;
  #include &lt;sys/ioctl.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  
  #define I2C_SLAVE  0x0703
  
  int main(int argc, char **argv)
  {
      uint8_t data, i2c_device_address = 0x21,
      i2c_register_address = 0x01;
  
      int file, rc;
  
     //Open I2C
      file = open("/dev/i2c-1", O_RDWR);
      if (file &lt; 0)
        err(errno, "Tried to open /dev/i2c-1"); 
  
  
      // I want to work with I2C Register at I2C Address 0x12
      rc = ioctl(file, I2C_SLAVE, i2c_register_address);
      if (rc &lt; 0)
          err(errno, "Tried to set device address '0x%02x'",
               i2c_device_address);
  
  
       // Read Content of I2C Register at I2C Address 0x12
       rc = read(file, &amp;data, 1);
       if (rc &lt; 0)
           err(errno, "Tried to read device at address '0x%02x'",
               i2c_device_address);
  
       // Print the Result
       // Expected (Default) Value = 0x80
       // What i got = 0x00
       printf("/dev/i2c-1: device 0x%02x at address 0x%02x: 0x%02x\n",
               i2c_device_address, i2c_register_address, data);
  
  
       // Terminal Output:
       // /dev/i2c-1: device 0x21 at address 0x01: 0x00
  
       return 0;
  
  }
  </code></pre>
  
  <p>I have the ov7670 camera and i am trying to read some registers for warm up.</p>
  
  <p>But i always get the same result: 0x00.
  The datasheet of the camera tells me that 0x80 is the default value for this register.</p>
  
  <p>When my camera isn't even connected to my Raspberry PI it still prints the same output when i run my program. There are not even errors.</p>
  
  <p>I used the command i2cdetect -r 1 and i got correct output.</p>
  
  <p>So the device must be ok and properly connected.</p>
  
  <p>I want to know how can i read and write to i2c registers with the basic Linux read, write &amp; ioctl functions?</p>
  
  <p><strong><em>UPDATE</em></strong>
  Now I check the return value of the read function and it says Remote I/O error</p>