初级会员

- 积分
- 123
- 金钱
- 123
- 注册时间
- 2020-2-19
- 在线时间
- 10 小时
|
发表于 2020-6-3 16:26:36
|
显示全部楼层
/**
******************************************************************************
* @file : max31855.c
* @brief : MAX31855 cold-junction compensated thermocouple-to-digital
* converter program body.
******************************************************************************
*
* Copyright (c) respeke, www.etdev.net
* All rights reserved.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "max31855.h"
#include "stm32f1xx_hal.h"
/* External variables --------------------------------------------------------*/
SPI_HandleTypeDef hspi1;
/* Private variables ---------------------------------------------------------*/
float tcTemp = 0, cjTemp = 0;
bool tcError = false;
/* Private function prototypes -----------------------------------------------*/
/**
* @brief Delay about 11ns per NOP (72MHz*1.25MIPS/MHz=90MIPS)
* @param None
* @retval None
*/
static void SPI_Delay(uint32_t counter)
{
for(int i=0; i<counter; i++)
{
__NOP();
}
}
/* SPI1 init function */
void MAX31855_SPIInit(void)
{
#if SPI_SIM
GPIO_InitTypeDef GPIO_InitStruct;
/* Peripheral clock enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/**SPI1 GPIO Configuration
PA4 ------> SPI1_NSS
PA5 ------> SPI1_SCK
PA6 ------> SPI1_MISO
PA7 ------> SPI1_MOSI
*/
HAL_GPIO_WritePin(MAX_SPI_GPIO_Port, MAX_SPI_CS_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(MAX_SPI_GPIO_Port, MAX_SPI_SCK_Pin, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = MAX_SPI_CS_Pin|MAX_SPI_SCK_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(MAX_SPI_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = MAX_SPI_MISO_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(MAX_SPI_GPIO_Port, &GPIO_InitStruct);
#else
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_16BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; // 2.25MBits/s
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
#endif
}
/* MAX31855 functions --------------------------------------------------------*/
static float ThermalCoupleTemp(uint16_t data)
{
float fraction = 0.0;
float temp = 0.0;
uint16_t intpart = 0;
/* Thermal couple fraction part: D19D18 */
// if(data&0x0008) fraction += 0.5;
// if(data&0x0004) fraction += 0.25;
fraction = ((data&0x000F) >> 2)*0.25;
intpart = data >> 4;
/* Negative number, add b'1111 to left */
if(data&0x8000)
{
intpart |= 0xF000;
}
temp = (int16_t)intpart + fraction;
return temp;
}
static float ColdJunctionTemp(uint16_t data)
{
float fraction = 0.0;
float temp = 0.0;
uint16_t intpart = 0;
/* Internal temperature fraction part: D7D6D5D4*/
// if(data&0x0080) fraction += 0.5;
// if(data&0x0040) fraction += 0.25;
// if(data&0x0020) fraction += 0.125;
// if(data&0x0010) fraction += 0.0625;
fraction = ((data&0x00FF) >> 4)*0.0625;
intpart = data >> 8;
/* Negative number, add b'11111111 to left */
if(data&0x8000)
{
intpart |= 0xFF00;
}
temp = (int16_t)intpart + fraction;
return temp;
}
static void MAX31855_ReadData(uint16_t *tc, uint16_t *cj)
{
#if SPI_SIM
uint32_t data = 0;
HAL_GPIO_WritePin(MAX_SPI_GPIO_Port, MAX_SPI_CS_Pin|MAX_SPI_SCK_Pin, GPIO_PIN_RESET);
SPI_Delay(100); // Delay 1us
for(int i=0; i<32; i++) |
|