Archived
1
0
This repo is archived. You can view files and clone it, but cannot push or open issues or pull requests.
keybrd/src/PortWrite_MCP23S17.cpp

47 lines
1.7 KiB
C++
Raw Normal View History

2016-09-01 02:29:59 +00:00
#include "PortWrite_MCP23S17.h"
2016-09-11 21:23:47 +00:00
/* push() writes data to registerAddr.
*/
2016-09-11 21:23:47 +00:00
void PortWrite_MCP23S17::push(const uint8_t registerAddr, const uint8_t data)
2016-09-01 02:29:59 +00:00
{
digitalWrite(SS, LOW); //enable Slave Select
SPI.transfer(port.DEVICE_ADDR << 1); //write command
SPI.transfer(registerAddr); //register address to write data to
2016-09-11 21:23:47 +00:00
SPI.transfer(data); //write the data
digitalWrite(SS, HIGH); //disable Slave Select
2016-09-01 02:29:59 +00:00
}
2016-09-11 21:23:47 +00:00
/* begin() is called from Scanner_IOE::begin().
Initiates SPI bus and configures write pins to output.
MCP23S17 SPI interface is 10 MHz max.
2016-09-01 02:29:59 +00:00
*/
void PortWrite_MCP23S17::begin()
{
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-11 21:23:47 +00:00
SPI.beginTransaction(SPISettings (5000000, MSBFIRST, SPI_MODE0)); //control SPI bus, 5 MHz
//SPI.endTransaction() not called to release SPI bus because keyboard only has one SPI device.
2016-09-11 21:23:47 +00:00
push(port.num, 0); //configure port direction (port.num) to output (0)
2016-09-01 02:29:59 +00:00
}
2016-09-11 21:23:47 +00:00
/* write() sets pin output to logicLevel.
pin is bitwise, where pin being set is 1.
logicLevel is HIGH or LOW.
write() does not overwrite the other pins.
2016-09-01 02:29:59 +00:00
*/
2016-09-11 21:23:47 +00:00
void PortWrite_MCP23S17::write(const uint8_t pin, const bool logicLevel)
2016-09-01 02:29:59 +00:00
{
2016-09-11 21:23:47 +00:00
if (logicLevel == LOW)
2016-09-01 02:29:59 +00:00
{
2016-09-11 21:23:47 +00:00
port.outputVal &= ~pin; //set pin 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-11 21:23:47 +00:00
port.outputVal |= pin; //set pin output to high
2016-09-01 02:29:59 +00:00
}
2016-09-11 21:23:47 +00:00
push(port.num + 0x12, port.outputVal); //set GPIO port pin to outputVal
2016-09-01 02:29:59 +00:00
}