]> kolegite.com Git - vmks.git/blob
4beb9d5ed4e04430ba8a9f1c7b94a2171cccd1df
[vmks.git] /
1 #include <Wire.h>
2
3 #define MASTER_INT_PIN A2
4 #define INT_PIN 3
5 #define SW0_PIN 7
6 #define SW1_PIN 6
7 #define SW2_PIN 5
8 #define SW3_PIN 4
9
10 #define SLAVE_ADDRESS 0x09
11
12 void i2c_send_handler(void);
13 void button_handler(void);
14
15 volatile uint8_t dip_sw = 0;
16 volatile bool new_data = false;
17
18 void setup()
19 {
20 pinMode(MASTER_INT_PIN, OUTPUT);
21 digitalWrite(MASTER_INT_PIN, LOW);
22 pinMode(INT_PIN, INPUT_PULLUP);
23 pinMode(SW0_PIN, INPUT_PULLUP);
24 pinMode(SW1_PIN, INPUT_PULLUP);
25 pinMode(SW2_PIN, INPUT_PULLUP);
26 pinMode(SW3_PIN, INPUT_PULLUP);
27
28 attachInterrupt(digitalPinToInterrupt(INT_PIN), button_handler, FALLING);
29
30 Wire.begin(SLAVE_ADDRESS);
31 Wire.onRequest(i2c_send_handler);
32
33 Serial.begin(9600);
34 }
35
36 void loop()
37 {
38 uint8_t tmp = digitalRead(SW0_PIN) |
39 (digitalRead(SW1_PIN) << 1) |
40 (digitalRead(SW2_PIN) << 2) |
41 (digitalRead(SW3_PIN) << 3);
42 dip_sw = tmp;
43 if (new_data)
44 {
45 new_data = false;
46 digitalWrite(MASTER_INT_PIN, HIGH);
47 delayMicroseconds(5);
48 digitalWrite(MASTER_INT_PIN, LOW);
49 delayMicroseconds(5);
50 }
51 }
52
53 void i2c_send_handler(void)
54 {
55 Wire.write((uint8_t *) &dip_sw, sizeof(dip_sw));
56 }
57
58 void button_handler(void)
59 {
60 new_data = true;
61 }