267 lines
7.2 KiB
C
267 lines
7.2 KiB
C
#include "MAX31865drv.h"
|
||
/***********************************************
|
||
调用方式: maxim_31865_write_register()
|
||
返回值:
|
||
函数说明: 寄存器写入
|
||
************************************************/
|
||
void maxim_31865_write_register(uint8_t uch_register_address, uint8_t uch_register_value)
|
||
{
|
||
SPI_CS_LOW;
|
||
SPI_WriteByte(uch_register_address);
|
||
SPI_WriteByte(uch_register_value);
|
||
SPI_CS_HIGH;
|
||
}
|
||
/***********************************************
|
||
调用方式: maxim_31865_write_nregisters()
|
||
返回值:
|
||
函数说明: 寄存器写入指定字节
|
||
************************************************/
|
||
void maxim_31865_write_nregisters(uint8_t uch_register_address,uint8_t *uch_buff,uint8_t uch_nBytes)
|
||
{
|
||
SPI_CS_LOW;
|
||
SPI_WriteByte(uch_register_address);
|
||
SPI_Write(uch_buff,uch_nBytes);
|
||
SPI_CS_HIGH;
|
||
}
|
||
/***********************************************
|
||
调用方式: maxim_31865_read_register()
|
||
返回值: uch_register_data
|
||
函数说明: 寄存器读出字节
|
||
************************************************/
|
||
uint8_t maxim_31865_read_register(uint8_t uch_register_address)
|
||
{
|
||
uint8_t uch_register_data;
|
||
SPI_CS_LOW;
|
||
SPI_WriteByte(uch_register_address);
|
||
uch_register_data=SPI_ReadByte();
|
||
SPI_CS_HIGH;
|
||
return (uch_register_data);
|
||
}
|
||
/***********************************************
|
||
调用方式: maxim_31865_read_nregisters()
|
||
返回值:
|
||
函数说明: 寄存器读出多字节
|
||
************************************************/
|
||
void maxim_31865_read_nregisters(uint8_t uch_register_address, uint8_t *uch_buff,uint8_t uch_nBytes)
|
||
{
|
||
SPI_CS_LOW;
|
||
SPI_WriteByte(uch_register_address);
|
||
SPI_Read(uch_buff,uch_nBytes);
|
||
SPI_CS_HIGH;
|
||
}
|
||
/***********************************************
|
||
调用方式: maxim_31865_init()
|
||
返回值:
|
||
函数说明: 初始化max31865配置
|
||
************************************************/
|
||
void maxim_31865_init(max31865_configuration* configuration)
|
||
{
|
||
uint8_t temp=0;
|
||
temp|=configuration->Vbias|configuration->Conversion_mode|configuration->Rtd_wire|configuration->Filter;
|
||
maxim_31865_write_register(0x80,temp);
|
||
}
|
||
/***********************************************
|
||
调用方式: maxim_set_fault_threshold()
|
||
返回值:
|
||
函数说明: 设置故障门限,输入为电阻值
|
||
************************************************/
|
||
void maxim_set_fault_threshold(float high_threshold, float low_threshold)
|
||
{
|
||
uint8_t au_threshold[4];
|
||
uint16_t u_threshold;
|
||
|
||
u_threshold= ((int)((high_threshold/REF_RES)*16383))<<1;
|
||
au_threshold[0]=(u_threshold>>8);
|
||
au_threshold[1]=u_threshold&0xff;
|
||
u_threshold=((int)((low_threshold/REF_RES)*16383))<<1;
|
||
au_threshold[2]=(u_threshold>>8);
|
||
au_threshold[3]=u_threshold&0xff;
|
||
|
||
maxim_31865_write_nregisters(0x83,au_threshold,4);
|
||
|
||
}
|
||
/***********************************************
|
||
调用方式: maxim_auto_fault_detection()
|
||
返回值:
|
||
函数说明: 自动故障检测
|
||
************************************************/
|
||
uint8_t maxim_auto_fault_detection(void)
|
||
{
|
||
uint8_t uch_status;
|
||
uint8_t uch_config;
|
||
uch_config=maxim_31865_read_register(0x00);
|
||
uch_config|=0x84;
|
||
maxim_31865_write_register(0x80, uch_config);
|
||
while((maxim_31865_read_register(0x00)&0xC0)!=0x00);
|
||
uch_status=maxim_31865_read_register(0x07);
|
||
return(uch_status);
|
||
}
|
||
/***********************************************
|
||
调用方式: maxim_manual_fault_detection()
|
||
返回值:
|
||
函数说明: 手动故障检测
|
||
************************************************/
|
||
uint8_t maxim_manual_fault_detection(void)
|
||
{
|
||
uint8_t uch_status;
|
||
uint8_t uch_config;
|
||
uch_config=maxim_31865_read_register(0x00);
|
||
uch_config|=0x80;
|
||
uch_config&=0xf3;
|
||
maxim_31865_write_register(0x80, uch_config);
|
||
delay_us(200);
|
||
uch_config|=0x88;
|
||
maxim_31865_write_register(0x80, uch_config);
|
||
delay_us(200);
|
||
uch_config|=0x8C;
|
||
maxim_31865_write_register(0x80, uch_config);
|
||
while((maxim_31865_read_register(0x00)&0x0C)!=0x00);
|
||
uch_status=maxim_31865_read_register(0x07);
|
||
return(uch_status);
|
||
}
|
||
/***********************************************
|
||
调用方式: maxim_clear_fault_status()
|
||
返回值:
|
||
函数说明: 清除故障寄存器
|
||
************************************************/
|
||
void maxim_clear_fault_status(void)
|
||
{
|
||
uint8_t uch_config;
|
||
|
||
uch_config=maxim_31865_read_register(0x00);
|
||
uch_config&=0xd3;
|
||
uch_config|=0x02;
|
||
maxim_31865_write_register(0x80, uch_config);
|
||
}
|
||
/***********************************************
|
||
调用方式: maxim_get_rtd_value()
|
||
返回值:
|
||
函数说明: 读取测量结果
|
||
************************************************/
|
||
void maxim_get_rtd_value(uint8_t *uch_buff)
|
||
{
|
||
maxim_31865_read_nregisters(0x01,uch_buff,2);
|
||
}
|
||
/***********************************************
|
||
调用方式: MAX31865_GetTemp1()
|
||
返回值: temp
|
||
函数说明: 获取温度
|
||
************************************************/
|
||
static float MAX31865_GetTemp1(uint8_t value[2])
|
||
{
|
||
unsigned int data;
|
||
float Rt;
|
||
float Rt0 = 100; //PT100
|
||
float Z1,Z2,Z3,Z4,temp;
|
||
float a = 3.9083e-3;
|
||
float b = -5.775e-7;
|
||
float rpoly;
|
||
data= value[0]<<8;
|
||
data|= value[1];
|
||
data>>=1; //去掉Fault位
|
||
Rt=(float)data/32768.0*REF_RES;
|
||
Z1 = -a;
|
||
Z2 = a*a-4*b;
|
||
Z3 = 4*b/Rt0;
|
||
Z4 = 2*b;
|
||
temp = Z2+Z3*Rt;
|
||
temp = (sqrt(temp)+Z1)/Z4;
|
||
if(temp>=0){
|
||
return temp;
|
||
}
|
||
rpoly = Rt;
|
||
temp = -242.02;
|
||
temp += 2.2228 * rpoly;
|
||
rpoly *= Rt; // square
|
||
temp += 2.5859e-3 * rpoly;
|
||
rpoly *= Rt; // ^3
|
||
temp -= 4.8260e-6 * rpoly;
|
||
rpoly *= Rt; // ^4
|
||
temp -= 2.8183e-8 * rpoly;
|
||
rpoly *= Rt; // ^5
|
||
temp += 1.5243e-10 * rpoly;
|
||
|
||
return temp;
|
||
}
|
||
/***********************************************
|
||
调用方式: Get_Hardware_SPI_Temp_Init()
|
||
返回值:
|
||
函数说明: 配置PT100参数
|
||
************************************************/
|
||
void Get_Hardware_SPI_Temp_Init(void)
|
||
{
|
||
max31865_configuration configuration;
|
||
/*配置MAX31865工作模式*/
|
||
configuration.Vbias = ON;
|
||
configuration.Rtd_wire = RTD_3wire;
|
||
configuration.Filter = Filter_50Hz;
|
||
maxim_31865_init(&configuration);
|
||
/*设置故障门限,输入为电阻值*/
|
||
maxim_set_fault_threshold(430, 0);
|
||
}
|
||
/***********************************************
|
||
调用方式: Get_Hardware_SPI_Temp()
|
||
返回值:
|
||
函数说明: 输出温度结果
|
||
************************************************/
|
||
float Get_Hardware_SPI_Temp(void)
|
||
{
|
||
max31865_configuration configuration;
|
||
|
||
uint8_t uch_fault_status;//故障代码
|
||
uint8_t auch_rtd[2];//数据缓存
|
||
float f_temperature;//温度转换
|
||
uch_fault_status = maxim_manual_fault_detection();
|
||
/*
|
||
*如果检测到RTD故障,则输入故障代码,停止程序运行,否则,将进入循环程序
|
||
*在循环程序中,采用单次测量方式,每秒测量一次,将测量结果转换成温度输出。
|
||
*如果在循环程序中检测到RTD故障,同样输入故障代码,但是,会循环检测,一旦
|
||
*故障消除,将会继续进行温度采集
|
||
*/
|
||
if((uch_fault_status & 0xFC) == 0){
|
||
configuration.Conversion_mode = One_Shot_Conversion;
|
||
maxim_31865_init(&configuration); //启动一次测量
|
||
|
||
while(DRDY_Pin_Value() == SET); //等待测量结束
|
||
|
||
maxim_get_rtd_value( auch_rtd); //读取测量结果
|
||
|
||
if((auch_rtd[1] & 0x01) == 0x01){ //如果故障标识置位,运行故障检测,输入故障代码
|
||
uch_fault_status = maxim_manual_fault_detection(); //运行手动故障检测,也可以运行自动故障检测
|
||
maxim_clear_fault_status(); //清除故障寄存器
|
||
printf("%d",uch_fault_status);
|
||
}
|
||
else{
|
||
//如果没有故障,则进行温度转换,输出温度转换结果
|
||
f_temperature = MAX31865_GetTemp1(auch_rtd);
|
||
}
|
||
|
||
}
|
||
else{
|
||
maxim_clear_fault_status();//清除故障寄存器
|
||
printf("%d",uch_fault_status);
|
||
}
|
||
return f_temperature;
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|