大家帮忙看下 关于stm32 与rc522通信的问题

2019-03-23 18:36发布

最近在弄毕业设计 要求的题目是用stm32 控制rc522 对 mifare1 s50卡进行读写stm32 与 rc522 的数字接口选用spi我最开始是将stm32开发板上的spi1与spi2连接起来。spi1发送10个字符让spi2接收,nss采用软件控制。成功实现了数据的发送和接收。我现在将stm32上的spi1与rc522连接。我想向rc522的FIFO中写10个0xf0然后读rc522的FIFO寄存器看看我是否成功将10个0xf0写入了rc522的FIFO中但是调试的时候发现,spi1接收回来的数据是0xff 不知道是怎么回事 大家请帮帮我相关代码如下:int main(void)
{

  //unsigned char status;
  RCC_Configuration();
  //LCD_Ini();
  Spi_Ini();
  PcdReset();
  for(i=0;i<10;i++)
  {
      WriteRawRC(FIFODataReg,0xF0);
  }
  te=ReadRawRC(FIFOLevelReg);
}
unsigned char ReadRawRC(unsigned char Address)
{
    unsigned char i, ucAddr;
    unsigned char ucResult=0;

    ucAddr = ((Address<<1)&0x7E)|0x80;

    SPI_NSSInternalSoftwareConfig(SPI1,SPI_NSSInternalSoft_Reset); //·¢3öslaveƬѡDÅoÅ
     while((SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE))==RESET); //2éÑˉspi·¢Ëíêy¾Y¼Ä′æÆ÷êÇ·ñ½«Ç°ò»′Îêy¾Y·¢Ëííê±Ï
     SPI_I2S_SendData(SPI1,ucAddr); //·¢Ëírc522¼Ä′æÆ÷μØÖ·
    while((SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE))==RESET);  //2éÑˉspi½óêÕêy¾Y¼Ä′æÆ÷ÖDêÇ·ñêÕμ½áËêy¾Y
     SPI_NSSInternalSoftwareConfig(SPI1,SPI_NSSInternalSoft_Set);  //è¡Ïû¶ÔslaveμÄƬѡ
    ucResult=SPI_I2S_ReceiveData(SPI1);  
    return ucResult;
}

void WriteRawRC(unsigned char Address, unsigned char value)
{  
    unsigned char i, ucAddr; 
    ucAddr = ((Address<<1)&0x7E);
   
     SPI_NSSInternalSoftwareConfig(SPI1,SPI_NSSInternalSoft_Reset); //·¢3öslaveƬѡDÅoÅ
     while((SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE))==RESET); //2éÑˉspi·¢Ëíêy¾Y¼Ä′æÆ÷êÇ·ñ½«Ç°ò»′Îêy¾Y·¢Ëííê±Ï
       SPI_I2S_SendData(SPI1, ucAddr); //·¢Ëírc522¼Ä′æÆ÷μØÖ·
     while((SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE))==RESET); //2éÑˉspi·¢Ëíêy¾Y¼Ä′æÆ÷êÇ·ñ½«Ç°ò»′Îêy¾Y·¢Ëííê±Ï
       SPI_I2S_SendData(SPI1,value);  //·¢ËíòaD′èërc522¼Ä′æÆ÷μÄêy¾Y
    SPI_NSSInternalSoftwareConfig(SPI1,SPI_NSSInternalSoft_Set); //è¡Ïû¶ÔslaveμÄƬѡ
}








void RCC_Configuration(void)
{
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
}        


void Spi_Ini(void)
{
     SPI_InitTypeDef SPI_InitStructure;
     GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);//′ò¿aíaéèspiμÄê±Öó

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
    GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

     
     SPI_Cmd(SPI1, DISABLE);             //±ØDëÏè½ûÄü,2ÅÄü¸Ä±äMODE
    SPI_InitStructure.SPI_Direction =SPI_Direction_2Lines_FullDuplex;  //á½Ïßè«Ë«1¤
    SPI_InitStructure.SPI_Mode =SPI_Mode_Master;       //Ö÷
    SPI_InitStructure.SPI_DataSize =SPI_DataSize_8b;       //8λ
    SPI_InitStructure.SPI_CPOL =SPI_CPOL_Low;         //CPOL=0ê±ÖóDü¿Õμí
    SPI_InitStructure.SPI_CPHA =SPI_CPHA_1Edge;       //CPHA=0 êy¾Y2¶»ñμú2¸ö
    SPI_InitStructure.SPI_NSS =SPI_NSS_Soft;         //èí¼tNSS
    SPI_InitStructure.SPI_BaudRatePrescaler =SPI_BaudRatePrescaler_16; //2·ÖÆμ 4M<max_rc522spi 10M
    SPI_InitStructure.SPI_FirstBit =SPI_FirstBit_MSB;      //¸ßλÔúÇ°
    SPI_InitStructure.SPI_CRCPolynomial =7;         //CRC7
   
    SPI_Init(SPI1,&SPI_InitStructure);
     SPI_SSOutputCmd(SPI1, ENABLE);
    SPI_Cmd(SPI1, ENABLE);
/*******spi2************/
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
    GPIO_Init(GPIOB, &GPIO_InitStructure);
     SPI_Cmd(SPI2, DISABLE);             //±ØDëÏè½ûÄü,2ÅÄü¸Ä±äMODE
    SPI_InitStructure.SPI_Direction =SPI_Direction_2Lines_FullDuplex;  //á½Ïßè«Ë«1¤
    SPI_InitStructure.SPI_Mode =SPI_Mode_Slave;       //Ö÷
    SPI_InitStructure.SPI_DataSize =SPI_DataSize_8b;       //8λ
    SPI_InitStructure.SPI_CPOL =SPI_CPOL_Low;         //CPOL=0ê±ÖóDü¿Õμí
    SPI_InitStructure.SPI_CPHA =SPI_CPHA_1Edge;       //CPHA=0 êy¾Y2¶»ñμú2¸ö
    SPI_InitStructure.SPI_NSS =SPI_NSS_Soft;         //èí¼tNSS
    SPI_InitStructure.SPI_BaudRatePrescaler =SPI_BaudRatePrescaler_2; //2·ÖÆμ 4M<max_rc522spi 10M
    SPI_InitStructure.SPI_FirstBit =SPI_FirstBit_MSB;      //¸ßλÔúÇ°
    SPI_InitStructure.SPI_CRCPolynomial =7;         //CRC7   
    SPI_Init(SPI2,&SPI_InitStructure);
    SPI_Cmd(SPI2, ENABLE);
/*******spi2************/
}
此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。