调试stm32f407开发板spi通讯问题

2019-07-20 05:30发布

小弟在调试stm32f407开发板spi通讯,想实现功能spi1做主机,spi2做从机,主机发送数据从机接收数据的功能。现在主机能发送数据从机接收不到数据,spi2始终进入不了中断,请各位大神指点迷津。谢谢。配置代码如下  接线方式PA4接PB12  PA5接PB13  PA7接PB15
void SPI1_Init(void)
{  
  GPIO_InitTypeDef  GPIO_InitStructure;
  SPI_InitTypeDef  SPI_InitStructure;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能时钟GPIOA
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);//使能spia

  //GPIOA4,5,6,7
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_4;//PA4~7复用功能输出
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上啦
  GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化

GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1); //PA5¸复用spi
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1); //PA6
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1); //PA7
GPIO_PinAFConfig(GPIOA,GPIO_PinSource4,GPIO_AF_SPI1);//PA4

//ÕâÀïÖ»Õë¶ÔSPI¿Ú³õʼ»¯
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;  //全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;  //主模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;  //8位
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;  //同步时钟为高电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //第二个跳变沿被采样
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;  //软件控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;  //分频值256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);

SPI_Cmd(SPI1, ENABLE); //  

}   

void SPI2_Init(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  SPI_InitTypeDef  SPI_InitStructure;
  NVIC_InitTypeDef   NVIC_InitStructure;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//??GPIOA??
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); //??SPI1??

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;      
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//复用推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  //GPIOFB3,4,5?????
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;//PB3~5??????
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//复用推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//??
  GPIO_Init(GPIOB, &GPIO_InitStructure);//???
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_SPI2); //PB13复用 SPI2
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_SPI2); //PB14复用 SPI2
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_SPI2); //PB15复用 SPI2
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_SPI2);//PB12复用SPI2

  RCC_APB2PeriphResetCmd(RCC_APB1Periph_SPI2,ENABLE);//??SPI1
  RCC_APB2PeriphResetCmd(RCC_APB1Periph_SPI2,DISABLE);//????SPI1
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;  //??SPI???????????:SPI??????????
  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; //??SPI????:????SPI
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //??SPI?????:SPI????8????
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //???????????????
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //?????????????(?????)?????
  SPI_InitStructure.SPI_NSS = SPI_NSS_Hard  ; //NSS?????(NSS??)????(??SSI?)??:??NSS???SSI???
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;  ; //??????????:????????256
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //???????MSB???LSB???:?????MSB???
  SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC???????
  SPI_Init(SPI2, &SPI_InitStructure);  //??SPI_InitStruct???????????SPIx???

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //ʹÄÜÖжϽÓÊÕ£¬ÉèÖÃNVIC£¬ÇÀÕ¼ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

SPI_I2S_ITConfig(SPI2,SPI_I2S_IT_RXNE,ENABLE);

  SPI_Cmd(SPI2, ENABLE); //??SPI??
}
u8 dataSpi=0;
void SPI2_IRQHandle(void)
{
  u16 temp=0;
// LED1_OFF;
if(SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET)
{
  SPI_I2S_ClearITPendingBit( SPI2 , SPI_I2S_IT_RXNE ) ;
  temp=SPI_I2S_ReceiveData(SPI2);
  dataSpi = temp;
  SPI_I2S_ClearITPendingBit(SPI2, SPI_I2S_IT_RXNE);
}
}
//主函数
int main(void)
{
Delay_Init();
  Usart_Config();
LED_Init();
SPI1_Init();        //³õʼ»¯SPI
  SPI1_SetSpeed(SPI_BaudRatePrescaler_256);  //ÉèÖÃΪ21MʱÖÓ,¸ßËÙģʽ
SPI2_Init();
  
while(1)
{  
  SPI1_WriteByte(0xaa);
  if(dataSpi)
  {
     USART_SendData1(USART1,dataSpi);
      dataSpi=0;
//  
  }   
//  
}
}











友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。