I'm trying to daisy-chain two shift register using a STM32F407 discovery kit, but I can't seem to get it right. I use the following function to send a byte to the shift register:
void selector_SendByte(uint8_t byte)
{
// Define timeout (ms)
uint16_t clock_timeout = 10000;
uint16_t latch_timeout = 1000;
for (int8_t i = 7; i >= 0; --i)
{
uint8_t bit = byte & (0x1 << i); // Read bit
HAL_GPIO_WritePin(SL_SD_GPIO_Port, SL_SD_Pin, bit);
// Toggle clock
HAL_GPIO_WritePin(SL_SP_GPIO_Port, SL_SP_Pin, 1);
while (clock_timeout--);
HAL_GPIO_WritePin(SL_SP_GPIO_Port, SL_SP_Pin, 0);
}
// Toggle latch
HAL_GPIO_WritePin(SL_SS_GPIO_Port, SL_SS_Pin, 1);
while (latch_timeout--);
HAL_GPIO_WritePin(SL_SS_GPIO_Port, SL_SS_Pin, 0);
}
Where SL_SD is the data, SL_SP is SRCLK, and SL_SS is RCLK.
I defined the following outputs:
#define Q0 0b0000000000000001
#define Q1 0b0000000000000010
#define Q2 0b0000000000000100
#define Q3 0b0000000000001000
#define Q4 0b0000000000010000
#define Q5 0b0000000000100000
#define Q6 0b0000000001000000
#define Q7 0b0000000010000000
// shift register 2
#define Q8 (Q0 << 8)
#define Q9 (Q1 << 8)
#define Q10 (Q2 << 8)
#define Q11 (Q3 << 8)
#define Q12 (Q4 << 8)
#define Q13 (Q5 << 8)
#define Q14 (Q6 << 8)
#define Q15 (Q7 << 8)
When calling selector_SendByte(Q0)
and then selector_SendByte(Q8)
it doesn't do what I want.
I connected a pull-up resistor of 10 kΩ to each output, and connected LEDs with 440 Ω resistors to each output. For the first shift register I can turn on the LEDs correctly, but the second shift register is not doing what I expected.
Any help on what I am missing?
doesn't do what I want
is not a useful description. What behaviour do you observe, and what do you require? \$\endgroup\$// Define timeout (ms)
I don't think one trip through one of the tight delay loops will take 1 ms. Bug: asclock_timeout
is used for every bit in turn, but set to a value purposefully just once, every delay but the first will count down from the maximal valueclock_timeout
can take on. I don't see a reason to have more than 1 μs between edges, and there is no explicit delay betweenHAL_GPIO_WritePin(SL_SP_GPIO_Port, SL_SP_Pin, 0)
andHAL_GPIO_WritePin(SL_SS_GPIO_Port, SL_SS_Pin, 1);
. \$\endgroup\$