SPI RX with 74HC165
it's been week since I stuck on this problem, I have a board like this
, SPI3 MOSI PB5 is connected to ic 74HC594s to drive LEDs, and MISO PB4 is connected to 74AC165D to get the four SW (buttons) for input purpose, and the PB3 for clock on both ICs. I have succeed in using SPI to output numbers on LEDs via 74HC594 so I known that the SPI was up and running correctly. Now with the 74HC165, the basic idea is similar to the 594, start the SPI, wait for it to complete, than get the data. Heres my code:
void SPI3_Setup()
{
SPI3->CR1 |= (0x1<<2);//MSTR = 1
SPI3->CR1 |= (0x1<<3);
SPI3->CR1 |= (0x1<<4);
SPI3->CR1 |= (0x1<<5);
SPI3->CR1 |= (0x1<<7);//LSB first
SPI3->CR1 |= (0x1<<11);//16bit data load
//Baudrate /256
SPI3->CR2 |= (0x1<<2);
SPI3->CR1 |= (0x1<<6);
}
void SPI3_send(uint16_t data)
{
uint16_t button_read;
GPIOE->ODR |= (0x1<<1);//switch this pin on off to latch
GPIOE->ODR &= ~(0x1<<1);
GPIOA->ODR &= ~(0x1<<15);//init spi
SPI3->DR = data;
while(SPI3->SR & (0x1<<7))
{
//wait for not busy
}
while((SPI3->SR & (0x1<<0)))
{
//wait for RXNE
button_read = SPI3->DR;
}
button_read_1 = button_read & (0x1<<0);get SW1 state
button_read_2 = button_read & (0x1<<5);get SW2 state
GPIOA->ODR |= (0x1<<15);
GPIOE->ODR |= (0x1<<0);
GPIOE->ODR &= ~(0x1<<0);
}For some reason, SW2 button which is store in button_read_2 couldn't be detected, while when I press SW1 button it change both button_read_1 and button_read_2 and cause both of them to true.
