久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1947|回復: 2
打印 上一主題 下一主題
收起左側

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

[復制鏈接]
跳轉到指定樓層
樓主
ID:391219 發表于 2022-10-14 19:29 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
150黑幣
這個是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);
      
}

這個是應用代碼:
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讀
在使用的過程中,怎么弄也讀不出來正確的數值,現在讀出來的都是0xff。請大家看看究竟是哪出現了問題。
情景是:
用STM32控制器件,通訊使用SPI1,器件要求spi: CPOL=0;CPHA=1;MODE=1;DORD為0,MSB最先輸出。

最佳答案

查看完整內容

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

使用道具 舉報

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

使用道具 舉報

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

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

使用道具 舉報

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

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: www.久草| 2一3sex性hd | 久久中文视频 | 中文字幕在线二区 | 99精品国产一区二区青青牛奶 | 久久精品播放 | 精品成人| 毛片在线看片 | 久久精品国产一区 | 国产午夜精品久久久 | 欧美精品久久 | 国产精品伦理一区 | 国产性色视频 | 91视频91| 精品国产乱码久久久久久闺蜜 | 欧美大片一区 | 性一爱一乱一交一视频 | 在线免费观看黄a | 国产1区2区在线观看 | 日韩欧美一二三区 | 九九热在线视频观看这里只有精品 | 伊人狠狠 | 色婷婷综合成人av | 国产一区二区三区在线观看免费 | 国产丝袜一区二区三区免费视频 | 91日b| 亚洲激情专区 | 免费久久视频 | 黄网免费看 | 91中文视频 | 欧美精品久久久 | 久久久久九九九女人毛片 | 色偷偷888欧美精品久久久 | 日韩一区二区三区在线视频 | 成人免费在线 | 国产成人99久久亚洲综合精品 | 国产日韩视频 | 亚洲高清在线免费观看 | 91九色视频 | 亚洲国产欧美日韩 | 亚洲精品三级 |