/* * Example code to read the contents of an I2C EEPROM chip. */ #include #include #include int main(void) { FILE *fp = NULL; char *data = NULL; int retval = EXIT_FAILURE; struct mpsse_context *hmc5883 = NULL; char buf[1024] = {0}; if((hmc5883 = MPSSE(I2C, FOUR_HUNDRED_KHZ, MSB)) != NULL && hmc5883->open) { printf("%s initialized at %dHz (I2C)\n", GetDescription(hmc5883), GetClock(hmc5883)); /* Write the EEPROM start address */ buf[0] = 0x3C; buf[1] = 0x00; buf[2] = 0x70; Start(hmc5883); Write(hmc5883, buf, 3); if(GetAck(hmc5883) == ACK) { /* Read the register 0x00 */ Start(hmc5883); data = Read(hmc5883, 1); if(data) { printf("Data %d\n",data[0]); free(data); } /* Tell libmpsse to send NACKs after reading data */ SendNacks(hmc5883); /* Read in one dummy byte, with a NACK */ Read(hmc5883, 1); } Stop(hmc5883); } else { printf("Failed to initialize MPSSE: %s\n", ErrorString(hmc5883)); } Close(hmc5883); return retval; }