目录
1.cubemx外设选择驱动
1.单片机时钟设置
根据自己的硬件晶振来设置,我的单片机是高速外部晶振和低速外部晶振。
时钟设置
2.硬件spi设置
没用使用硬件的片选信号引脚,我们这里是使用任意一个引脚来作为硬件SPI的片选引脚。(具体的看代码实现)
3.外部中断使能
外部中断引脚设置为PA3,下降沿触发。硬件SPI的片选引脚为PA4。
外部中断:外部中断是指由CPU之外的外部设备或信号产生的中断请求,如键盘输入、鼠标移动、定时器到期、硬件故障等,它们会打断正在执行的程序或操作系统内核的运行,引起CPU的注意,进而进行相应的中断处理。外部中断可以使系统实现实时响应和事件处理,提高了系统的可靠性和稳定性。
4.NVIC的设置
2.Max31685如何接线
3.具体的代码实现
#include "PT100.h"
#include <stdio.h>
#include <math.h>
/* MAX31865参考电阻 */
#define RREF 430 //430Ω
void MAX31865_Init()
{
//MAX31865_SB_Write(0x80,0xC1);//二线、四线配置
MAX31865_SB_Write(0x80,0xD1);//三线配置
HAL_Delay(10);
}
float Get_tempture(void)//PT100
{
unsigned int data;
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_SB_Read(0x01)<<8;
data|=MAX31865_SB_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.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;
return temp;
}
uint8_t MAX31865_SB_Read(uint8_t addr)//SPI Single-Byte Read
{
uint8_t read;
MAX31685_CS_LOW();
HAL_SPI_Transmit(&hspi1, &addr, 1, 60);
HAL_SPI_Receive(&hspi1, &read, 1, 60);
MAX31685_CS_HIGH();
return read;
}
void MAX31865_SB_Write(uint8_t addr,uint8_t wdata)//SPI Single-Byte Write
{
uint8_t dat[2];
dat[0] = addr;
dat[1] = wdata;
MAX31685_CS_LOW();
HAL_SPI_Transmit(&hspi1,dat,2,60);
MAX31685_CS_HIGH();
}
#ifndef __PT100_H__
#define __PT100_H__
#include "spi.h"
#include "gpio.h"
//#define MAX31685_RDY() HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) //iso module is not connect
#define MAX31685_CS_HIGH() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET)
#define MAX31685_CS_LOW() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET)
void MAX31865_Init(void);
uint8_t MAX31865_SB_Read(uint8_t addr);
void MAX31865_SB_Write(uint8_t addr,uint8_t wdata);
float Get_tempture(void);
#endif
/*********主函数可以添加*********/ if(s_ucTemperFlag == 1) { s_ucTemperFlag = 0; //温度标准位置零 s_fNowTemper = Get_tempture(); snprintf(Temperabuf,16,"T:%.3f C",s_fNowTemper); OLED_ShowString(2,1,Temperabuf); //显示温度 } /***************/ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if(GPIO_Pin == GPIO_PIN_3) { if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_3) == GPIO_PIN_RESET) { if(s_ucTemperFlag == 0) s_ucTemperFlag = 1; //温度转换标准位 } } }
使用外部中断来接收温度转换完成标准。