stm32-max31865-spi-temperature-sensing

by Marwen Maghrebi
#include "main.h"
#include "Max31856.h"
#include<stdio.h>
#include<string.h>

/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart1;

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define RREF      430.0
#define RNOMINAL  100.0

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
static void MX_USART1_UART_Init(void);

int main(void)
{
/* USER CODE BEGIN 1 */
uint16_t rtd = 0;
uint8_t fault;
max31865_fault_cycle_t fault_cycle = MAX31865_FAULT_AUTO;
float ratio;
char MSG1[20];
char buffer[50]; // Adjust the size according to your needs
/* USER CODE END 1 */

HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SPI1_Init();
MX_USART1_UART_Init();

/* USER CODE BEGIN 2 */
begin(MAX31865_4WIRE);  // set to 2WIRE or 4WIRE as necessary
/* USER CODE END 2 */

while (1)
{
    rtd = readRTD();
    ratio = rtd;
    ratio /= 32768;
    
    sprintf(MSG1,"RTD value: %d",rtd);
    HAL_UART_Transmit(&huart1,(uint8_t*) MSG1, sizeof(MSG1), 100);
    HAL_UART_Transmit(&huart1,(uint8_t*) "\n\r",2, 10);
    
    sprintf(MSG1,"Ratio = %.8f" ,ratio);
    HAL_UART_Transmit(&huart1,(uint8_t*) MSG1, sizeof(MSG1), 100);
    HAL_UART_Transmit(&huart1,(uint8_t*) "\n\r",2, 10);
    
    sprintf(MSG1,"Resistance = %.8f",RREF*ratio);
    HAL_UART_Transmit(&huart1,(uint8_t*) MSG1, sizeof(MSG1), 100);
    HAL_UART_Transmit(&huart1,(uint8_t*) "\n\r",2, 10);
    
    sprintf(MSG1,"Temperature = %.2f",temperature(RNOMINAL, RREF));
    HAL_UART_Transmit(&huart1,(uint8_t*) "***********", 10, 50);
    HAL_UART_Transmit(&huart1,(uint8_t*) MSG1, sizeof(MSG1), 100);
    HAL_UART_Transmit(&huart1,(uint8_t*) "************", 10, 50);
    HAL_UART_Transmit(&huart1,(uint8_t*) "\n\r",2, 10);
    
    fault = readFault(fault_cycle);
    if (fault) {
         sprintf(buffer, "Fault 0x%X\n", fault);
         HAL_UART_Transmit(&huart1, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
    
         if (fault & MAX31865_FAULT_HIGHTHRESH) {
             sprintf(buffer, "RTD High Threshold\n");
             HAL_UART_Transmit(&huart1, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
         }
         if (fault & MAX31865_FAULT_LOWTHRESH) {
             sprintf(buffer, "RTD Low Threshold\n");
             HAL_UART_Transmit(&huart1, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
         }
         if (fault & MAX31865_FAULT_REFINLOW) {
             sprintf(buffer, "REFIN- > 0.85 x Bias\n");
             HAL_UART_Transmit(&huart1, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
         }
         if (fault & MAX31865_FAULT_REFINHIGH) {
             sprintf(buffer, "REFIN- < 0.85 x Bias - FORCE- open\n");
             HAL_UART_Transmit(&huart1, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
         }
         if (fault & MAX31865_FAULT_RTDINLOW) {
             sprintf(buffer, "RTDIN- < 0.85 x Bias - FORCE- open\n");
             HAL_UART_Transmit(&huart1, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
         }
         if (fault & MAX31865_FAULT_OVUV) {
             sprintf(buffer, "Under/Over voltage\n");
             HAL_UART_Transmit(&huart1, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
         }
         clearFault();
     }
     HAL_Delay(1000);
    }
}
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00