STM32F0x8 SPI与25LC256接收问题[外设库]

STM32F0x8 SPI with 25LC256 recieve problem [Peripheral Lib]

我使用的是STM32F0探索板。 我正在尝试对 EEPROM 进行编程以保存 QR 码并将其显示在 LCD 上。 现在我正在努力让 STM32 从 EEPROM 接收数据,如果我缩短 MISO 和 MOSI(我改变了一点但不多),我的代码就可以工作,但是当我试图让它与 EEPROM 一起工作时,没用。

下面是我的代码。头文件:

#include "stm32f0xx_conf.h"
#include <string.h>

//Pin definitions
//#define NSSA GPIO_PinSource12
#define SCKA GPIO_PinSource13
#define MISOA GPIO_PinSource14
#define MOSIA GPIO_PinSource15
#define TXPINA GPIO_PinSource9
//#define NSS GPIO_Pin_12
#define CS GPIO_Pin_12
#define SCK GPIO_Pin_13
#define MISO GPIO_Pin_14
#define MOSI GPIO_Pin_15
#define TXPIN GPIO_Pin_9
#define HOLD GPIO_Pin_11
#define GreenLED_Pin GPIO_Pin_9
#define BlueLED_Pin GPIO_Pin_8

//EEPROM INSTRUCTIONS:
uint8_t READMEM  = 0x003;    //READ  -   0000 0011     Read data from memory array beginning at selected address
uint8_t WRITEMEM = 0x002;    //WRITE -   0000 0010     Write data to memory array beginning at selected address
uint8_t READSR   = 0x005;    //RDSR  -   0000 0101     Read STATUS register
uint8_t WRITESR  = 0x001;    //WRSR  -   0000 0001     Write STATUS register
uint8_t WRITEDI  = 0x004;    //WRDI  -   0000 0100     Reset the write enable latch (disable write operations)
uint8_t WRITEEN  = 0x006;    //WREN  -   0000 0110     Set the write enable latch (enable write operations)
uint8_t NULLBYTE = 0x000;

SPI初始化:

void SPI_EEPROM()

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOB,SCKA,GPIO_AF_0);
GPIO_PinAFConfig(GPIOB,MISOA,GPIO_AF_0);
GPIO_PinAFConfig(GPIOB,MOSIA,GPIO_AF_0);

GPIO_InitTypeDef SPIGPIO;
SPIGPIO.GPIO_Mode = GPIO_Mode_AF;
SPIGPIO.GPIO_OType = GPIO_OType_PP;
SPIGPIO.GPIO_PuPd = GPIO_PuPd_NOPULL;
SPIGPIO.GPIO_Speed = GPIO_Speed_50MHz;

SPIGPIO.GPIO_Pin = SCK;
GPIO_Init(GPIOB, &SPIGPIO);
SPIGPIO.GPIO_Pin = MISO;
GPIO_Init(GPIOB, &SPIGPIO);
SPIGPIO.GPIO_Pin = MOSI;
GPIO_Init(GPIOB, &SPIGPIO);
SPI_I2S_DeInit(SPI2);
SPI_InitTypeDef SPIEPR;
SPIEPR.SPI_Mode = SPI_Mode_Master;
SPIEPR.SPI_CPHA = SPI_CPHA_1Edge;
SPIEPR.SPI_CPOL = SPI_CPOL_Low;
SPIEPR.SPI_FirstBit = SPI_FirstBit_MSB;
SPIEPR.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; //-> _16~3.2MHZ CLOCK | _32 ~1.6MHZ CLOCK
SPIEPR.SPI_NSS = SPI_NSS_Soft;
SPIEPR.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPIEPR.SPI_DataSize = SPI_DataSize_8b;
SPI_Init(SPI2, &SPIEPR);
SPI_NSSInternalSoftwareConfig(SPI2,SPI_NSSInternalSoft_Set);
//SPI2 -> CR2 |= SPI_CR2_FRXTH;
SPI_Cmd(SPI2,ENABLE);

GPIO初始化:

void GPIO()

GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = BlueLED_Pin|GreenLED_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_InitTypeDef GPIO2_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

GPIO2_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO2_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO2_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO2_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO2_InitStructure.GPIO_Pin = HOLD;
GPIO_Init(GPIOB, &GPIO2_InitStructure);
GPIO2_InitStructure.GPIO_Pin = CS;
GPIO_Init(GPIOB, &GPIO2_InitStructure);

简单的收发函数:

uint8_t SPISendByte(uint8_t data)

uint8_t databck;
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) != RESET);
SPI_SendData8(SPI2,data);                                                      
while(SPI_GetReceptionFIFOStatus(SPI2) != 0x0000)
  SPI_ReceiveData8(SPI2); //read RXFIFO until empty

while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); //wait until TXFIFO is empty
SPI_SendData8(SPI2,NULLBYTE); //send 0-byte to recive answer from EEPROM
while(SPI_GetReceptionFIFOStatus(SPI2) != 0x0200); //wait until the RXFIFO is a quarter full (8bit)
return SPI_ReceiveData8(SPI2);

主要功能:

SystemInit();
SPI_EEPROM();
GPIO();
    GPIO_ResetBits(GPIOB, CS); //CS low to enable the EEPROM
SPI_SendData8(SPI2,WRITEEN); //send the WREN instruction to set the write enable latch
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) != RESET);
GPIO_SetBits(GPIOB, CS); //Toggle on and off to set bit in the Status register
GPIO_ResetBits(GPIOB, CS);
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) != RESET);
USART_SendData(USART1, SPISendByte(READSR)); //Read Status Reg and print it to USART (should be ..0010)

我按照其数据sheet 告诉我的方式设置了 EEPROM。来自 sheet 的时钟设置(应为 CPOL 和 CPHA 0):

Instructions, addresses or data present on the SI pin are latched on the rising edge of the clock input, while data on the SO pin is updated after the falling edge of the clock input.

这不是我的全部代码,而是我用于测试的代码。 它应该工作;我用我的示波器仔细检查了它(它 returns ..0010 在具有逻辑模式的 MISO 上)并用调试器查看 RXFIFO 是否为空并接收实际数据: 它确实如此,但代码总是 returns 0。 我也尝试了很多不同的标志而不是读取 RXFIFO,但这似乎是最好的解决方案。

所以,在我的 table 和我的同事偷了我的示波器后,它起作用了,所以我发现示波器通道拉低了我的信号,因为我的输入处于 50 欧姆模式并且不是 1MOhm。切换修复它。