]> kolegite.com Git - vmks.git/commitdiff
added I2C example
authorvl_garistov <vl_garistov@gmail.com>
Wed, 24 Mar 2021 01:12:31 +0000 (03:12 +0200)
committervl_garistov <vl_garistov@gmail.com>
Wed, 24 Mar 2021 01:12:31 +0000 (03:12 +0200)
Datasheets/AS7265x_DS000612_1-00.pdf [new file with mode: 0644]
Datasheets/PCT2075.pdf [new file with mode: 0644]
Datasheets/TI_I2C_app_note.pdf [new file with mode: 0644]
Examples/I2C_simple_demo/I2C_simple_demo.png [new file with mode: 0644]
Examples/I2C_simple_demo/I2C_simple_demo_master/I2C_simple_demo_master.ino [new file with mode: 0644]
Examples/I2C_simple_demo/I2C_simple_demo_slave/I2C_simple_demo_slave.ino [new file with mode: 0644]

diff --git a/Datasheets/AS7265x_DS000612_1-00.pdf b/Datasheets/AS7265x_DS000612_1-00.pdf
new file mode 100644 (file)
index 0000000..d9d295d
Binary files /dev/null and b/Datasheets/AS7265x_DS000612_1-00.pdf differ
diff --git a/Datasheets/PCT2075.pdf b/Datasheets/PCT2075.pdf
new file mode 100644 (file)
index 0000000..e8b11ab
Binary files /dev/null and b/Datasheets/PCT2075.pdf differ
diff --git a/Datasheets/TI_I2C_app_note.pdf b/Datasheets/TI_I2C_app_note.pdf
new file mode 100644 (file)
index 0000000..913e34f
Binary files /dev/null and b/Datasheets/TI_I2C_app_note.pdf differ
diff --git a/Examples/I2C_simple_demo/I2C_simple_demo.png b/Examples/I2C_simple_demo/I2C_simple_demo.png
new file mode 100644 (file)
index 0000000..dcbccd5
Binary files /dev/null and b/Examples/I2C_simple_demo/I2C_simple_demo.png differ
diff --git a/Examples/I2C_simple_demo/I2C_simple_demo_master/I2C_simple_demo_master.ino b/Examples/I2C_simple_demo/I2C_simple_demo_master/I2C_simple_demo_master.ino
new file mode 100644 (file)
index 0000000..a2f4273
--- /dev/null
@@ -0,0 +1,57 @@
+#include <Wire.h>
+
+#define SLAVE_ADDRESS  0x09
+#define DELAY_TIME             200
+
+size_t i2c_write(uint8_t address, void *data, size_t size);
+size_t i2c_read(uint8_t address, void *data, size_t size);
+
+void setup()
+{
+       Wire.begin();
+       Serial.begin(9600);
+}
+
+void loop()
+{
+       static bool led_status = false;
+       uint16_t pot_value = 0;
+
+       Serial.print("Turning LED ");
+       Serial.println(led_status ? "on": "off");
+       i2c_write(SLAVE_ADDRESS, &led_status, sizeof(led_status));
+       led_status = !led_status;
+       
+       i2c_read(SLAVE_ADDRESS, &pot_value, sizeof(pot_value));
+       Serial.print("Received pot_value = ");
+       Serial.println(pot_value);
+       
+       delay(DELAY_TIME);
+}
+
+size_t i2c_write(uint8_t address, void *data, size_t size)
+{      
+       int error_code;
+       size_t bytes_written;
+
+       Wire.beginTransmission(address);
+       bytes_written = Wire.write((uint8_t *) data, size);
+       error_code =  Wire.endTransmission();
+
+       return error_code ? 0: bytes_written;
+}
+
+size_t i2c_read(uint8_t address, void *data, size_t size)
+{
+       size_t bytes_received = Wire.requestFrom(address, size);
+
+       if (bytes_received == size)
+       {
+               for (size_t i = 0; i < size; i++)
+               {
+                       ((uint8_t *) data)[i] = Wire.read();
+               }
+       }
+       
+       return bytes_received;
+}
diff --git a/Examples/I2C_simple_demo/I2C_simple_demo_slave/I2C_simple_demo_slave.ino b/Examples/I2C_simple_demo/I2C_simple_demo_slave/I2C_simple_demo_slave.ino
new file mode 100644 (file)
index 0000000..00d6760
--- /dev/null
@@ -0,0 +1,51 @@
+#include <Wire.h>
+
+#define POT_PIN        A0
+#define LED_PIN        10
+
+#define SLAVE_ADDRESS  0x09
+
+void i2c_receive_handler(int bytes_received);
+void i2c_send_handler(void);
+
+volatile bool led_status = false;
+volatile uint16_t pot_value = 0;
+
+void setup()
+{
+       pinMode(POT_PIN, INPUT);
+       pinMode(LED_PIN, OUTPUT);
+       digitalWrite(LED_PIN, LOW);
+
+       Wire.begin(SLAVE_ADDRESS);
+       Wire.onRequest(i2c_send_handler);
+       Wire.onReceive(i2c_receive_handler);
+
+       Serial.begin(9600);
+}
+
+void loop()
+{
+       digitalWrite(LED_PIN, led_status);
+       pot_value = analogRead(POT_PIN);
+}
+
+void i2c_send_handler(void)
+{      
+       //Only for demonstration, never actually put Serial.print() in an ISR
+       Serial.print("Sending pot_value = ");
+       Serial.println(pot_value);
+       Wire.write((uint8_t *) &pot_value, sizeof(pot_value));
+}
+
+void i2c_receive_handler(int bytes_received)
+{
+       led_status = Wire.read();
+       // Ignore all bytes but the first
+       for (int i = 1; i < bytes_received; i++)
+       {
+               Wire.read();
+       }
+       //Only for demonstration, never actually put Serial.print() in an ISR
+       Serial.println(led_status ? "LED on": "LED off");
+}