本帖最后由 ding731868572 于 2019-10-10 10:45 编辑
求助谁用过美信的max31865,我用它测试PT100的温度采集做出来了,但是对PT100的开路和短路的故障判定不知道怎么做?有谁用过和我说一下.下面是我的PT100的采集代码:
#define SPI_FAIL 1
#define SPI_OK 0
#define Manual_Mode
#define Fault_Auto_Detect
#define Rref 4000 //if using PT100, change this from 4000 to 400
extern u8 pData[15];
bool MAX31865_msater_state = FALSE;
u8 MAX31865_master_timer = 0; //IIC忙超时计数器
void delay(u32 X)
{
while(X--);
}
void Inital_MAX31865(void)
{
#ifdef Auto_Mode
MAX31865_Config=0xD1; //Auto Mode, 3-Wire, 50Hz
Data_Buffer[0]=MAX31865_Config;
if(SPI_Operation(0x80,Data_Buffer,1)==SPI_FAIL)
{
Operation_Fault=1;
return;
}
#endif
#ifdef Manual_Mode
MAX31865_Config=0x91; //Manual Mode, 3-Wire, 50Hz
Data_Buffer[0]=MAX31865_Config;
if(SPI_Operation(0x80,Data_Buffer,1)==SPI_FAIL)
{
Operation_Fault=1;
return;
}
#endif
Data_Buffer[0]=0xFF;
Data_Buffer[1]=0xFF; //High Fault Threshold ,this value can be changed as needed
Data_Buffer[2]=0x00;
Data_Buffer[3]=0x00; //Low Fault Threshold , this value can be changed as needed
if(SPI_Operation(0x83,Data_Buffer,4)==SPI_FAIL)
{
Operation_Fault=1;
return;
}
}
//故障检测
void Fault_Detect(void)
{
#ifdef Fault_Auto_Detect
Data_Buffer[0]=0x84 ;
if(SPI_Operation(0x80,Data_Buffer,1)==SPI_FAIL) // write 0x86 to configuration register
{
Operation_Fault=1;
return;
}
else
{
Operation_Fault = 0;
}
//DelayMS(1); //delay 1ms
while((Data_Buffer[0]&0x0C)!=0x00) //wait for to finish fault detection
{
if(SPI_Operation(0x00,Data_Buffer,1)==SPI_FAIL)
{
Operation_Fault=1;
return;
}
}
if(SPI_Operation(0x07,Data_Buffer,1)==SPI_FAIL) //read back Fault Status register
{
Operation_Fault=1;
return;
}
MAX31855_Fault_Status=Data_Buffer[0];
Data_Buffer[0]=MAX31865_Config|0x02;
if(SPI_Operation(0x80,Data_Buffer,1)==SPI_FAIL) //write back configuration register
{
Operation_Fault=1;
return;
}
#endif
#ifdef Fault_Manual_Detect
Data_Buffer[0]=0x88;
if(SPI_Operation(0x00,Data_Buffer,1)==SPI_FAIL) //write 0x88 to configuration register
{
Operation_Fault=1;
return;
}
delay_1us(1000);; //wait for 1ms
Data_Buffer[0]=0x8c;
if(SPI_Operation(0x00,Data_Buffer,1)==SPI_FAIL) //write 0x8C to configuration register
{
Operation_Fault=1;
return;
}
while((Data_Buffer[0]&0x0C)!=0x00) //wait for to finish Fault detection
{
if(SPI_Operation(0x00,Data_Buffer,1)==SPI_FAIL)
{
Operation_Fault=1;
return;
}
}
if(SPI_Operation(0x07,Data_Buffer,1)==SPI_FAIL) //read back Fault status
{
Operation_Fault=1;
return;
}
MAX31855_Fault_Status=Data_Buffer[0];
Data_Buffer[0]=MAX31865_Config|0x02; //write back configuration register
if(SPI_Operation(0x80,Data_Buffer,1)==SPI_FAIL)
{
Operation_Fault=1;
return;
}
#endif
}
float get_pt100_temp(void)
{
if(Operation_Fault==0) // if SPI communication is ok, it always run this code
{
#ifdef Manual_Mode
Data_Buffer[0]=(MAX31865_Config|0x20); //set 1-shot=1
if(SPI_Operation(0x80,Data_Buffer,1)==SPI_FAIL)
{
Operation_Fault=1;
return 0;
}
#endif
while(MAX31865_READY_PIN()) //waiting if DRDY is not low
{
MAX31865_msater_state = TRUE;
if(MAX31865_master_timer > 10) //超时20ms就复位
{
MAX31865_msater_state = FALSE;
Inital_MAX31865();
break;
}
}
if(SPI_Operation(0x01,Data_Buffer,2)==SPI_FAIL) //read back RTD_MSB and RTD_LSB
{
Operation_Fault=1;
return 0;
}
RTD_H=Data_Buffer[0];
RTD_L=Data_Buffer[1];
if((RTD_L&0x01)==0x01) //detect fault
{
Fault_Detect();
// if(RTD_L == 0xff||(RTD_L>=250))
// {
open_circuit_fault = 1; //PT100开路故障;0->无故障;1->有故障
// }
// can add source code here to handle Fault status
//Note: this software is not tested under Fault conditon.
}
else
{
AD_Value=(((RTD_H<<8)|RTD_L)&0xFFFE)>>1;
RTD_Resistor=((AD_Value*4000.0000)/32768.00);
RTD_Temperature=(AD_Value/32.00)-256; //没校正过的温度大于100°时就不准了
if(RTD_Resistor>=(Rref/4)) //temperature>0
{
//矫正过的温度
Corrected_Temperature=((sqrt(pow(0.0039083,2)+4*0.0000005775*(1-(RTD_Resistor/Rref)*4))-0.0039083)/(2*(0-0.0000005775)));
}
else
{
if(Rref==400)
Corrected_Temperature=0-241.96+2.2163*RTD_Resistor+0.0028541*pow(RTD_Resistor,2)-0.000009912*pow(RTD_Resistor,3)+0.000000017052*pow(RTD_Resistor,4);
if(Rref==4000)
Corrected_Temperature=0-241.96+2.2163*RTD_Resistor*0.1+0.0028541*pow((RTD_Resistor*0.1),2)-0.000009912*pow((RTD_Resistor*0.1),3)+0.000000017052*pow((RTD_Resistor*0.1),4);
}
// open_circuit_fault = 0; //PT100开路无故障;0->无故障;1->有故障
if(Corrected_Temperature < -150)
{
short_circuit_fault = 1; //PT100短路故障
}
else if(Corrected_Temperature > -140)
{
short_circuit_fault = 0; //PT100无短路故障
}
if(Corrected_Temperature > 800)
{
open_circuit_fault = 1; //PT100开路故障
}
else if(Corrected_Temperature < 700)
{
open_circuit_fault = 0; //PT100无开路故障
}
}
}
PT100_FAULT = (short_circuit_fault<<1)|(open_circuit_fault<<2)|(Operation_Fault<<3);
return 1;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>