当前位置: 代码迷 >> 单片机 >> STM32 SPI接收数据有关问题
  详细解决方案

STM32 SPI接收数据有关问题

热度:82   发布时间:2016-04-28 16:07:05.0
STM32 SPI接收数据问题
STM32和ADS8328一块AD转换芯片进行通讯,使用SPI。问题是,接收数据时RXNE始终为reset状态,部分程序如下,望各位高人指点,急用,多谢
void ADS8328_SPI_Init(void)
{
  SPI_InitTypeDef SPI_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
   
  /* Enable SPI1 clocks */
  RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 ,ENABLE);
  
  /* Configure SPI1 pins: SCK, MISO and MOSI */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; //SCK MISO MOSI
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure PA4 as Output push-pull, used as select */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //片选
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  /* Configure PA9 */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //采样启动信号CONVEST
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  /* Configure PA8 */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //采样结束标志信号
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  

  /* SPI1 configuration */ 
  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_1Edge; //数据在第一个边沿采样
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS为软件控制
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //波特率预分频为4
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //高位在前
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI1, &SPI_InitStructure);
  
  /* Enable SPI1 */
  SPI_Cmd(SPI1, ENABLE); //开启SPI设备
}



/*******************************************************************************
* Function Name : 
* Description : Reads a byte from the SPI Flash.
* This function must be used only if the Start_Read_Sequence
* function has been previously called.
* Input : None
* Output : None
* Return : Byte Read from the SPI Flash.
*******************************************************************************/
u8 ADS8328_ReadByte()
{
  ADS8328WriteHalfword(Dummy_Byte);
  /* Loop while DR register in not emplty */
  while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //等待发送缓冲器为空
  /* Send byte through the SPI1 peripheral */
  SPI_I2S_SendData(SPI1, 0x00); //发送一个数据

  // Wait to receive a byte 
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); // 此处运行不下去了
  SPI_I2S_ClearFlag(SPI1,SPI_I2S_FLAG_RXNE); //直接软件清除标志位。