max31865芯片读取pt100
引入max31865.c和.h文件,引入spi.h和.c文件
在主函數調用初始化函數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);即可讀取溫度
總結
以上是生活随笔為你收集整理的max31865芯片读取pt100的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 毕业设计 树莓派视觉远程监控遥控小车设计
- 下一篇: a-btest 数据挖掘_挖掘新的垂直鼠