本帖最后由 yumiao 于 2016-11-2 10:02 编辑
问题:
程序运行时一直停在 while(SPI_GetFlagStatus(SPI2,SPI_FLAG_RXNE)==RESET) 不知道是什么问题, 请指教。感激不尽 。
IO连接:
PA5 ------------------> PB13
PA6 ------------------> PB14
PA7 ------------------> PB15
代码:
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "system_stm32f4xx.h"
/***************************************************************************/
void RCC_Configuration()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
}
void SPI_Configuration()
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_DeInit(GPIOA);
GPIO_DeInit(GPIOB);
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_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_6|GPIO_Pin_5;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_15|GPIO_Pin_14|GPIO_Pin_13;
GPIO_Init(GPIOB,&GPIO_InitStructure);
/* SPI configuration -------------------------------------------------------*/
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2,ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2,DISABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_Cmd(SPI1, DISABLE);
SPI_DeInit(SPI1);
SPI_Cmd(SPI2, DISABLE);
SPI_DeInit(SPI2);
SPI_Init(SPI1, &SPI_InitStructure);
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
SPI_Cmd(SPI2,ENABLE);
}
/***************************************************************************/
void Delay(int nCount)
{
int c1=nCount;
int c2=nCount;
for( ; c1>0; c1--)
{
for( ;c2>0; c2--);
};
}
/***************************************************************************/
int main(void)
{
RCC_Configuration();
SPI_Configuration();
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);
STM_EVAL_LEDOn(LED3);
STM_EVAL_LEDOn(LED4);
while(1)
{
uint16_t data=0;
SPI_SendData(SPI1,0x55);
while(SPI_GetFlagStatus(SPI2,SPI_FLAG_RXNE)==RESET)
{
STM_EVAL_LEDOn(LED4);
Delay(0xfffff);
STM_EVAL_LEDOff(LED4);
Delay(0xfffff);
}
STM_EVAL_LEDOn(LED4);
data=SPI_ReceiveData(SPI2);
STM_EVAL_LEDOff(LED3);
if(data==0x55)
{
while(1)
{
};
}
else while(1)
{
};
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
你是SPI2做从机,对吧?
没有收到数据,几个原因:
主机没有发送数据,
从机没有打开接收功能,
硬件接线有问题。
至少要使能吧?
一周热门 更多>