arduinoでi2c その2

#概要

arduinoでi2cやってみる。
degitalwriteだけで、i2cを叩いてみる。

#サンプルコード

```
#define WireDelay 			20
#define SCL 				A5
#define SDA    				A4
#define PCF8574_ADDRESS	 	0x27 << 1 + 0

void i2c_init(void) {
	pinMode(SCL, OUTPUT);
	pinMode(SDA, OUTPUT);
	pinMode(SCL, OUTPUT);
	pinMode(13, OUTPUT);
	digitalWrite(SCL, HIGH);
	digitalWrite(SDA, HIGH);
	digitalWrite(13, HIGH);
	delay(10);
	Serial.println("init");
}
void i2c_start(unsigned char address) {
	digitalWrite(13, HIGH);
	uint8_t temp;
	uint8_t data = address;
	digitalWrite(SDA, LOW);
	delayMicroseconds(WireDelay);
	digitalWrite(SCL, LOW);
	delayMicroseconds(WireDelay);
	for (temp = 0x80; temp; temp >>= 1)
	{
		digitalWrite(SDA, (data & temp) ? HIGH : LOW);
		delayMicroseconds(WireDelay);
		digitalWrite(SCL, HIGH);
		delayMicroseconds(WireDelay);
		digitalWrite(SCL, LOW);
	}
	pinMode(SDA, INPUT_PULLUP);
	delayMicroseconds(WireDelay);
	digitalWrite(SCL, HIGH);
	if (digitalRead(SDA))
	{
	}
	else
	{
		digitalWrite(13, LOW);
	}
	delayMicroseconds(WireDelay);
	digitalWrite(SCL, LOW);
	pinMode(SDA, OUTPUT);
	digitalWrite(SDA, HIGH);
}
void i2c_write(unsigned char data) {
	digitalWrite(13, HIGH);
	uint8_t temp;
	for (temp = 0x80; temp; temp >>= 1)
	{
		digitalWrite(SDA, (data & temp) ? HIGH : LOW);
		delayMicroseconds(WireDelay);
		digitalWrite(SCL, HIGH);
		delayMicroseconds(WireDelay);
		digitalWrite(SCL, LOW);
	}
	pinMode(SDA, INPUT_PULLUP);
	delayMicroseconds(WireDelay);
	digitalWrite(SCL, HIGH);
	if (digitalRead(SDA))
	{
	}
	else
	{
		digitalWrite(13, LOW);
	}
	delayMicroseconds(WireDelay);
	digitalWrite(SCL, LOW);
	pinMode(SDA, OUTPUT);
	digitalWrite(SDA, HIGH);
}

void i2c_stop(void) {
	delayMicroseconds(WireDelay);
	digitalWrite(SDA, LOW);
	delayMicroseconds(WireDelay);
	digitalWrite(SCL, HIGH);
	delayMicroseconds(WireDelay);
	digitalWrite(SDA, HIGH);
}

void send(uint8_t d) {
	uint8_t s;
	i2c_start(PCF8574_ADDRESS);
	i2c_write(d);
	s = d | 0x04;
	i2c_write(s);
	delayMicroseconds(1);
	s = d & 0xfb;
	i2c_write(s);
	delayMicroseconds(1);
	i2c_stop();
}
void set(uint8_t d) {
	uint8_t s;
	uint8_t f1 = d & 0xf0;
	uint8_t f2 = d << 4;
	i2c_start(PCF8574_ADDRESS);
	s = f1;
	i2c_write(s);
	s = f1 | 0x04;
	i2c_write(s);
	delayMicroseconds(1);
	s = f1 & 0xfb;
	i2c_write(s);
	i2c_stop();
	i2c_start(PCF8574_ADDRESS);
	s = f2;
	i2c_write(s);
	s = f2 | 0x04;
	i2c_write(s);
	delayMicroseconds(1);
	s = f2 & 0xfb;
	i2c_write(s);
	i2c_stop();
}
void data(uint8_t d) {
	uint8_t s;
	uint8_t f1 = d & 0xf0;
	uint8_t f2 = d << 4;
	i2c_start(PCF8574_ADDRESS);
	s = f1;
	i2c_write(s);
	s = f1 | 0x05;
	i2c_write(s);
	delayMicroseconds(1);
	s = f1 & 0xfa;
	i2c_write(s);
	i2c_stop();
	i2c_start(PCF8574_ADDRESS);
	s = f2;
	i2c_write(s);
	s = f2 | 0x05;
	i2c_write(s);
	delayMicroseconds(1);
	s = f2 & 0xfa | 0x08;
	i2c_write(s);
	i2c_stop();
}
void setup() {
	Serial.begin(9600);
	i2c_init();
	send(0x03);
	delayMicroseconds(50);
	send(0x03);
	delayMicroseconds(50);
	send(0x03);
	delayMicroseconds(15);
	send(0x02);
	set(0x28);
	set(0x0c);
	set(0x01);
	set(0x06);
	data(0x68);
	data(0x65);
	data(0x6c);
	data(0x6c);
	data(0x6f);
	data(0x2c);
	data(0x20);
	data(0x77);
	data(0x6f);
	data(0x72);
	data(0x6c);
	data(0x64);
	data(0x21);
	Serial.println("ok");
}
void loop() {
	delay(100);
}





```


以上。