I2C Bus ( ไอ กำลังสอง ซี) [ Inter-IC protocol aka Two Wire Interface] ถูกพัฒนาโดย Philips Semiconductors
เป็นหนึ่งในระบบบัส ที่ใช้สื้อสาร (ส่งข้อมูล) ระหว่าง ไอซี ต่างๆใน วงจร อิเล็กทรอนิกส์
โดยจะใช้สายสัญญานเพียงสองเส้น
เส้นแรกจะเป็น Clock เพื่อ synchronise เวลา ในการส่งข้อมูล เรียกว่า Serial Clock Line (SCL)
เส้นที่สองจะเป็น Data เพื่อใช้ในการรับส่ง data เรียกว่า Serial Data Line (SDA)
ในกรณที่ใช้กับ Arduino
จะต่อ
SDA ที่ Analog Pin4
SCL ที่ Analog Pin5
และต่อ Pull-up resistor ในแต่ละ bus line.
เราสามารถพ่วง i2c devices ได้มากถึง 128 ในหนึ่งระบบ two wires
ในแต่ละ device จะมี address เป็นของตัวเอง เพื่อที่เราจะใช้อ้างถึง device หรือ node นั้น
ด่านล่างจะเป็นรูปแบบของ code ที่ เอาไว้ใช้ อ่าน/เขียน (สื่อสาร) กับ I2C IC
#include "Wire.h"
void setup()
{
}
void loop()
{
//Write something into IC
Wire.beginTransmission(/*I2C_Device_Address*/);
Wire.write(/*byteToWriteHere*/); //consult your datasheet
Wire.endTransmission();
//Read something from IC
Wire.beginTransmission(/*I2C_Device_Address*/);
Wire.write(); //consult your datasheet
Wire.endTransmission();
Wire.requestFrom(/*I2C_Device_Address*/);
Wire.read(); //this will return as byte
}
หมายเหตุ
ผู้เขียนใช้ Arduino 1.0 กับ board Arduino UNO
No comments:
Post a Comment