====== I2C ====== I2C 직렬 컴퓨터 버스 주변기기를 연결하기 위해 사용된다. {{http://i.imgur.com/99a3Z.png?300}} 데이터와 클럭은 pull-up {{http://i.imgur.com/U3al8.png?300}} Time {{http://i.imgur.com/NtRbM.png?300}} {{http://i.imgur.com/dgtdQ.png?300}} ===== 실제 실험 해보기 ===== arduino i2c 관련 핀 * Analog4 - SDA * Analog5 - SCL Time {{http://i.imgur.com/VQtTJ.png?300}} ==== 24LC256, EEPROM ==== {{{ #include //Include the Arduino I2C Library void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) { int rdata = data; Wire.beginTransmission(deviceaddress); Wire.write((int)(eeaddress >> 8)); // MSB Wire.write((int)(eeaddress & 0xFF)); // LSB Wire.write(rdata); Wire.endTransmission(); } void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) { Wire.beginTransmission(deviceaddress); Wire.write((int)(eeaddresspage >> 8)); // MSB Wire.write((int)(eeaddresspage & 0xFF)); // LSB byte c; for ( c = 0; c < length; c++) Wire.write(data[c]); Wire.endTransmission(); } byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) { byte rdata = 0xFF; Wire.beginTransmission(deviceaddress); Wire.write((int)(eeaddress >> 8)); // MSB Wire.write((int)(eeaddress & 0xFF)); // LSB Wire.endTransmission(); Wire.requestFrom(deviceaddress,1); if (Wire.available()) rdata = Wire.read(); return rdata; } void setup() { char data[] = "hello world"; Wire.begin(); //Start I2C connections Serial.begin(9600); //i2c_eeprom_write_page(0x50, 0, (byte *)data, sizeof(data)); // write to EEPROM delay(10); //add a small delay Serial.println("Memory written"); } void loop() { int addr=0; //EEPROM Address 0 byte b = i2c_eeprom_read_byte(0x50, 0); // access the first address from the memory while (b!=0) { Serial.print((char)b); //print content to serial port addr++; //increase address b = i2c_eeprom_read_byte(0x50, addr); //access an address from the memory } Serial.println(" "); delay(2000); } }}} master가 slave에 데이터를 보낸다. {{http://i.imgur.com/lHqBZ.png?300}} master가 slave의 데이터를 읽는다. {{http://i.imgur.com/NE9xJ.png?300}} ==== Tmp102 ==== {{{ #include int tmp102Address = 0x48; void setup(){ Serial.begin(9600); Wire.begin(); } void loop(){ float celsius = getTemperature(); Serial.print("Celsius: "); Serial.println(celsius); float fahrenheit = (1.8 * celsius) + 32; Serial.print("Fahrenheit: "); Serial.println(fahrenheit); delay(200); //just here to slow down the output. You can remove this } float getTemperature(){ Wire.requestFrom(tmp102Address,2); byte MSB = Wire.read(); byte LSB = Wire.read(); //it's a 12bit int, using two's compliment for negative int TemperatureSum = ((MSB << 8) | LSB) >> 4; float celsius = TemperatureSum*0.0625; return celsius; } }}}