-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathPort_MCP23S17.h
53 lines (47 loc) · 2.13 KB
/
Port_MCP23S17.h
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
#ifndef PORT_MCP23S17_H
#define PORT_MCP23S17_H
#include <Arduino.h>
#include <inttypes.h>
#include <SPI.h>
#include "PortInterface.h"
/* Port_MCP23S17
write pins are connected to matrix Row (strobe pin) or LED.
readPins are connected to matrix column to read which keys are pressed.
slaveSelect is Arduino-pin number connected to pin 11 (CS a.k.a. SS).
Instantiation
------------
MCP23S17 datasheet identifies ports by letters, while class Port_MCP23S17 uses portNum
for port A, use portNum=0
for port B, use portNum=1
readPins parameter configures port's pins.
Example instantiation:
const uint8_t IOE_ADDR = 0x20; //MCP23S17 address, all 3 ADDR pins are grounded
Port_MCP23S17 portB(IOE_ADDR, 1, 0); //all pins are set to output for strobes and LEDs
Port_MCP23S17 portA(IOE_ADDR, 0, 1<<0 | 1<<1 ); //pin 0 and pin 1 are set to input for reading,
//remaining pins can be used for LEDs
Diode orientation
----------------
Diode orientation is explained in keybrd_library_user_guide.md > Diode orientation
MCP23S17 data sheet
------------------
http://www.onsemi.com/pub_link/Collateral/MCP23S17-D.PDF
*/
class Port_MCP23S17 : public PortInterface
{
private:
const uint8_t slaveSelect; //controller-pin number
const uint8_t deviceAddr;
const uint8_t portNum; //port identification number
uint8_t outputVal; //bit pattern for strobe and LEDs
const uint8_t readPins; //bit pattern, IODIR 0=output, 1=input
uint8_t transfer(const uint8_t command, const uint8_t registerAddr, const uint8_t data);
public:
Port_MCP23S17(const uint8_t slaveSelect, const uint8_t deviceAddr, const uint8_t portNum, const uint8_t readPins)
: slaveSelect(slaveSelect), deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {}
void beginProtocol();
void begin(const uint8_t activeState);
virtual void writeLow(const uint8_t pin);
virtual void writeHigh(const uint8_t pin);
virtual uint8_t read();
};
#endif