本文介绍了STM32 SPI慢速计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用STM32F4及其SPI与本教程中的74HC595进行通信。区别在于初学者,为简单起见,我使用的是非DMA版本。我使用STMCubeMX来配置SPI和GPIO

I'm using a STM32F4 and its SPI to talk to a 74HC595 like in this tutorial. Difference is for starters I'm using the non-DMA version for simplicity. I used STMCubeMX to configure SPI and GPIO

问题是:我没有得到锁存PIN,我将其设置为PA8以便在传输过程中足够快地切换。

Issue is: I'm not getting the latch PIN, that I set to PA8 to toggle during transmission fast enough.

我正在使用的代码:

        spiTxBuf[0] = 0b00000010;

        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);


        HAL_SPI_Transmit(&hspi1, spiTxBuf, 1, HAL_MAX_DELAY);
//        while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);

        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);

        HAL_Delay(1);

我尝试过的事情:


  1. 将引脚PA8的最大输出速度设置为非常快

  1. Set the Maxium Output Speed of the Pin PA8 to Very Fast

等待完成SPI(请参见上面的注释行)

Wait for the SPI to be done (see commented line above)

我该如何快速切换?我应该在SPI完成后创建并中断并在那里设置锁存器吗?

How do i get that to toggle faster? Should i create and interrupt for when the SPI is done and set the latch there?

推荐答案

如果可能,请使用硬件NSS引脚

有些 STM32控制器可以自动切换其 NSS 引脚,并在传输后具有可配置的延迟。请查阅参考手册,如果您是其中的一员,请将移位器的锁存器引脚重新连接到MCU上的 SPIx_NSS 引脚。

Some STM32 controllers can toggle their NSS pin automatically, with a configurable delay after transmission. Check the Reference Manual, if yours is one of these, reconnect the latch pin of the shifter to the SPIx_NSS pin on the MCU.

不要使用HAL

HAL的运行速度非常慢,并且对于任何时间要求严格的应用来说都过于复杂。不要使用它。

HAL is quite slow and overcomplicated for anything with tight timing requirements. Don't use it.

只需执行参考手册中的SPI发送过程即可。

Just implement the SPI transmit procedure in the Reference Manual.

SPI->CR1 |= SPI_CR1_SPE; // this is required only once
GPIOA->BSRR = 1 << (8 + 16);
*(volatile uint8_t *)&SPI->DR = 0b00000010;
while((SPI->SR & (SPI_SR_TXE | SPI_SR_BSY)) != SPI_SR_TXE)
    ;
GPIOA->BSRR = 1 << 8;

这篇关于STM32 SPI慢速计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 17:25
查看更多