打印

调试STM32的SPI时遇到个问题

[复制链接]
2390|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
sunyq101|  楼主 | 2011-9-26 18:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
int main(void)
{
  //uint16_t a;
  /* System Clocks Configuration **********************************************/
  RCC_Configuration();
  SPI_Configuration();
  GPIO_SetBits(GPIOA, GPIO_Pin_4);
  while (1)
  {  
  Delay(1000);
  GPIO_ResetBits(GPIOA, GPIO_Pin_4);
  Delay(10);
   SPI_I2S_SendData(SPI1,0x42);
   while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
   while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY)==SET);
  SPI_read();
  GPIO_SetBits(GPIOA, GPIO_Pin_4);
  
   }
}
void SPI_send(u8 data)
{
   SPI_I2S_SendData(SPI1,data);
   while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
   while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY)==SET);
   
}
u16 SPI_read(void)
{
      u8 j;
   SPI_BiDirectionalLineConfig(SPI1,SPI_Direction_Rx);
   for(j=0;j<4;j++)
   {
         while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
    nReceive[j]=SPI_I2S_ReceiveData(SPI1);
    }
    j=nReceive[0];
             SPI_BiDirectionalLineConfig(SPI1,SPI_Direction_Tx);
return j;
}
void RCC_Configuration(void)
{   
  /* Setup the microcontroller system. Initialize the Embedded Flash Interface,  
     initialize the PLL and update the SystemFrequency variable. */
  SystemInit();
}

void SPI_Configuration(void)
{
  SPI_InitTypeDef  SPI_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
   
  /* Enable SPI2 GPIOB clocks */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 ,ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  /* Configure SPI2 pins: SCK, MISO and MOSI */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  /* Configure PB.12 as Output push-pull, used as Flash Chip 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);
  /* SPI2 configuration */
  SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  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_128;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI1, &SPI_InitStructure);
  
  /* Enable SPI2  */
  SPI_Cmd(SPI1, ENABLE);   
}

程序大概是这样的
问题是第一次读回来的数据是对的,第二次读的数据就变成了FF。
有没有大侠帮忙找下问题在哪。
沙发
pentral0311| | 2011-9-30 17:00 | 只看该作者
跟你通讯的片子 CS脚或者 SPI时钟是否配置正确

使用特权

评论回复
板凳
香水城| | 2011-9-30 21:58 | 只看该作者
目标是什么?第一次怎么读的?第二次又是怎么读的?两次读之间都做了什么?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

3

帖子

1

粉丝