2016-09-01 02:29:59 +00:00
|
|
|
#include "PortWrite_MCP23S17.h"
|
|
|
|
|
2016-09-01 07:08:52 +00:00
|
|
|
/* writePort() sets registerAddr to data.
|
|
|
|
*/
|
2016-09-01 02:29:59 +00:00
|
|
|
void PortWrite_MCP23S17::writePort(const uint8_t registerAddr, const uint8_t data)
|
|
|
|
{
|
2016-09-01 07:08:52 +00:00
|
|
|
digitalWrite(SS, LOW); //enable Slave Select
|
|
|
|
SPI.transfer(port.ADDR << 1); //write command
|
|
|
|
SPI.transfer(registerAddr); //register address to write data to
|
|
|
|
SPI.transfer(data); //data
|
|
|
|
digitalWrite(SS, HIGH); //disable Slave Select
|
2016-09-01 02:29:59 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 07:08:52 +00:00
|
|
|
/* begin() should be called once from sketch in setup().
|
|
|
|
PortRead_MCP23S17 and PortWrite_MCP23S17 should be on seperate ports on the same MCP23S17.
|
2016-09-01 02:29:59 +00:00
|
|
|
Output pins can be used for strobe pins and LEDs.
|
|
|
|
*/
|
|
|
|
void PortWrite_MCP23S17::begin()
|
|
|
|
{
|
2016-09-01 07:08:52 +00:00
|
|
|
pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
|
|
|
|
digitalWrite(SS, HIGH); //disable Slave Select
|
2016-09-01 02:29:59 +00:00
|
|
|
SPI.begin();
|
2016-09-01 07:08:52 +00:00
|
|
|
SPI.beginTransaction(SPISettings (SPI_CLOCK_DIV8, MSBFIRST, SPI_MODE0)); //control SPI bus todo is slow clock needed?
|
|
|
|
|
|
|
|
writePort(port.num, 0); //configure port direction (port.num) to output (0)
|
2016-09-01 02:29:59 +00:00
|
|
|
|
2016-09-01 07:08:52 +00:00
|
|
|
//SPI.endTransaction() is not called to release the SPI bus
|
|
|
|
// because keyboard only has one SPI device.
|
2016-09-01 02:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-09-03 02:24:55 +00:00
|
|
|
strobePin is bitwise, where pin being strobed is 1.
|
|
|
|
pinLogicLevel is HIGH or LOW.
|
2016-09-01 02:29:59 +00:00
|
|
|
port.outputVal can be shared by LEDs.
|
|
|
|
The functions does not reset the other pins so that they can be used for LEDs.
|
|
|
|
*/
|
2016-09-03 02:24:55 +00:00
|
|
|
void PortWrite_MCP23S17::write(const uint8_t strobePin, const uint8_t pinLogicLevel)
|
2016-09-01 02:29:59 +00:00
|
|
|
{
|
2016-09-03 02:24:55 +00:00
|
|
|
if (pinLogicLevel == LOW)
|
2016-09-01 02:29:59 +00:00
|
|
|
{
|
2016-09-03 02:24:55 +00:00
|
|
|
port.outputVal &= ~strobePin; //set strobePin output to low
|
2016-09-01 02:29:59 +00:00
|
|
|
}
|
2016-09-03 02:24:55 +00:00
|
|
|
else
|
2016-09-01 02:29:59 +00:00
|
|
|
{
|
2016-09-03 02:24:55 +00:00
|
|
|
port.outputVal |= strobePin; //set strobePin output to high
|
2016-09-01 02:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writePort(port.num + 0x12, port.outputVal); //set GPIO port pins for stobe and LEDs
|
|
|
|
}
|