前段时间一直在做一个MAX31865+PT100的测试,在淘宝买了一个模块,调试了很久,一直没有调试通,昨天晚上在家的时候一直在找问题,终于在今天晚上完成了调试,所以写个分享和大家说一下进度。
淘宝上的模块有点坑,本来是可以选择焊接的,但是说巧不巧,调试的时候通过错误状态寄存器发现了提示RTD+短路到GND,导致数据一直读取不出来来,而错误位一直提示错误,导致读取到的数据一直为0,经过排查,是网上后买的模块本生存在短路导致的,用小刀隔开之后就正常了,下面先上个图。由于一直在调试这个SPI,所以其他的都还没有做测试,这里就直接放仿真的图来。
好了,下面放一部分代码上来,以及自己搜集的资料。希望能够给大家一些帮助。
#include "MAX_31865.h"
#include "DRV_SPI.h"
void LEDIO_Init(void)
{
GPIO_Config_T configStruct;
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOE);
configStruct.pin =GPIO_PIN_6;
configStruct.mode = GPIO_MODE_OUT_PP;
configStruct.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOE, &configStruct);
}
void LED_Flash(void)
{
GPIO_ResetBit(GPIOE,GPIO_PIN_6);
Delay(500);
GPIO_SetBit(GPIOE,GPIO_PIN_6);
Delay(500);
}
float DataTemp=0;
unsigned char REG_ADD0=0,FaultStatus=0;
int main(void)
{
LEDIO_Init();
Delay(10);
MAX_31865_Init(MAX31865_3WIRE);
Delay(10);
while (1)
{
DataTemp=MAX_31865_Test();
LED_Flash();
FaultStatus=ReadFaultREG();
}
}
#include "DRV_SPI.h"
/*
=====================================
SPI_CS---->PB12
SPI_SCLK-->PB13
SPI_MISO-->PB14
SPI_MOSI-->PB15
=====================================
*/
void SPI_FullDuplex_Init(void)
{
GPIO_Config_T gpioConfig;
SPI_Config_T spiConfig;
/* Enable related Clock */
RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_SPI2);
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOB);
/* config PIN_6 MISO */
gpioConfig.pin = GPIO_PIN_14 ;
gpioConfig.mode = GPIO_MODE_IN_FLOATING;
gpioConfig.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOB, &gpioConfig);
/* config PIN_5->SCK , PIN_7->MOSI */
gpioConfig.pin = GPIO_PIN_13 | GPIO_PIN_15;
gpioConfig.mode = GPIO_MODE_AF_PP;
gpioConfig.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOB, &gpioConfig);
/* config PIN_6 cs */
gpioConfig.pin = GPIO_PIN_12 ;
gpioConfig.mode = GPIO_MODE_OUT_PP;
gpioConfig.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOB, &gpioConfig);
SPI_ConfigStructInit(&spiConfig);
spiConfig.length = SPI_DATA_LENGTH_8B;
spiConfig.baudrateDiv = SPI_BAUDRATE_DIV_64;
/* 2 line full duplex */
spiConfig.direction = SPI_DIRECTION_2LINES_FULLDUPLEX;
/* LSB first */
spiConfig.firstBit = SPI_FIRSTBIT_MSB;
/* Slave mode */
spiConfig.mode = SPI_MODE_MASTER;
/* Polarity is low */
spiConfig.polarity = SPI_CLKPOL_LOW;
/* Software select slave enable */
spiConfig.nss = SPI_NSS_SOFT;
/* Phase is 1 edge */
spiConfig.phase = SPI_CLKPHA_2EDGE;
/* SPI config */
SPI_Config(SPI2, &spiConfig);
SPI_ConfigDataSize(SPI2, SPI_DATA_LENGTH_8B);
SPI_Enable(SPI2);
SPI_CS_HIGH();
}
void SPI_WriteByte(unsigned char Data)
{
while (SPI_I2S_ReadStatusFlag(SPI2, SPI_FLAG_BSY) != RESET){};
SPI_I2S_TxData(SPI2, Data);
}
unsigned char SPI_ReadByte(void)
{
unsigned char Data=0;
Data = SPI_I2S_RxData(SPI2);
while (SPI_I2S_ReadStatusFlag(SPI2, SPI_FLAG_RXBNE) != RESET);
return Data;
}
unsigned char SPI_Read_Write(unsigned char Data)
{
unsigned char RTNData=0;
while (SPI_I2S_ReadStatusFlag(SPI2, SPI_FLAG_TXBE) == RESET){};
SPI_I2S_TxData(SPI2, Data);
while(SPI_I2S_ReadStatusFlag(SPI2, SPI_FLAG_RXBNE) == RESET){};
RTNData = SPI_I2S_RxData(SPI2);
return RTNData;
}
float MAX31865_ReadTemp(float RTDnominal, float RefResistor)
{
float Z1, Z2, Z3, Z4, Rt, temp;
float rpoly = Rt;
Rt = MAX31865_ReadRTD();
Rt /= 32768.0;
Rt *= RefResistor;
Z1 = -RTD_A;
Z2 = RTD_A * RTD_A - (4.0 * RTD_B);
Z3 = (4.0 * RTD_B) / RTDnominal;
Z4 = 2.0 * RTD_B;
temp = Z2 + (Z3 * Rt);
temp = (sqrt(temp) + Z1) / Z4;
if (temp >= 0)
return temp;
// ugh.
Rt /= RTDnominal;
Rt *= 100.0; // normalize to 100 ohm
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;
}
float MAX_31865_Test(void)
{
float TempData=0.0;
TempData=MAX31865_ReadTemp(RTD_NNUM,REG_REF);
Delay(10);
return TempData;
}
max31865.rar
(7.55 MB)
|