找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2135|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

STM32單片機(jī)使用SPI控制器件,都返回值一直為0xFF

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:391219 發(fā)表于 2022-10-14 19:29 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
150黑幣
這個(gè)是SPI初始化代碼
void SPIx_I2Cx_GPIOs_Init(void* bus_type)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                         RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
                         RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO,  DISABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                         RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
                         RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO,  ENABLE);
      
        //SPI1_NSS
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//PA4
        GPIO_Init(GPIOA,&GPIO_InitStructure);
      //SPI1_CLK
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//PA5
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        //SPI_MISO
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//PA6
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        //SPI_MOSI
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//PA7
        GPIO_Init(GPIOA, &GPIO_InitStructure);
      
        SPI_I2S_DeInit(bus_type);

        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;   
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//PD4
        GPIO_Init(GPIOD, &GPIO_InitStructure);
      
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PD3
        GPIO_Init(GPIOD, &GPIO_InitStructure);
        // SPIx Enable = 0 for SPI Mode // SPIx Enable = RSN for GP22
        GPIO_WriteBit(GPIOD, GPIO_Pin_3, Bit_RESET);
}

void SPIx_I2Cx_Interface_Init(void* bus_type)
{
        SPI_InitTypeDef SPI_InitStructure;
        //RCC_SYSCLKConfig (RCC_SYSCLKSource_HSI);
        //RCC_HCLKConfig (RCC_SYSCLK_Div1);
        RCC_APB2PeriphResetCmd (RCC_APB2Periph_SPI1, DISABLE);
        RCC_APB2PeriphClockCmd (RCC_APB2Periph_SPI1, ENABLE);
      
        // All are defined in stm32f10x_spi.h
  SPI_InitStructure.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode              = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize          = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL              = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA              = SPI_CPHA_2Edge;//
  SPI_InitStructure.SPI_NSS               = SPI_NSS_Soft;
  // SPI frequence devider
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
  SPI_InitStructure.SPI_FirstBit          = SPI_FirstBit_MSB;

  // Apply SPIx configuration
  SPI_Init(bus_type, &SPI_InitStructure);
  // Enabling the SPIx Interface
  SPI_Cmd(bus_type, ENABLE);

  // Enabling the NSS Output during transmission
//  SPI_SSOutputCmd (bus_type, ENABLE);
      
        GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
      
}

這個(gè)是應(yīng)用代碼:
unsigned char Send_24Bit_Opcode(unsigned char command, unsigned short address, unsigned char data)
{

  unsigned char    Result_read = 0;

  // x >> y, mean x is shifted by y bit-positions to the right
  unsigned char Byte_0  = data;
  unsigned char Byte_1  = address;
  unsigned char Byte_2  = address>>8 | command;
  // SPIx BUS_TYPE -------------------------------------------------------------

  // Deactivating Reset SPIx
  GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET);
  // GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);

   while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==0) {}
   SPI_I2S_SendData(SPI1, Byte_2);     // send byte 2

   while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==0) {}
   SPI_I2S_SendData(SPI1, Byte_1);     // send byte 1
   if (command==0x90) // write command
   {
     while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==0) {}
     SPI_I2S_SendData(SPI1, Byte_0);     // send byte 0

    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==0) {}
    Simple_delay_43ns((void*)10); // important delay (16) at SPI freq.=750kHz
   }

   if (command==0x10) // read command
   {
     while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==RESET) {};
     Simple_delay_43ns((void*)10); // important delay (16) at SPI freq.=750kHz

     //Compulsory reads to DR and SR to clear OVR,
     //so that next incoming data is saved
     SPI_I2S_ReceiveData(SPI1);                     // To clear OVR
     SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE); // To clear OVR

     //Reading byte1
     SPI_I2S_SendData(SPI1, 0xFF);  // DUMMY WRITE
     // Wait until RX buffer is not empty, then read the received data
     while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==0) {}

     Result_read = SPI_I2S_ReceiveData(SPI1); //  Read
    }

     // Reset to device SPIx
     GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
     //GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
  return Result_read;
}


Send_24Bit_Opcode(0x90,1,0x55);//0x90 寫
err =  Send_24Bit_Opcode(0x10,1,0xff);//err = 0x55 0x10讀
在使用的過程中,怎么弄也讀不出來正確的數(shù)值,現(xiàn)在讀出來的都是0xff。請大家看看究竟是哪出現(xiàn)了問題。
情景是:
用STM32控制器件,通訊使用SPI1,器件要求spi: CPOL=0;CPHA=1;MODE=1;DORD為0,MSB最先輸出。

最佳答案

查看完整內(nèi)容

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; 由軟件控制就不需要 SPI_SSOutputCmd (bus_type, ENABLE);
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂1 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:419968 發(fā)表于 2022-10-14 19:29 | 只看該作者
SPI_InitStructure.SPI_NSS               = SPI_NSS_Soft; 由軟件控制就不需要 SPI_SSOutputCmd (bus_type, ENABLE);
回復(fù)

使用道具 舉報(bào)

板凳
ID:391219 發(fā)表于 2022-10-15 12:19 | 只看該作者
adject 發(fā)表于 2022-10-14 22:57
SPI_InitStructure.SPI_NSS               = SPI_NSS_Soft; 由軟件控制就不需要 SPI_SSOutputCmd (bus_typ ...

是的,我注釋掉了,忘記修改了。我經(jīng)過昨天測試,感覺是硬件問題,正在修改
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表