English 中文(简体)
SPI在STM32F103ZE读数据为零
原标题:SPI is reading data as zero in STM32F103ZE

I am using STM32F103ZE I am not getting SPI data correctly. Master is transmitting correctly. But always read as zero where a non zero value has been sent.

主编: (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

Anybody have an ANSWER,

Thanks Hari

最佳回答

我知道,这个问题很老。 然而,由于我过去几天也面临同样的问题,我试图为未来的读者提供答案。

The following code works on the STM32F407, which is used on the STM32 discovery board. What I can see from the datasheet, the SPI peripheral is the same as on the STM32F103, so I expect the code to run on the other microcontroller without modifications.

#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
", SPI1->DR);

Two things are different in this initialization procedure from the code posted in the question:

  1. Do not select bidirectional mode for ordinary SPI with the 3 lines SCK, MISO and MOSI. Both MISO and MOSI are unidirectional lines.

  2. I use hardware slave select management, i.e. the bit SSM is not set. This way, the SPI peripheral can automatically detect when the device has been asserted (the pin NSS goes low) and will store the MOSI bits in a shift register. When enough bits have been read (8 or 16 depending on the selected data format), the flag RXNE is set in the status register and the transmitted value can be read from the register DR.

希望会有所帮助。

问题回答

我从数据登记册中获取0x00数值的问题完全相同。

就我而言,问题是,MISO线被定为浮动投入。 该公司在将其改为“OType_PP”。 我的STM32F429组合代码如下:

void SPI1_Config(void){
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIOE->BSRRL |= GPIO_Pin_7;

SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStruct);

SPI_Cmd(SPI1, ENABLE);}

And sending function:

uint8_t SPI1_Send(uint8_t data){
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

SPI_I2S_SendData(SPI1, data);

while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

return SPI_I2S_ReceiveData(SPI1);}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签