From d9465be221fce1ae4b1bffe1901c517ef1d3917a Mon Sep 17 00:00:00 2001 From: wolfv6 Date: Wed, 2 Nov 2016 15:15:16 -0600 Subject: [PATCH] reverse SS HIGH/LOW in Scanner_ShiftRegsPISOMultiRow::scan() --- doc/keybrd_library_user_guide.md | 1 + src/Scanner_ShiftRegsPISOMultiRow.cpp | 28 ++++++++++++++++++-------- src/Scanner_ShiftRegsPISOSingleRow.cpp | 2 +- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/doc/keybrd_library_user_guide.md b/doc/keybrd_library_user_guide.md index 2dd0930..3e17be0 100644 --- a/doc/keybrd_library_user_guide.md +++ b/doc/keybrd_library_user_guide.md @@ -142,6 +142,7 @@ The following is a listing of items to check when a new keybrd sketch or keyboar Development-environment items to check: * If the keyboard has an I/O expander, power cycle (replug the USB) after loading the HEX file. +* Sometimes sketch will not loading properly if two instances of Arduino IDE are open. * For compile error: ``` 'KEY_A' was not declared in this scope diff --git a/src/Scanner_ShiftRegsPISOMultiRow.cpp b/src/Scanner_ShiftRegsPISOMultiRow.cpp index a9125d4..b23c539 100644 --- a/src/Scanner_ShiftRegsPISOMultiRow.cpp +++ b/src/Scanner_ShiftRegsPISOMultiRow.cpp @@ -21,32 +21,44 @@ Initializes shift register's shift/load pin. */ void Scanner_ShiftRegsPISOMultiRow::begin() { + digitalWrite(slaveSelect, HIGH); //initialize ??only needed for first scan SPI.begin(); //todo move this to constructor or init() } /* scan() strobes the row's strobePin and returns state of the shift register's input pins. strobePin is Arduino pin number connected to this row. Bit patterns are 1 bit per key. + +Scanner_ShiftRegsPISOMultiRow class was tested on two sets of 74HC165 shift registers +and 74AHC1G126 tri-state buffer chips + +74HC165 is not an SPI device. +The 74HC165 SH/LD polarity is the inverse of SPI slave-select convention. +Most SPI chips will high-Z their MISO pin when their slave select signal is high. +To use SPI-like protocol, 74HC*126 will high-Z the MISO pin when the slave select signal is low. + +SPI.beginTransaction() and SPI.endTransaction() are not needed, +but would be needed if trackball uses interrupts. */ read_pins_t Scanner_ShiftRegsPISOMultiRow::scan(const uint8_t strobePin) { read_pins_t readState = 0; //bits, 1 means key is pressed, 0 means released - SPI.beginTransaction( SPISettings(5000000, MSBFIRST, SPI_MODE0) ); //control SPI bus, 5 MHz - - digitalWrite(slaveSelect, LOW); //load parallel inputs to registers - digitalWrite(strobePin, strobeOn); //strobe on - delayMicroseconds(3); //time to stabilize photo-transistor todo need? - delayMicroseconds(50); //todo for sr2_LEDs_strobe.ino - digitalWrite(slaveSelect, HIGH); //shift the data toward a serial output + //SPI.beginTransaction( SPISettings(5000000, MSBFIRST, SPI_MODE0) ); //control SPI bus, 5 MHz + + delayMicroseconds(20); //photo-transistor at 3.3v needs 20 ms to stablize + + digitalWrite(slaveSelect, HIGH); //shift the data toward a serial output digitalWrite(strobePin, strobeOff); //strobe off SPI.transfer(&readState, byte_count); - SPI.endTransaction(); + //SPI.endTransaction(); + + digitalWrite(slaveSelect, LOW); //load parallel inputs to registers return readState; } diff --git a/src/Scanner_ShiftRegsPISOSingleRow.cpp b/src/Scanner_ShiftRegsPISOSingleRow.cpp index c8d8bcc..8dd8fe6 100644 --- a/src/Scanner_ShiftRegsPISOSingleRow.cpp +++ b/src/Scanner_ShiftRegsPISOSingleRow.cpp @@ -36,9 +36,9 @@ read_pins_t Scanner_ShiftRegsPISOSingleRow::scan(const uint8_t strobePin) read_pins_t readState = 0; //bits, 1 means key is pressed, 0 means released //read all the column pins - digitalWrite(slaveSelect, LOW); //load parallel inputs to the register digitalWrite(slaveSelect, HIGH); //shift the data toward a serial output SPI.transfer(&readState, byte_count); + digitalWrite(slaveSelect, LOW); //load parallel inputs to the register return readState; }