【GD32E503评测】——06.通过差分ADC检测PT100传感器温度值
[复制链接]
本帖最后由 xld0932 于 2021-2-20 23:35 编辑
一、PT100温度传感器
PT100温度传感器是一种将温度变量转换为可传送的标准化输出信号的仪表。主要用于工业过程温度参数的测量和控制。温度的采集范围可以在-200℃~+850℃。根据PT100引出线数可以分为2线制、3线制和4线制三种。
二、MAX31865至数字输出转换器
MAX31865是简单易用的热敏电阻至数字输出转换器,优化用于铂电阻温度检测器(RTD),适配100R至1000R铂电阻RTD(PT100至PT1000)。外部电阻设置RTD灵敏度,高精度Δ- Σ ADC将RTD电阻与基准电阻之比转换为数字输出。MAX31865输入具有高达±45V的过压保护,提供可配置的RTD及电缆开路、短路条件检测。
MAX31865接口为SPI接口,兼容于2线、3线和4线的传感器连接,采样达到了15位ADC分辨率,标称温度分辨率精确到了0.03125℃,最大转换时间不超过21ms。典型的应用电路如下所示:
三、硬件连接
我们使用LCD触摸屏的接口部分,正好有一个SPI接口,我们将MAX31865模块的接口与MCU的SPI相连接,并给MAX31865模块提供3.3V的工作电压,硬件连接图如下所示:
四、程序设计
配置与MAX31865通讯的SPI端口引脚、配置MAX31865寄存器;通过SHELL命令来调用温度读取函数:
/*******************************************************************************
* @file MAX31865.h
* @author King
* @version V1.00
* @date 08-Jan-2021
* @brief ......
*******************************************************************************
* @attention *
*******************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAX31865_H__
#define __MAX31865_H__
#ifdef __cplusplus
extern "C" {
#endif
#undef EXTERN
#ifdef __MAX31865_C__
#define EXTERN
#else
#define EXTERN extern
#endif
/* Includes ------------------------------------------------------------------*/
#include "config.h"
/* Exported constants --------------------------------------------------------*/
#define SPI_SCLK_PORT GPIOA
#define SPI_SCLK_PIN GPIO_PIN_5
#define SPI_MISO_PORT GPIOA
#define SPI_MISO_PIN GPIO_PIN_6
#define SPI_MOSI_PORT GPIOA
#define SPI_MOSI_PIN GPIO_PIN_7
#define SPI_CS_PORT GPIOE
#define SPI_CS_PIN GPIO_PIN_4
/* Exported types ------------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
#define SPI_SCLK_L() gpio_bit_write(SPI_SCLK_PORT, SPI_SCLK_PIN, RESET)
#define SPI_SCLK_H() gpio_bit_write(SPI_SCLK_PORT, SPI_SCLK_PIN, SET)
#define SPI_MISO_READ() gpio_input_bit_get(SPI_MISO_PORT, SPI_MISO_PIN)
#define SPI_MOSI_L() gpio_bit_write(SPI_MOSI_PORT, SPI_MOSI_PIN, RESET)
#define SPI_MOSI_H() gpio_bit_write(SPI_MOSI_PORT, SPI_MOSI_PIN, SET)
#define SPI_CS_L() gpio_bit_write(SPI_CS_PORT, SPI_CS_PIN, RESET)
#define SPI_CS_H() gpio_bit_write(SPI_CS_PORT, SPI_CS_PIN, SET)
/* Exported functions --------------------------------------------------------*/
EXTERN void MAX31865_Init(void);
EXTERN void MAX31865_GetConversionValue(void);
#ifdef __cplusplus
}
#endif
#endif
/******************* (C) COPYRIGHT 2021 *************************END OF FILE***/
/*******************************************************************************
* @file MAX31865.c
* @author King
* @version V1.00
* @date 08-Jan-2021
* @brief ......
*******************************************************************************
* @attention
*
*******************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#define __MAX31865_C__
/* Includes ------------------------------------------------------------------*/
#include "MAX31865.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
float Temperature = 0.0;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported function prototypes ----------------------------------------------*/
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void MAX31865_Delay(void)
{
uint32_t i;
i = 0x1000;
while(i--);
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void MAX31865_SCLK(uint8_t Level)
{
if(Level) SPI_SCLK_H();
else SPI_SCLK_L();
MAX31865_Delay();
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
uint8_t MAX31865_MISO(void)
{
return SPI_MISO_READ();
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void MAX31865_MOSI(uint8_t Level)
{
if(Level) SPI_MOSI_H();
else SPI_MOSI_L();
MAX31865_Delay();
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void MAX31865_CS(uint8_t Enable)
{
if(Enable) SPI_CS_H();
else SPI_CS_L();
MAX31865_Delay();
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
uint8_t MAX31865_ReadByte(void)
{
uint8_t i = 0, Data = 0;
for(i = 0; i < 8; i++)
{
MAX31865_SCLK(0);
Data <<= 1;
if(MAX31865_MISO() == 1)
{
Data |= 0x01;
}
MAX31865_SCLK(1);
}
return Data;
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void MAX31865_WriteByte(uint8_t Data)
{
uint8_t i = 0;
MAX31865_SCLK(1);
for(i = 0; i < 8; i++)
{
MAX31865_SCLK(0);
if(Data & 0x80)
{
MAX31865_MOSI(1);
}
else
{
MAX31865_MOSI(0);
}
MAX31865_SCLK(1);
Data <<= 1;
}
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
uint8_t MAX31865_ReadREG(uint8_t Address)
{
uint8_t Value = 0;
MAX31865_CS(0);
MAX31865_WriteByte(Address);
Value = MAX31865_ReadByte();
MAX31865_CS(1);
return Value;
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void MAX31865_WriteREG(uint8_t Address, uint8_t Value)
{
MAX31865_CS(0);
MAX31865_WriteByte(Address);
MAX31865_WriteByte(Value);
MAX31865_CS(1);
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void MAX31865_GetConversionValue(void)
{
unsigned int data;
float Rt;
float Rt0 = 100; //---PT100
float Z1,Z2,Z3,Z4,temp;
float a = 3.9083e-3;
float b = -5.7750e-7;
float rpoly;
data = MAX31865_ReadREG(0x01) << 8;
data |= MAX31865_ReadREG(0x02);
data>>=1; //---去掉Fault位
Rt=(float)data/32768.0*430; //---RREF=430
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)
{
printf("\r\nTemp : %0.1f\r\n", temp);
}
else
{
rpoly = Rt;
temp = -242.02;
temp += 2.2228 * rpoly;
rpoly *= Rt; // square
temp += 2.5859e-3 * rpoly;
rpoly *= Rt; // ^3
temp -= 4.8260e-6 * rpoly;
rpoly *= Rt; // ^4
temp -= 2.8183e-8 * rpoly;
rpoly *= Rt; // ^5
temp += 1.5243e-10 * rpoly;
printf("\r\nTemp : %0.3f\r\n", temp);
}
Temperature = temp;
}
SHELL_EXPORT_CMD(temp, MAX31865_GetConversionValue, read PT100 temperature);
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void MAX31865_InitGPIO(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOE);
gpio_init(SPI_SCLK_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SPI_SCLK_PIN);
gpio_init(SPI_MOSI_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SPI_MOSI_PIN);
gpio_init(SPI_MISO_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, SPI_MISO_PIN);
gpio_init(SPI_CS_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SPI_CS_PIN);
SPI_CS_H(); SPI_SCLK_H();
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void MAX31865_Init(void)
{
MAX31865_InitGPIO();
MAX31865_WriteREG(0x80, 0xC3);
printf("\r\nMAX31865 Configuration : 0x%02x\r\n", MAX31865_ReadREG(0x00));
}
/******************* (C) COPYRIGHT 2021 *************************END OF FILE***/
五、运行结果
程序编译无误后,下载到开发板运行;上电后我们看到MAX31865寄存器配置读取成功,输入SHELL命令temp来调用温度读取函数,将读到的温度值打印输出出来,如下图所示:
六、工程代码
Project_MAX31865.zip
(429.73 KB, 下载次数: 21)
|