252 lines
7.2 KiB
C
252 lines
7.2 KiB
C
![]() |
#include "./BSP/TP1000/TP1000.h"
|
|||
|
#include "./SYSTEM/spi3/spi3.h"
|
|||
|
#include "math.h"
|
|||
|
#include "./SYSTEM/delay/delay.h"
|
|||
|
|
|||
|
tp1000_cs_drdy TP1000_CS_DRDY;
|
|||
|
/***********************************************
|
|||
|
调用方式: TP1000_Init()
|
|||
|
函数说明: 初始化芯片DRDY引脚
|
|||
|
************************************************/
|
|||
|
void TP1000_Init(void)
|
|||
|
{
|
|||
|
|
|||
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|||
|
|
|||
|
/* GPIO Ports Clock Enable */
|
|||
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
|||
|
__HAL_RCC_GPIOG_CLK_ENABLE();
|
|||
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|||
|
|
|||
|
/*Configure GPIO pin : PtPin */
|
|||
|
GPIO_InitStruct.Pin = RTD_DRDY1_Pin;
|
|||
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|||
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|||
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|||
|
|
|||
|
/*Configure GPIO pin : PtPin */
|
|||
|
GPIO_InitStruct.Pin = RTD_DRDY2_Pin;
|
|||
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|||
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|||
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|||
|
|
|||
|
/*Configure GPIO pins : PGPin PGPin */
|
|||
|
GPIO_InitStruct.Pin = RTD_DRDY3_Pin;
|
|||
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|||
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|||
|
HAL_GPIO_Init(RTD_DRDY3_GPIO_Port, &GPIO_InitStruct);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/***********************************************
|
|||
|
调用方式: max31865_Read()
|
|||
|
返回值:
|
|||
|
函数说明: 从max31865读取数据
|
|||
|
************************************************/
|
|||
|
uint8_t max31865_Read(uint8_t addr){
|
|||
|
|
|||
|
uint8_t read;
|
|||
|
HAL_GPIO_WritePin(TP1000_CS_DRDY.RTD_NSS_GPIO_Port, TP1000_CS_DRDY.RTD_NSS_Pin, GPIO_PIN_RESET);
|
|||
|
HAL_SPI_Transmit(&hspi3, &addr, 1, 60);
|
|||
|
HAL_SPI_Receive(&hspi3, &read, 1, 60);
|
|||
|
HAL_GPIO_WritePin(TP1000_CS_DRDY.RTD_NSS_GPIO_Port, TP1000_CS_DRDY.RTD_NSS_Pin, GPIO_PIN_SET);
|
|||
|
return read;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/***********************************************
|
|||
|
调用方式: max31865_Write()
|
|||
|
返回值:
|
|||
|
函数说明: 从max31865读取数据
|
|||
|
************************************************/
|
|||
|
void max31865_Write(uint8_t addr, uint8_t wdata){
|
|||
|
uint8_t dat[2];
|
|||
|
dat[0] = addr;
|
|||
|
dat[1] = wdata;
|
|||
|
HAL_GPIO_WritePin(TP1000_CS_DRDY.RTD_NSS_GPIO_Port, TP1000_CS_DRDY.RTD_NSS_Pin, GPIO_PIN_RESET);
|
|||
|
HAL_SPI_Transmit(&hspi3,dat,2,60);
|
|||
|
HAL_GPIO_WritePin(TP1000_CS_DRDY.RTD_NSS_GPIO_Port, TP1000_CS_DRDY.RTD_NSS_Pin, GPIO_PIN_SET);
|
|||
|
}
|
|||
|
|
|||
|
/***********************************************
|
|||
|
调用方式: max31865_init()
|
|||
|
返回值:
|
|||
|
函数说明: 初始化max31865配置
|
|||
|
************************************************/
|
|||
|
void max31865_init(void){
|
|||
|
|
|||
|
max31865_Write(0x80,0xc1);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/***********************************************
|
|||
|
调用方式: DRDY_Pin_Value(Port, Pin)
|
|||
|
返回值: 返回高低电平值
|
|||
|
函数说明: 读取引脚电平,判断数据是否准备好
|
|||
|
************************************************/
|
|||
|
uint8_t DRDY_Pin_Value(void)
|
|||
|
{
|
|||
|
return HAL_GPIO_ReadPin(TP1000_CS_DRDY.RTD_DRDY_GPIO_Port, TP1000_CS_DRDY.RTD_DRDY_Pin);
|
|||
|
}
|
|||
|
|
|||
|
/***********************************************
|
|||
|
调用方式: max31865_get_rtd_value()
|
|||
|
返回值:
|
|||
|
函数说明: 读取测量结果
|
|||
|
************************************************/
|
|||
|
void max31865_get_rtd_value(uint8_t *uch_buff)
|
|||
|
{
|
|||
|
uch_buff[0] = max31865_Read(0x01);
|
|||
|
|
|||
|
uch_buff[1] = max31865_Read(0x02);
|
|||
|
}
|
|||
|
|
|||
|
/***********************************************
|
|||
|
调用方式: MAX31865_GetTemp1()
|
|||
|
返回值: temp
|
|||
|
函数说明: 获取温度
|
|||
|
************************************************/
|
|||
|
static float MAX31865_GetTemp1(uint8_t value[2])
|
|||
|
{
|
|||
|
unsigned int data;
|
|||
|
float Rt;
|
|||
|
float Rt0 = 1000; //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*4300;
|
|||
|
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
/***********************************************
|
|||
|
调用方式: max31865_fault_detection()
|
|||
|
返回值:
|
|||
|
函数说明: 手动故障检测
|
|||
|
************************************************/
|
|||
|
uint8_t max31865_fault_detection(void){
|
|||
|
|
|||
|
uint8_t uch_status;
|
|||
|
uint8_t uch_config;
|
|||
|
uch_config = max31865_Read(0x00);
|
|||
|
uch_config|=0x80;
|
|||
|
uch_config&=0xf3;
|
|||
|
max31865_Write(0x80,uch_config);
|
|||
|
delay_us(200);
|
|||
|
uch_config|=0x88;
|
|||
|
max31865_Write(0x80,uch_config);
|
|||
|
delay_us(200);
|
|||
|
uch_config|=0x8C;
|
|||
|
max31865_Write(0x80,uch_config);
|
|||
|
while((max31865_Read(0x00)&0x0C)!=0x00);
|
|||
|
uch_status=max31865_Read(0x07);
|
|||
|
return uch_status;
|
|||
|
}
|
|||
|
|
|||
|
/***********************************************
|
|||
|
调用方式: max31865_clear_fault_status()
|
|||
|
返回值:
|
|||
|
函数说明: 清除故障寄存器
|
|||
|
************************************************/
|
|||
|
void max31865_clear_fault_status(void)
|
|||
|
{
|
|||
|
uint8_t uch_config;
|
|||
|
|
|||
|
uch_config=max31865_Read(0x00);
|
|||
|
uch_config&=0xd3;
|
|||
|
uch_config|=0x02;
|
|||
|
max31865_Write(0x80, uch_config);
|
|||
|
}
|
|||
|
|
|||
|
/***********************************************
|
|||
|
调用方式: Get_Hardware_SPI_Temp()
|
|||
|
返回值:
|
|||
|
函数说明: 输出温度结果
|
|||
|
************************************************/
|
|||
|
void Get_Hardware_SPI_Temp(uint8_t auch_rtd[2]){
|
|||
|
|
|||
|
float f_temperature = 0; //温度转换
|
|||
|
uint8_t uch_fault_status; //故障代码
|
|||
|
|
|||
|
uch_fault_status = max31865_fault_detection();
|
|||
|
printf("故障代码%d\r\n",uch_fault_status);
|
|||
|
//没有故障,启动一次转换
|
|||
|
if((uch_fault_status & 0xFC)==0){
|
|||
|
|
|||
|
max31865_init(); //启动一次测量
|
|||
|
|
|||
|
while(DRDY_Pin_Value() == GPIO_PIN_SET); //等待测量结束
|
|||
|
|
|||
|
max31865_get_rtd_value(auch_rtd); //读取测量结果
|
|||
|
|
|||
|
f_temperature = MAX31865_GetTemp1(auch_rtd); //数据转换
|
|||
|
|
|||
|
}
|
|||
|
else{
|
|||
|
printf("错误代码%d\r\n",(uch_fault_status & 0xFC));
|
|||
|
auch_rtd[0] = 0xFF; // 设置默认值或错误码
|
|||
|
auch_rtd[1] = 0xFF;
|
|||
|
f_temperature = -999; //显示温度采集故障故障码
|
|||
|
max31865_clear_fault_status();
|
|||
|
uch_fault_status = max31865_fault_detection();
|
|||
|
}
|
|||
|
|
|||
|
// printf("temp%d\r\n",auch_rtd[0]);
|
|||
|
// printf("temp%d\r\n",auch_rtd[1]);
|
|||
|
printf("temp%f\r\n",f_temperature);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
tp1000_cs_drdy Get_Hardware_SPI3_Temp(void){
|
|||
|
TP1000_CS_DRDY.RTD_DRDY_GPIO_Port = RTD_DRDY1_GPIO_Port;
|
|||
|
TP1000_CS_DRDY.RTD_DRDY_Pin = RTD_DRDY1_Pin;
|
|||
|
TP1000_CS_DRDY.RTD_NSS_GPIO_Port = SPI3_NSS1_GPIO_Port;
|
|||
|
TP1000_CS_DRDY.RTD_NSS_Pin = SPI3_NSS1_Pin;
|
|||
|
|
|||
|
printf("通道1\r\n");
|
|||
|
Get_Hardware_SPI_Temp(TP1000_CS_DRDY.f_temperature1);
|
|||
|
|
|||
|
TP1000_CS_DRDY.RTD_DRDY_GPIO_Port = RTD_DRDY2_GPIO_Port;
|
|||
|
TP1000_CS_DRDY.RTD_DRDY_Pin = RTD_DRDY2_Pin;
|
|||
|
TP1000_CS_DRDY.RTD_NSS_GPIO_Port = SPI3_NSS2_GPIO_Port;
|
|||
|
TP1000_CS_DRDY.RTD_NSS_Pin = SPI3_NSS2_Pin;
|
|||
|
|
|||
|
printf("通道2\r\n");
|
|||
|
Get_Hardware_SPI_Temp(TP1000_CS_DRDY.f_temperature2);
|
|||
|
|
|||
|
TP1000_CS_DRDY.RTD_DRDY_GPIO_Port = RTD_DRDY3_GPIO_Port;
|
|||
|
TP1000_CS_DRDY.RTD_DRDY_Pin = RTD_DRDY3_Pin;
|
|||
|
TP1000_CS_DRDY.RTD_NSS_GPIO_Port = SPI3_NSS3_GPIO_Port;
|
|||
|
TP1000_CS_DRDY.RTD_NSS_Pin = SPI3_NSS3_Pin;
|
|||
|
|
|||
|
printf("通道3\r\n");
|
|||
|
Get_Hardware_SPI_Temp(TP1000_CS_DRDY.f_temperature3);
|
|||
|
|
|||
|
return TP1000_CS_DRDY;
|
|||
|
}
|