change Port_MCP23S17, add slaveSelect to constructor parameter
This commit is contained in:
parent
df4abb3808
commit
45674eb9b9
@ -12,11 +12,11 @@ uint8_t Port_MCP23S17::transfer(const uint8_t command, const uint8_t registerAdd
|
|||||||
uint8_t portState; //bit pattern
|
uint8_t portState; //bit pattern
|
||||||
|
|
||||||
SPI.beginTransaction( SPISettings(5000000, MSBFIRST, SPI_MODE0) ); //control SPI bus, 5 MHz
|
SPI.beginTransaction( SPISettings(5000000, MSBFIRST, SPI_MODE0) ); //control SPI bus, 5 MHz
|
||||||
digitalWrite(SS, LOW); //enable Slave Select
|
digitalWrite(slaveSelect, LOW); //enable Slave Select
|
||||||
SPI.transfer(command); //write or read command
|
SPI.transfer(command); //write or read command
|
||||||
SPI.transfer(registerAddr); //register address to write data to
|
SPI.transfer(registerAddr); //register address to write data to
|
||||||
portState = SPI.transfer(data); //write data, read portState
|
portState = SPI.transfer(data); //write data, read portState
|
||||||
digitalWrite(SS, HIGH); //disable Slave Select
|
digitalWrite(slaveSelect, HIGH); //disable Slave Select
|
||||||
SPI.endTransaction();
|
SPI.endTransaction();
|
||||||
|
|
||||||
return portState;
|
return portState;
|
||||||
@ -26,8 +26,8 @@ uint8_t Port_MCP23S17::transfer(const uint8_t command, const uint8_t registerAdd
|
|||||||
*/
|
*/
|
||||||
void Port_MCP23S17::beginProtocol()
|
void Port_MCP23S17::beginProtocol()
|
||||||
{
|
{
|
||||||
pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
|
pinMode(slaveSelect, OUTPUT); //configure controller's Slave Select pin to output
|
||||||
digitalWrite(SS, HIGH); //disable Slave Select
|
digitalWrite(slaveSelect, HIGH); //disable Slave Select
|
||||||
SPI.begin();
|
SPI.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,13 +39,13 @@ void Port_MCP23S17::begin(const uint8_t activeState)
|
|||||||
{
|
{
|
||||||
uint8_t pullUp; //bits, GPPU 0=pull-up disabled, 1=pull-up enabled
|
uint8_t pullUp; //bits, GPPU 0=pull-up disabled, 1=pull-up enabled
|
||||||
|
|
||||||
if (activeState == LOW) //if active low
|
if (activeState == LOW) //if active low
|
||||||
{
|
{
|
||||||
pullUp = readPins; //0=pull-up disabled (for LED), 1=pull-up enabled (for read)
|
pullUp = readPins; //0=pull-up disabled (for LED), 1=pull-up enabled (for read)
|
||||||
}
|
}
|
||||||
else //if active high
|
else //if active high
|
||||||
{
|
{
|
||||||
pullUp = 0; //0=pull-up disabled (for external pull-down resistors)
|
pullUp = 0; //0=pull-up disabled (for external pull-down resistors)
|
||||||
}
|
}
|
||||||
|
|
||||||
transfer(deviceAddr << 1, portNum, readPins); //configure IODIR
|
transfer(deviceAddr << 1, portNum, readPins); //configure IODIR
|
||||||
@ -57,7 +57,7 @@ pin is bit pattern, where pin being set is 1.
|
|||||||
*/
|
*/
|
||||||
void Port_MCP23S17::writeLow(const uint8_t pin)
|
void Port_MCP23S17::writeLow(const uint8_t pin)
|
||||||
{
|
{
|
||||||
outputVal &= ~pin; //set pin output to low
|
outputVal &= ~pin; //set pin output to low
|
||||||
transfer(deviceAddr << 1, portNum + 0x12, outputVal); //set GPIO port to outputVal
|
transfer(deviceAddr << 1, portNum + 0x12, outputVal); //set GPIO port to outputVal
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ pin is bit pattern, where pin being set is 1.
|
|||||||
*/
|
*/
|
||||||
void Port_MCP23S17::writeHigh(const uint8_t pin)
|
void Port_MCP23S17::writeHigh(const uint8_t pin)
|
||||||
{
|
{
|
||||||
outputVal |= pin; //set pin output to high
|
outputVal |= pin; //set pin output to high
|
||||||
transfer(deviceAddr << 1, portNum + 0x12, outputVal); //set GPIO port to outputVal
|
transfer(deviceAddr << 1, portNum + 0x12, outputVal); //set GPIO port to outputVal
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,8 +9,7 @@
|
|||||||
write pins are connected to matrix Row (strobe pin) or LED.
|
write pins are connected to matrix Row (strobe pin) or LED.
|
||||||
readPins are connected to matrix column to read which keys are pressed.
|
readPins are connected to matrix column to read which keys are pressed.
|
||||||
|
|
||||||
Slave Select is hardcoded to Arduino Pin 10.
|
slaveSelect is Arduino-pin number connected to pin 11 (CS a.k.a. SS).
|
||||||
Arduino Pin 10 avoids the speed penalty of digitalWrite.
|
|
||||||
|
|
||||||
Instantiation
|
Instantiation
|
||||||
------------
|
------------
|
||||||
@ -36,14 +35,15 @@ MCP23S17 data sheet
|
|||||||
class Port_MCP23S17 : public PortInterface
|
class Port_MCP23S17 : public PortInterface
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
const uint8_t slaveSelect; //controller-pin number
|
||||||
const uint8_t deviceAddr;
|
const uint8_t deviceAddr;
|
||||||
const uint8_t portNum; //port identification number
|
const uint8_t portNum; //port identification number
|
||||||
uint8_t outputVal; //bit pattern for strobe and LEDs
|
uint8_t outputVal; //bit pattern for strobe and LEDs
|
||||||
const uint8_t readPins; //bit pattern, IODIR 0=output, 1=input
|
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);
|
uint8_t transfer(const uint8_t command, const uint8_t registerAddr, const uint8_t data);
|
||||||
public:
|
public:
|
||||||
Port_MCP23S17(const uint8_t deviceAddr, const uint8_t portNum, const uint8_t readPins)
|
Port_MCP23S17(const uint8_t slaveSelect, const uint8_t deviceAddr, const uint8_t portNum, const uint8_t readPins)
|
||||||
: deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {}
|
: slaveSelect(slaveSelect), deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {}
|
||||||
void beginProtocol();
|
void beginProtocol();
|
||||||
void begin(const uint8_t activeState);
|
void begin(const uint8_t activeState);
|
||||||
virtual void writeLow(const uint8_t pin);
|
virtual void writeLow(const uint8_t pin);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include <PortWriteInterface.h>
|
#include <PortWriteInterface.h>
|
||||||
|
|
||||||
/* Port_ShiftRegs
|
/* Port_ShiftRegs
|
||||||
slaveSelect is controller-pin number connected to shift register RCLK pin a.k.a. SS or ST
|
slaveSelect is Arduino-pin number connected to shift register RCLK pin a.k.a. SS or ST
|
||||||
*/
|
*/
|
||||||
class Port_ShiftRegs : public PortWriteInterface
|
class Port_ShiftRegs : public PortWriteInterface
|
||||||
{
|
{
|
||||||
|
@ -47,8 +47,9 @@ LED_uC LED_capsLck(21);
|
|||||||
|
|
||||||
// --------------- RIGHT SCANNER ---------------
|
// --------------- RIGHT SCANNER ---------------
|
||||||
const uint8_t IOE_ADDR = 0x20; //MCP23S17 address, all 3 ADDR pins are grounded
|
const uint8_t IOE_ADDR = 0x20; //MCP23S17 address, all 3 ADDR pins are grounded
|
||||||
Port_MCP23S17 portA(IOE_ADDR, 0, 1<<0 | 1<<1 ); //for read
|
const uint8_t slaveSelect = 10;
|
||||||
Port_MCP23S17 portB(IOE_ADDR, 1, 0); //for strobe
|
Port_MCP23S17 portA(slaveSelect , IOE_ADDR, 0, 1<<0 | 1<<1 ); //for read
|
||||||
|
Port_MCP23S17 portB(slaveSelect , IOE_ADDR, 1, 0); //for strobe
|
||||||
|
|
||||||
Scanner_IOE scanner_R(LOW, portB, portA);
|
Scanner_IOE scanner_R(LOW, portB, portA);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user