SPI在STM32F103ZE中将数据读取为零

Har*_*nan 1 c embedded spi stm32

我正在使用STM32F103ZE我没有正确获取SPI数据.师父正确传播.但是,在发送非零值的情况下,始终读为零.

主配置:(MSP430)

The master configuration is correct. (I tested it.)
Master Mode, MSB First, 8-bit SPI, 
Inactive state is high, SS grounded, 1 MHz clock, no dividers

Slave Config(STM32F103ZE)

    Using SPI2.
    SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx
    SPI_InitStructure.SPI_Mode = SPI_Mode_Slave
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB
    SPI_InitStructure.SPI_CRCPolynomial = 7

任何人都有答案,

谢谢哈里

Meh*_*olf 5

我知道,问题已经很久了.尽管如此,由于我在最后几天遇到了同样的问题,我将尽力为未来的读者提供答案.

以下代码适用于STM32F407,STM32F407用于STM32发现板.从数据表中我可以看到,SPI外设与STM32F103相同,所以我希望代码可以在其他微控制器上运行而无需修改.

#include "stm32f4xx.h"

[... configure the pins SCK, MISO, MOSI and NSS ...]

// Configure the SPI as: Slave, 8 bit data, clock high when idle, capture on 
// 1st edge, baud rate prescaler 2, MSB first.
SPI1->CR1 = SPI_CR1_CPOL;
// No interrupts, no DMA and Motorola frame format.
SPI1->CR2 = 0;
// Enable the peripheral.
SPI1->CR1 |= SPI_CR1_SPE;

// Wait until some data has been sent to the slave and print it.
while ((SPI1->SR & SPI_SR_RXNE) == 0);
printf("Received: %d\n", SPI1->DR);
Run Code Online (Sandbox Code Playgroud)

在此初始化过程中,与问题中发布的代码有两点不同:

  1. 不要使用3行SCK,MISO和MOSI为普通SPI选择双向模式.MISO和MOSI都是单向线.

  2. 我使用硬件从属选择管理,即SSM未设置该位.这样,SPI外设可以自动检测器件何时被置位(引脚NSS变为低电平)并将MOSI位存储在移位寄存器中.当读取了足够的位(8或16,具体取决于所选的数据格式)时,标志RXNE位于状态寄存器中,并且可以从寄存器中读取发送的值DR.

希望有所帮助.

  • 请注意,STM32F1和STM32F4系列之间的外设接口语义完全不同 - 外设可能相同,但它连接到处理器的方式**以及为它提供时钟并将其连接到I/O引脚所需的步骤**明显不同. (2认同)