2024-06-28 06:23 PM
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.
2024-06-29 05:35 AM - edited 2024-07-02 06:45 AM
I don't know off the top off my head what the 74HC594 and 74AC165D chips do. So you're asking me to look up two datasheets, only then to have to read your schematic and then your code, and then read your post several times to get over the language barrier and understand what you're asking. I'm sorry, you're making it too hard to help you.
2024-07-02 06:23 AM - edited 2024-07-02 06:50 AM
Which processor? Can you rewrite using hal.functions for easy understanding
2024-07-02 08:57 AM
1.When PL is low, the input at A to H is stored and then shifted out... you seems to be making GPIO->E1 high and then low.
2. Since you are reading only eight inputs, you can go for 8 bit data mode.
3. 74HC165 seems to be outputting D7 to D0, make sure you do the bit masking correctly.
4. Check the timing of E1 high low transition to meet the required minimum duration, else insert delay.
5.Check if your CPOL and CPHA is programmed correctly based on the timing diagram of 165.