引入max31865.c和.h文件,引入spi.h和.c文件
在主函数调用初始化函数MAX31865_init(3);//3线制
#include "MAX31865.h"
#include "spi.h"
/*********************** Global variables *************************/
void max31865_cs_init(void)//温度采集放大器 片选初始化
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PA端口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能PB端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //LED0-->PB.12 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOB.12
}
/*********************** Begin Private functions *************************/
void MAX31865_read(uint8_t ch, uint8_t addr, uint8_t *buffer, uint8_t len)
{
addr &= ~MAX31865_READ; // Force read bit on address
if (ch == 1)
{
MAX31865_CH1_CS;
}
else if(ch == 2)
{
MAX31865_CH2_CS;
}
spi_write(&addr, 1);// Write addr
spi_read(buffer, len);// Read data
MAX31865_CS_NULL;
}
/************************************************
void MAX31865_write(uint8_t ch, uint8_t addr, uint8_t data)
{
addr |= MAX31865_WRITE; // Force write bit on address
if (ch == 1)
{
MAX31865_CH1_CS;
}
else if(ch == 2)
{
MAX31865_CH2_CS;
}
spi_write(&addr, 1); // Write addr
spi_write(&data, 1); // Write data
MAX31865_CS_NULL;
}
void enableBias(uint8_t ch, uint8_t enable)//Disable bias voltage使能d7
{
uint8_t status;
MAX31865_read(ch,MAX31856_CONFIG_REG, &status, 1);
if (enable)
{
status |= MAX31856_CONFIG_BIAS;
} else
{
status &= ~MAX31856_CONFIG_BIAS;
}
MAX31865_write(ch,MAX31856_CONFIG_REG, status);
}
/**********************************
void autoConvert(uint8_t ch, uint8_t enable)//设置转换模式开关
{
uint8_t status;
MAX31865_read(ch,MAX31856_CONFIG_REG, &status, 1);
if (enable)
{
status |= MAX31856_CONFIG_MODEAUTO;
} else
{
status &= ~MAX31856_CONFIG_MODEAUTO;
}
MAX31865_write(ch,MAX31856_CONFIG_REG, status);
}
/********************************************************
void setWires(uint8_t ch, uint8_t numwires)//设置几线制接法
{
uint8_t status;
MAX31865_read(ch,MAX31856_CONFIG_REG, &status, 1);
if (numwires == 3) // 3-wire
{
status |= MAX31856_CONFIG_3WIRE;
} else // 2-4 wire
{
status &= ~MAX31856_CONFIG_3WIRE;
}
MAX31865_write(ch,MAX31856_CONFIG_REG, status);
}
/****************************
void single_shot(uint8_t ch)//读取温度,一次读取温度值
{
uint8_t status;
// Read config register
MAX31865_read(ch,MAX31856_CONFIG_REG, &status, 1);
// Enable 1shot bit, and write back
status |= MAX31856_CONFIG_1SHOT;
MAX31865_write(ch,MAX31856_CONFIG_REG, status);
}
/*********************** End Private functions *************************/
void MAX31865_init(uint8_t wires)//温度采集 支持2-4线RTD选择
{
spi1_init();//SDI SCL SDO io初始化
max31865_cs_init(); //片选io初始化
setWires(1,wires); // Set 2,3 or 4 wire sensor
enableBias(1,OFF); // Disable bias voltage
autoConvert(1,OFF); // Disable auto conversion
}
/**
float MAX31865_readTemp(uint8_t ch)//读取温度函数
{
uint8_t buffer[2];
uint16_t data = 0;
float resistance = 0;
float temp = 0;
// Activate bias voltage to read sensor data, and wait for the capacitors to fill
enableBias(ch,ON);
DELAY(10);
// Perform a single conversion, and wait for the result
single_shot(ch);
DELAY(65);
// Read data from max31865 data registers
MAX31865_read(ch,MAX31856_RTDMSB_REG, buffer, 2);
// Combine 2 bytes into 1 number, and shift 1 down to remove fault bit
data = buffer[0] << 8;
data |= buffer[1];
data >>= 1;
// Calculate the actual resistance of the sensor
resistance = ((float) data * RREF) / FACTOR;
// Calculate the temperature from the measured resistance
temp = ((resistance / 100) - 1) / ALPHA;
// Disable bias voltage to reduce power usage
enableBias(ch,OFF);
return temp;
}
在主函数调用MAX31865_readTemp(1);即可读取温度