1 | #include <Wire.h>
|
2 |
|
3 | #define Mcp_Reg_IODIRA 0x00 // IO direction (0 = output, 1 = input (Default))
|
4 | #define Mcp_Reg_IODIRB 0x01
|
5 | #define Mcp_Reg_IOPOLA 0x02 // IO polarity (0 = normal, 1 = inverse)
|
6 | #define Mcp_Reg_IOPOLB 0x03
|
7 | #define Mcp_Reg_GPINTENA 0x04 // Interrupt on change (0 = disable, 1 = enable)
|
8 | #define Mcp_Reg_GPINTENB 0x05
|
9 | #define Mcp_Reg_DEFVALA 0x06 // Default comparison for interrupt on change (interrupts on opposite)
|
10 | #define Mcp_Reg_DEFVALB 0x07
|
11 | #define Mcp_Reg_INTCONA 0x08 // Interrupt control (0 = interrupt on change from previous, 1 = interrupt on change from DEFVAL)
|
12 | #define Mcp_Reg_INTCONB 0x09
|
13 | #define Mcp_Reg_IOCON 0x0A // IO Configuration: bank/mirror/seqop/disslw/haen/odr/intpol/notimp
|
14 | //#define Mcp_Reg_IOCON 0x0B // same as 0x0A
|
15 | #define Mcp_Reg_GPPUA 0x0C // Pull-up resistor (0 = disabled, 1 = enabled)
|
16 | #define Mcp_Reg_GPPUB 0x0D
|
17 | #define Mcp_Reg_INFTFA 0x0E // Interrupt flag (read only) : (0 = no interrupt, 1 = pin caused interrupt)
|
18 | #define Mcp_Reg_INFTFB 0x0F
|
19 | #define Mcp_Reg_INTCAPA 0x10 // Interrupt capture (read only) : value of GPIO at time of last interrupt
|
20 | #define Mcp_Reg_INTCAPB 0x11
|
21 | #define Mcp_Reg_GPIOA 0x12 // Port value. Write to change, read to obtain value
|
22 | #define Mcp_Reg_GPIOB 0x13
|
23 | #define Mcp_Reg_OLLATA 0x14 // Output latch. Write to latch output.
|
24 | #define Mcp_Reg_OLLATB 0x15
|
25 |
|
26 | #define mcp 0x21
|
27 |
|
28 | #define MCPResetPin 4 // hier hängt der MCP mit seinem Reset-Pin dran
|
29 | #define MCPInterruptA 0 // hier hängt der MCP mit seinem Interrupt-Pin 19 dran
|
30 | #define MCPInterruptB 1 // hier hängt der MCP mit seinem Interrupt-Pin 19 dran
|
31 | #define LEDOnBoard 13 // pin 13, LED aufm Arduino-Board
|
32 |
|
33 | byte WireTransmitResult = 0;
|
34 | boolean SwitchPressed = false;
|
35 | boolean isInInit = false;
|
36 | byte inputStateA = 0;
|
37 | byte inputStateB = 0;
|
38 |
|
39 |
|
40 | void setup()
|
41 | {
|
42 | Serial.begin(9600);
|
43 | Serial.println("Start");
|
44 | Wire.begin();
|
45 | initiic();
|
46 | pinMode (LEDOnBoard, OUTPUT);
|
47 | digitalWrite (LEDOnBoard, LOW);
|
48 | attachInterrupt(MCPInterruptA, ISRSwitchPressed, FALLING);
|
49 | // attachInterrupt(MCPInterruptB, ISRSwitchPressed, FALLING);
|
50 | Serial.println("Bereit");
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 | void loop()
|
56 | {
|
57 | if (SwitchPressed)
|
58 | {
|
59 | inputStateA = readiicbyte(mcp, Mcp_Reg_INTCAPA);
|
60 | inputStateB = readiicbyte(mcp, Mcp_Reg_INTCAPB);
|
61 |
|
62 | Serial.print("LOOP inputStateA: ");
|
63 | Serial.println(inputStateA, BIN);
|
64 | Serial.print("LOOP inputStateB: ");
|
65 | Serial.println(inputStateB, BIN);
|
66 |
|
67 | digitalWrite (LEDOnBoard, LOW);
|
68 | SwitchPressed = false;
|
69 | }
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 | void ISRSwitchPressed ()
|
75 | {
|
76 | Serial.println("ISRSwitchPressed");
|
77 | digitalWrite (LEDOnBoard, HIGH);
|
78 |
|
79 | inputStateA = readiicbyte(mcp, Mcp_Reg_INTCAPA);
|
80 | inputStateB = readiicbyte(mcp, Mcp_Reg_INTCAPB);
|
81 |
|
82 | Serial.print("ISR inputStateA: ");
|
83 | Serial.println(inputStateA, BIN);
|
84 | Serial.print("ISR inputStateB: ");
|
85 | Serial.println(inputStateB, BIN);
|
86 |
|
87 | if (SwitchPressed || isInInit)
|
88 | {
|
89 | /*
|
90 | Serial.println(inputState, BIN);
|
91 | Serial.print("ISR SP: ");
|
92 | Serial.println((String)isInInit);
|
93 | Serial.print("ISR iI: ");
|
94 | Serial.println((String)SwitchPressed);
|
95 | Serial.println("ISR returned");
|
96 | */
|
97 | digitalWrite (LEDOnBoard, LOW);
|
98 | }
|
99 | else
|
100 | {
|
101 | SwitchPressed = true;
|
102 | }
|
103 | }
|
104 |
|
105 |
|
106 |
|
107 | void writeiic(uint8_t adr, uint8_t port, byte value)
|
108 | {
|
109 | Wire.beginTransmission( adr );
|
110 | Wire.write( port );
|
111 | Wire.write( value );
|
112 | WireTransmitResult = Wire.endTransmission();
|
113 |
|
114 | // String t = "writeiic: ";
|
115 | // t += " Adr: " + String(adr) + " Port: " + String(port) + " Value: " + String(value);
|
116 | // Serial.println(t);
|
117 | }
|
118 |
|
119 |
|
120 |
|
121 | byte readiicbyte(uint8_t adr, uint8_t port)
|
122 | {
|
123 | Serial.println("IIC Lesen");
|
124 | byte returnword = 0x00;
|
125 |
|
126 | Wire.beginTransmission(adr);
|
127 | Wire.write(port);
|
128 | Wire.endTransmission();
|
129 | Wire.requestFrom((int)adr, 1);
|
130 |
|
131 | int c = 0;
|
132 |
|
133 | if (Wire.available())
|
134 | {
|
135 | returnword = Wire.read();
|
136 | }
|
137 | return returnword;
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 | void initiic()
|
143 | {
|
144 | Serial.println("Init Wire");
|
145 | isInInit = true;
|
146 | SwitchPressed = false;
|
147 |
|
148 | pinMode(MCPResetPin, OUTPUT);
|
149 | digitalWrite(MCPResetPin, LOW);
|
150 | delay(5);
|
151 | digitalWrite(MCPResetPin, HIGH);
|
152 |
|
153 | writeiic(mcp, Mcp_Reg_IODIRA, 0x00); // Alles auf Output setzen
|
154 | writeiic(mcp, Mcp_Reg_GPIOA, 0x00); // Alle Pins auf 0 setzen
|
155 | writeiic(mcp, Mcp_Reg_IODIRB, 0x00); // Alles auf Output setzen
|
156 | writeiic(mcp, Mcp_Reg_GPIOB, 0x00); // alle Pins auf 0 setzen
|
157 |
|
158 | byte inputMaskA = 0xFF;
|
159 | byte inputMaskB = 0xFF;
|
160 | byte polA = 0xFF;
|
161 | byte polB = 0xFF;
|
162 |
|
163 | writeiic(mcp, Mcp_Reg_IODIRA, inputMaskA); // Richtung (Ein-/Ausgang) einstellen
|
164 | writeiic(mcp, Mcp_Reg_IODIRB, inputMaskB); // Richtung (Ein-/Ausgang) einstellen
|
165 |
|
166 | writeiic(mcp, Mcp_Reg_GPPUA, inputMaskA); // Pull-Up Widerstände einrichten
|
167 | writeiic(mcp, Mcp_Reg_GPPUB, inputMaskB); // Pull-Up Widerstände einrichten
|
168 |
|
169 | writeiic(mcp, Mcp_Reg_IOPOLA, polA); // Polarität einstellen (für Eingänge 0 oder 1)
|
170 | writeiic(mcp, Mcp_Reg_IOPOLB, polB); // Polarität einstellen (für Eingänge 0 oder 1)
|
171 |
|
172 | writeiic(mcp, Mcp_Reg_GPINTENA, inputMaskA); // Interrupt aktivieren
|
173 | writeiic(mcp, Mcp_Reg_GPINTENB, inputMaskB); // Interrupt aktivieren
|
174 |
|
175 | writeiic(mcp, Mcp_Reg_DEFVALA, inputMaskA); // Interrupt Default-Wert festlegen
|
176 | writeiic(mcp, Mcp_Reg_DEFVALB, inputMaskB); // Interrupt Default-Wert festlegen
|
177 |
|
178 | writeiic(mcp, Mcp_Reg_INTCONA, inputMaskA); // Interrupt Vergleich einstellen, 0 = Änderung zum Vorgänger, 1 = Änderung zum DEFVAL-Wert
|
179 | writeiic(mcp, Mcp_Reg_INTCONB, inputMaskB); // Interrupt Vergleich einstellen, 0 = Änderung zum Vorgänger, 1 = Änderung zum DEFVAL-Wert
|
180 |
|
181 | readiicbyte(mcp, Mcp_Reg_INTCAPA); // Interrupts lesen und dadurch löschen
|
182 | readiicbyte(mcp, Mcp_Reg_INTCAPB); // Interrupts lesen und dadurch löschen
|
183 |
|
184 | Serial.println("Init Fertig");
|
185 | isInInit = false;
|
186 | }
|