max31865芯片读取pt100

  1. 引入max31865.c和.h文件,引入spi.h和.c文件

  1. 在主函数调用初始化函数MAX31865_init(3);//3线制

#include "MAX31865.h"

#include "spi.h"

/*********************** Global variables *************************/

void max31865_cs_init(void)//温度采集放大器 片选初始化

{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PA端口时钟

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能PB端口时钟

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //LED0-->PB.12 端口配置

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz

GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOB.12

}

/*********************** Begin Private functions *************************/

void MAX31865_read(uint8_t ch, uint8_t addr, uint8_t *buffer, uint8_t len)

{

addr &= ~MAX31865_READ; // Force read bit on address

if (ch == 1)

{

MAX31865_CH1_CS;

}

else if(ch == 2)

{

MAX31865_CH2_CS;

}

spi_write(&addr, 1);// Write addr

spi_read(buffer, len);// Read data

MAX31865_CS_NULL;

}

/************************************************

void MAX31865_write(uint8_t ch, uint8_t addr, uint8_t data)

{

addr |= MAX31865_WRITE; // Force write bit on address

if (ch == 1)

{

MAX31865_CH1_CS;

}

else if(ch == 2)

{

MAX31865_CH2_CS;

}

spi_write(&addr, 1); // Write addr

spi_write(&data, 1); // Write data

MAX31865_CS_NULL;

}

void enableBias(uint8_t ch, uint8_t enable)//Disable bias voltage使能d7

{

uint8_t status;

MAX31865_read(ch,MAX31856_CONFIG_REG, &status, 1);

if (enable)

{

status |= MAX31856_CONFIG_BIAS;

} else

{

status &= ~MAX31856_CONFIG_BIAS;

}

MAX31865_write(ch,MAX31856_CONFIG_REG, status);

}

/**********************************

void autoConvert(uint8_t ch, uint8_t enable)//设置转换模式开关

{

uint8_t status;

MAX31865_read(ch,MAX31856_CONFIG_REG, &status, 1);

if (enable)

{

status |= MAX31856_CONFIG_MODEAUTO;

} else

{

status &= ~MAX31856_CONFIG_MODEAUTO;

}

MAX31865_write(ch,MAX31856_CONFIG_REG, status);

}

/********************************************************

void setWires(uint8_t ch, uint8_t numwires)//设置几线制接法

{

uint8_t status;

MAX31865_read(ch,MAX31856_CONFIG_REG, &status, 1);

if (numwires == 3) // 3-wire

{

status |= MAX31856_CONFIG_3WIRE;

} else // 2-4 wire

{

status &= ~MAX31856_CONFIG_3WIRE;

}

MAX31865_write(ch,MAX31856_CONFIG_REG, status);

}

/****************************

void single_shot(uint8_t ch)//读取温度,一次读取温度值

{

uint8_t status;

// Read config register

MAX31865_read(ch,MAX31856_CONFIG_REG, &status, 1);

// Enable 1shot bit, and write back

status |= MAX31856_CONFIG_1SHOT;

MAX31865_write(ch,MAX31856_CONFIG_REG, status);

}

/*********************** End Private functions *************************/

void MAX31865_init(uint8_t wires)//温度采集 支持2-4线RTD选择

{

spi1_init();//SDI SCL SDO io初始化

max31865_cs_init(); //片选io初始化

setWires(1,wires); // Set 2,3 or 4 wire sensor

enableBias(1,OFF); // Disable bias voltage

autoConvert(1,OFF); // Disable auto conversion

}

/**

float MAX31865_readTemp(uint8_t ch)//读取温度函数

{

uint8_t buffer[2];

uint16_t data = 0;

float resistance = 0;

float temp = 0;

// Activate bias voltage to read sensor data, and wait for the capacitors to fill

enableBias(ch,ON);

DELAY(10);

// Perform a single conversion, and wait for the result

single_shot(ch);

DELAY(65);

// Read data from max31865 data registers

MAX31865_read(ch,MAX31856_RTDMSB_REG, buffer, 2);

// Combine 2 bytes into 1 number, and shift 1 down to remove fault bit

data = buffer[0] << 8;

data |= buffer[1];

data >>= 1;

// Calculate the actual resistance of the sensor

resistance = ((float) data * RREF) / FACTOR;

// Calculate the temperature from the measured resistance

temp = ((resistance / 100) - 1) / ALPHA;

// Disable bias voltage to reduce power usage

enableBias(ch,OFF);

return temp;

}

在主函数调用MAX31865_readTemp(1);即可读取温度

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值