-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMCP23017.ino
199 lines (154 loc) · 4.93 KB
/
MCP23017.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*#include <Adafruit_MCP23X17.h>
Adafruit_MCP23X17 myMCP;
uint8_t mcp_module_num;
uint8_t mcp_status;
uint16_t IntCounter;
uint8_t intCapReg;
bool mcp_boot ( uint8_t mybootnum ) {
mcp_module_num = mybootnum;
tcaselect(TCA_MCP);
mcp_status = 0;
if (!myMCP.begin_I2C()) return false;
for (uint8_t loop = 0; loop < 16; loop++) {
if (loop < 8) {
myMCP.pinMode(loop, INPUT_PULLUP); //Port A: all pins are INPUT
}else{
myMCP.pinMode(loop, OUTPUT); //Port B: all pins are OUTPUT
}
}
myMCP.writeGPIOB ( 0b00000000 );
myMCP.setupInterrupts( true, false, HIGH); //mirror, open drain, polarity
myMCP.setupInterruptPin( 9, CHANGE ); //only button for now
return true;
}
void mcp_read () {
if (!ModuleData[mcp_module_num].Get.ModuleDetected) return;
mcp_status = 1;
tcaselect(TCA_MCP);
SensorData.mcpData.LastInt = intCapReg;
SensorData.mcpData.PortA = myMCP.readGPIOA();
SensorData.mcpData.PortB = myMCP.readGPIOB();
}
void mcp_print () {
switch (mcp_status) {
case 0:
printf( "MCP: Not Detected\n" );
mcp_status = 2;
break;
case 1:
printf( "MCP: IN(A)[" BYTE_TO_BINARY_PATTERN "] OUT(B)[" BYTE_TO_BINARY_PATTERN "] Int[" BYTE_TO_BINARY_PATTERN "](%u)(%u) \n",
BYTE_TO_BINARY(SensorData.mcpData.PortA),
BYTE_TO_BINARY(SensorData.mcpData.PortB),
BYTE_TO_BINARY(SensorData.mcpData.LastInt),
IntCounter,
inthit );
break;
case 2: //silent stop
break;
}
}
void mcpWritePin(uint8_t pin, uint8_t value) {
myMCP.digitalWrite( (pin && 0x07) + 0x07, value );
}
bool mcpReadPin(uint8_t pin) {
return myMCP.digitalRead( pin );
}
void mcpPinMode(uint8_t pin, uint8_t mode) {
//myMCP.setPinMode( pin, B, mode ); // este nao faz muito sentido
}
void mcpTogglePin(uint8_t pin) {
//printf("MCP: Toggle pin B%u\n", pin);
uint8_t pinfilter;
pinfilter = (pin && 0x07) + 0x07;
printf("MCP: pinfilter %X, pin %X\n", pinfilter, pin);
myMCP.digitalWrite( pinfilter, !myMCP.digitalRead(pinfilter) );
//myMCP.togglePin( pin, B );
}
void mcp_eventcheck(){
intCapReg = myMCP.getLastInterruptPin();
if (mcpintevent) {
IntCounter++;
//printf("MCP: Int Event Ocurred %u times.\n", IntCounter);
mcpTogglePin( MCPOUT_LED );
mcpintevent = false;
}
}
*/
#include <MCP23017.h>
#define MCP_ADDRESS 0x20
MCP23017 myMCP = MCP23017(MCP_ADDRESS);
uint8_t mcp_module_num;
uint8_t mcp_status;
uint16_t IntCounter;
uint8_t intCapReg;
bool mcp_boot ( uint8_t mybootnum ) {
mcp_module_num = mybootnum;
tcaselect(TCA_MCP);
mcp_status = 0;
if (!myMCP.Init()) return false;
myMCP.setPortMode(0b00000000, A, INPUT_PULLUP); //Port A: all pins are INPUT
//myMCP.setPortPullUp(MCPINT_BUTTON, A);
myMCP.setPortMode(0b11111111, B); //Port B: all pins are OUTPUT
//myMCP.setAllPins (B, OFF);
myMCP.setInterruptPinPol(LOW);
myMCP.setIntMirror( ON );
myMCP.setIntOdr(true);
myMCP.setInterruptOnChangePort(0b01000000, A); //only button for now
return true;
}
void mcp_read () {
if (!ModuleData[mcp_module_num].Register.ModuleDetected) return;
mcp_status = 1;
tcaselect(TCA_MCP);
SensorData.mcpData.LastInt = intCapReg;
SensorData.mcpData.PortA = myMCP.getPort(A);
SensorData.mcpData.PortB = myMCP.getPort(B);
}
void mcp_print () {
switch (mcp_status) {
case 0:
printf( "MCP: Not Detected\n" );
mcp_status = 2;
break;
case 1:
printf( "MCP: IN(A)[" BYTE_TO_BINARY_PATTERN "] OUT(B)[" BYTE_TO_BINARY_PATTERN "] Int[" BYTE_TO_BINARY_PATTERN "](%u)(%u) \n",
BYTE_TO_BINARY(SensorData.mcpData.PortA),
BYTE_TO_BINARY(SensorData.mcpData.PortB),
BYTE_TO_BINARY(SensorData.mcpData.LastInt),
IntCounter, inthit);
break;
case 2: //silent stop
break;
}
}
void mcpWritePin(uint8_t pin, uint8_t value) {
//printf("MCP: Write pin B%u - %u\n", pin, value);
myMCP.setPin(pin, B, value);
}
bool mcpReadPin(uint8_t pin) {
return myMCP.getPin( pin, A );
}
void mcpPinMode(uint8_t pin, uint8_t mode) {
myMCP.setPinMode( pin, B, mode ); // este nao faz muito sentido
}
void mcpTogglePin(uint8_t pin) {
//printf("MCP: Toggle pin B%u\n", pin);
myMCP.togglePin( pin, B );
}
bool ledstatus;
void mcpSetALL() {
//only toggle all for now
//printf("MCP: setall %u\n", ledstatus);
myMCP.setAllPins (B, ledstatus);
ledstatus = !ledstatus;
}
void mcp_eventcheck(){
intCapReg = myMCP.getIntCap(A);
if (mcpintevent) {
IntCounter++;
//printf("MCP: Int Event Ocurred %u times.\n", IntCounter);
mcpTogglePin( MCPOUT_LED );
}
mcpintevent = false;
}
//EOF