打印

用mini外接了一个MAX31865模块出了问题

[复制链接]
1697|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
车水马龙|  楼主 | 2020-4-25 18:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有人用过MAX31865模块吗,
我往配置寄存器里写入0xD1,读取出来的却是0xD9
然后读取RTD寄存器得到的值一直是FF
下面是代码
MAIN.c
#include "stm32f10x.h"
#include "./spi/bsp_spi.h"
#include "./usart/bsp_usart_dma.h"
#include "./max31865/bsp_max31865.h"

uint8_t read_temp_HIGH;
uint8_t read_temp_LOW;
uint16_t read_temp;
int main(void)
{
SPI_MAX31865_Config();

DEBUG_UART_Config();

USARTx_DMA_Config();


MAX31865_SendData(MAX31865_Configuration_Write,0xd1);//写0xd1(1101_0001)到配置寄存器:3-wire RTD,50Hz filter select

read_temp = MAX31865_ReceiveData(MAX31865_Configuration_Read);

read_temp_HIGH = MAX31865_ReceiveData(MAX31865_RTD_MSB_Read);
read_temp_LOW = MAX31865_ReceiveData(MAX31865_RTD_LSB_Read);

  read_temp = (read_temp_HIGH << 8) | read_temp_LOW;
printf("%x\r\n ",read_temp);


while(1);
}
/******** end of file *********/


SPI.c
#include "./spi/bsp_spi.h"

void SPI_MAX31865_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;

/* 第一步:初始化GPIO */
SPI_MAX31865_GPIO_APBxClkCmd(SPI_MAX31865_GPIO_CLK, ENABLE);

//打开SPI外设的时钟
SPI_MAX31865_APBxClkCmd(SPI_MAX31865_CLK, ENABLE);

//将SPI的GPIO配置为推挽复用模式
GPIO_InitStructure.GPIO_Pin = SPI_MAX31865_SCK_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_MAX31865_SCK_GPIO_PORT, &GPIO_InitStructure);
  
//MOSI
GPIO_InitStructure.GPIO_Pin = SPI_MAX31865_MOSI_GPIO_PIN;
GPIO_Init(SPI_MAX31865_MOSI_GPIO_PORT, &GPIO_InitStructure);


  //MISO
GPIO_InitStructure.GPIO_Pin = SPI_MAX31865_MISO_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(SPI_MAX31865_MISO_GPIO_PORT, &GPIO_InitStructure);

//CS 软件控制
GPIO_InitStructure.GPIO_Pin = SPI_MAX31865_CS_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(SPI_MAX31865_CS_GPIO_PORT, &GPIO_InitStructure);

/* 初始化DRDY标志位 */
/* 第一步,打开外设的时钟 */
MAX31865_DRDY_APBxClkCmd(MAX31865_DRDY_GPIO_CLK, ENABLE);
/* 第二步, 配置外设初始化结构体 */
GPIO_InitStructure.GPIO_Pin = MAX31865_DRDY_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* 第三步, 调用外设初始化函数,把配置好的结构体成员写到寄存器里面 */
GPIO_Init(MAX31865_DRDY_GPIO_PORT, &GPIO_InitStructure);

//配置SPI工作模式
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CRCPolynomial = 0;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

SPI_Init(SPI_MAX31865x,&SPI_InitStructure);

SPI_Cmd(SPI_MAX31865x,ENABLE);
}
uint32_t time_out = 10000;
uint8_t SPI_TimeOut_CallBack(uint8_t data)
{
printf("\r\nSPI 检测超时,错误代码:%d",data);

return 0;
}

// 发送一个字节
uint8_t SPI_MAX31865_Send_Data(uint8_t data)
{
uint8_t read_temp;
time_out = SPI_MAX31865_WAIT_TIMEOUT;

/* 检测TXE */
while (SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) == RESET )
{
  //超时
  if(time_out-- == 0)
    {
   return SPI_TimeOut_CallBack(1);
  }
}
   
SPI_I2S_SendData(SPI1,data);
time_out = SPI_MAX31865_WAIT_TIMEOUT;

/* 检测RNXE */
while (SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) == RESET )
{
  //超时
  if(time_out-- == 0)
    {
   return SPI_TimeOut_CallBack(2);
  }
}
   
read_temp = (uint8_t)SPI_I2S_ReceiveData(SPI1);

return read_temp;
}
//接收一个字节
uint8_t SPI_MAX31865_Receive_Data(void)
{
return SPI_MAX31865_Send_Data(0xFF);
}


/******** end of file *********/


MAX31865.c
#include "./max31865/bsp_max31865.h"
#include "./spi/bsp_spi.h"


/* 向MAX31865发送数据 */
void MAX31865_SendData(uint8_t addr , uint8_t data)
{
SPI_MAX31865_CS_LOW;

SPI_MAX31865_Send_Data(addr);

SPI_MAX31865_Send_Data(data);

SPI_MAX31865_CS_HIGH;
}

/* 从MAX31865读取数据 */
uint8_t MAX31865_ReceiveData(uint8_t addr)
{
uint8_t data;

SPI_MAX31865_CS_LOW;

SPI_MAX31865_Send_Data(addr);

data = SPI_MAX31865_Receive_Data();

SPI_MAX31865_CS_HIGH;

return data;
}
/******** end of file *********/

使用特权

评论回复

相关帖子

沙发
chaqs3| | 2021-9-22 17:05 | 只看该作者
看下手册,D3和D2是Fault Detection,错误检测,查手册“Manual cycle 1 still running; waiting for
user to write 11”,初始化SPI总线后要延时10ms左右再配置0x00配置寄存器

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

474

主题

476

帖子

0

粉丝