MCP23S17 SPI interface is 10 MHz max. | MCP23S17 SPI interface is 10 MHz max. | ||||
The electrical limitation to bus speed is bus capacitance and the length of the wires involved. | The electrical limitation to bus speed is bus capacitance and the length of the wires involved. | ||||
Longer wires require lower clock speeds. | Longer wires require lower clock speeds. | ||||
begin() is called from Scanner_IOE::begin() twice, once each for refPortWrite and refPortRead. | |||||
The first 4 lines only need to be called once, but seem to work OK if called a second time. | |||||
*/ | */ | ||||
void PortMCP23S17::begin(const uint8_t strobeOn) | void PortMCP23S17::begin(const uint8_t strobeOn) | ||||
{ | { | ||||
{ | { | ||||
pullUp = 0; | pullUp = 0; | ||||
} | } | ||||
//todo | |||||
Keyboard.print(" strobeOn="); Keyboard.print(strobeOn); | |||||
Keyboard.print(" readPins="); Keyboard.print(readPins, BIN); | |||||
Keyboard.print(" pullUp="); Keyboard.println(pullUp, BIN); | |||||
transfer(port.DEVICE_ADDR << 1, port.num, readPins); //configure IODIR | transfer(port.DEVICE_ADDR << 1, port.num, readPins); //configure IODIR | ||||
transfer(port.DEVICE_ADDR << 1, port.num + 0x0C, pullUp); //configure GPPU | transfer(port.DEVICE_ADDR << 1, port.num + 0x0C, pullUp); //configure GPPU | ||||
{ | { | ||||
port.outputVal |= pin; //set pin output to high | port.outputVal |= pin; //set pin output to high | ||||
} | } | ||||
//todo | |||||
//Keyboard.print(" readPins="); Keyboard.print(readPins, BIN); | |||||
Keyboard.print(" pin="); Keyboard.print(pin, BIN); | |||||
Keyboard.print(" logicLevel="); Keyboard.print(logicLevel); | |||||
Keyboard.print(" outputVal="); Keyboard.println(port.outputVal, BIN); | |||||
//Keyboard.print(" ="); Keyboard.print(); | |||||
//delay(200); | |||||
transfer(port.DEVICE_ADDR << 1, port.num + 0x12, port.outputVal); //set GPIO port to outputVal | transfer(port.DEVICE_ADDR << 1, port.num + 0x12, port.outputVal); //set GPIO port to outputVal | ||||
} | } |
void Scanner_IOE::begin() | void Scanner_IOE::begin() | ||||
{ | { | ||||
refPortRead.begin(strobeOn); | refPortRead.begin(strobeOn); | ||||
refPortWrite.begin(strobeOn); | |||||
} | } | ||||
/* scan() is called on every iteration of sketch loop(). | /* scan() is called on every iteration of sketch loop(). | ||||
{ | { | ||||
uint8_t readState; //bits, 1 means key is pressed, 0 means released | uint8_t readState; //bits, 1 means key is pressed, 0 means released | ||||
delay(2000);//todo | |||||
//strobe on | //strobe on | ||||
refPortWrite.write(strobePin, strobeOn); | refPortWrite.write(strobePin, strobeOn); | ||||
delayMicroseconds(3); //time to stabilize voltage | delayMicroseconds(3); //time to stabilize voltage | ||||
//delayMicroseconds(300); //todo | |||||
delay(2000); | |||||
//read the port pins | //read the port pins | ||||
readState = refPortRead.read(); | readState = refPortRead.read(); | ||||
/* unit test for PortRead_MCP23S17 | |||||
Picture of hardware is in unit_tests/PortRead_MCP23S17/PortRead_MCP23S17_bb.JPG | |||||
/* unit test for PortMCP23S17 | |||||
Picture of hardware is in unit_tests/PortMCP23S17_read/PortMCP23S17_bb.JPG | |||||
The setup is an MCP23S17 I/O expander on a Teensy LC controller. | The setup is an MCP23S17 I/O expander on a Teensy LC controller. | ||||
MCP23S17 port-B pins are alternately grounded and energized. | MCP23S17 port-B pins are alternately grounded and energized. | ||||
portBState is a bitwise reading of port B. | portBState is a bitwise reading of port B. | ||||
*/ | */ | ||||
#include "PortIOE.h" | #include "PortIOE.h" | ||||
#include "PortRead_MCP23S17.h" | |||||
#include "Scanner_IOE.h" | |||||
#include "PortMCP23S17.h" | |||||
const uint8_t PortIOE::DEVICE_ADDR = 0x20; //MCP23S17 address, all 3 ADDR pins are grounded | const uint8_t PortIOE::DEVICE_ADDR = 0x20; //MCP23S17 address, all 3 ADDR pins are grounded | ||||
PortIOE portB(1, 0); | |||||
PortIOE portB(1); | |||||
PortRead_MCP23S17 portBRead(portB, ~0); | |||||
PortMCP23S17 portBRead(portB, ~0); | |||||
void setup() | void setup() | ||||
{ | { | ||||
uint8_t portBState; //bit pattern | uint8_t portBState; //bit pattern | ||||
delay(6000); | delay(6000); | ||||
portBRead.begin(LOW); | |||||
portBRead.begin(HIGH); //HIGH or LOW, not matter | |||||
portBState = portBRead.read(); | portBState = portBRead.read(); | ||||
Keyboard.print("portBState = "); | Keyboard.print("portBState = "); |
/* unit test for PortWrite_MCP23S17 | |||||
Picture of hardware is in unit_tests/PortRead_MCP23S17/PortRead_MCP23S17_bb.JPG | |||||
/* unit test for PortMCP23S17 | |||||
Picture of hardware is in unit_tests/PortMCP23S17_read/PortMCP23S17_bb.JPG | |||||
The setup is an MCP23S17 I/O expander on a Teensy LC controller. | The setup is an MCP23S17 I/O expander on a Teensy LC controller. | ||||
MCP23S17 port-A GPIO pins are not connected to anything. | MCP23S17 port-A GPIO pins are not connected to anything. | ||||
Port-A GPIO-pin ouputs alternate between 0 and 3.3 volts. | Port-A GPIO-pin ouputs alternate between 0 and 3.3 volts. | ||||
Use a volt meter to measure port-A GPIO-pin outputs. | Use a volt meter to measure port-A GPIO-pin outputs. | ||||
MCP23S17 on 3.3v does not output enough power to reliable light LEDs | |||||
LED lights w/o resistor | |||||
LED not light with 56 ohm resistor | |||||
OR low-voltage LED, with forward voltage less than 2 volts. | |||||
*/ | */ | ||||
#include "PortIOE.h" | #include "PortIOE.h" | ||||
#include "PortWrite_MCP23S17.h" | |||||
#include "PortMCP23S17.h" | |||||
const uint8_t PortIOE::DEVICE_ADDR = 0x20; //MCP23S17 address, all 3 ADDR pins are grounded | const uint8_t PortIOE::DEVICE_ADDR = 0x20; //MCP23S17 address, all 3 ADDR pins are grounded | ||||
PortIOE portA(0, 0); | |||||
PortIOE portA(0); | |||||
PortWrite_MCP23S17 portAWrite(portA); //PortAWrite needed for begin() | |||||
PortMCP23S17 portAWrite(portA, 0); //PortAWrite needed for begin() | |||||
//const uint8_t GPIOA = 0x12; //LEDs are on port A | //const uint8_t GPIOA = 0x12; //LEDs are on port A | ||||
void setup() | void setup() | ||||
{ | { | ||||
delay(6000); | delay(6000); | ||||
portAWrite.begin(); | |||||
//Keyboard.print("start blinking"); | |||||
portAWrite.begin(LOW); //HIGH or LOW, not matter if readPins=0 | |||||
Keyboard.print("start writing"); | |||||
} | } | ||||
void loop() | void loop() |