]> kolegite.com Git - vmks.git/commitdiff
added a second I2C example, with interrupt
authorvl_garistov <vl_garistov@gmail.com>
Wed, 24 Mar 2021 08:59:08 +0000 (10:59 +0200)
committervl_garistov <vl_garistov@gmail.com>
Wed, 24 Mar 2021 08:59:08 +0000 (10:59 +0200)
Examples/I2C_interrupt_to_master/I2C_interrupt_to_master.png [new file with mode: 0644]
Examples/I2C_interrupt_to_master/I2C_interrupt_to_master_master/I2C_interrupt_to_master_master.ino [new file with mode: 0644]
Examples/I2C_interrupt_to_master/I2C_interrupt_to_master_slave/I2C_interrupt_to_master_slave.ino [new file with mode: 0644]

diff --git a/Examples/I2C_interrupt_to_master/I2C_interrupt_to_master.png b/Examples/I2C_interrupt_to_master/I2C_interrupt_to_master.png
new file mode 100644 (file)
index 0000000..41c47c8
Binary files /dev/null and b/Examples/I2C_interrupt_to_master/I2C_interrupt_to_master.png differ
diff --git a/Examples/I2C_interrupt_to_master/I2C_interrupt_to_master_master/I2C_interrupt_to_master_master.ino b/Examples/I2C_interrupt_to_master/I2C_interrupt_to_master_master/I2C_interrupt_to_master_master.ino
new file mode 100644 (file)
index 0000000..47b5705
--- /dev/null
@@ -0,0 +1,62 @@
+#include <Wire.h>
+
+#define INT_PIN        2
+
+#define SLAVE_ADDRESS  0x09
+
+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 slave_interrupt(void);
+
+volatile bool new_data = false;
+
+void setup()
+{
+       Wire.begin();
+       Serial.begin(9600);
+       attachInterrupt(digitalPinToInterrupt(INT_PIN), slave_interrupt, RISING);
+}
+
+void loop()
+{
+       uint8_t dip_sw;
+
+       if (new_data)
+       {
+               i2c_read(SLAVE_ADDRESS, &dip_sw, sizeof(dip_sw));
+               Serial.println(dip_sw);
+               new_data = false;
+       }
+}
+
+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;
+}
+
+void slave_interrupt(void)
+{
+       new_data = true;
+}
\ No newline at end of file
diff --git a/Examples/I2C_interrupt_to_master/I2C_interrupt_to_master_slave/I2C_interrupt_to_master_slave.ino b/Examples/I2C_interrupt_to_master/I2C_interrupt_to_master_slave/I2C_interrupt_to_master_slave.ino
new file mode 100644 (file)
index 0000000..4beb9d5
--- /dev/null
@@ -0,0 +1,61 @@
+#include <Wire.h>
+
+#define MASTER_INT_PIN A2
+#define INT_PIN        3
+#define SW0_PIN        7
+#define SW1_PIN        6
+#define SW2_PIN        5
+#define SW3_PIN        4
+
+#define SLAVE_ADDRESS  0x09
+
+void i2c_send_handler(void);
+void button_handler(void);
+
+volatile uint8_t dip_sw = 0;
+volatile bool new_data = false;
+
+void setup()
+{
+       pinMode(MASTER_INT_PIN, OUTPUT);
+       digitalWrite(MASTER_INT_PIN, LOW);
+       pinMode(INT_PIN, INPUT_PULLUP);
+       pinMode(SW0_PIN, INPUT_PULLUP);
+       pinMode(SW1_PIN, INPUT_PULLUP);
+       pinMode(SW2_PIN, INPUT_PULLUP);
+       pinMode(SW3_PIN, INPUT_PULLUP);
+
+       attachInterrupt(digitalPinToInterrupt(INT_PIN), button_handler, FALLING);
+
+       Wire.begin(SLAVE_ADDRESS);
+       Wire.onRequest(i2c_send_handler);
+
+       Serial.begin(9600);
+}
+
+void loop()
+{
+       uint8_t tmp = digitalRead(SW0_PIN) |
+                                (digitalRead(SW1_PIN) << 1) |
+                                (digitalRead(SW2_PIN) << 2) |
+                                (digitalRead(SW3_PIN) << 3);
+       dip_sw = tmp;
+       if (new_data)
+       {
+               new_data = false;
+               digitalWrite(MASTER_INT_PIN, HIGH);
+               delayMicroseconds(5);
+               digitalWrite(MASTER_INT_PIN, LOW);
+               delayMicroseconds(5);
+       }
+}
+
+void i2c_send_handler(void)
+{
+       Wire.write((uint8_t *) &dip_sw, sizeof(dip_sw));
+}
+
+void button_handler(void)
+{
+       new_data = true;
+}