Embedded development--STM32 hardware SPI driver 74HC595

This article is the hardware SPI port driver 74HC595.
IO port analog timing software driver 74HC595 see this article: Embedded Development – ​​IO Port Expansion – 74HC595

The hardware connection is as follows:

Only one 74HC595 is used. If multiple 74HC595s need to be cascaded, refer to the previous link. There is a circuit inside, and multiple 74HC595s can be connected without additional signal lines.
insert image description here

The settings in CubeMX are as follows:

SPI1 is used. When driving 74HC595, the host does not need input, so MISO is not used.
insert image description hereinsert image description here
insert image description here
Special attention, the default data width is 4bits, which needs to be changed to 8bits.
The highest speed of 74HC595 can reach 50MHz, so the baud rate here can be set as much as possible. Higher, 10MHz is over.
Others are conventional settings, don’t worry, if you need a tutorial, read this article: Embedded Development – ​​CubeMX Getting Started Tutorial
After setting, generate the project.

the code

The programming in Keil is very simple, in fact, it is to call a function HAL_SPI_Transmit(&hspi1, &temp, 1, 1000).
The following code is a demo of the water light

  while(1)
  {
    
    
    for(i=0; i<8; i++)
    {
    
    
      temp = 1<<i;
      HAL_SPI_Transmit(&hspi1, &temp, 1, 1000);
      HAL_Delay(300);
    }
  }

transmission time

insert image description hereIt takes about 9.5us from the beginning of the high-level preparation to the final sending completion time of 11.36us, which takes less than 2us. If the software configures the IO port to simulate the drive, the time will far exceed this time. It can be seen that the hardware The efficiency is still much higher.

Guess you like

Origin blog.csdn.net/13011803189/article/details/128096477