Archived
1
0

change Port_MCP23S17, add slaveSelect to constructor parameter

This commit is contained in:
wolfv6 2016-11-16 08:29:51 -07:00
parent df4abb3808
commit 45674eb9b9
4 changed files with 17 additions and 16 deletions

View File

@ -12,11 +12,11 @@ uint8_t Port_MCP23S17::transfer(const uint8_t command, const uint8_t registerAdd
uint8_t portState; //bit pattern
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(registerAddr); //register address to write data to
portState = SPI.transfer(data); //write data, read portState
digitalWrite(SS, HIGH); //disable Slave Select
digitalWrite(slaveSelect, HIGH); //disable Slave Select
SPI.endTransaction();
return portState;
@ -26,8 +26,8 @@ uint8_t Port_MCP23S17::transfer(const uint8_t command, const uint8_t registerAdd
*/
void Port_MCP23S17::beginProtocol()
{
pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
digitalWrite(SS, HIGH); //disable Slave Select
pinMode(slaveSelect, OUTPUT); //configure controller's Slave Select pin to output
digitalWrite(slaveSelect, HIGH); //disable Slave Select
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
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
{
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
@ -57,7 +57,7 @@ pin is bit pattern, where pin being set is 1.
*/
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
}
@ -66,7 +66,7 @@ pin is bit pattern, where pin being set is 1.
*/
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
}

View File

@ -9,8 +9,7 @@
write pins are connected to matrix Row (strobe pin) or LED.
readPins are connected to matrix column to read which keys are pressed.
Slave Select is hardcoded to Arduino Pin 10.
Arduino Pin 10 avoids the speed penalty of digitalWrite.
slaveSelect is Arduino-pin number connected to pin 11 (CS a.k.a. SS).
Instantiation
------------
@ -36,14 +35,15 @@ MCP23S17 data sheet
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 deviceAddr, const uint8_t portNum, const uint8_t readPins)
: deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {}
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);

View File

@ -6,7 +6,7 @@
#include <PortWriteInterface.h>
/* 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
{

View File

@ -47,8 +47,9 @@ LED_uC LED_capsLck(21);
// --------------- RIGHT SCANNER ---------------
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
Port_MCP23S17 portB(IOE_ADDR, 1, 0); //for strobe
const uint8_t slaveSelect = 10;
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);