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

50 lines
2.0 KiB
C++
Raw Normal View History

2016-09-01 02:29:59 +00:00
#include "PortWrite_MCP23S17.h"
/* 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)
{
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
}
/* 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()
{
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();
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
//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
}
/*
pin is bitwise, where pin being strobed is 1.
strobe is HIGH or LOW (for active high or active low).
port.outputVal can be shared by LEDs.
The functions does not reset the other pins so that they can be used for LEDs.
*/
void PortWrite_MCP23S17::write(const uint8_t pin, const bool strobe)
{
if (strobe == LOW) //if active low
{
port.outputVal &= ~pin; //set pin output to low
}
else //if active high
{
port.outputVal |= pin; //set pin output to high
}
writePort(port.num + 0x12, port.outputVal); //set GPIO port pins for stobe and LEDs
}