有人用过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
",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("
SPI 检测超时,错误代码:%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 *********/
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>