I haven't check the forum so much today, been busy with the new Design Tool and House maintaining
I think you can use the current 4017 for the button matrix as well because you have used diodes they will prevent
false readings.
and you can combine the LD/SHIFT + OUT_CLK and separate out the SERIAL_DATA_OUT from the added 74HC165
now in the last minute I did some calculations on the pulldown resistors they should be minimum 33k each,
because at worst case scenario all 20+8 bars + buttons will be connected to one row and that parallel connects all resistors to GND
which will be 33k/28 =1.1k => 3mA @ 3v3
also you should use the faster and "newer" model of the 4017 => 74HC4017E (74HC4017E datasheet specifies the timings down to 2V)
new schematic (explains much better):

code for that:
Code:
#define NOP3 "nop\n\t""nop\n\t""nop\n\t"
#define NOP4 "nop\n\t""nop\n\t""nop\n\t""nop\n\t"
#define NOP5 "nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t"
#define NOP6 "nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t"
#define NOP15 NOP4 NOP6 NOP5
#define NOP30 NOP10 NOP10 NOP10
// @ F_CPU @ 600Mhz 60NOP = 100nS
// 90NOP = 150nS
#define P150ns __asm__(NOP30 NOP30 NOP30)
#define P300ns __asm__(NOP30 NOP30 NOP30 NOP30 NOP30 NOP30)
// IO defines (pin-numbers is just an example)
#define IN_RESET 0
#define IN_CLK 1
#define OUT_CLK 2
#define OUT_LOAD_SHIFT 3
#define SERIAL_DATA_OUT 4
#define SERIAL_DATA2_OUT 5
#define BARCOUNT 20
#define BARVALUECOUNT 9
#define MATRIXROWCOUNT 4
byte bars[BARCOUNT];
byte btnMatrix[MATRIXROWCOUNT];
byte btnMatrixCurrent;
void setup()
{
pinMode(IN_RESET, OUTPUT);
pinMode(IN_CLK, OUTPUT);
pinMode(OUT_CLK, OUTPUT);
pinMode(OUT_LOAD_SHIFT, OUTPUT);
pinMode(SERIAL_DATA_OUT, INPUT);
// inital states
digitalWrite(IN_RESET, LOW);
digitalWrite(IN_CLK, LOW);
digitalWrite(OUT_CLK, LOW);
digitalWrite(OUT_LOAD_SHIFT, HIGH); // normal state
}
void loop()
{
doBarsRead();
if (bars[0] == 1) // first drawbar equals one
{
// do something
}
else if (bars[0] == 2) // first drawbar equals two
{
// do something
}
// ...
if (bars[19] == 1) // last drawbar equals one
{
// do something
}
else if (bars[19] == 2) // last drawbar equals two
{
// do something
}
}
void doBarsRead()
{
digitalWrite(IN_RESET, HIGH);
P300ns; // min time @ 2V = 230nS using fast common model 74HC4017E
digitalWrite(IN_RESET, LOW);
P300ns; // extra delay to be on the safe side
int btnMatrixIndex = 0;
for (int ri = 1; ri <= BARVALUECOUNT; ri++)
{
// LATCH data
digitalWrite(OUT_LOAD_SHIFT, LOW);
P150ns; // max @ 2v 74HC165 (it must be HC model because it have VCC 2-6V) HCT is 4.5-5.5v only
digitalWrite(OUT_LOAD_SHIFT, HIGH);
P150ns; // extra delay to be on the safe side
btnMatrixCurrent = 0;
for (int ci = 0; ci < BARCOUNT; ci++)
{
if (digitalRead(SERIAL_DATA_OUT) == HIGH)
bars[ci] = ri;
if (ci < 8)
{
if (digitalRead(SERIAL_DATA2_OUT) == HIGH)
btnMatrixCurrent |= 0x01;
btnMatrixCurrent = btnMatrixCurrent << 1;
}
// next col clock input
digitalWrite(OUT_CLK, HIGH);
P150ns;
digitalWrite(OUT_CLK, LOW);
P150ns; // extra delay to be on the safe side
btnMatrix
}
if (btnMatrixIndex < MATRIXROWCOUNT)
btnMatrix[btnMatrixIndex++] = btnMatrixCurrent;
// select next row
digitalWrite(IN_CLK, HIGH);
P300ns; // min time @ 2V = 230nS using fast common model 74HC4017E
digitalWrite(IN_CLK, LOW);
P300ns; // extra delay to be on the safe side
}
// some calculations of the different delays in nS give
// 300*2 + (150*2 + 300*2 + 150*20)*9 = 35.7uS => ~28011 in refresh-rate
// now each bars[i] have the selected value
}
if you reverse the button matrix diodes
you can do this:

and the little different code: (note that I shift each btnMatrix[ci] for each in bit read, this is very efficient)
Code:
#define NOP3 "nop\n\t""nop\n\t""nop\n\t"
#define NOP4 "nop\n\t""nop\n\t""nop\n\t""nop\n\t"
#define NOP5 "nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t"
#define NOP6 "nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t"
#define NOP15 NOP4 NOP6 NOP5
#define NOP30 NOP10 NOP10 NOP10
// @ F_CPU @ 600Mhz 60NOP = 100nS
// 90NOP = 150nS
#define P150ns __asm__(NOP30 NOP30 NOP30)
#define P300ns __asm__(NOP30 NOP30 NOP30 NOP30 NOP30 NOP30)
// IO defines (pin-numbers is just an example)
#define IN_RESET 0
#define IN_CLK 1
#define OUT_CLK 2
#define OUT_LOAD_SHIFT 3
#define SERIAL_DATA_OUT 4
#define SERIAL_DATA2_OUT 5
#define BARCOUNT 20
#define BARVALUECOUNT 9
#define MATRIXROWCOUNT 8
#define MATRIXCOLCOUNT 4
byte bars[BARCOUNT];
byte btnMatrix[MATRIXCOLCOUNT];
void setup()
{
pinMode(IN_RESET, OUTPUT);
pinMode(IN_CLK, OUTPUT);
pinMode(OUT_CLK, OUTPUT);
pinMode(OUT_LOAD_SHIFT, OUTPUT);
pinMode(SERIAL_DATA_OUT, INPUT);
// inital states
digitalWrite(IN_RESET, LOW);
digitalWrite(IN_CLK, LOW);
digitalWrite(OUT_CLK, LOW);
digitalWrite(OUT_LOAD_SHIFT, HIGH); // normal state
}
void loop()
{
doBarsRead();
if (bars[0] == 1) // first drawbar equals one
{
// do something
}
else if (bars[0] == 2) // first drawbar equals two
{
// do something
}
// ...
if (bars[19] == 1) // last drawbar equals one
{
// do something
}
else if (bars[19] == 2) // last drawbar equals two
{
// do something
}
}
void doBarsRead()
{
digitalWrite(IN_RESET, HIGH);
P300ns; // min time @ 2V = 230nS using fast common model 74HC4017E
digitalWrite(IN_RESET, LOW);
P300ns; // extra delay to be on the safe side
int btnMatrixIndex = 0;
for (int ri = 1; ri <= BARVALUECOUNT; ri++)
{
// LATCH data
digitalWrite(OUT_LOAD_SHIFT, LOW);
P150ns; // max @ 2v 74HC165 (it must be HC model because it have VCC 2-6V) HCT is 4.5-5.5v only
digitalWrite(OUT_LOAD_SHIFT, HIGH);
P150ns; // extra delay to be on the safe side
// first shift in the 20 PULLBARS
for (int ci = 0; ci < BARCOUNT; ci++)
{
if (digitalRead(SERIAL_DATA_OUT) == HIGH)
bars[ci] = ri;
}
// then shift in the button matrix only the first 8 lines
if (btnMatrixIndex < MATRIXROWCOUNT)
{
for (int ci = 0; ci < MATRIXCOLCOUNT; ci++)
{
if (digitalRead(SERIAL_DATA2_OUT) == HIGH)
btnMatrix[ci] |= 0x01;
btnMatrix[ci] = btnMatrix[ci] << 1; // visualize this like the matrix is rotated 90 degrees
// next col clock input
digitalWrite(OUT_CLK, HIGH);
P150ns;
digitalWrite(OUT_CLK, LOW);
P150ns; // extra delay to be on the safe side
}
btnMatrixIndex++;
}
// select next row
digitalWrite(IN_CLK, HIGH);
P300ns; // min time @ 2V = 230nS using fast common model 74HC4017E
digitalWrite(IN_CLK, LOW);
P300ns; // extra delay to be on the safe side
}
// some calculations of the different delays in nS give
// 300*2 + (150*2 + 300*2 + 150*20)*9 = 35.7uS => ~28011 in refresh-rate
// now each bars[i] have the selected value
}