这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界» 论坛首页» 嵌入式开发» STM32» STM32驱动铂电阻温度检测器MAX31865的读写程序

共10条 1/1 1 跳转至

STM32驱动铂电阻温度检测器MAX31865的读写程序

工程师
2021-01-05 20:30:24 打赏

MAX31865是一款RTD铂电阻至数字转换器,支持100Ω至1kΩ (0°C时)铂电阻RTD (PT100至PT1000)。

image.png


STM32读写MAX31865的源码程序如下,希望对大家有帮助!


#define IO_GLOBAL


#include "io.h"

#include "main.h"

/**

* @brief MAX31865芯片所用单片机引脚的初始化

* @param

* @param

* @retval

*/

static void MAX31865_Port_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct;

/*##-1- Enable peripherals and GPIO Cloc #########################*/

/* Enable GPIO TX/RX clock */

__HAL_RCC_GPIOF_CLK_ENABLE();

/* SPI SCK GPIO pin configuration******** PG1 */

GPIO_InitStruct.Pin = GPIO_PIN_8; //哪个引脚

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; //输出

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; //速度选最快的

HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

/* SPI MISO GPIO pin configuration****** PF15 */

GPIO_InitStruct.Pin = GPIO_PIN_6;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);


/* SPI MOSI GPIO pin configuration**** PF13 */

GPIO_InitStruct.Pin = GPIO_PIN_4;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);


/* SPI cs GPIO pin configuration**** PF11 */

GPIO_InitStruct.Pin = GPIO_PIN_2;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

/* re GPIO pin configuration****** PB1 */

GPIO_InitStruct.Pin = GPIO_PIN_0;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

NCS_H;

SCLK_H;

}

/**

* @brief IO资源引脚的初始化

* @param

* @param

* @retval

*/

void ConfigurePortPins(void)

{

MAX31865_Port_Init();

}


#define DT_NUM 0x30 //延时时间

/**

* @brief 简单粗暴的延时函数

* @param

* @param

* @retval

*/

void Delay(u32 nCount)

{

for(; nCount != 0; nCount--);

}


/**

* @brief 读max31865的寄存器

* @param 地址

* @param

* @retval

*/


u8 MAX31865_SB_Read(u8 addr)//SPI Single-Byte Read

{

u8 i=0;

u8 read = 0;

NCS_L;

Delay(DT_NUM);

for(i = 0; i < 8; i++)

{

SCLK_L;

if (addr & 0x80)

{SDI_H;}

else

{SDI_L;}

Delay(DT_NUM);

SCLK_H;

addr <<= 1;

Delay(DT_NUM);

}

Delay(DT_NUM);

for (i = 0; i < 8; i++)

{

SCLK_L;

read = read<<1;

Delay(DT_NUM);

if(SDO)

{

read++;

}

SCLK_H;

Delay(DT_NUM);

}

NCS_H;

return read;

}


/**

* @brief 写max31865的寄存器

* @param 地址

* @param 数据

* @retval

*/

void MAX31865_SB_Write(u8 addr,u8 wdata)//SPI Single-Byte Write

{

u8 i = 0;

NCS_L; //拉低片选信号

Delay(DT_NUM); //粗暴延时

//写地址

for(i = 0; i < 8; i++) //0-7

{

SCLK_L; //时钟线拉高

if (addr & 0x80) // xxxx xxxx & 1000 0000 addr最高位是否为1简单判断

{SDI_H;} // 为1 发送高电平

else

{SDI_L;} //为0 发送低电平

Delay(DT_NUM); //粗暴延时

SCLK_H; //时钟线拉低

addr <<= 1; //数据左移 判断次高位

Delay(DT_NUM); //粗暴延时

}

//写数据

for(i = 0; i < 8; i++)

{

SCLK_L;

if (wdata & 0x80)

{SDI_H;}

else {SDI_L;}

Delay(DT_NUM);

SCLK_H;

wdata <<= 1;

Delay(DT_NUM);

}

//操作完成拉高片选

NCS_H;

}



/**

* @brief 简单滤波计算温度值

* @param

* @param

* @retval 扩大100倍的温度

*/

u8 temp1;

u8 temp2;

float Get_tempture(void)//PT100

{

float temps;

u16 dtemp[2];

u16 data_temp;

u8 i;

static u8 cnt_num =0;

static u16 temp1_num[8];

static u32 temp1_sum;

static u16 temp1_min =0;

static u16 temp1_max =0;

dtemp[0]=MAX31865_SB_Read(0x01);//读RTD_MSB

dtemp[1]=MAX31865_SB_Read(0x02);//读RTD_LSB

data_temp=(dtemp[0]<<7)+(dtemp[1]>>1);//Get 15Bit DATA;

temp1=dtemp[0];

temp2=dtemp[1];

temp1_ad = data_temp;


if(cnt_num<8)cnt_num++;

else cnt_num=0;

temp1_num[cnt_num] = data_temp;


temp1_min = temp1_num[cnt_num];

temp1_max = temp1_num[cnt_num];


if(power_time < 10) return 0;

for (i=0,temp1_sum=0; i<8; i++)

{

temp1_sum += temp1_num[i];

if (temp1_num[i]<=temp1_min) temp1_min = temp1_num[i];

if (temp1_num[i]>=temp1_max) temp1_max = temp1_num[i];

}


temp1_sum -= temp1_min;

temp1_sum -= temp1_max;

temps = temp1_sum/6;

temps=(temps*402)/32768;//Here is the rtd R value;

temps=(temps-100)*100/0.385055;//A gruad 本身值经扩大100倍单位为1摄氏度

return temps;

}


/**

* @brief 得到PT100温度值

* @param

* @param

* @retval

*/


void GetPT100_Temp(void)

{


u8 i;

static u8 cnt_num =0;

static float temp1_num[64];

static float temp1_sum;

static float temp1_min =0;

static float temp1_max =0;

if(cnt_num<64)cnt_num++;

else cnt_num=0;

temp1_num[cnt_num] = Get_tempture();


temp1_min = temp1_num[cnt_num];

temp1_max = temp1_num[cnt_num];


if(power_time < 10) return;

for (i=0,temp1_sum=0; i<64; i++)

{

temp1_sum += temp1_num[i];

if (temp1_num[i]<=temp1_min) temp1_min = temp1_num[i];

if (temp1_num[i]>=temp1_max) temp1_max = temp1_num[i];

}

temp1_sum -= temp1_min;

temp1_sum -= temp1_max;

tempture = temp1_sum/62;

Fault_Status=MAX31865_SB_Read(0x07);//Get Fault_Status

}


/**

* @brief 读取IO资源

* @param

* @param

* @retval

*/





关键词: MAX31865 铂电阻

工程师
2021-01-05 20:45:30 打赏
2楼

模拟的SPI接口时序,为何不用硬件SPI来读写呢?


工程师
2021-01-05 20:49:56 打赏
3楼

不错!正好需要


工程师
2021-01-05 20:54:31 打赏
4楼

maxim 出品的IC确实很棒! 用这个做接口方便铂电阻温度探测线性化多了!



工程师
2021-01-05 21:29:14 打赏
5楼

程序写的蛮好的


工程师
2021-01-05 22:59:40 打赏
6楼

挺不错的机器


工程师
2021-01-12 23:45:21 打赏
7楼

感谢分享


菜鸟
2021-05-20 08:50:56 打赏
8楼

我发现这代码跟我在阿莫电子论坛上分享的一样


专家
2021-05-20 08:57:20 打赏
9楼

学习


专家
2021-05-20 08:58:01 打赏
10楼

收藏一下


共10条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册]