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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2118|回復: 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);
回復

使用道具 舉報

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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: aaaaaa大片免费看最大的 | 午夜成人在线视频 | 亚洲国产精品久久久 | 欧美一区二区三区国产精品 | wwwxxx日本在线观看 | 成人片免费看 | 免费黄色的视频 | 久久综合九九 | 久久com| 91精品一区二区三区久久久久久 | 在线免费观看视频黄 | 国产电影一区二区 | 免费骚视频 | 曰批视频在线观看 | 污书屋| 欧美日产国产成人免费图片 | 国产免费自拍 | 毛片毛片毛片毛片毛片 | 精品国产乱码久久久久久丨区2区 | 欧美日韩福利视频 | 日韩电影中文字幕在线观看 | 色男人天堂av | 成人在线视频网址 | 欧美日韩视频在线 | 欧美电影在线观看网站 | 成人在线观看免费视频 | 亚洲国产伊人 | 国产精品色婷婷久久58 | 99视频在线看 | 久久久这里都是精品 | 成人h视频在线 | 中文字幕韩在线第一页 | 91看片在线 | 亚洲福利| 精品国产欧美一区二区三区不卡 | 免费一区二区三区 | 婷婷综合| 中文字幕不卡在线观看 | 国产亚洲精品精品国产亚洲综合 | 中文在线视频观看 | 在线观看国产网站 |