请问stm32F030C8T6 SPI从机通讯收到全0是为什么?

2019-07-14 16:48发布

各路英雄好,我全用STM32f030c8t6不久,现需要它只接收ARM9的spi数据即可,arm9这边的是用了很多年的系统,使用spi   400Khz   CPOL=0, CPHA=0的方式往外发近100字节的连续数据。使用逻辑分析仪抓的波形比较好,但现在我使用c8t6的SPI2做从机, 中断中收到后打印的全是0, 希望有经验的GGJJ指点一下,在此谢过了!!  如下为初始化代码,是参考的官方例程STM32F0xx_StdPeriph_Lib_V1.3.1ProjectsSTM32F0xx_StdPeriph_ExamplesSPISPI_TwoBoardsDataExchangeInterrupt这里面的代码,而且我再三确认过ARM9 CS--> 030 CS  ARM9 MOSI---> 030 MOSI  ARM9 sck ---> 030 SCK


static void SPI2_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the SPI periph */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

  /* Enable SCK, MOSI, MISO and NSS GPIO clocks */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_0);   
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_0);  
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_0);
  //GPIO_PinAFConfig(RCC_AHBPeriph_GPIOA, SPIx_MISO_SOURCE, SPIx_MISO_AF);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;   
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  /* SPI  CS pin configuration */   
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* SPI SCK pin configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* SPI  MOSI pin configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* SPI  MISO ---> A0 Input GPIO configuration */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
  RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);

  /* SPI configuration -------------------------------------------------------*/
  SPI_I2S_DeInit(SPI2);
  //SPI_Cmd(SPI2, DISABLE);

  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
  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_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI2, &SPI_InitStructure);

  /* Configure the SPI interrupt priority */
  NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Initialize the FIFO threshold */
  SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_QF);

  /* Enable the Rx buffer not empty interrupt */
  SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);

  /* Enable the SPI Error interrupt */
  SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_ERR, ENABLE);

  /* Enable the SPI peripheral */
  SPI_Cmd(SPI2, ENABLE);
}



void SPI2_IRQHandler(void)
{
//#if defined (SPI_SLAVE)  
#if 1   
#if 0  
  /* SPI in Slave Tramitter mode--------------------------------------- */
  if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_TXE) == SET)
  {
    SPI_SendData8(SPI2, TxBuffer[Tx_Idx++]);
    if (Tx_Idx == GetVar_NbrOfData())
    {
      /* Disable the Tx buffer empty interrupt */
      SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, DISABLE);
    }
  }
#endif  
  /* SPI in Slave Receiver mode--------------------------------------- */
  while(SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET)
  {
      RxChr = SPI_ReceiveData8(SPI2);
      printf("%02x ", RxChr);
      RxBuffer[Rx_Idx++] = RxChr;
      if(Rx_Idx==64)
          Rx_Idx = 0;
  }
#if 1  
  /* SPI Error interrupt--------------------------------------- */
  if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_OVR) == SET)
  {
    SPI_ReceiveData8(SPI2);
    SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_OVR);
    printf("OV ");   
  }
#endif  
#if 1  
  /* SPI Error interrupt--------------------------------------- */
  if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_FRE) == SET)
  {
    SPI_ReceiveData8(SPI2);
    SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_FRE);
    printf("FE ");  
  }
#endif   
#endif /* SPI_SLAVE*/




在线等待啊,
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
8条回答
TOPCB
1楼-- · 2019-07-14 20:57
在中断中加断点,看一下RxChr的值是多少。
60user56
2楼-- · 2019-07-15 01:16
啊,大侠们都去哪里了,过来瞧瞧啊
60user56
3楼-- · 2019-07-15 04:25
嗯,查看过了 RxChr还真是0,本来是直接读到数组里面的,全0,读到字节变量里面,还是有问题
60user56
4楼-- · 2019-07-15 05:05
 精彩回答 2  元偷偷看……
60user56
5楼-- · 2019-07-15 06:08
再发2张寄存器的图:
jerry181855
6楼-- · 2019-07-15 08:35
自发自收试一下

一周热门 更多>