All pastes #715731 Raw Edit

Untitled

public text v1 · immutable
#715731 ·published 2007-09-26 13:04 UTC
rendered paste body
#include <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>

//#include <linux/i2c.h>
#include <linux/i2c-dev.h>

//FIXME
#ifndef I2C_SLAVE
#define I2C_SLAVE       0x0703
#endif


int main ()
{
  int file;

  if ((file = open("/dev/i2c-0", O_RDWR)) < 0) {
    printf("Error, could not open i2c device\n");
    exit(-1);
  }

  /* slave addres is 0b100101 */
  int addr = 0x4d;    /* set this to the I2C address */

  /* set the I2C slave address */
  if (ioctl(file, I2C_SLAVE, addr) < 0) {
    printf("Error, I2C_SLAVE ioctl error\n");
    exit(-1);
  }

  printf ("Made it past setting slave address\n");

#if 0
  char write_data[] = "\x23\x53\x23\x23";


  if ((count = write(file, write_data, sizeof(write_data))) != sizeof(write_data)) {
    printf("Error, only wrote %i bytes, expected %i bytes\n", count, sizeof(write_data));
    exit(-1);
  }
#endif

  unsigned char read_data[2];
  unsigned short read_value;
  float read_value_v;
  int count;

  count = read(file, read_data, 2);
  printf("read %i bytes, 0 = 0x%x, 1 = 0x%x\n", count, read_data[0], read_data[1]);

  read_value = read_data[0] << 8 | read_data[1];
  read_value_v = (((float)read_value)/4096) * 3.3;


  printf("read_value = 0x%x\n", read_value);
  printf("read_value_v = %f volts\n", read_value_v);

  return 0;
}