max31865 SPI通信失败

2019-07-20 01:36发布

使用stm32f302读写MAX31865,初始化,无法读出数据,都是0xff。
尝试使用硬件SPI和GPIO模拟SPI,都无法读出。
感觉就是读写SPI,但是不知道为什么就不行。
有用示波器去量波形,感觉没什么问题。
有人使用过这个芯片吗?谢谢!

下面是代码:
u8 SPI_MAX31865_Init(void)
{
  
    SPI_InitTypeDef  SPI_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
   
    RCC_APB1PeriphClockCmd(SPI_MAX31865_SPI_CLK, ENABLE);
   
    RCC_AHBPeriphClockCmd(SPI_MAX31865_SPI_MOSI_GPIO_CLK | SPI_MAX31865_SPI_MISO_GPIO_CLK |
                          SPI_MAX31865_SPI_SCK_GPIO_CLK,  ENABLE);
   
    RCC_APB1PeriphClockCmd(SPI_MAX31865_CS_GPIO_CLK, ENABLE);   
   
    /*!< Configure SPI_MAX31865_SPI_CS_PIN pin: SPI_MAX31865 Card CS pin */
    GPIO_InitStructure.GPIO_Pin = SPI_MAX31865_CS_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
    GPIO_Init(SPI_MAX31865_CS_GPIO_PORT, &GPIO_InitStructure);
    /* Deselect the MAX31865: Chip Select high */
    SPI_MAX31865_CS_HIGH();
   
    /*!< Configure SPI_MAX31865_SPI pins: SCK */   
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
   
    /*!< Configure SPI_MAX31865_SPI pins: SCK */
    GPIO_InitStructure.GPIO_Pin = SPI_MAX31865_SPI_SCK_PIN;
    GPIO_Init(SPI_MAX31865_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
   
    /*!< Configure SPI_MAX31865_SPI pins: MISO */
    GPIO_InitStructure.GPIO_Pin = SPI_MAX31865_SPI_MISO_PIN;
    GPIO_Init(SPI_MAX31865_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
   
    /*!< Configure SPI_MAX31865_SPI pins: MOSI */
    GPIO_InitStructure.GPIO_Pin = SPI_MAX31865_SPI_MOSI_PIN;
    GPIO_Init(SPI_MAX31865_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
         
   
    /* Configure DRDS */
    RCC_AHBPeriphClockCmd(SPI_MAX31865_DR_GPIO_CLK, ENABLE);
   
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;   
    GPIO_InitStructure.GPIO_Pin = SPI_MAX31865_DR_PIN;
    GPIO_Init(SPI_MAX31865_DR_GPIO_PORT, &GPIO_InitStructure);
   
   
    GPIO_PinAFConfig(SPI_MAX31865_SPI_SCK_GPIO_PORT, SPI_MAX31865_SPI_SCK_SOURCE, GPIO_AF_5);
    GPIO_PinAFConfig(SPI_MAX31865_SPI_MISO_GPIO_PORT, SPI_MAX31865_SPI_MISO_SOURCE, GPIO_AF_5);
    GPIO_PinAFConfig(SPI_MAX31865_SPI_MOSI_GPIO_PORT, SPI_MAX31865_SPI_MOSI_SOURCE, GPIO_AF_5);   
   
   
    /* SPI2 configuration */
    /* SPI ADS1118: data input on the DIO pin is sampled on the rising edge of the CLK.
    Data on the DO and DIO pins are clocked out on the falling edge of CLK.*/
    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_High;                                                                        
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;                                                                                 
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;                                                                                    
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;         
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;                                                            
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPI_MAX31865_SPI, &SPI_InitStructure);
    SPI_RxFIFOThresholdConfig(SPI_MAX31865_SPI, SPI_RxFIFOThreshold_QF);
   
    /* Enable SPI1  */
    SPI_Cmd(SPI_MAX31865_SPI, ENABLE);
   
    /* wait for MAX31865 to set up */
    delay_ms(200);
   
    u8 read_value = 0;
    while(read_value != 0xc2)
    {
        SPI_MAX31865_Write( 0x80,0xc2 );
        
        delay_us(50);
        
        read_value = SPI_MAX31865_Read(0x00);
        
        delay_ms(10);
        
    }
   
    return 1;
}

uint8_t sEE_SendByte(uint8_t value)
{
  /*!< Loop while DR register in not empty */
  while (SPI_I2S_GetFlagStatus(SPI_MAX31865_SPI, SPI_I2S_FLAG_TXE) == RESET);
  /*!< Send byte through the SPI peripheral */
  SPI_SendData8(SPI_MAX31865_SPI, value);
  
  /*!< Wait to receive a byte */
  while (SPI_I2S_GetFlagStatus(SPI_MAX31865_SPI, SPI_I2S_FLAG_RXNE) == RESET);
  /*!< Return the byte read from the SPI bus */
  return SPI_ReceiveData8(SPI_MAX31865_SPI);
}
void SPI_MAX31865_Write(u8 address, u8 value)         
{
    SPI_MAX31865_CS_LOW();   
    sEE_SendByte(address);                  
    sEE_SendByte(value);
    SPI_MAX31865_CS_HIGH();
}
uint8_t SPI_MAX31865_Read(u8 address)         
{
    u8 value = 0;
   
    SPI_MAX31865_CS_LOW();  
    sEE_SendByte(address);                  
    value = sEE_SendByte(0x00);   
    SPI_MAX31865_CS_HIGH();
   
    return value;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
15条回答
lison0103
1楼-- · 2019-07-20 09:46
 精彩回答 2  元偷偷看……
lison0103
2楼-- · 2019-07-20 06:44
最好发现是硬件问题。。。
正点原子
3楼-- · 2019-07-20 12:39
没用过,帮顶
sin88
4楼-- · 2019-07-20 17:50
我今天调通这个东东了,明天换的优化下,顺便把故障检测也做了,完全看手册敲的代码啊,
sin88
5楼-- · 2019-07-20 20:13
 精彩回答 2  元偷偷看……
sep
6楼-- · 2019-07-20 21:42
sin88 发表于 2016-12-20 20:35
我今天调通这个东东了,明天换的优化下,顺便把故障检测也做了,完全看手册敲的代码啊,

你好,我也在搞这个,请问可以分享下吗。。不胜感激,谢谢

一周热门 更多>