200 lines
4.2 KiB
C
200 lines
4.2 KiB
C
#include "basic.h"
|
||
|
||
|
||
void Delay_Times(int d)
|
||
{
|
||
int m = 0,n = 0;
|
||
for(m = 0; m < d;m++)
|
||
{
|
||
for(n=0 ; n < 10; n++);
|
||
}
|
||
}
|
||
|
||
|
||
/***********************************************
|
||
调用方式:MAX31865_Init()
|
||
返回值:
|
||
SDO ---> PB14
|
||
CS ---> PB12
|
||
SCLK ---> PB13
|
||
SDI ---> PB15
|
||
DRDY ---> PD8
|
||
函数说明:MAX31865 初始化,软件模拟
|
||
************************************************/
|
||
void MAX31865_Init(void)
|
||
{
|
||
GPIO_InitTypeDef GPIO_InitStructure ;
|
||
|
||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOD, ENABLE);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = MAX31865_CS|MAX31865_SCLK|MAX31865_SDI;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //输出模式
|
||
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
|
||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
|
||
GPIO_Init(MAX31865_CONTROL_PORT,&GPIO_InitStructure);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = MAX31865_SDO;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //配置输入模式为上拉输入
|
||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
||
GPIO_Init(MAX31865_CONTROL_PORT,&GPIO_InitStructure);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = MAX31865_DRDY;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //配置输入模式为上拉输入
|
||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
||
GPIO_Init(MAX31865_DRDYL_PORT,&GPIO_InitStructure);
|
||
|
||
MAX31865_CS_SET;
|
||
MAX31865_SCLK_SET;
|
||
}
|
||
|
||
/***********************************************
|
||
调用方式:MAX31865_Write()
|
||
返回值:
|
||
函数说明:MAX31865 写寄存器,addr:寄存器地址,data:数据
|
||
************************************************/
|
||
void MAX31865_Write(unsigned char addr, unsigned char data)
|
||
{
|
||
unsigned char i;
|
||
MAX31865_CS_CLR;
|
||
// MAX31865_SCLK_SET;
|
||
Delay_Times(20);
|
||
for(i=0;i<8;i++) //写地址
|
||
{
|
||
MAX31865_SCLK_CLR;
|
||
//Delay_Times(50);
|
||
if(addr & 0x80)
|
||
MAX31865_SDI_SET;
|
||
else
|
||
MAX31865_SDI_CLR;
|
||
Delay_Times(20);
|
||
MAX31865_SCLK_SET;
|
||
addr <<= 1;
|
||
Delay_Times(20);
|
||
}
|
||
|
||
Delay_Times(20);
|
||
|
||
for(i=0;i<8;i++) //写数据
|
||
{
|
||
MAX31865_SCLK_CLR;
|
||
//Delay_Times(50);
|
||
if(data & 0x80)
|
||
MAX31865_SDI_SET;
|
||
else
|
||
MAX31865_SDI_CLR;
|
||
Delay_Times(20);
|
||
MAX31865_SCLK_SET;
|
||
data <<= 1;
|
||
Delay_Times(20);
|
||
|
||
}
|
||
MAX31865_CS_SET;
|
||
//Delay_Times(20);
|
||
}
|
||
/***********************************************
|
||
调用方式:MAX31865_Read()
|
||
返回值: data
|
||
函数说明:MAX31865 读寄存器 ,addr:寄存器地址
|
||
************************************************/
|
||
unsigned char MAX31865_Read(unsigned char addr)
|
||
{
|
||
unsigned char i;
|
||
unsigned char data=0;
|
||
MAX31865_CS_CLR;
|
||
// MAX31865_SCLK_SET;
|
||
Delay_Times(20);
|
||
for(i=0;i<8;i++) //写地址
|
||
{
|
||
MAX31865_SCLK_CLR;
|
||
//Delay_Times(50);
|
||
if(addr&0x80)
|
||
MAX31865_SDI_SET;
|
||
else
|
||
MAX31865_SDI_CLR;
|
||
Delay_Times(20);
|
||
MAX31865_SCLK_SET;
|
||
addr <<= 1;
|
||
Delay_Times(20);
|
||
}
|
||
|
||
Delay_Times(20);
|
||
|
||
for(i=0;i<8;i++) //读数据
|
||
{
|
||
MAX31865_SCLK_CLR;
|
||
Delay_Times(20);
|
||
data <<= 1;
|
||
MAX31865_SCLK_SET;
|
||
if(MAX31865_SDO_READ)
|
||
//data++;
|
||
data|=0x01;
|
||
else
|
||
data|=0x00;
|
||
|
||
Delay_Times(20);
|
||
}
|
||
MAX31865_CS_SET;
|
||
//Delay_Times(20);
|
||
return data;
|
||
}
|
||
/***********************************************
|
||
调用方式:MAX31865_Cfg()
|
||
返回值:
|
||
函数说明:MAX31865 配置
|
||
************************************************/
|
||
void MAX31865_Cfg(void)
|
||
{
|
||
//BIAS ON,自动,4线,50HZ 根据文件修改四线还是三线
|
||
MAX31865_Write(0x80, 0xD1);
|
||
}
|
||
/***********************************************
|
||
调用方式:MAX31865_GetTemp()
|
||
返回值:
|
||
函数说明:MAX31865 获取温度
|
||
************************************************/
|
||
float MAX31865_GetTemp(void)
|
||
{
|
||
unsigned int data = 0;
|
||
float Rt;
|
||
float Rt0 = 100; //PT100
|
||
float Z1,Z2,Z3,Z4,temp;
|
||
float a = 3.9083e-3;
|
||
float b = -5.775e-7;
|
||
float rpoly;
|
||
//MAX31865_Write(0x80, 0xD3);
|
||
data = MAX31865_Read(0x01) << 8;
|
||
data |= MAX31865_Read(0x02);
|
||
data>>=1; //去掉Fault位
|
||
//printf("Read=0x%02X\r\n",data);
|
||
Rt=(float)data/32768.0f*RREF;
|
||
//printf("Rt=0x%.1f\r\n",Rt);
|
||
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.2228f * rpoly;
|
||
rpoly *= Rt; // square
|
||
temp += 2.5859e-3f * rpoly;
|
||
rpoly *= Rt; // ^3
|
||
temp -= 4.8260e-6f * rpoly;
|
||
rpoly *= Rt; // ^4
|
||
temp -= 2.8183e-8f * rpoly;
|
||
rpoly *= Rt; // ^5
|
||
temp += 1.5243e-10f * rpoly;
|
||
|
||
return temp;
|
||
}
|
||
|
||
|
||
|
||
|