找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 822|回复: 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 单片机教程网

快速回复 返回顶部 返回列表